Skip to content

Commit

Permalink
Merge pull request #34 from picqer/use-consumes-as-content-type
Browse files Browse the repository at this point in the history
Fix: Use Content-Type from specs to prevent 'The supplied content-type media type is not supported.'
  • Loading branch information
robvanaarle committed Jul 13, 2023
2 parents 32ce665 + d50bca0 commit efa3ed4
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/BaseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class BaseClient
{
protected const API_TOKEN_URI = 'https://login.bol.com/token';
protected const API_ENDPOINT = 'https://api.bol.com/';
protected const API_CONTENT_TYPE_JSON = 'application/vnd.retailer.v10+json';
protected const API_CONTENT_TYPE_FALLBACK = 'application/vnd.retailer.v10+json';
protected const API_ACCEPT_FALLBACK = 'application/vnd.retailer.v10+json';

/**
* @var bool Whether request will be sent to the demo endpoint.
Expand Down Expand Up @@ -390,13 +391,13 @@ private function prepareAndExecuteRequest(string $method, string $url, array $op

$httpOptions = [];
$httpOptions['headers'] = [
'Accept' => $options['produces'] ?? static::API_CONTENT_TYPE_JSON,
'Accept' => $options['produces'] ?? static::API_ACCEPT_FALLBACK,
'Authorization' => sprintf('Bearer %s', $this->accessToken->getToken()),
];

// encode the body if a model is supplied for it
if (isset($options['body']) && $options['body'] instanceof AbstractModel) {
$httpOptions['headers']['Content-Type'] = static::API_CONTENT_TYPE_JSON;
$httpOptions['headers']['Content-Type'] = $options['consumes'] ?? static::API_CONTENT_TYPE_FALLBACK;
$httpOptions['body'] = json_encode($options['body']->toArray(true));
}

Expand Down
26 changes: 26 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function getCommissions(array $commissionQueries): array
$options = [
'body' => Model\BulkCommissionRequest::constructFromArray(['commissionQueries' => $commissionQueries]),
'produces' => 'application/vnd.retailer.v9+json',
'consumes' => 'application/vnd.retailer.v9+json',
];
$responseTypes = [
'200' => Model\BulkCommissionResponse::class,
Expand Down Expand Up @@ -105,6 +106,7 @@ public function getChunkRecommendations(array $productContents): array
$options = [
'body' => Model\ChunkRecommendationsRequest::constructFromArray(['productContents' => $productContents]),
'produces' => 'application/vnd.retailer.v9+json',
'consumes' => 'application/vnd.retailer.v9+json',
];
$responseTypes = [
'200' => Model\ChunkRecommendationsResponse::class,
Expand All @@ -129,6 +131,7 @@ public function postProductContent(Model\CreateProductContentSingleRequest $crea
$options = [
'body' => $createProductContentSingleRequest,
'produces' => 'application/vnd.retailer.v9+json',
'consumes' => 'application/vnd.retailer.v9+json',
];
$responseTypes = [
'202' => Model\ProcessStatus::class,
Expand Down Expand Up @@ -430,6 +433,7 @@ public function postOffer(Model\CreateOfferRequest $createOfferRequest): Model\P
$options = [
'body' => $createOfferRequest,
'produces' => 'application/vnd.retailer.v9+json',
'consumes' => 'application/vnd.retailer.v9+json',
];
$responseTypes = [
'202' => Model\ProcessStatus::class,
Expand All @@ -454,6 +458,7 @@ public function postOfferExport(string $format): Model\ProcessStatus
$options = [
'body' => Model\CreateOfferExportRequest::constructFromArray(['format' => $format]),
'produces' => 'application/vnd.retailer.v9+json',
'consumes' => 'application/vnd.retailer.v9+json',
];
$responseTypes = [
'202' => Model\ProcessStatus::class,
Expand Down Expand Up @@ -502,6 +507,7 @@ public function postUnpublishedOfferReport(string $format): Model\ProcessStatus
$options = [
'body' => Model\CreateUnpublishedOfferReportRequest::constructFromArray(['format' => $format]),
'produces' => 'application/vnd.retailer.v9+json',
'consumes' => 'application/vnd.retailer.v9+json',
];
$responseTypes = [
'202' => Model\ProcessStatus::class,
Expand Down Expand Up @@ -575,6 +581,7 @@ public function putOffer(string $offerId, Model\UpdateOfferRequest $updateOfferR
$options = [
'body' => $updateOfferRequest,
'produces' => 'application/vnd.retailer.v9+json',
'consumes' => 'application/vnd.retailer.v9+json',
];
$responseTypes = [
'202' => Model\ProcessStatus::class,
Expand Down Expand Up @@ -623,6 +630,7 @@ public function updateOfferPrice(string $offerId, Model\Pricing $pricing): Model
$options = [
'body' => Model\UpdateOfferPriceRequest::constructFromArray(['pricing' => $pricing]),
'produces' => 'application/vnd.retailer.v9+json',
'consumes' => 'application/vnd.retailer.v9+json',
];
$responseTypes = [
'202' => Model\ProcessStatus::class,
Expand All @@ -648,6 +656,7 @@ public function updateOfferStock(string $offerId, Model\UpdateOfferStockRequest
$options = [
'body' => $updateOfferStockRequest,
'produces' => 'application/vnd.retailer.v9+json',
'consumes' => 'application/vnd.retailer.v9+json',
];
$responseTypes = [
'202' => Model\ProcessStatus::class,
Expand Down Expand Up @@ -711,6 +720,7 @@ public function cancelOrderItem(array $orderItems): Model\ProcessStatus
$options = [
'body' => Model\ContainerForTheOrderItemsThatHaveToBeCancelled::constructFromArray(['orderItems' => $orderItems]),
'produces' => 'application/vnd.retailer.v9+json',
'consumes' => 'application/vnd.retailer.v9+json',
];
$responseTypes = [
'202' => Model\ProcessStatus::class,
Expand Down Expand Up @@ -738,6 +748,7 @@ public function shipOrderItem(Model\ShipmentRequest $shipmentRequest): Model\Pro
$options = [
'body' => $shipmentRequest,
'produces' => 'application/vnd.retailer.v9+json',
'consumes' => 'application/vnd.retailer.v9+json',
];
$responseTypes = [
'202' => Model\ProcessStatus::class,
Expand Down Expand Up @@ -1103,6 +1114,7 @@ public function postReplenishment(Model\CreateReplenishmentRequest $createReplen
$options = [
'body' => $createReplenishmentRequest,
'produces' => 'application/vnd.retailer.v9+json',
'consumes' => 'application/vnd.retailer.v9+json',
];
$responseTypes = [
'202' => Model\ProcessStatus::class,
Expand Down Expand Up @@ -1149,6 +1161,7 @@ public function postPickupTimeSlots(Model\PickupTimeSlotsRequest $pickupTimeSlot
$options = [
'body' => $pickupTimeSlotsRequest,
'produces' => 'application/vnd.retailer.v9+json',
'consumes' => 'application/vnd.retailer.v9+json',
];
$responseTypes = [
'200' => Model\PickupTimeSlotsResponse::class,
Expand All @@ -1173,6 +1186,7 @@ public function postRequestProductDestinations(array $eans): Model\ProcessStatus
$options = [
'body' => Model\RequestProductDestinationsRequest::constructFromArray(['eans' => $eans]),
'produces' => 'application/vnd.retailer.v9+json',
'consumes' => 'application/vnd.retailer.v9+json',
];
$responseTypes = [
'202' => Model\ProcessStatus::class,
Expand Down Expand Up @@ -1222,6 +1236,7 @@ public function postProductLabels(Model\ProductLabelsRequest $productLabelsReque
$options = [
'body' => $productLabelsRequest,
'produces' => 'application/vnd.retailer.v9+pdf',
'consumes' => 'application/vnd.retailer.v9+json',
];
$responseTypes = [
'200' => 'string',
Expand Down Expand Up @@ -1272,6 +1287,7 @@ public function putReplenishment(string $replenishmentId, Model\UpdateReplenishm
$options = [
'body' => $updateReplenishmentRequest,
'produces' => 'application/vnd.retailer.v9+json',
'consumes' => 'application/vnd.retailer.v9+json',
];
$responseTypes = [
'202' => Model\ProcessStatus::class,
Expand Down Expand Up @@ -1455,6 +1471,7 @@ public function handleReturn(int $rmaId, Model\ReturnRequest $returnRequest): Mo
$options = [
'body' => $returnRequest,
'produces' => 'application/vnd.retailer.v9+json',
'consumes' => 'application/vnd.retailer.v9+json',
];
$responseTypes = [
'202' => Model\ProcessStatus::class,
Expand Down Expand Up @@ -1536,6 +1553,7 @@ public function createShippingLabel(Model\ShippingLabelRequest $shippingLabelReq
$options = [
'body' => $shippingLabelRequest,
'produces' => 'application/vnd.retailer.v9+json',
'consumes' => 'application/vnd.retailer.v9+json',
];
$responseTypes = [
'202' => Model\ProcessStatus::class,
Expand All @@ -1561,6 +1579,7 @@ public function getDeliveryOptions(array $orderItems): array
$options = [
'body' => Model\DeliveryOptionsRequest::constructFromArray(['orderItems' => $orderItems]),
'produces' => 'application/vnd.retailer.v9+json',
'consumes' => 'application/vnd.retailer.v9+json',
];
$responseTypes = [
'200' => Model\DeliveryOptionsResponse::class,
Expand Down Expand Up @@ -1636,6 +1655,7 @@ public function postPushNotificationSubscription(Model\CreateSubscriptionRequest
$options = [
'body' => $createSubscriptionRequest,
'produces' => 'application/vnd.retailer.v9+json',
'consumes' => 'application/vnd.retailer.v9+json',
];
$responseTypes = [
'202' => Model\ProcessStatus::class,
Expand Down Expand Up @@ -1682,6 +1702,7 @@ public function postTestPushNotification(string $subscriptionId): Model\ProcessS
$url = "retailer/subscriptions/test/${subscriptionId}";
$options = [
'produces' => 'application/vnd.retailer.v9+json',
'consumes' => 'application/vnd.retailer.v9+json',
];
$responseTypes = [
'202' => Model\ProcessStatus::class,
Expand Down Expand Up @@ -1732,6 +1753,7 @@ public function putPushNotificationSubscription(string $subscriptionId, Model\Up
$options = [
'body' => $updateSubscriptionRequest,
'produces' => 'application/vnd.retailer.v9+json',
'consumes' => 'application/vnd.retailer.v9+json',
];
$responseTypes = [
'202' => Model\ProcessStatus::class,
Expand All @@ -1755,6 +1777,7 @@ public function deletePushNotificationSubscription(string $subscriptionId): Mode
$url = "retailer/subscriptions/${subscriptionId}";
$options = [
'produces' => 'application/vnd.retailer.v9+json',
'consumes' => 'application/vnd.retailer.v9+json',
];
$responseTypes = [
'202' => Model\ProcessStatus::class,
Expand All @@ -1781,6 +1804,7 @@ public function addTransportInformationByTransportId(string $transportId, Model\
$options = [
'body' => $changeTransportRequest,
'produces' => 'application/vnd.retailer.v9+json',
'consumes' => 'application/vnd.retailer.v9+json',
];
$responseTypes = [
'202' => Model\ProcessStatus::class,
Expand Down Expand Up @@ -1868,6 +1892,7 @@ public function uploadInvoice(string $invoice, string $shipmentId): ?Model\Proce
],
],
'produces' => 'application/vnd.retailer.v10+json',
'consumes' => 'multipart/form-data',
];
$responseTypes = [
'202' => Model\ProcessStatus::class,
Expand Down Expand Up @@ -1932,6 +1957,7 @@ public function getProcessStatusBulk(array $processStatusQueries): array
$options = [
'body' => Model\BulkProcessStatusRequest::constructFromArray(['processStatusQueries' => $processStatusQueries]),
'produces' => 'application/vnd.retailer.v10+json',
'consumes' => 'application/vnd.retailer.v10+json',
];
$responseTypes = [
'200' => Model\ProcessStatusResponse::class,
Expand Down
5 changes: 5 additions & 0 deletions src/OpenApi/ClientGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ protected function generateMethod(string $path, string $httpMethod, array &$code
$this->addBodyParam($arguments, $code);
$this->addFormData($arguments, $code);
$code[] = sprintf(' \'produces\' => \'%s\',', $methodDefinition['produces'][0]);

if ($methodDefinition['consumes'] ?? false) {
$code[] = sprintf(' \'consumes\' => \'%s\',', $methodDefinition['consumes'][0]);
}

$code[] = ' ];';
$options = '$options';

Expand Down
29 changes: 29 additions & 0 deletions tests/BaseClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,35 @@ public function testRequestAddsProducesAsAcceptHeader()
$this->assertEquals('application/vnd.retailer.v10+pdf', $actualOptions['headers']['Accept']);
}

public function testRequestAddsConsumesAsContentTypeHeader()
{
$this->authenticateByClientCredentials();

$model = new $this->modelClass();
$model->foo = 'bar';

$actualOptions = null;
$response = Message::parseResponse(file_get_contents(__DIR__ . '/Fixtures/http/200-string'));
$this->httpClientMock
->expects($this->once())
->method('request')
->willReturnCallback(function ($method, $uri, $options) use ($response, &$actualOptions) {
$actualOptions = $options;
return $response;
});

$this->client->request('POST', 'foobar', [
'body' => $model,
'consumes' => 'application/vnd.retailer.vxx+json'
], [
'200' => 'string'
]);

$this->assertArrayHasKey('headers', $actualOptions);
$this->assertArrayHasKey('Accept', $actualOptions['headers']);
$this->assertEquals('application/vnd.retailer.vxx+json', $actualOptions['headers']['Content-Type']);
}

public function testRequestJsonEncodesBodyModelIntoBody()
{
$this->authenticateByClientCredentials();
Expand Down

0 comments on commit efa3ed4

Please sign in to comment.