protected function processRestRequest($method = NULL, $path = NULL, array $body = [])
{
$client = $this->getClient();
$options = [RequestOptions::HTTP_ERRORS => false, RequestOptions::HEADERS => ['User-Agent' => "Postmark-PHP (PHP Version:{$this->version}, OS:{$this->os})", 'Accept' => 'application/json', 'Content-Type' => 'application/json', $this->authorization_header => $this->authorization_token]];
if (!empty($body)) {
$cleanParams = array_filter($body, function ($value) {
return $value !== null;
});
switch ($method) {
case 'GET':
case 'HEAD':
case 'DELETE':
case 'OPTIONS':
$options[RequestOptions::QUERY] = $cleanParams;
break;
case 'PUT':
case 'POST':
case 'PATCH':
$options[RequestOptions::JSON] = $cleanParams;
break;
}
}
$response = $client->request($method, $path, $options);
switch ($response->getStatusCode()) {
case 200:
return json_decode($response->getBody(), true);
case 401:
$ex = new PostmarkException();
$ex->message = 'Unauthorized: Missing or incorrect API token in header. ' . 'Please verify that you used the correct token when you constructed your client.';
$ex->httpStatusCode = 401;
throw $ex;
case 500:
$ex = new PostmarkException();
$ex->httpStatusCode = 500;
$ex->message = 'Internal Server Error: This is an issue with Postmark’s servers processing your request. ' . 'In most cases the message is lost during the process, ' . 'and Postmark is notified so that we can investigate the issue.';
throw $ex;
case 503:
$ex = new PostmarkException();
$ex->httpStatusCode = 503;
$ex->message = 'The Postmark API is currently unavailable, please try your request later.';
throw $ex;
// This should cover case 422, and any others that are possible:
// This should cover case 422, and any others that are possible:
default:
$ex = new PostmarkException();
$body = json_decode($response->getBody(), true);
$ex->httpStatusCode = $response->getStatusCode();
$ex->postmarkApiErrorCode = $body['ErrorCode'];
$ex->message = $body['Message'];
throw $ex;
}
}