/** * Making request using Guzzle * * @param string $method Type of request, either POST, GET, PUT or DELETE * @param string $endpoint The operation / task for API * @param array $data The parameter need to be passed * @param array $options The options like header, body, etc * * @return EntityBodyInterface|string * @throws \Exception */ private function sendRequest($method, $endpoint, array $data = array(), array $options = array()) { $uri = $this->buildUri($endpoint); if ($method === "GET" || $method === "PUT") { $uri = $this->buildUri($endpoint, $data); } elseif (count($data)) { $options['body'] = $data; } $this->request = $this->client->createRequest($method, $uri, $options); $this->response = $this->client->send($this->request); if ($this->response->getStatusCode() >= 400) { $bt = debug_backtrace(); $caller = $bt[2]; if (isset($caller['class']) && $caller['class'] === get_class(new StacklaModel())) { $json = (string) $this->response->getBody(); if (JsonValidator::validate($json, true)) { $content = json_decode($json, true); if (isset($content['errors'])) { $caller['object']->fromArray($content); } } } if ($this->logger) { $this->logger->addError('-> REQUEST [' . $this->request->getMethod() . ' - ' . $this->request->getUrl() . "]", array($this->request->getMethod() !== "GET" ? (string) $this->request->getBody() : "")); $this->logger->addError('<- RESPONSE [' . $this->response->getStatusCode() . ':' . $this->response->getReasonPhrase() . "]", array((string) $this->response->getBody())); } } else { if ($this->logger) { $this->logger->addInfo('-> REQUEST [' . $this->request->getMethod() . ' - ' . $this->request->getUrl() . "]", array($this->request->getMethod() !== "GET" ? (string) $this->request->getBody() : "")); $this->logger->addInfo('<- RESPONSE [' . $this->response->getStatusCode() . ':' . $this->response->getReasonPhrase() . "]", array($this->response->json())); } } $statusCode = $this->response->getStatusCode(); switch ($statusCode) { case 200: return (string) $this->response->getBody(); case 400: throw ApiException::create(sprintf("Server return %s error code. Bad request: The request could not be understood. %s", $this->response->getStatusCode(), (string) $this->response->getBody()), $statusCode, (string) $this->response->getBody()); case 401: throw ApiException::create(sprintf("Server return %s error code. Unauthorized: Authentication credentials invalid or not authorised to access resource", $this->response->getStatusCode()), $statusCode, (string) $this->response->getBody()); case 403: throw ApiException::create(sprintf("\n Server return %s error code. Rate limit exceeded: Too many requests in the current time window", $this->response->getStatusCode()), $statusCode, (string) $this->response->getBody()); case 404: throw ApiException::create(sprintf("Server return %s error code. Invalid resource: Invalid resource specified or resource not found", $this->response->getStatusCode()), $statusCode, (string) $this->response->getBody()); default: throw ApiException::create(sprintf("Server return %s error code.Server error: An error on the server prohibited a successful response; please contact support. %s", $this->response->getStatusCode(), (string) $this->response->getBody()), $statusCode, (string) $this->response->getBody()); } }