Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the accept language parameter #48

Merged
merged 3 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -831,6 +833,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 @@ -864,6 +867,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 @@ -958,6 +962,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
Loading