Skip to content

Commit ddd88d8

Browse files
chore: refactor methods
1 parent ecf153b commit ddd88d8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+142
-424
lines changed

scripts/lint

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ set -e
55
cd -- "$(dirname -- "$0")/.."
66

77
echo "==> Running PHPStan"
8-
exec -- ./vendor/bin/phpstan analyse --memory-limit=1G
8+
exec -- ./vendor/bin/phpstan analyse --memory-limit=2G

src/Client.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function __construct(?string $apiKey = null, ?string $baseUrl = null)
7070
{
7171
$this->apiKey = (string) ($apiKey ?? getenv('SCRAPEGRAPHAI_API_KEY'));
7272

73-
$base = $baseUrl ?? getenv(
73+
$baseUrl ??= getenv(
7474
'SCRAPEGRAPHAI_BASE_URL'
7575
) ?: 'https://api.scrapegraphai.com/v1';
7676

@@ -85,7 +85,7 @@ public function __construct(?string $apiKey = null, ?string $baseUrl = null)
8585
headers: [
8686
'Content-Type' => 'application/json', 'Accept' => 'application/json',
8787
],
88-
baseUrl: $base,
88+
baseUrl: $baseUrl,
8989
options: $options,
9090
);
9191

src/Core/BaseClient.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
* body: mixed,
3030
* }
3131
*/
32-
class BaseClient
32+
abstract class BaseClient
3333
{
3434
protected UriInterface $baseUrl;
3535

@@ -77,14 +77,11 @@ public function request(
7777
// @phpstan-ignore-next-line
7878
$rsp = $this->sendRequest($opts, req: $request, data: $body, redirectCount: 0, retryCount: 0);
7979

80-
$decoded = Util::decodeContent($rsp);
81-
8280
if (!is_null($stream)) {
8381
return new $stream(
8482
convert: $convert,
8583
request: $request,
86-
response: $rsp,
87-
stream: $decoded
84+
response: $rsp
8885
);
8986
}
9087

@@ -93,23 +90,20 @@ public function request(
9390
convert: $convert,
9491
client: $this,
9592
request: $req,
93+
response: $rsp,
9694
options: $opts,
97-
data: $decoded,
9895
);
9996
}
10097

10198
if (!is_null($convert)) {
102-
return Conversion::coerce($convert, value: $decoded);
99+
return Conversion::coerceResponse($convert, response: $rsp);
103100
}
104101

105-
return $decoded;
102+
return Util::decodeContent($rsp);
106103
}
107104

108105
/** @return array<string, string> */
109-
protected function authHeaders(): array
110-
{
111-
return [];
112-
}
106+
abstract protected function authHeaders(): array;
113107

114108
/**
115109
* @internal

src/Core/Concerns/SdkResponse.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Scrapegraphai\Core\Concerns;
4+
5+
use Psr\Http\Message\ResponseInterface;
6+
use Scrapegraphai\Core\Util;
7+
8+
/**
9+
* @internal
10+
* SdkResponse must only be used in conjunction with classes that use the SdkModel trait
11+
*/
12+
trait SdkResponse
13+
{
14+
private ?ResponseInterface $_rawResponse;
15+
16+
public static function fromResponse(ResponseInterface $response): static
17+
{
18+
$instance = new static;
19+
$instance->_rawResponse = $response;
20+
$instance->__unserialize(Util::decodeContent($response)); // @phpstan-ignore-line
21+
22+
return $instance;
23+
}
24+
25+
public function getRawResponse(): ?ResponseInterface
26+
{
27+
return $this->_rawResponse;
28+
}
29+
}

src/Core/Contracts/BasePage.php

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

55
namespace Scrapegraphai\Core\Contracts;
66

7+
use Psr\Http\Message\ResponseInterface;
78
use Scrapegraphai\Client;
89
use Scrapegraphai\Core\Conversion\Contracts\Converter;
910
use Scrapegraphai\Core\Conversion\Contracts\ConverterSource;
@@ -30,7 +31,7 @@ public function __construct(
3031
Client $client,
3132
array $request,
3233
RequestOptions $options,
33-
mixed $data,
34+
ResponseInterface $response,
3435
);
3536

3637
public function hasNextPage(): bool;

