Skip to content

Commit

Permalink
Add x-amzn-shipping-business-id header param to ShippingV2 requests
Browse files Browse the repository at this point in the history
  • Loading branch information
jlevers committed Jun 29, 2024
1 parent 29a2872 commit c498373
Show file tree
Hide file tree
Showing 16 changed files with 279 additions and 60 deletions.
5 changes: 4 additions & 1 deletion resources/generator-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
"namespaceSuffixes": {
"resource": null
},
"datetimeFormat": "Y-m-d\\TH:i:s\\Z"
"datetimeFormat": "Y-m-d\\TH:i:s\\Z",
"ignoredParams": {
"header": ["x-amz-access-token"]
}
}
56 changes: 48 additions & 8 deletions resources/metadata/modifications.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
{
"seller": {
"catalog-items": {
"2022-04-01": [
{
"comment": "Remove required 'Pagination' and 'Refinements', see https://github.com/jlevers/selling-partner-api/issues/740",
"action": "replace",
"path": "components.schemas.ItemSearchResults.required",
"value": [
"items",
"numberOfResults"
]
}
]
},
"fba-inbound": {
"0": [
{
Expand Down Expand Up @@ -74,16 +87,43 @@
}
]
},
"catalog-items": {
"2022-04-01": [
"shipping": {
"2": [
{
"comment": "Remove required 'Pagination' and 'Refinements', see https://github.com/jlevers/selling-partner-api/issues/740",
"comment": "Make the x-amzn-shipping-business-id header optional, because making it required would break backwards compatibility. This will be removed in the next major release.",
"action": "replace",
"path": "components.schemas.ItemSearchResults.required",
"value": [
"items",
"numberOfResults"
]
"path": "paths./shipping/v2/shipments/rates.post.parameters.1.required",
"value": false
},
{
"comment": "Make the x-amzn-shipping-business-id header optional, because making it required would break backwards compatibility. This will be removed in the next major release.",
"action": "replace",
"path": "paths./shipping/v2/shipments.post.parameters.1.required",
"value": false
},
{
"comment": "Make the x-amzn-shipping-business-id header optional, because making it required would break backwards compatibility. This will be removed in the next major release.",
"action": "replace",
"path": "paths./shipping/v2/oneClickShipment.post.parameters.1.required",
"value": false
},
{
"comment": "Make the x-amzn-shipping-business-id header optional, because making it required would break backwards compatibility. This will be removed in the next major release.",
"action": "replace",
"path": "paths./shipping/v2/shipments/{shipmentId}/documents.get.parameters.5.required",
"value": false
},
{
"comment": "Make the x-amzn-shipping-business-id header optional, because making it required would break backwards compatibility. This will be removed in the next major release.",
"action": "replace",
"path": "paths./shipping/v2/shipments/{shipmentId}/cancel.put.parameters.2.required",
"value": false
},
{
"comment": "Make the x-amzn-shipping-business-id header optional, because making it required would break backwards compatibility. This will be removed in the next major release.",
"action": "replace",
"path": "paths./shipping/v2/accessPoints.get.parameters.4.required",
"value": false
}
]
}
Expand Down
12 changes: 6 additions & 6 deletions resources/models/seller/shipping/v2.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"name": "x-amzn-shipping-business-id",
"in": "header",
"description": "Amazon shipping business to assume for this request. The default is AmazonShipping_UK.",
"required": true,
"required": false,
"schema": {
"type": "string",
"enum": [
Expand Down Expand Up @@ -598,7 +598,7 @@
"name": "x-amzn-shipping-business-id",
"in": "header",
"description": "Amazon shipping business to assume for this request. The default is AmazonShipping_UK.",
"required": true,
"required": false,
"schema": {
"type": "string",
"enum": [
Expand Down Expand Up @@ -1036,7 +1036,7 @@
"name": "x-amzn-shipping-business-id",
"in": "header",
"description": "Amazon shipping business to assume for this request. The default is AmazonShipping_UK.",
"required": true,
"required": false,
"schema": {
"type": "string",
"enum": [
Expand Down Expand Up @@ -1984,7 +1984,7 @@
"name": "x-amzn-shipping-business-id",
"in": "header",
"description": "Amazon shipping business to assume for this request. The default is AmazonShipping_UK.",
"required": true,
"required": false,
"schema": {
"type": "string",
"enum": [
Expand Down Expand Up @@ -2408,7 +2408,7 @@
"name": "x-amzn-shipping-business-id",
"in": "header",
"description": "Amazon shipping business to assume for this request. The default is AmazonShipping_UK.",
"required": true,
"required": false,
"schema": {
"type": "string",
"enum": [
Expand Down Expand Up @@ -2920,7 +2920,7 @@
"name": "x-amzn-shipping-business-id",
"in": "header",
"description": "Amazon shipping business to assume for this request. The default is AmazonShipping_UK.",
"required": true,
"required": false,
"schema": {
"type": "string",
"enum": [
Expand Down
34 changes: 32 additions & 2 deletions src/Generator/Generators/RequestGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Crescat\SaloonSdkGenerator\Data\Generator\Endpoint;
use Crescat\SaloonSdkGenerator\Generators\RequestGenerator as SDKGenerator;
use Crescat\SaloonSdkGenerator\Helpers\MethodGeneratorHelper;
use Crescat\SaloonSdkGenerator\Helpers\NameHelper;
use Exception;
use Illuminate\Support\Collection;
Expand All @@ -24,6 +25,8 @@ class RequestGenerator extends SDKGenerator
{
protected function generateRequestClass(Endpoint $endpoint): PhpFile
{
$methods = [];

$middleware = json_decode(file_get_contents(METADATA_DIR.'/middleware.json'));
$grantlessOperations = json_decode(file_get_contents(METADATA_DIR.'/scopes.json'));
$restrictedOperations = json_decode(file_get_contents(METADATA_DIR.'/restricted.json'));
Expand Down Expand Up @@ -55,6 +58,7 @@ protected function generateRequestClass(Endpoint $endpoint): PhpFile
);

$constructor = $this->generateConstructor($endpoint, $classType);
$methods['__construct'] = $constructor;

$path = $this->buildGenericPath($endpoint->pathSegments);
$httpMethod = strtolower($endpoint->method->value);
Expand Down Expand Up @@ -101,9 +105,10 @@ protected function generateRequestClass(Endpoint $endpoint): PhpFile
// Remove the constructor if it's not being used
if (count($constructor->getParameters()) === 0 && $constructor->getBody() === '') {
$classType->removeMethod('__construct');
unset($methods['__construct']);
}

$classType->addMethod('resolveEndpoint')
$methods['resolveEndpoint'] = $classType->addMethod('resolveEndpoint')
->setPublic()
->setReturnType('string')
->addBody(
Expand Down Expand Up @@ -168,11 +173,12 @@ protected function generateRequestClass(Endpoint $endpoint): PhpFile
$createDtoMethod
->addParameter('response')
->setType(Response::class);
$methods['createDtoFromResponse'] = $createDtoMethod;

if ($endpoint->bodySchema) {
$returnValText = $this->generateDefaultBody($endpoint->bodySchema);

$classType
$methods['defaultBody'] = $classType
->addMethod('defaultBody')
->setReturnType('array')
->addBody(
Expand All @@ -183,6 +189,30 @@ protected function generateRequestClass(Endpoint $endpoint): PhpFile
$namespace->addUse($bodyFQN);
}

if ($endpoint->queryParameters) {
$methods['defaultQuery'] = MethodGeneratorHelper::generateArrayReturnMethod(
$classType,
'defaultQuery',
$this->queryParams,
$this->config->datetimeFormat,
withArrayFilterWrapper: true
);
}

if ($endpoint->headerParameters) {
$methods['defaultHeaders'] = MethodGeneratorHelper::generateArrayReturnMethod(
$classType,
'defaultHeaders',
$this->headerParams,
$this->config->datetimeFormat,
withArrayFilterWrapper: true
);
}

// By explicitly setting the list of methods here, we control the order they are rendered in the class.
// Without this, they would be rendered in whatever order they were created.
$classType->setMethods($methods);

$namespace
->addUse(SaloonHttpMethod::class)
->addUse(Request::class)
Expand Down
10 changes: 9 additions & 1 deletion src/Generator/Generators/ResourceGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,15 @@ public function generateResourceClass(string $resourceName, array $endpoints): ?
}

foreach ($endpoint->queryParameters as $parameter) {
if (in_array($parameter->name, $this->config->ignoredQueryParams)) {
if (in_array($parameter->name, $this->config->ignoredParams['query'])) {
continue;
}
MethodGeneratorHelper::addParameterToMethod($method, $parameter);
$args[] = new Literal(sprintf('$%s', NameHelper::safeVariableName($parameter->name)));
}

foreach ($endpoint->headerParameters as $parameter) {
if (in_array($parameter->rawName, $this->config->ignoredParams['header'])) {
continue;
}
MethodGeneratorHelper::addParameterToMethod($method, $parameter);
Expand Down
69 changes: 47 additions & 22 deletions src/Seller/ShippingV2/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,41 +22,49 @@ class Api extends BaseResource
{
/**
* @param GetRatesRequest $getRatesRequest The request schema for the getRates operation. When the channelType is Amazon, the shipTo address is not required and will be ignored.
* @param ?string $xAmznShippingBusinessId Amazon shipping business to assume for this request. The default is AmazonShipping_UK.
*/
public function getRates(GetRatesRequest $getRatesRequest): Response
public function getRates(GetRatesRequest $getRatesRequest, ?string $xAmznShippingBusinessId = null): Response
{
$request = new GetRates($getRatesRequest);
$request = new GetRates($getRatesRequest, $xAmznShippingBusinessId);

return $this->connector->send($request);
}

/**
* @param PurchaseShipmentRequest $purchaseShipmentRequest The request schema for the purchaseShipment operation.
* @param ?string $xAmznShippingBusinessId Amazon shipping business to assume for this request. The default is AmazonShipping_UK.
*/
public function purchaseShipment(PurchaseShipmentRequest $purchaseShipmentRequest): Response
{
$request = new PurchaseShipment($purchaseShipmentRequest);
public function purchaseShipment(
PurchaseShipmentRequest $purchaseShipmentRequest,
?string $xAmznShippingBusinessId = null,
): Response {
$request = new PurchaseShipment($purchaseShipmentRequest, $xAmznShippingBusinessId);

return $this->connector->send($request);
}

/**
* @param OneClickShipmentRequest $oneClickShipmentRequest The request schema for the OneClickShipment operation. When the channelType is not Amazon, shipTo is required and when channelType is Amazon shipTo is ignored.
* @param ?string $xAmznShippingBusinessId Amazon shipping business to assume for this request. The default is AmazonShipping_UK.
*/
public function oneClickShipment(OneClickShipmentRequest $oneClickShipmentRequest): Response
{
$request = new OneClickShipment($oneClickShipmentRequest);
public function oneClickShipment(
OneClickShipmentRequest $oneClickShipmentRequest,
?string $xAmznShippingBusinessId = null,
): Response {
$request = new OneClickShipment($oneClickShipmentRequest, $xAmznShippingBusinessId);

return $this->connector->send($request);
}

/**
* @param string $trackingId A carrier-generated tracking identifier originally returned by the purchaseShipment operation.
* @param string $carrierId A carrier identifier originally returned by the getRates operation for the selected rate.
* @param ?string $xAmznShippingBusinessId Amazon shipping business to assume for this request. The default is AmazonShipping_UK.
*/
public function getTracking(string $trackingId, string $carrierId): Response
public function getTracking(string $trackingId, string $carrierId, ?string $xAmznShippingBusinessId = null): Response
{
$request = new GetTracking($trackingId, $carrierId);
$request = new GetTracking($trackingId, $carrierId, $xAmznShippingBusinessId);

return $this->connector->send($request);
}
Expand All @@ -66,52 +74,69 @@ public function getTracking(string $trackingId, string $carrierId): Response
* @param string $packageClientReferenceId The package client reference identifier originally provided in the request body parameter for the getRates operation.
* @param ?string $format The file format of the document. Must be one of the supported formats returned by the getRates operation.
* @param ?float $dpi The resolution of the document (for example, 300 means 300 dots per inch). Must be one of the supported resolutions returned in the response to the getRates operation.
* @param ?string $xAmznShippingBusinessId Amazon shipping business to assume for this request. The default is AmazonShipping_UK.
*/
public function getShipmentDocuments(
string $shipmentId,
string $packageClientReferenceId,
?string $format = null,
?float $dpi = null,
?string $xAmznShippingBusinessId = null,
): Response {
$request = new GetShipmentDocuments($shipmentId, $packageClientReferenceId, $format, $dpi);
$request = new GetShipmentDocuments($shipmentId, $packageClientReferenceId, $format, $dpi, $xAmznShippingBusinessId);

return $this->connector->send($request);
}

/**
* @param string $shipmentId The shipment identifier originally returned by the purchaseShipment operation.
* @param ?string $xAmznShippingBusinessId Amazon shipping business to assume for this request. The default is AmazonShipping_UK.
*/
public function cancelShipment(string $shipmentId): Response
public function cancelShipment(string $shipmentId, ?string $xAmznShippingBusinessId = null): Response
{
$request = new CancelShipment($shipmentId);
$request = new CancelShipment($shipmentId, $xAmznShippingBusinessId);

return $this->connector->send($request);
}

public function getAccessPoints(array $accessPointTypes, string $countryCode, string $postalCode): Response
{
$request = new GetAccessPoints($accessPointTypes, $countryCode, $postalCode);
/**
* @param ?string $xAmznShippingBusinessId Amazon shipping business to assume for this request. The default is AmazonShipping_UK.
*/
public function getAccessPoints(
array $accessPointTypes,
string $countryCode,
string $postalCode,
?string $xAmznShippingBusinessId = null,
): Response {
$request = new GetAccessPoints($accessPointTypes, $countryCode, $postalCode, $xAmznShippingBusinessId);

return $this->connector->send($request);
}

/**
* @param SubmitNdrFeedbackRequest $submitNdrFeedbackRequest The request schema for the NdrFeedback operation
* @param ?string $xAmznShippingBusinessId Amazon shipping business to assume for this request. The default is AmazonShipping_UK.
*/
public function submitNdrFeedback(SubmitNdrFeedbackRequest $submitNdrFeedbackRequest): Response
{
$request = new SubmitNdrFeedback($submitNdrFeedbackRequest);
public function submitNdrFeedback(
SubmitNdrFeedbackRequest $submitNdrFeedbackRequest,
?string $xAmznShippingBusinessId = null,
): Response {
$request = new SubmitNdrFeedback($submitNdrFeedbackRequest, $xAmznShippingBusinessId);

return $this->connector->send($request);
}

/**
* @param string $requestToken The request token returned in the response to the getRates operation.
* @param string $rateId The rate identifier for the shipping offering (rate) returned in the response to the getRates operation.
* @param ?string $xAmznShippingBusinessId Amazon shipping business to assume for this request. The default is AmazonShipping_UK.
*/
public function getAdditionalInputs(string $requestToken, string $rateId): Response
{
$request = new GetAdditionalInputs($requestToken, $rateId);
public function getAdditionalInputs(
string $requestToken,
string $rateId,
?string $xAmznShippingBusinessId = null,
): Response {
$request = new GetAdditionalInputs($requestToken, $rateId, $xAmznShippingBusinessId);

return $this->connector->send($request);
}
Expand Down
7 changes: 7 additions & 0 deletions src/Seller/ShippingV2/Requests/CancelShipment.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ class CancelShipment extends Request

/**
* @param string $shipmentId The shipment identifier originally returned by the purchaseShipment operation.
* @param ?string $xAmznShippingBusinessId Amazon shipping business to assume for this request. The default is AmazonShipping_UK.
*/
public function __construct(
protected string $shipmentId,
protected ?string $xAmznShippingBusinessId = null,
) {}

public function resolveEndpoint(): string
Expand All @@ -47,4 +49,9 @@ public function createDtoFromResponse(Response $response): CancelShipmentRespons

return $responseCls::deserialize($response->json(), $responseCls);
}

public function defaultHeaders(): array
{
return array_filter(['x-amzn-shipping-business-id' => $this->xAmznShippingBusinessId]);
}
}
Loading

0 comments on commit c498373

Please sign in to comment.