Skip to content

Commit b1442ec

Browse files
author
craghurampec
committed
api session changes
1 parent c21509e commit b1442ec

File tree

5 files changed

+54
-7
lines changed

5 files changed

+54
-7
lines changed

src/BwtApi.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22

33
namespace AlwaysOpen\BwtApi;
44

5-
class BwtApi {}
5+
class BwtApi {
6+
public const BWT_AMAZON_ENGINE = 'bwt_amazon_product';
7+
}

src/BwtApiClient.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use AlwaysOpen\BwtApi\DTOs\Amazon\AmazonRequestCreationResponse;
66
use AlwaysOpen\BwtApi\DTOs\Amazon\AmazonResults;
7+
use AlwaysOpen\BwtApi\DTOs\Amazon\AmazonSessionStatusResponse;
78
use AlwaysOpen\BwtApi\DTOs\BatchRequest;
89
use GuzzleHttp\Exception\GuzzleException;
910
use GuzzleHttp\Promise\PromiseInterface;
@@ -24,14 +25,14 @@ public function __construct(
2425
?string $apiKey = null,
2526
) {
2627
$this->baseUrl = $baseUrl ?? config('bwt-api.base_url', 'https://bwt.com/api/');
27-
$this->apiKey = $apiKey ?? config('bwt-api.username') ?? '';
28+
$this->apiKey = $apiKey ?? config('bwt-api.api_key') ?? '';
2829
}
2930

3031
protected function getAuthHeader(): array
3132
{
3233
return [
3334
'Authorization' => 'Bearer '.$this->apiKey,
34-
];
35+
];
3536
}
3637

3738
/**
@@ -61,6 +62,27 @@ protected function makeRequest(
6162
}, 2000);
6263
}
6364

65+
public function getAmazonSessionStatus(
66+
string $id,
67+
): AmazonSessionStatusResponse {
68+
try {
69+
$response = $this->makeRequest(
70+
'get',
71+
$this->baseUrl .= "/$id",
72+
null,
73+
3,
74+
);
75+
} catch (Throwable $e) {
76+
throw new RuntimeException('API request failed: '.$e->getMessage(), $e->getCode(), $e);
77+
}
78+
79+
if (! $response->successful()) {
80+
throw new RuntimeException('API request failed: '.$response->body(), $response->getStatusCode());
81+
}
82+
83+
return AmazonSessionStatusResponse::from($response->json());
84+
}
85+
6486
public function getAmazonResults(
6587
string $id,
6688
int $limit = 1,
@@ -69,7 +91,7 @@ public function getAmazonResults(
6991
try {
7092
$response = $this->makeRequest(
7193
'get',
72-
$this->baseUrl .= "$id/results?limit=$limit&offset=$offset",
94+
$this->baseUrl .= "/$id/results?limit=$limit&offset=$offset",
7395
null,
7496
3,
7597
);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace AlwaysOpen\BwtApi\DTOs\Amazon;
4+
5+
use Carbon\Carbon;
6+
use Spatie\LaravelData\Attributes\DataCollectionOf;
7+
use Spatie\LaravelData\Attributes\WithCast;
8+
use Spatie\LaravelData\Casts\DateTimeInterfaceCast;
9+
use Spatie\LaravelData\Data;
10+
11+
class AmazonSessionStatusResponse extends Data
12+
{
13+
public function __construct(
14+
public readonly int $status,
15+
public readonly string $message,
16+
#[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'])]
17+
public readonly ?Carbon $created_at,
18+
#[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'])]
19+
public readonly ?Carbon $finished_at,
20+
) {}
21+
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
<?php
22

3-
namespace AlwaysOpen\BwtApi\DTOs;
3+
namespace AlwaysOpen\BwtApi\DTOs\Amazon;
44

55
use Spatie\LaravelData\Data;
6+
use AlwaysOpen\BwtApi\Enums\ScrapingModes;
67

78
class ProductRequestItem extends Data
89
{
910
public function __construct(
1011
public readonly string $requested_asin,
1112
public readonly string $region,
12-
public readonly string $postal_code,
13-
public readonly int $scraping_mode,
13+
public readonly string $postal_code = '00000',
14+
public readonly int $scraping_mode = ScrapingModes::INVENTORY->value,
1415
public readonly ?int $max_offer_pages = null,
1516
) {}
1617
}

src/DTOs/BatchRequest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace AlwaysOpen\BwtApi\DTOs;
44

5+
use AlwaysOpen\BwtApi\DTOs\Amazon\ProductRequestItem;
56
use Spatie\LaravelData\Data;
67

78
class BatchRequest extends Data

0 commit comments

Comments
 (0)