From b1442ecdf6eb0b5922bc419fe792b866abb63dd0 Mon Sep 17 00:00:00 2001 From: craghurampec Date: Thu, 18 Dec 2025 12:01:42 -0500 Subject: [PATCH 1/2] api session changes --- src/BwtApi.php | 4 ++- src/BwtApiClient.php | 28 +++++++++++++++++-- .../Amazon/AmazonSessionStatusResponse.php | 21 ++++++++++++++ src/DTOs/Amazon/ProductRequestItem.php | 7 +++-- src/DTOs/BatchRequest.php | 1 + 5 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 src/DTOs/Amazon/AmazonSessionStatusResponse.php diff --git a/src/BwtApi.php b/src/BwtApi.php index a65141c..e241d29 100755 --- a/src/BwtApi.php +++ b/src/BwtApi.php @@ -2,4 +2,6 @@ namespace AlwaysOpen\BwtApi; -class BwtApi {} +class BwtApi { + public const BWT_AMAZON_ENGINE = 'bwt_amazon_product'; +} \ No newline at end of file diff --git a/src/BwtApiClient.php b/src/BwtApiClient.php index 5cd03f0..9eb6c57 100644 --- a/src/BwtApiClient.php +++ b/src/BwtApiClient.php @@ -4,6 +4,7 @@ use AlwaysOpen\BwtApi\DTOs\Amazon\AmazonRequestCreationResponse; use AlwaysOpen\BwtApi\DTOs\Amazon\AmazonResults; +use AlwaysOpen\BwtApi\DTOs\Amazon\AmazonSessionStatusResponse; use AlwaysOpen\BwtApi\DTOs\BatchRequest; use GuzzleHttp\Exception\GuzzleException; use GuzzleHttp\Promise\PromiseInterface; @@ -24,14 +25,14 @@ public function __construct( ?string $apiKey = null, ) { $this->baseUrl = $baseUrl ?? config('bwt-api.base_url', 'https://bwt.com/api/'); - $this->apiKey = $apiKey ?? config('bwt-api.username') ?? ''; + $this->apiKey = $apiKey ?? config('bwt-api.api_key') ?? ''; } protected function getAuthHeader(): array { return [ 'Authorization' => 'Bearer '.$this->apiKey, - ]; + ]; } /** @@ -61,6 +62,27 @@ protected function makeRequest( }, 2000); } + public function getAmazonSessionStatus( + string $id, + ): AmazonSessionStatusResponse { + try { + $response = $this->makeRequest( + 'get', + $this->baseUrl .= "/$id", + null, + 3, + ); + } catch (Throwable $e) { + throw new RuntimeException('API request failed: '.$e->getMessage(), $e->getCode(), $e); + } + + if (! $response->successful()) { + throw new RuntimeException('API request failed: '.$response->body(), $response->getStatusCode()); + } + + return AmazonSessionStatusResponse::from($response->json()); + } + public function getAmazonResults( string $id, int $limit = 1, @@ -69,7 +91,7 @@ public function getAmazonResults( try { $response = $this->makeRequest( 'get', - $this->baseUrl .= "$id/results?limit=$limit&offset=$offset", + $this->baseUrl .= "/$id/results?limit=$limit&offset=$offset", null, 3, ); diff --git a/src/DTOs/Amazon/AmazonSessionStatusResponse.php b/src/DTOs/Amazon/AmazonSessionStatusResponse.php new file mode 100644 index 0000000..edaf8ce --- /dev/null +++ b/src/DTOs/Amazon/AmazonSessionStatusResponse.php @@ -0,0 +1,21 @@ +value, public readonly ?int $max_offer_pages = null, ) {} } diff --git a/src/DTOs/BatchRequest.php b/src/DTOs/BatchRequest.php index 4bb5e63..419b623 100644 --- a/src/DTOs/BatchRequest.php +++ b/src/DTOs/BatchRequest.php @@ -2,6 +2,7 @@ namespace AlwaysOpen\BwtApi\DTOs; +use AlwaysOpen\BwtApi\DTOs\Amazon\ProductRequestItem; use Spatie\LaravelData\Data; class BatchRequest extends Data From cf2ca468716da336b98ab921ffa505acd7660d15 Mon Sep 17 00:00:00 2001 From: craghurampec <151176973+craghurampec@users.noreply.github.com> Date: Thu, 18 Dec 2025 17:02:03 +0000 Subject: [PATCH 2/2] Fix styling --- src/BwtApi.php | 5 +++-- src/BwtApiClient.php | 2 +- src/DTOs/Amazon/AmazonSessionStatusResponse.php | 3 +-- src/DTOs/Amazon/ProductRequestItem.php | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/BwtApi.php b/src/BwtApi.php index e241d29..726bbce 100755 --- a/src/BwtApi.php +++ b/src/BwtApi.php @@ -2,6 +2,7 @@ namespace AlwaysOpen\BwtApi; -class BwtApi { +class BwtApi +{ public const BWT_AMAZON_ENGINE = 'bwt_amazon_product'; -} \ No newline at end of file +} diff --git a/src/BwtApiClient.php b/src/BwtApiClient.php index 9eb6c57..e70de09 100644 --- a/src/BwtApiClient.php +++ b/src/BwtApiClient.php @@ -32,7 +32,7 @@ protected function getAuthHeader(): array { return [ 'Authorization' => 'Bearer '.$this->apiKey, - ]; + ]; } /** diff --git a/src/DTOs/Amazon/AmazonSessionStatusResponse.php b/src/DTOs/Amazon/AmazonSessionStatusResponse.php index edaf8ce..a5e4dda 100644 --- a/src/DTOs/Amazon/AmazonSessionStatusResponse.php +++ b/src/DTOs/Amazon/AmazonSessionStatusResponse.php @@ -3,7 +3,6 @@ namespace AlwaysOpen\BwtApi\DTOs\Amazon; use Carbon\Carbon; -use Spatie\LaravelData\Attributes\DataCollectionOf; use Spatie\LaravelData\Attributes\WithCast; use Spatie\LaravelData\Casts\DateTimeInterfaceCast; use Spatie\LaravelData\Data; @@ -18,4 +17,4 @@ public function __construct( #[WithCast(DateTimeInterfaceCast::class, format: ['Y-m-d H:i:s', 'Y-m-d\TH:i:s\+H:i', 'Y-m-d H:i:s.u'])] public readonly ?Carbon $finished_at, ) {} -} \ No newline at end of file +} diff --git a/src/DTOs/Amazon/ProductRequestItem.php b/src/DTOs/Amazon/ProductRequestItem.php index 7e96a68..26f6366 100644 --- a/src/DTOs/Amazon/ProductRequestItem.php +++ b/src/DTOs/Amazon/ProductRequestItem.php @@ -2,8 +2,8 @@ namespace AlwaysOpen\BwtApi\DTOs\Amazon; -use Spatie\LaravelData\Data; use AlwaysOpen\BwtApi\Enums\ScrapingModes; +use Spatie\LaravelData\Data; class ProductRequestItem extends Data {