function makeHttpRequest($method, $url, $apiRequest = null, $queryString = null) { $urlEndPoint = $this->apiConfiguration->getDefaultEndPoint() . '/' . $url; $data = ['headers' => ['Authorization' => 'Bearer ' . $this->apiConfiguration->getAccessToken()], 'json' => $apiRequest, 'query' => $queryString]; try { switch ($method) { case 'post': $response = $this->guzzle->post($urlEndPoint, $data); break; case 'put': $response = $this->guzzle->put($urlEndPoint, $data); break; case 'delete': $response = $this->guzzle->delete($urlEndPoint, $data); break; case 'get': $response = $this->guzzle->get($urlEndPoint, $data); break; default: throw new \Exception('Missing request method!'); } if (in_array(current($response->getHeader('Content-Type')), ['image/png', 'image/jpg'])) { $result = $response->getBody()->getContents(); } else { $result = json_decode($response->getBody(), true); } return $result; } catch (ConnectException $c) { throw $c; } catch (RequestException $e) { throw $e; } }
/** * @param string $method The Http verb * @param string $url The relative URL after the host name * @param array|null $payload Contents of the body * @param array|null $queryString Data to add as a queryString to the url * @param bool $firstTry * @return mixed * @throws ConnectException * @throws RequestException * @throws UnauthorizedClientException */ protected function tryRequest($method, $url, $payload = null, $queryString = null, $firstTry = true) { try { if (empty($this->apiConfiguration->getAccessToken())) { $this->refreshAccessToken(); } return $this->httpRequest->makeHttpRequest($method, $url, $payload, $queryString); } catch (ConnectException $c) { throw $c; } catch (RequestException $e) { if ($e->getResponse()->getStatusCode() == 401) { if ($firstTry) { $this->refreshAccessToken(); return $this->tryRequest($method, $url, $payload, $queryString, false); } } else { throw $e; } } }