Skip to content

Commit

Permalink
Merge pull request #11 from webgriffe/decoratable-api-client-no-bc-break
Browse files Browse the repository at this point in the history
Refactor ApiClient implementation to be more generic and decoratable
  • Loading branch information
mmenozzi authored May 4, 2020
2 parents ce79757 + a497b35 commit 40a8c54
Showing 1 changed file with 32 additions and 59 deletions.
91 changes: 32 additions & 59 deletions src/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,40 @@ public function __construct(
* @throws GuzzleException
* @throws \HttpException
*/
public function findProductModel(string $code): ?array
public function authenticatedRequest(string $uri, string $method, array $headers): array
{
if (strpos($uri, '/') === 0) {
$uri = $this->baseUrl . $uri;
}

if (!(bool) $this->token) {
$this->login();
}

$headers = array_merge(
$headers,
[
'Content-Type' => 'application/json',
'Authorization' => sprintf('Bearer %s', $this->token),
]
);
$request = new Request($method, $uri, $headers);
$response = $this->httpClient->send($request);
$statusClass = (int) ($response->getStatusCode() / 100);
$responseResult = json_decode($response->getBody()->getContents(), true);
if ($statusClass !== 2) {
throw new \HttpException($responseResult['message'], $responseResult['code']);
}

return $responseResult;
}

/**
* @throws GuzzleException
* @throws \HttpException
*/
public function findProductModel(string $code): ?array
{
return $this->getResourceOrNull(sprintf('/api/rest/v1/product-models/%s', $code));
}

Expand All @@ -68,10 +96,6 @@ public function findProductModel(string $code): ?array
*/
public function findFamilyVariant(string $familyCode, string $familyVariantCode): ?array
{
if (!(bool) $this->token) {
$this->login();
}

return $this->getResourceOrNull(
sprintf('/api/rest/v1/families/%s/variants/%s', $familyCode, $familyVariantCode)
);
Expand All @@ -83,10 +107,6 @@ public function findFamilyVariant(string $familyCode, string $familyVariantCode)
*/
public function findAttribute(string $code): ?array
{
if (!(bool) $this->token) {
$this->login();
}

return $this->getResourceOrNull(sprintf('/api/rest/v1/attributes/%s', $code));
}

Expand Down Expand Up @@ -120,10 +140,6 @@ public function downloadFile(string $code): \SplFileInfo
*/
public function findProduct(string $code): ?array
{
if (!(bool) $this->token) {
$this->login();
}

return $this->getResourceOrNull(sprintf('/api/rest/v1/products/%s', $code));
}

Expand All @@ -133,10 +149,6 @@ public function findProduct(string $code): ?array
*/
public function findAttributeOption(string $attributeCode, string $optionCode): ?array
{
if (!(bool) $this->token) {
$this->login();
}

return $this->getResourceOrNull(sprintf('/api/rest/v1/attributes/%s/options/%s', $attributeCode, $optionCode));
}

Expand All @@ -146,21 +158,17 @@ public function findAttributeOption(string $attributeCode, string $optionCode):
*/
public function findProductsModifiedSince(\DateTime $date): array
{
if (!(bool) $this->token) {
$this->login();
}

$endpoint = sprintf(
'/api/rest/v1/products?search={"updated":[{"operator":">","value":"%s"}]}&limit=20&page=1',
$date->format('Y-m-d H:i:s')
);
$responseResult = $this->doRequestByEndpoint($endpoint);
$responseResult = $this->authenticatedRequest($endpoint, 'GET', []);
$products = $responseResult['_embedded']['items'];

while ($nextPageUrl = ($responseResult['_links']['next']['href'] ?? null)) {
Assert::string($nextPageUrl);
/** @var string $nextPageUrl */
$responseResult = $this->doRequest($nextPageUrl);
$responseResult = $this->authenticatedRequest($nextPageUrl, 'GET', []);

/** @noinspection SlowArrayOperationsInLoopInspection */
$products = array_merge($products, $responseResult['_embedded']['items']);
Expand Down Expand Up @@ -200,56 +208,21 @@ private function login(): void
$this->token = $responseResult['access_token'];
}

/**
* @return mixed
*
* @throws GuzzleException
* @throws \HttpException
*/
private function doRequestByEndpoint(string $endpoint)
{
return $this->doRequest($this->baseUrl . $endpoint);
}

/**
* @return mixed
*
* @throws GuzzleException
* @throws \HttpException
*/
private function doRequest(string $uri)
{
$headers = [
'Content-Type' => 'application/json',
'Authorization' => sprintf('Bearer %s', $this->token),
];
$request = new Request('GET', $uri, $headers);
$response = $this->httpClient->send($request);
$statusClass = (int) ($response->getStatusCode() / 100);
$responseResult = json_decode($response->getBody()->getContents(), true);
if ($statusClass !== 2) {
throw new \HttpException($responseResult['message'], $responseResult['code']);
}

return $responseResult;
}

/**
* @throws \HttpException
* @throws GuzzleException
*/
private function getResourceOrNull(string $endpoint): ?array
{
try {
$response = $this->doRequestByEndpoint($endpoint);
$response = $this->authenticatedRequest($endpoint, 'GET', []);
} catch (\HttpException $exception) {
if ($exception->getCode() !== 404) {
throw $exception;
}

return null;
}
Assert::isArray($response);

return $response;
}
Expand Down

0 comments on commit 40a8c54

Please sign in to comment.