Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/BwtApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@

namespace AlwaysOpen\BwtApi;

class BwtApi {}
class BwtApi
{
public const BWT_AMAZON_ENGINE = 'bwt_amazon_product';
}
26 changes: 24 additions & 2 deletions src/BwtApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,7 +25,7 @@ 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
Expand Down Expand Up @@ -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,
Expand All @@ -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,
);
Expand Down
20 changes: 20 additions & 0 deletions src/DTOs/Amazon/AmazonSessionStatusResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace AlwaysOpen\BwtApi\DTOs\Amazon;

use Carbon\Carbon;
use Spatie\LaravelData\Attributes\WithCast;
use Spatie\LaravelData\Casts\DateTimeInterfaceCast;
use Spatie\LaravelData\Data;

class AmazonSessionStatusResponse extends Data
{
public function __construct(
public readonly int $status,
public readonly string $message,
#[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 $created_at,
#[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,
) {}
}
7 changes: 4 additions & 3 deletions src/DTOs/Amazon/ProductRequestItem.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
<?php

namespace AlwaysOpen\BwtApi\DTOs;
namespace AlwaysOpen\BwtApi\DTOs\Amazon;

use AlwaysOpen\BwtApi\Enums\ScrapingModes;
use Spatie\LaravelData\Data;

class ProductRequestItem extends Data
{
public function __construct(
public readonly string $requested_asin,
public readonly string $region,
public readonly string $postal_code,
public readonly int $scraping_mode,
public readonly string $postal_code = '00000',
public readonly int $scraping_mode = ScrapingModes::INVENTORY->value,
public readonly ?int $max_offer_pages = null,
) {}
}
1 change: 1 addition & 0 deletions src/DTOs/BatchRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace AlwaysOpen\BwtApi\DTOs;

use AlwaysOpen\BwtApi\DTOs\Amazon\ProductRequestItem;
use Spatie\LaravelData\Data;

class BatchRequest extends Data
Expand Down
Loading