src/Core/Contracts/BaseStream.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,10 @@
1818
*/
1919
interface BaseStream extends \IteratorAggregate
2020
{
21-
/**
22-
* @param \Generator<TInner> $stream
23-
*/
2421
public function __construct(
2522
Converter|ConverterSource|string $convert,
2623
RequestInterface $request,
2724
ResponseInterface $response,
28-
\Generator $stream,
2925
);
3026

3127
/**

src/Core/Conversion.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44

55
namespace Scrapegraphai\Core;
66

7+
use Psr\Http\Message\ResponseInterface;
78
use Scrapegraphai\Core\Conversion\CoerceState;
89
use Scrapegraphai\Core\Conversion\Contracts\Converter;
910
use Scrapegraphai\Core\Conversion\Contracts\ConverterSource;
11+
use Scrapegraphai\Core\Conversion\Contracts\ResponseConverter;
1012
use Scrapegraphai\Core\Conversion\DumpState;
1113

14+
/**
15+
* @internal
16+
*/
1217
final class Conversion
1318
{
1419
public static function dump_unknown(mixed $value, DumpState $state): mixed
@@ -38,6 +43,15 @@ public static function dump_unknown(mixed $value, DumpState $state): mixed
3843
return $value;
3944
}
4045

46+
public static function coerceResponse(Converter|ConverterSource|string $target, ResponseInterface $response): mixed
47+
{
48+
if (is_a($target, ResponseConverter::class, allow_string: true)) {
49+
return $target::fromResponse($response);
50+
}
51+
52+
return self::coerce($target, Util::decodeContent($response));
53+
}
54+
4155
public static function coerce(Converter|ConverterSource|string $target, mixed $value, CoerceState $state = new CoerceState): mixed
4256
{
4357
if ($value instanceof $target) {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Scrapegraphai\Core\Conversion\Contracts;
6+
7+
use Psr\Http\Message\ResponseInterface;
8+
9+
/**
10+
* @internal
11+
*/
12+
interface ResponseConverter
13+
{
14+
/**
15+
* @internal
16+
*/
17+
public static function fromResponse(ResponseInterface $response): static;
18+
}

src/Core/Implementation/HasRawResponse.php

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/Crawl/CrawlGetResultsResponse.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
use Scrapegraphai\Core\Attributes\Api;
88
use Scrapegraphai\Core\Concerns\SdkModel;
9+
use Scrapegraphai\Core\Concerns\SdkResponse;
910
use Scrapegraphai\Core\Contracts\BaseModel;
11+
use Scrapegraphai\Core\Conversion\Contracts\ResponseConverter;
1012
use Scrapegraphai\Crawl\CrawlGetResultsResponse\Result;
1113
use Scrapegraphai\Crawl\CrawlGetResultsResponse\Status;
1214

@@ -17,16 +19,14 @@
1719
* taskID?: string,
1820
* traceback?: string|null,
1921
* }
20-
* When used in a response, this type parameter can define a $rawResponse property.
21-
* @template TRawResponse of object = object{}
22-
*
23-
* @mixin TRawResponse
2422
*/
25-
final class CrawlGetResultsResponse implements BaseModel
23+
final class CrawlGetResultsResponse implements BaseModel, ResponseConverter
2624
{
2725
/** @use SdkModel<crawl_get_results_response> */
2826
use SdkModel;
2927

28+
use SdkResponse;
29+
3030
/**
3131
* Successful crawl results.
3232
*
@@ -70,7 +70,7 @@ public static function with(
7070
$obj = new self;
7171

7272
null !== $result && $obj->result = $result;
73-
null !== $status && $obj->status = $status instanceof Status ? $status->value : $status;
73+
null !== $status && $obj['status'] = $status;
7474
null !== $taskID && $obj->taskID = $taskID;
7575
null !== $traceback && $obj->traceback = $traceback;
7676

@@ -96,7 +96,7 @@ public function withResult(mixed $result): self
9696
public function withStatus(Status|string $status): self
9797
{
9898
$obj = clone $this;
99-
$obj->status = $status instanceof Status ? $status->value : $status;
99+
$obj['status'] = $status;
100100

101101
return $obj;
102102
}

0 commit comments

Comments
 (0)