Skip to content

Commit f0d8265

Browse files
authored
Creating initial Google Url support (#8)
* Creating initial Google Url support
1 parent 16714bf commit f0d8265

File tree

7 files changed

+287
-1
lines changed

7 files changed

+287
-1
lines changed

src/DTOs/Google/GoogleShoppingPricingRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public function __construct(
1010
public readonly string $source,
1111
public readonly string $domain,
1212
public readonly string $query,
13-
public readonly ?array $parse = null,
13+
public readonly ?bool $parse = null,
1414
public readonly ?array $context = null
1515
) {}
1616

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace AlwaysOpen\OxylabsApi\DTOs\Google\Url;
4+
5+
use AlwaysOpen\OxylabsApi\Enums\RenderOption;
6+
use Spatie\LaravelData\Data;
7+
8+
class GoogleUrlRequest extends Data
9+
{
10+
public function __construct(
11+
public readonly string $source,
12+
public readonly string $domain,
13+
public readonly string $query,
14+
public readonly ?RenderOption $render = null,
15+
public readonly ?int $start_page = null,
16+
public readonly ?int $pages = null,
17+
public readonly ?bool $parse = null,
18+
public readonly ?array $context = null
19+
) {}
20+
21+
public function toArray(): array
22+
{
23+
return array_filter([
24+
'source' => $this->source,
25+
'domain' => $this->domain,
26+
'query' => $this->query,
27+
'parse' => $this->parse,
28+
'context' => $this->context,
29+
'start_page' => $this->start_page,
30+
'pages' => $this->pages,
31+
'render' => $this->render,
32+
]);
33+
}
34+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace AlwaysOpen\OxylabsApi\DTOs\Google\Url;
4+
5+
use AlwaysOpen\OxylabsApi\DTOs\PushPullJob;
6+
use AlwaysOpen\OxylabsApi\DTOs\Traits\ValidResponse;
7+
use Spatie\LaravelData\Attributes\DataCollectionOf;
8+
use Spatie\LaravelData\Data;
9+
10+
class GoogleUrlResponse extends Data
11+
{
12+
use ValidResponse;
13+
14+
public function __construct(
15+
/* @var GoogleUrlResult[] $results */
16+
#[DataCollectionOf(GoogleUrlResult::class)]
17+
public readonly ?array $results = null,
18+
public readonly ?PushPullJob $job = null,
19+
) {}
20+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace AlwaysOpen\OxylabsApi\DTOs\Google\Url;
4+
5+
use AlwaysOpen\OxylabsApi\Traits\Renderable;
6+
use Illuminate\Support\Carbon;
7+
use Spatie\LaravelData\Attributes\WithCast;
8+
use Spatie\LaravelData\Casts\DateTimeInterfaceCast;
9+
use Spatie\LaravelData\Data;
10+
11+
class GoogleUrlResult extends Data
12+
{
13+
use Renderable;
14+
15+
public const int MAX_PRICES_PER_PAGE = 3;
16+
17+
public function __construct(
18+
// @TODO handle parsed results in next version
19+
public readonly ?string $content,
20+
public readonly int $page,
21+
public readonly string $url,
22+
public readonly string $job_id,
23+
public readonly bool $is_render_forced,
24+
public readonly int $status_code,
25+
#[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'])]
26+
public readonly ?Carbon $created_at = null,
27+
#[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'])]
28+
public readonly ?Carbon $updated_at = null,
29+
public readonly ?string $parser_type = null,
30+
public readonly ?string $parser_preset = null,
31+
) {}
32+
33+
public function nextPage(): int
34+
{
35+
return $this->page + 1;
36+
}
37+
}

src/OxylabsApiClient.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use AlwaysOpen\OxylabsApi\DTOs\Google\GoogleShoppingPricingResponse;
1717
use AlwaysOpen\OxylabsApi\DTOs\Google\GoogleShoppingProductRequest;
1818
use AlwaysOpen\OxylabsApi\DTOs\Google\GoogleShoppingProductResponse;
19+
use AlwaysOpen\OxylabsApi\DTOs\Google\Url\GoogleUrlRequest;
20+
use AlwaysOpen\OxylabsApi\DTOs\Google\Url\GoogleUrlResponse;
1921
use AlwaysOpen\OxylabsApi\DTOs\GoogleSearchRequest;
2022
use AlwaysOpen\OxylabsApi\DTOs\PushPullBatchJobResponse;
2123
use AlwaysOpen\OxylabsApi\DTOs\PushPullJob;
@@ -504,4 +506,32 @@ public function getWalmartProductResult(
504506

505507
return WalmartProductResponse::from($response);
506508
}
509+
510+
/**
511+
* @throws RuntimeException
512+
*/
513+
public function googleUrl(
514+
GoogleUrlRequest $request,
515+
?int $allowedRetries = null,
516+
bool $logResponseBody = true,
517+
): PushPullJob {
518+
return $this->makePostRequest(OxylabsApi::TARGET_GOOGLE, $request->toArray(), $allowedRetries, $logResponseBody);
519+
}
520+
521+
/**
522+
* @throws Throwable
523+
* @throws ConnectionException
524+
*/
525+
public function getGoogleUrlResult(
526+
string $job_id,
527+
bool $check_status = false,
528+
int $status_check_limit = 5,
529+
int $status_wait_seconds = 3,
530+
?string $type = 'parsed',
531+
bool $logResponseBody = true,
532+
): GoogleUrlResponse {
533+
$response = $this->getPushPullResults($job_id, $check_status, $status_check_limit, $status_wait_seconds, $type, $logResponseBody);
534+
535+
return GoogleUrlResponse::from($response);
536+
}
507537
}

tests/Feature/OxylabsApiClientTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use AlwaysOpen\OxylabsApi\DTOs\Amazon\AmazonSellersRequest;
99
use AlwaysOpen\OxylabsApi\DTOs\Google\GoogleShoppingPricingRequest;
1010
use AlwaysOpen\OxylabsApi\DTOs\Google\GoogleShoppingProductRequest;
11+
use AlwaysOpen\OxylabsApi\DTOs\Google\Url\GoogleUrlRequest;
1112
use AlwaysOpen\OxylabsApi\DTOs\Walmart\WalmartProductRequest;
1213
use AlwaysOpen\OxylabsApi\Enums\ParseStatus;
1314
use AlwaysOpen\OxylabsApi\Enums\RenderOption;
@@ -287,6 +288,29 @@ public function test_walmart_screenshot()
287288
$this->assertTrue($saved);
288289
}
289290

291+
public function test_google_url()
292+
{
293+
Http::fake([
294+
'data.oxylabs.io/v1/queries' => Http::response($this->getFixtureJsonContent('push_pull_job.json'), 200),
295+
'data.oxylabs.io/v1/queries/7350883412053343233/results?type=html' => Http::response($this->getFixtureJsonContent('google_url_results.json'), 200),
296+
]);
297+
298+
$client = new OxylabsApiClient(username: 'user', password: 'pass');
299+
300+
$request = new GoogleUrlRequest(
301+
source: OxylabsApi::TARGET_GOOGLE,
302+
domain: 'com',
303+
query: '123456',
304+
render: RenderOption::HTML,
305+
);
306+
307+
$client->googleUrl($request);
308+
309+
$result = $client->getGoogleUrlResult('7350883412053343233', type: 'html');
310+
311+
$this->assertTrue($result->results[0]->isRaw());
312+
}
313+
290314
public function test_amazon_screenshot()
291315
{
292316
Http::fake([

tests/Fixtures/google_url_results.json

Lines changed: 141 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)