* @author Sebastian Ertner * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) * @link https://www.temando.com/ */ class Adapter implements CarrierApiInterface, CompletionApiInterface, ContainerApiInterface, LocationApiInterface, EventStreamApiInterface { /** * @var string */ private $endpoint; /** * @var string */ private $accountId; /** * @var string */ private $bearerToken; /** * @var RequestHeadersInterface */ private $requestHeaders; /** * @var AuthenticationInterface */ private $auth; /** * @var RestClientInterface */ private $restClient; /** * @var ParserInterface */ private $responseParser; /** * @var LoggerInterface */ private $logger; /** * Adapter constructor. * @param WsConfigInterface $config * @param RequestHeadersInterface $requestHeaders * @param AuthenticationInterface $auth * @param RestClientInterface $restClient * @param ParserInterface $responseParser * @param LoggerInterface $logger */ public function __construct( WsConfigInterface $config, RequestHeadersInterface $requestHeaders, AuthenticationInterface $auth, RestClientInterface $restClient, ParserInterface $responseParser, LoggerInterface $logger ) { $this->endpoint = $config->getApiEndpoint(); $this->accountId = $config->getAccountId(); $this->bearerToken = $config->getBearerToken(); $this->requestHeaders = $requestHeaders; $this->auth = $auth; $this->restClient = $restClient; $this->responseParser = $responseParser; $this->logger = $logger; } /** * @param ListRequestInterface $request * @return CarrierConfiguration[] * @throws AdapterException */ public function getCarrierConfigurations(ListRequestInterface $request) { $uri = sprintf('%s/carriers/configuration', $this->endpoint); $queryParams = $request->getRequestParams(); $this->logger->log(LogLevel::DEBUG, sprintf('%s?%s', $uri, http_build_query($queryParams))); try { $this->auth->connect($this->accountId, $this->bearerToken); $headers = $this->requestHeaders->getHeaders(); $rawResponse = $this->restClient->get($uri, $queryParams, $headers); $this->logger->log(LogLevel::DEBUG, $rawResponse); /** @var GetCarrierConfigurations $response */ $response = $this->responseParser->parse($rawResponse, GetCarrierConfigurations::class); $configurations = $response->getData(); } catch (RestClientErrorException $e) { $this->logger->log(LogLevel::ERROR, $e->getMessage()); /** @var Errors $response */ $response = $this->responseParser->parse($e->getMessage(), Errors::class); throw AdapterException::errorResponse($response, $e); } catch (\Exception $e) { $this->logger->critical($e->getMessage(), ['exception' => $e]); $configurations = []; } return $configurations; } /** * @param ListRequestInterface $request * @return CarrierIntegration[] * @throws AdapterException */ public function getCarrierIntegrations(ListRequestInterface $request) { $uri = sprintf('%s/carriers', $this->endpoint); $queryParams = $request->getRequestParams(); $this->logger->log(LogLevel::DEBUG, sprintf('%s?%s', $uri, http_build_query($queryParams))); try { $this->auth->connect($this->accountId, $this->bearerToken); $headers = $this->requestHeaders->getHeaders(); $rawResponse = $this->restClient->get($uri, $queryParams, $headers); $this->logger->log(LogLevel::DEBUG, $rawResponse); /** @var GetCarrierIntegrations $response */ $response = $this->responseParser->parse($rawResponse, GetCarrierIntegrations::class); $carriers = $response->getData(); } catch (RestClientErrorException $e) { $this->logger->log(LogLevel::ERROR, $e->getMessage()); /** @var Errors $response */ $response = $this->responseParser->parse($e->getMessage(), Errors::class); throw AdapterException::errorResponse($response, $e); } catch (\Exception $e) { $this->logger->critical($e->getMessage(), ['exception' => $e]); $carriers = []; } return $carriers; } /** * @param ItemRequestInterface $request * @return void * @throws AdapterException */ public function deleteCarrierConfiguration(ItemRequestInterface $request) { $uri = sprintf('%s/carriers/configuration/%s', $this->endpoint, ...$request->getPathParams()); $this->logger->log(LogLevel::DEBUG, $uri); try { $this->auth->connect($this->accountId, $this->bearerToken); $headers = $this->requestHeaders->getHeaders(); $this->restClient->delete($uri, $headers); } catch (RestClientErrorException $e) { $this->logger->log(LogLevel::ERROR, $e->getMessage()); /** @var Errors $response */ $response = $this->responseParser->parse($e->getMessage(), Errors::class); throw AdapterException::errorResponse($response, $e); } catch (\Exception $e) { throw AdapterException::create($e); } } /** * @param ListRequestInterface $request * @return Location[] * @throws AdapterException */ public function getLocations(ListRequestInterface $request) { $uri = sprintf('%s/locations', $this->endpoint); $queryParams = $request->getRequestParams(); $this->logger->log(LogLevel::DEBUG, sprintf('%s?%s', $uri, http_build_query($queryParams))); try { $this->auth->connect($this->accountId, $this->bearerToken); $headers = $this->requestHeaders->getHeaders(); $rawResponse = $this->restClient->get($uri, $queryParams, $headers); $this->logger->log(LogLevel::DEBUG, $rawResponse); /** @var GetLocations $response */ $response = $this->responseParser->parse($rawResponse, GetLocations::class); $locations = $response->getData(); } catch (RestClientErrorException $e) { $this->logger->log(LogLevel::ERROR, $e->getMessage()); /** @var Errors $response */ $response = $this->responseParser->parse($e->getMessage(), Errors::class); throw AdapterException::errorResponse($response, $e); } catch (\Exception $e) { $this->logger->critical($e->getMessage(), ['exception' => $e]); $locations = []; } return $locations; } /** * @param ItemRequestInterface $request * * @return void * @throws AdapterException */ public function deleteLocation(ItemRequestInterface $request) { $uri = sprintf('%s/locations/%s', $this->endpoint, ...$request->getPathParams()); $this->logger->log(LogLevel::DEBUG, $uri); try { $this->auth->connect($this->accountId, $this->bearerToken); $headers = $this->requestHeaders->getHeaders(); $this->restClient->delete($uri, $headers); } catch (RestClientErrorException $e) { $this->logger->log(LogLevel::ERROR, $e->getMessage(), ['exception' => $e]); /** @var Errors $response */ $response = $this->responseParser->parse($e->getMessage(), Errors::class); throw AdapterException::errorResponse($response, $e); } catch (\Exception $e) { throw AdapterException::create($e); } } /** * @param ListRequestInterface $request * @return Container[] * @throws AdapterException */ public function getContainers(ListRequestInterface $request) { $uri = sprintf('%s/containers', $this->endpoint); $queryParams = $request->getRequestParams(); $this->logger->log(LogLevel::DEBUG, sprintf('%s?%s', $uri, http_build_query($queryParams))); try { $this->auth->connect($this->accountId, $this->bearerToken); $headers = $this->requestHeaders->getHeaders(); $rawResponse = $this->restClient->get($uri, $queryParams, $headers); $this->logger->log(LogLevel::DEBUG, $rawResponse); /** @var GetContainers $response */ $response = $this->responseParser->parse($rawResponse, GetContainers::class); $containers = $response->getData(); } catch (RestClientErrorException $e) { $this->logger->log(LogLevel::ERROR, $e->getMessage()); /** @var Errors $response */ $response = $this->responseParser->parse($e->getMessage(), Errors::class); throw AdapterException::errorResponse($response, $e); } catch (\Exception $e) { $this->logger->critical($e->getMessage(), ['exception' => $e]); $containers = []; } return $containers; } /** * @param ItemRequestInterface $request * * @return void * @throws AdapterException */ public function deleteContainer(ItemRequestInterface $request) { $uri = sprintf('%s/containers/%s', $this->endpoint, ...$request->getPathParams()); $this->logger->log(LogLevel::DEBUG, $uri); try { $this->auth->connect($this->accountId, $this->bearerToken); $headers = $this->requestHeaders->getHeaders(); $this->restClient->delete($uri, $headers); } catch (RestClientErrorException $e) { $this->logger->log(LogLevel::ERROR, $e->getMessage()); /** @var Errors $response */ $response = $this->responseParser->parse($e->getMessage(), Errors::class); throw AdapterException::errorResponse($response, $e); } catch (\Exception $e) { throw AdapterException::create($e); } } /** * @param ListRequestInterface $request * @return Completion[] * @throws AdapterException */ public function getCompletions(ListRequestInterface $request) { $uri = sprintf('%s/completions', $this->endpoint); $queryParams = $request->getRequestParams(); $queryParams['filter'] = rawurlencode(json_encode($queryParams['filter'])); $this->logger->log(LogLevel::DEBUG, sprintf('%s?%s', $uri, http_build_query($queryParams))); try { $this->auth->connect($this->accountId, $this->bearerToken); $headers = $this->requestHeaders->getHeaders(); $rawResponse = $this->restClient->get($uri, $queryParams, $headers); $this->logger->log(LogLevel::DEBUG, $rawResponse); /** @var GetCompletions $response */ $response = $this->responseParser->parse($rawResponse, GetCompletions::class); $completions = $response->getData(); } catch (RestClientErrorException $e) { $this->logger->log(LogLevel::ERROR, $e->getMessage()); /** @var Errors $response */ $response = $this->responseParser->parse($e->getMessage(), Errors::class); throw AdapterException::errorResponse($response, $e); } catch (\Exception $e) { $this->logger->critical($e->getMessage(), ['exception' => $e]); $completions = []; } return $completions; } /** * @param ItemRequestInterface $request * @return Completion * @throws AdapterException */ public function getCompletion(ItemRequestInterface $request) { $uri = sprintf('%s/completions/%s', $this->endpoint, ...$request->getPathParams()); $this->logger->log(LogLevel::DEBUG, $uri); try { $this->auth->connect($this->accountId, $this->bearerToken); $headers = $this->requestHeaders->getHeaders(); $rawResponse = $this->restClient->get($uri, [], $headers); $this->logger->log(LogLevel::DEBUG, $rawResponse); /** @var GetCompletion $response */ $response = $this->responseParser->parse($rawResponse, GetCompletion::class); $completion = $response->getData(); } catch (RestClientErrorException $e) { $this->logger->log(LogLevel::ERROR, $e->getMessage()); /** @var Errors $response */ $response = $this->responseParser->parse($e->getMessage(), Errors::class); throw AdapterException::errorResponse($response, $e); } catch (\Exception $e) { throw AdapterException::create($e); } return $completion; } /** * @param StreamCreateRequestInterface $request * @return void * @throws AdapterException */ public function createStream(StreamCreateRequestInterface $request) { $uri = sprintf('%s/streams', $this->endpoint); $requestBody = $request->getRequestBody(); $this->logger->log(LogLevel::DEBUG, sprintf("%s\n%s", $uri, $requestBody)); try { $this->auth->connect($this->accountId, $this->bearerToken); $headers = $this->requestHeaders->getHeaders(); $rawResponse = $this->restClient->post($uri, $requestBody, $headers); $this->logger->log(LogLevel::DEBUG, $rawResponse); } catch (RestClientErrorException $e) { $this->logger->log(LogLevel::ERROR, $e->getMessage()); /** @var Errors $response */ $response = $this->responseParser->parse($e->getMessage(), Errors::class); throw AdapterException::errorResponse($response, $e); } catch (\Exception $e) { throw AdapterException::create($e); } } /** * @param ItemRequestInterface $request * @return void * @throws AdapterException */ public function deleteStream(ItemRequestInterface $request) { $uri = sprintf('%s/streams/%s', $this->endpoint, ...$request->getPathParams()); $this->logger->log(LogLevel::DEBUG, $uri); try { $this->auth->connect($this->accountId, $this->bearerToken); $headers = $this->requestHeaders->getHeaders(); $this->restClient->delete($uri, $headers); } catch (RestClientErrorException $e) { $this->logger->log(LogLevel::ERROR, $e->getMessage()); /** @var Errors $response */ $response = $this->responseParser->parse($e->getMessage(), Errors::class); throw AdapterException::errorResponse($response, $e); } catch (\Exception $e) { throw AdapterException::create($e); } } /** * @param ListRequestInterface $request * * @return StreamEvent[] * @throws AdapterException */ public function getStreamEvents(ListRequestInterface $request) { $uri = sprintf('%s/streams/%s/events', $this->endpoint, ...$request->getPathParams()); $queryParams = $request->getRequestParams(); $this->logger->log(LogLevel::DEBUG, sprintf('%s?%s', $uri, http_build_query($queryParams))); try { $this->auth->connect($this->accountId, $this->bearerToken); $headers = $this->requestHeaders->getHeaders(); $rawResponse = $this->restClient->get($uri, $queryParams, $headers); $this->logger->log(LogLevel::DEBUG, $rawResponse); /** @var GetStreamEvents $response */ $response = $this->responseParser->parse($rawResponse, GetStreamEvents::class); $events = $response->getData(); } catch (RestClientErrorException $e) { $this->logger->log(LogLevel::ERROR, $e->getMessage()); /** @var Errors $response */ $response = $this->responseParser->parse($e->getMessage(), Errors::class); throw AdapterException::errorResponse($response, $e); } catch (\Exception $e) { $this->logger->critical($e->getMessage(), ['exception' => $e]); $events = []; } return $events; } /** * @param StreamEventItemRequest $request * @return void * @throws AdapterException */ public function deleteStreamEvent(StreamEventItemRequest $request) { $uri = sprintf('%s/streams/%s/events/%s', $this->endpoint, ...$request->getPathParams()); $this->logger->log(LogLevel::DEBUG, $uri); try { $this->auth->connect($this->accountId, $this->bearerToken); $headers = $this->requestHeaders->getHeaders(); $this->restClient->delete($uri, $headers); } catch (RestClientErrorException $e) { $this->logger->log(LogLevel::ERROR, $e->getMessage()); /** @var Errors $response */ $response = $this->responseParser->parse($e->getMessage(), Errors::class); throw AdapterException::errorResponse($response, $e); } catch (\Exception $e) { throw AdapterException::create($e); } } }