public function Api::testFastlyApiConnection in Fastly 8.3
Function for testing Fastly API connection.
Return value
array Returns keyed array with 'status' and 'message' of test connection.
File
- src/
Api.php, line 610
Class
- Api
- Fastly API for Drupal.
Namespace
Drupal\fastlyCode
public function testFastlyApiConnection() {
if (empty($this->host) || empty($this->serviceId) || empty($this->apiKey)) {
return [
'status' => FALSE,
'message' => $this
->t('Please enter credentials first'),
];
}
$url = '/service/' . $this->serviceId;
$headers = [
'Fastly-Key' => $this->apiKey,
'Accept' => 'application/json',
];
try {
$message = '';
$response = $this
->vclQuery($url, [], "GET", $headers);
if ($response
->getStatusCode() == "200") {
$status = TRUE;
$response_body = json_decode($response
->getBody());
if (!empty($response_body->name)) {
$args = [
'%service_name' => $response_body->name,
];
$message = $this
->t('Connection Successful on service %service_name', $args);
}
}
else {
$status = FALSE;
$response_body = json_decode($response
->getBody());
if (!empty($response_body->name)) {
$args = [
'%name]' => $response_body->name,
'@status' => $response
->getStatusCode(),
];
$message = $this
->t('Connection not Successful on service %name - @status', $args);
$this->logger
->critical($message);
}
else {
$args = [
'@status' => $response
->getStatusCode(),
];
$message = $this
->t('Connection not Successful on service - status : @status', $args);
$this->logger
->critical($message);
}
}
return [
'status' => $status,
'message' => $message,
];
} catch (Exception $e) {
return [
'status' => FALSE,
'message' => $e
->getMessage(),
];
}
}