Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/DTOs/Google/GoogleShoppingPricingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public function __construct(
public readonly string $source,
public readonly string $domain,
public readonly string $query,
public readonly ?array $parse = null,
public readonly ?bool $parse = null,
public readonly ?array $context = null
) {}

Expand Down
34 changes: 34 additions & 0 deletions src/DTOs/Google/Url/GoogleUrlRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace AlwaysOpen\OxylabsApi\DTOs\Google\Url;

use AlwaysOpen\OxylabsApi\Enums\RenderOption;
use Spatie\LaravelData\Data;

class GoogleUrlRequest extends Data
{
public function __construct(
public readonly string $source,
public readonly string $domain,
public readonly string $query,
public readonly ?RenderOption $render = null,
public readonly ?int $start_page = null,
public readonly ?int $pages = null,
public readonly ?bool $parse = null,
public readonly ?array $context = null
) {}

public function toArray(): array
{
return array_filter([
'source' => $this->source,
'domain' => $this->domain,
'query' => $this->query,
'parse' => $this->parse,
'context' => $this->context,
'start_page' => $this->start_page,
'pages' => $this->pages,
'render' => $this->render,
]);
}
}
20 changes: 20 additions & 0 deletions src/DTOs/Google/Url/GoogleUrlResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace AlwaysOpen\OxylabsApi\DTOs\Google\Url;

use AlwaysOpen\OxylabsApi\DTOs\PushPullJob;
use AlwaysOpen\OxylabsApi\DTOs\Traits\ValidResponse;
use Spatie\LaravelData\Attributes\DataCollectionOf;
use Spatie\LaravelData\Data;

class GoogleUrlResponse extends Data
{
use ValidResponse;

public function __construct(
/* @var GoogleUrlResult[] $results */
#[DataCollectionOf(GoogleUrlResult::class)]
public readonly ?array $results = null,
public readonly ?PushPullJob $job = null,
) {}
}
37 changes: 37 additions & 0 deletions src/DTOs/Google/Url/GoogleUrlResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace AlwaysOpen\OxylabsApi\DTOs\Google\Url;

use AlwaysOpen\OxylabsApi\Traits\Renderable;
use Illuminate\Support\Carbon;
use Spatie\LaravelData\Attributes\WithCast;
use Spatie\LaravelData\Casts\DateTimeInterfaceCast;
use Spatie\LaravelData\Data;

class GoogleUrlResult extends Data
{
use Renderable;

public const int MAX_PRICES_PER_PAGE = 3;

public function __construct(
// @TODO handle parsed results in next version
public readonly ?string $content,
public readonly int $page,
public readonly string $url,
public readonly string $job_id,
public readonly bool $is_render_forced,
public readonly int $status_code,
#[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 = null,
#[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 $updated_at = null,
public readonly ?string $parser_type = null,
public readonly ?string $parser_preset = null,
) {}

public function nextPage(): int
{
return $this->page + 1;
}
}
30 changes: 30 additions & 0 deletions src/OxylabsApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use AlwaysOpen\OxylabsApi\DTOs\Google\GoogleShoppingPricingResponse;
use AlwaysOpen\OxylabsApi\DTOs\Google\GoogleShoppingProductRequest;
use AlwaysOpen\OxylabsApi\DTOs\Google\GoogleShoppingProductResponse;
use AlwaysOpen\OxylabsApi\DTOs\Google\Url\GoogleUrlRequest;
use AlwaysOpen\OxylabsApi\DTOs\Google\Url\GoogleUrlResponse;
use AlwaysOpen\OxylabsApi\DTOs\GoogleSearchRequest;
use AlwaysOpen\OxylabsApi\DTOs\PushPullBatchJobResponse;
use AlwaysOpen\OxylabsApi\DTOs\PushPullJob;
Expand Down Expand Up @@ -504,4 +506,32 @@ public function getWalmartProductResult(

return WalmartProductResponse::from($response);
}

/**
* @throws RuntimeException
*/
public function googleUrl(
GoogleUrlRequest $request,
?int $allowedRetries = null,
bool $logResponseBody = true,
): PushPullJob {
return $this->makePostRequest(OxylabsApi::TARGET_GOOGLE, $request->toArray(), $allowedRetries, $logResponseBody);
}

/**
* @throws Throwable
* @throws ConnectionException
*/
public function getGoogleUrlResult(
string $job_id,
bool $check_status = false,
int $status_check_limit = 5,
int $status_wait_seconds = 3,
?string $type = 'parsed',
bool $logResponseBody = true,
): GoogleUrlResponse {
$response = $this->getPushPullResults($job_id, $check_status, $status_check_limit, $status_wait_seconds, $type, $logResponseBody);

return GoogleUrlResponse::from($response);
}
}
24 changes: 24 additions & 0 deletions tests/Feature/OxylabsApiClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use AlwaysOpen\OxylabsApi\DTOs\Amazon\AmazonSellersRequest;
use AlwaysOpen\OxylabsApi\DTOs\Google\GoogleShoppingPricingRequest;
use AlwaysOpen\OxylabsApi\DTOs\Google\GoogleShoppingProductRequest;
use AlwaysOpen\OxylabsApi\DTOs\Google\Url\GoogleUrlRequest;
use AlwaysOpen\OxylabsApi\DTOs\Walmart\WalmartProductRequest;
use AlwaysOpen\OxylabsApi\Enums\ParseStatus;
use AlwaysOpen\OxylabsApi\Enums\RenderOption;
Expand Down Expand Up @@ -287,6 +288,29 @@ public function test_walmart_screenshot()
$this->assertTrue($saved);
}

public function test_google_url()
{
Http::fake([
'data.oxylabs.io/v1/queries' => Http::response($this->getFixtureJsonContent('push_pull_job.json'), 200),
'data.oxylabs.io/v1/queries/7350883412053343233/results?type=html' => Http::response($this->getFixtureJsonContent('google_url_results.json'), 200),
]);

$client = new OxylabsApiClient(username: 'user', password: 'pass');

$request = new GoogleUrlRequest(
source: OxylabsApi::TARGET_GOOGLE,
domain: 'com',
query: '123456',
render: RenderOption::HTML,
);

$client->googleUrl($request);

$result = $client->getGoogleUrlResult('7350883412053343233', type: 'html');

$this->assertTrue($result->results[0]->isRaw());
}

public function test_amazon_screenshot()
{
Http::fake([
Expand Down
141 changes: 141 additions & 0 deletions tests/Fixtures/google_url_results.json

Large diffs are not rendered by default.