Skip to content

Commit

Permalink
Merge pull request #48 from jobjen02/accept-language
Browse files Browse the repository at this point in the history
Use the accept language parameter
  • Loading branch information
casperbakker authored Jul 5, 2024
2 parents 819f346 + 94f1497 commit c38d156
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/BaseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,11 @@ private function prepareAndExecuteRequest(string $method, string $url, array $op
'Authorization' => sprintf('Bearer %s', $this->accessToken->getToken()),
];

// pass through Accept-Language header
if (!empty($options['language'])) {
$httpOptions['headers']['Accept-Language'] = $options['language'];
}

// encode the body if a model is supplied for it
if (isset($options['body']) && $options['body'] instanceof AbstractModel) {
$httpOptions['headers']['Content-Type'] = $options['consumes'] ?? static::API_CONTENT_TYPE_FALLBACK;
Expand Down
5 changes: 5 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public function getCatalogProduct(string $ean, ?string $AcceptLanguage = null):
$url = "retailer/content/catalog-products/{$ean}";
$options = [
'produces' => 'application/vnd.retailer.v10+json',
'language' => $AcceptLanguage,
];
$responseTypes = [
'200' => Model\CatalogProduct::class,
Expand Down Expand Up @@ -280,6 +281,7 @@ public function getProductRanks(string $ean, string $date, ?Enum\GetProductRanks
'page' => $page,
],
'produces' => 'application/vnd.retailer.v10+json',
'language' => $AcceptLanguage,
];
$responseTypes = [
'200' => Model\ProductRanks::class,
Expand Down Expand Up @@ -833,6 +835,7 @@ public function getProductList(Model\ProductListRequest $productListRequest, ?st
'body' => $productListRequest,
'produces' => 'application/vnd.retailer.v10+json',
'consumes' => 'application/json',
'language' => $AcceptLanguage,
];
$responseTypes = [
'200' => Model\ProductListResponse::class,
Expand Down Expand Up @@ -866,6 +869,7 @@ public function getProductListFilters(?Enum\GetProductListFiltersCountryCode $co
'category-id' => $categoryId,
],
'produces' => 'application/vnd.retailer.v10+json',
'language' => $AcceptLanguage,
];
$responseTypes = [
'200' => Model\ProductListFiltersResponse::class,
Expand Down Expand Up @@ -960,6 +964,7 @@ public function getProductPlacement(string $ean, ?Enum\GetProductPlacementCountr
'country-code' => $countryCode?->value,
],
'produces' => 'application/vnd.retailer.v10+json',
'language' => $AcceptLanguage,
];
$responseTypes = [
'200' => Model\ProductPlacementResponse::class,
Expand Down
27 changes: 27 additions & 0 deletions src/OpenApi/ClientGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,27 @@ public function generateClient()
file_put_contents(__DIR__ . '/../Client.php', implode("\n", $code));
}


/**
* @param array $methodDefinition
* @param string $parameterName;
* @return array|null
*/
public function findMethodDefinitionParameter(array $methodDefinition, string $parameterName): array|null
{
if (empty($methodDefinition['parameters'])) {
return null;
}

foreach ($methodDefinition['parameters'] as $parameter) {
if ($parameter['name'] === $parameterName) {
return $parameter;
}
}

return null;
}

protected function generateMethod(string $path, string $httpMethod, array &$code): void
{
$methodDefinition = $this->specs['paths'][$path][$httpMethod];
Expand Down Expand Up @@ -122,6 +143,12 @@ protected function generateMethod(string $path, string $httpMethod, array &$code
$code[] = ' ];';
$options = '$options';

$acceptLanguage = $this->findMethodDefinitionParameter($methodDefinition, 'Accept-Language');

if ($acceptLanguage !== null) {
$code[] = sprintf(' \'language\' => %s,', '$AcceptLanguage');
}

$this->addResponseTypes($methodDefinition['responses'], $code);

$code[] = '';
Expand Down

0 comments on commit c38d156

Please sign in to comment.