/**
* Send request
*
* @param string $address API address
* @param string $method Request method
* @param \stdClass $body Post body
*
* @return string Request response
* @throws Exception\ConnectionException
* @throws \Exception
*/
public function sendRequest($address, $method = self::METHOD_GET, \stdClass $body = null)
{
// Build request url
$url = "http://{$this->client->getHost()}/api/{$address}";
// Open connection
$this->getAdapter()->open();
// Send and get response
$results = $this->getAdapter()->send($url, $method, $body ? json_encode($body) : null);
$status = $this->getAdapter()->getHttpStatusCode();
$contentType = $this->getAdapter()->getContentType();
// Close connection
$this->getAdapter()->close();
// Throw connection exception if status code isn't 200 or wrong content type
if ($status != 200 || $contentType != 'application/json') {
throw new ConnectionException('Connection failure');
}
// Parse json results
$jsonResults = json_decode($results);
// Get first element in array if it's an array response
if (is_array($jsonResults)) {
$jsonResults = $jsonResults[0];
}
// Get error type
if (isset($jsonResults->error)) {
throw $this->getExceptionByType($jsonResults->error->type, $jsonResults->error->description);
}
// Get success object only if available
if (isset($jsonResults->success)) {
$jsonResults = $jsonResults->success;
}
return $jsonResults;
}