/** * Make a Http Request * @param string $method The Http verb * @param string $url The relative URL after the host name * @param array|null $apiRequest Contents of the body * @param array|null $queryString Data to add as a queryString to the url * @return mixed * @throws RequiredFieldMissingException * @throws ConnectException * @throws RequestException * @throws \Exception */ public function makeHttpRequest($method, $url, $apiRequest = null, $queryString = null) { $this->apiConfiguration->validate(); $urlEndPoint = $this->apiConfiguration->getApiEndPoint() . '/' . $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 bool $returnResponse If true the CreateAccessTokenResponse will be returned * @return CreateAccessTokenResponse * @throws RequiredFieldMissingException|UnauthorizedClientException */ public function requestAccessToken($returnResponse = false) { $accessTokenResponse = $this->httpRequest->requestAccessToken(); $this->apiConfiguration->setAccessToken($accessTokenResponse->getAccessToken()); $this->httpRequest->setApiConfiguration($this->apiConfiguration); if ($returnResponse) { return $accessTokenResponse; } else { return null; } }