Skip to content

Commit c38d156

Browse files
authored
Merge pull request #48 from jobjen02/accept-language
Use the accept language parameter
2 parents 819f346 + 94f1497 commit c38d156

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

src/BaseClient.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,11 @@ private function prepareAndExecuteRequest(string $method, string $url, array $op
423423
'Authorization' => sprintf('Bearer %s', $this->accessToken->getToken()),
424424
];
425425

426+
// pass through Accept-Language header
427+
if (!empty($options['language'])) {
428+
$httpOptions['headers']['Accept-Language'] = $options['language'];
429+
}
430+
426431
// encode the body if a model is supplied for it
427432
if (isset($options['body']) && $options['body'] instanceof AbstractModel) {
428433
$httpOptions['headers']['Content-Type'] = $options['consumes'] ?? static::API_CONTENT_TYPE_FALLBACK;

src/Client.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ public function getCatalogProduct(string $ean, ?string $AcceptLanguage = null):
105105
$url = "retailer/content/catalog-products/{$ean}";
106106
$options = [
107107
'produces' => 'application/vnd.retailer.v10+json',
108+
'language' => $AcceptLanguage,
108109
];
109110
$responseTypes = [
110111
'200' => Model\CatalogProduct::class,
@@ -280,6 +281,7 @@ public function getProductRanks(string $ean, string $date, ?Enum\GetProductRanks
280281
'page' => $page,
281282
],
282283
'produces' => 'application/vnd.retailer.v10+json',
284+
'language' => $AcceptLanguage,
283285
];
284286
$responseTypes = [
285287
'200' => Model\ProductRanks::class,
@@ -833,6 +835,7 @@ public function getProductList(Model\ProductListRequest $productListRequest, ?st
833835
'body' => $productListRequest,
834836
'produces' => 'application/vnd.retailer.v10+json',
835837
'consumes' => 'application/json',
838+
'language' => $AcceptLanguage,
836839
];
837840
$responseTypes = [
838841
'200' => Model\ProductListResponse::class,
@@ -866,6 +869,7 @@ public function getProductListFilters(?Enum\GetProductListFiltersCountryCode $co
866869
'category-id' => $categoryId,
867870
],
868871
'produces' => 'application/vnd.retailer.v10+json',
872+
'language' => $AcceptLanguage,
869873
];
870874
$responseTypes = [
871875
'200' => Model\ProductListFiltersResponse::class,
@@ -960,6 +964,7 @@ public function getProductPlacement(string $ean, ?Enum\GetProductPlacementCountr
960964
'country-code' => $countryCode?->value,
961965
],
962966
'produces' => 'application/vnd.retailer.v10+json',
967+
'language' => $AcceptLanguage,
963968
];
964969
$responseTypes = [
965970
'200' => Model\ProductPlacementResponse::class,

src/OpenApi/ClientGenerator.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,27 @@ public function generateClient()
6464
file_put_contents(__DIR__ . '/../Client.php', implode("\n", $code));
6565
}
6666

67+
68+
/**
69+
* @param array $methodDefinition
70+
* @param string $parameterName;
71+
* @return array|null
72+
*/
73+
public function findMethodDefinitionParameter(array $methodDefinition, string $parameterName): array|null
74+
{
75+
if (empty($methodDefinition['parameters'])) {
76+
return null;
77+
}
78+
79+
foreach ($methodDefinition['parameters'] as $parameter) {
80+
if ($parameter['name'] === $parameterName) {
81+
return $parameter;
82+
}
83+
}
84+
85+
return null;
86+
}
87+
6788
protected function generateMethod(string $path, string $httpMethod, array &$code): void
6889
{
6990
$methodDefinition = $this->specs['paths'][$path][$httpMethod];
@@ -122,6 +143,12 @@ protected function generateMethod(string $path, string $httpMethod, array &$code
122143
$code[] = ' ];';
123144
$options = '$options';
124145

146+
$acceptLanguage = $this->findMethodDefinitionParameter($methodDefinition, 'Accept-Language');
147+
148+
if ($acceptLanguage !== null) {
149+
$code[] = sprintf(' \'language\' => %s,', '$AcceptLanguage');
150+
}
151+
125152
$this->addResponseTypes($methodDefinition['responses'], $code);
126153

127154
$code[] = '';

0 commit comments

Comments
 (0)