Skip to content

Commit

Permalink
Feeds resource
Browse files Browse the repository at this point in the history
  • Loading branch information
keithbrink committed Oct 4, 2021
1 parent 108020b commit 6d3e1da
Show file tree
Hide file tree
Showing 46 changed files with 600 additions and 37 deletions.
3 changes: 2 additions & 1 deletion src/AmznSPA.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* @property \Jasara\AmznSPA\Resources\AuthorizationResource $authorization
* @property \Jasara\AmznSPA\Resources\NotificationsResource $notifications
* @property \Jasara\AmznSPA\Resources\FulfillmentInboundResource $fulfillment_inbound
* @property \Jasara\AmznSPA\Resources\FeedsResource $feeds
*/
class AmznSPA
{
Expand All @@ -29,7 +30,7 @@ public function __get($name)

$resource_getter = new ResourceGetter($this->config);

if (! method_exists($resource_getter, $function)) {
if (!method_exists($resource_getter, $function)) {
throw new InvalidResourceException($name . ' is not a supported resource.');
}

Expand Down
29 changes: 20 additions & 9 deletions src/AmznSPAHttp.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public function post(string $url, array $data): array
return $this->call('post', $url, $data);
}

public function delete(string $url): array
{
return $this->call('delete', $url);
}

public function getGrantless(string $url, array $data = []): array
{
return $this->call(
Expand Down Expand Up @@ -169,16 +174,16 @@ private function setupHttp(Factory $http, bool $grantless = false): void

private function getToken(bool $grantless = false): string
{
if (! $grantless) {
if (!$grantless) {
$tokens = $this->config->getTokens();
if (! $tokens->access_token || ($tokens->expires_at && $tokens->expires_at->subMinutes(5)->isPast())) {
if (!$tokens->access_token || ($tokens->expires_at && $tokens->expires_at->subMinutes(5)->isPast())) {
$this->refreshTokens();
}

return $this->config->getTokens()->access_token;
} else {
$grantless_token = $this->config->getGrantlessToken();
if (! $grantless_token->access_token || ($grantless_token->expires_at && $grantless_token->expires_at->subMinutes(5)->isPast())) {
if (!$grantless_token->access_token || ($grantless_token->expires_at && $grantless_token->expires_at->subMinutes(5)->isPast())) {
$this->refreshGrantlessToken();
}

Expand Down Expand Up @@ -219,14 +224,14 @@ private function signRequest(string $aws_region): void

private function handleRequestException(RequestException $e, bool $grantless)
{
if (! $this->shouldRefreshToken($e->response->json())) {
if (!$this->shouldRefreshToken($e->response->json())) {
throw $e;
}
if (! $this->shouldRetry()) {
if (!$this->shouldRetry()) {
throw $e;
}

if (! $grantless) {
if (!$grantless) {
$this->refreshTokens();
} else {
$this->refreshGrantlessToken();
Expand All @@ -239,7 +244,7 @@ public function shouldReturnErrorResponse(RequestException $e): bool
return false;
}

if (! Arr::get($e->response->json(), 'errors')) {
if (!Arr::get($e->response->json(), 'errors')) {
return false;
}

Expand Down Expand Up @@ -282,7 +287,7 @@ private function getMetadata(Response $response): MetadataSchema

return new MetadataSchema(
jasara_notes: $jasara_notes,
amzn_request_id: $amzn_request_id,
amzn_request_id: $amzn_request_id,
);
}

Expand All @@ -298,7 +303,7 @@ private function handleResponse(Response $response, string $method, string $url)
]);
}

public function cleanData(array $data): array
private function cleanData(array $data): array
{
$filtered_keys = [
'x-amz-access-token' => '[filtered]',
Expand All @@ -308,6 +313,12 @@ public function cleanData(array $data): array
'refresh_token' => '[filtered]',
];

foreach ($data as $key => $value) {
if (is_array($value)) {
$data[$key] = $this->cleanData($value);
}
}

$filtered_data = array_intersect_key($filtered_keys, $data) + $data;

return $filtered_data;
Expand Down
50 changes: 50 additions & 0 deletions src/Constants/AmazonEnums.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,48 @@ class AmazonEnums
'EXTERNAL_FULFILLMENT_SHIPMENT_STATUS_CHANGE',
];

const FEED_TYPES = [
// Listing feeds
'JSON_LISTINGS_FEED',
'POST_PRODUCT_DATA',
'POST_INVENTORY_AVAILABILITY_DATA',
'POST_PRODUCT_OVERRIDES_DATA',
'POST_PRODUCT_PRICING_DATA',
'POST_PRODUCT_IMAGE_DATA',
'POST_PRODUCT_RELATIONSHIP_DATA',
'POST_FLAT_FILE_INVLOADER_DATA',
'POST_FLAT_FILE_LISTINGS_DATA',
'POST_FLAT_FILE_BOOKLOADER_DATA',
'POST_FLAT_FILE_CONVERGENCE_LISTINGS_DATA',
'POST_FLAT_FILE_LISTINGS_DATA',
'POST_FLAT_FILE_PRICEANDQUANTITYONLY_UPDATE_DATA',
'POST_UIEE_BOOKLOADER_DATA',
'POST_STD_ACES_DATA',
// Order feeds
'POST_ORDER_ACKNOWLEDGEMENT_DATA',
'POST_PAYMENT_ADJUSTMENT_DATA',
'POST_ORDER_FULFILLMENT_DATA',
'POST_INVOICE_CONFIRMATION_DATA',
'POST_EXPECTED_SHIP_DATE_SOD',
'POST_FLAT_FILE_ORDER_ACKNOWLEDGEMENT_DATA',
'POST_FLAT_FILE_PAYMENT_ADJUSTMENT_DATA',
'POST_FLAT_FILE_FULFILLMENT_DATA',
'POST_EXPECTED_SHIP_DATE_SOD_FLAT_FILE',
// Fulfillment by Amazon feeds
'POST_FULFILLMENT_ORDER_REQUEST_DATA',
'POST_FULFILLMENT_ORDER_CANCELLATION_REQUEST_DATA',
'POST_FBA_INBOUND_CARTON_CONTENTS',
'POST_FLAT_FILE_FULFILLMENT_ORDER_REQUEST_DATA',
'POST_FLAT_FILE_FULFILLMENT_ORDER_CANCELLATION_REQUEST_DATA',
'POST_FLAT_FILE_FBA_CREATE_INBOUND_PLAN',
'POST_FLAT_FILE_FBA_UPDATE_INBOUND_PLAN',
'POST_FLAT_FILE_FBA_CREATE_REMOVAL',
// Business feeds
'RFQ_UPLOAD_FEED',
// Easy Ship feeds
'POST_EASYSHIP_DOCUMENTS',
];

const PREP_INSTRUCTIONS = [
'Polybagging',
'BubbleWrapping',
Expand Down Expand Up @@ -130,4 +172,12 @@ class AmazonEnums
'2D_BARCODE',
'INTERACTIVE',
];

const PROCESSING_STATUSES = [
'CANCELLED',
'DONE',
'FATAL',
'IN_PROGRESS',
'IN_QUEUE',
];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Jasara\AmznSPA\DataTransferObjects\Requests\Feeds;

use Jasara\AmznSPA\DataTransferObjects\Requests\BaseRequest;
use Jasara\AmznSPA\DataTransferObjects\Validators\StringEnumValidator;

class CreateFeedDocumentSpecification extends BaseRequest
{
public string $content_type;
}
20 changes: 20 additions & 0 deletions src/DataTransferObjects/Requests/Feeds/CreateFeedSpecification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Jasara\AmznSPA\DataTransferObjects\Requests\Feeds;

use Jasara\AmznSPA\Constants\AmazonEnums;
use Jasara\AmznSPA\DataTransferObjects\Requests\BaseRequest;
use Jasara\AmznSPA\DataTransferObjects\Validators\StringArrayEnumValidator;
use Jasara\AmznSPA\DataTransferObjects\Validators\StringEnumValidator;

class CreateFeedSpecification extends BaseRequest
{
#[StringEnumValidator(AmazonEnums::FEED_TYPES)]
public string $feed_type;

public array $marketplace_ids;

public string $input_feed_document_id;

public ?array $feed_options;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Jasara\AmznSPA\DataTransferObjects\Responses\Feeds;

use Jasara\AmznSPA\DataTransferObjects\Responses\BaseResponse;

class CreateFeedDocumentResponse extends BaseResponse
{
public string $feed_document_id;

public string $url;
}
10 changes: 10 additions & 0 deletions src/DataTransferObjects/Responses/Feeds/CreateFeedResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Jasara\AmznSPA\DataTransferObjects\Responses\Feeds;

use Jasara\AmznSPA\DataTransferObjects\Responses\BaseResponse;

class CreateFeedResponse extends BaseResponse
{
public string $feed_id;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Jasara\AmznSPA\DataTransferObjects\Responses\Feeds;

use Jasara\AmznSPA\DataTransferObjects\Responses\BaseResponse;
use Jasara\AmznSPA\DataTransferObjects\Schemas\Feeds\FeedDocumentSchema;

class GetFeedDocumentResponse extends BaseResponse
{
public FeedDocumentSchema $feed_document;
}
15 changes: 15 additions & 0 deletions src/DataTransferObjects/Responses/Feeds/GetFeedResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Jasara\AmznSPA\DataTransferObjects\Responses\Feeds;

use Jasara\AmznSPA\DataTransferObjects\Responses\BaseResponse;
use Jasara\AmznSPA\DataTransferObjects\Schemas\Feeds\FeedListSchema;
use Jasara\AmznSPA\DataTransferObjects\Schemas\Feeds\FeedSchema;
use Spatie\DataTransferObject\Attributes\CastWith;
use Spatie\DataTransferObject\Casters\ArrayCaster;
use Spatie\DataTransferObject\DataTransferObject;

class GetFeedResponse extends BaseResponse
{
public ?FeedSchema $feed;
}
18 changes: 18 additions & 0 deletions src/DataTransferObjects/Responses/Feeds/GetFeedsResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Jasara\AmznSPA\DataTransferObjects\Responses\Feeds;

use Jasara\AmznSPA\DataTransferObjects\Responses\BaseResponse;
use Jasara\AmznSPA\DataTransferObjects\Schemas\Feeds\FeedListSchema;
use Jasara\AmznSPA\DataTransferObjects\Schemas\Feeds\FeedSchema;
use Spatie\DataTransferObject\Attributes\CastWith;
use Spatie\DataTransferObject\Casters\ArrayCaster;
use Spatie\DataTransferObject\DataTransferObject;

class GetFeedsResponse extends BaseResponse
{
#[CastWith(ArrayCaster::class, itemType: FeedSchema::class)]
public FeedListSchema $feeds;

public ?string $next_token;
}
16 changes: 16 additions & 0 deletions src/DataTransferObjects/Schemas/Feeds/FeedDocumentSchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Jasara\AmznSPA\DataTransferObjects\Schemas\Feeds;

use Jasara\AmznSPA\DataTransferObjects\Validators\StringEnumValidator;
use Spatie\DataTransferObject\DataTransferObject;

class FeedDocumentSchema extends DataTransferObject
{
public string $feed_document_id;

public string $url;

#[StringEnumValidator(['GZIP'])]
public ?string $compression_algorithm;
}
16 changes: 16 additions & 0 deletions src/DataTransferObjects/Schemas/Feeds/FeedListSchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Jasara\AmznSPA\DataTransferObjects\Schemas\Feeds;

use Illuminate\Support\Collection;

class FeedListSchema extends Collection
{
/**
* @codeCoverageIgnore
*/
public function offsetGet($key): FeedSchema
{
return parent::offsetGet($key);
}
}
30 changes: 30 additions & 0 deletions src/DataTransferObjects/Schemas/Feeds/FeedSchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Jasara\AmznSPA\DataTransferObjects\Schemas\Feeds;

use Carbon\CarbonImmutable;
use Jasara\AmznSPA\DataTransferObjects\Casts\CarbonFromStringCaster;
use Spatie\DataTransferObject\Attributes\CastWith;
use Spatie\DataTransferObject\DataTransferObject;

class FeedSchema extends DataTransferObject
{
public string $feed_id;

public string $feed_type;

public ?array $marketplace_ids;

#[CastWith(CarbonFromStringCaster::class)]
public CarbonImmutable $created_time;

public string $processing_status;

#[CastWith(CarbonFromStringCaster::class)]
public ?CarbonImmutable $processing_start_time;

#[CastWith(CarbonFromStringCaster::class)]
public ?CarbonImmutable $processing_end_time;

public ?string $result_feed_document_id;
}
Loading

0 comments on commit 6d3e1da

Please sign in to comment.