From f566b27a274db61218c98bc775642fec51799888 Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Tue, 12 Aug 2025 10:43:05 +0000 Subject: [PATCH 01/44] chore: update SDK settings --- .stats.yml | 2 +- README.md | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/.stats.yml b/.stats.yml index 636c6f9..561b1cc 100644 --- a/.stats.yml +++ b/.stats.yml @@ -1,4 +1,4 @@ configured_endpoints: 15 openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/scrapegraphai%2Fscrapegraphai-969ebada41127057e4cda129b2e7206224743b5c7fd33aa8ae062ff71b775ac9.yml openapi_spec_hash: 2b2c2c684e6f6885398efca5f2b1f854 -config_hash: 30d69c79e34a1ea6a0405573ce30d927 +config_hash: 6889576ba0fdc14f2c71cea09a60a0f6 diff --git a/README.md b/README.md index 193884d..84596a4 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ > > This library has not yet been exhaustively tested in production environments and may be missing some features you'd expect in a stable release. As we continue development, there may be breaking changes that require updates to your code. > -> **We'd love your feedback!** Please share any suggestions, bug reports, feature requests, or general thoughts by [filing an issue](https://www.github.com/stainless-sdks/scrapegraphai-php/issues/new). +> **We'd love your feedback!** Please share any suggestions, bug reports, feature requests, or general thoughts by [filing an issue](https://www.github.com/ScrapeGraphAI/scrapegraphai-php/issues/new). The Scrapegraphai PHP library provides convenient access to the Scrapegraphai REST API from any PHP 8.1.0+ application. @@ -19,12 +19,14 @@ The REST API documentation can be found on [scrapegraphai.com](https://scrapegra To use this package, install via Composer by adding the following to your application's `composer.json`: + + ```json { "repositories": [ { "type": "vcs", - "url": "git@github.com:stainless-sdks/scrapegraphai-php.git" + "url": "git@github.com:ScrapeGraphAI/scrapegraphai-php.git" } ], "require": { @@ -33,6 +35,8 @@ To use this package, install via Composer by adding the following to your applic } ``` + + ## Usage ```php @@ -236,4 +240,4 @@ PHP 8.1.0 or higher. ## Contributing -See [the contributing documentation](https://github.com/stainless-sdks/scrapegraphai-php/tree/main/CONTRIBUTING.md). +See [the contributing documentation](https://github.com/ScrapeGraphAI/scrapegraphai-php/tree/main/CONTRIBUTING.md). From d00f77a024ea0faa3f08b2fcdcac387b5c248c1a Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Thu, 14 Aug 2025 03:18:03 +0000 Subject: [PATCH 02/44] feat(client): use with for constructors --- README.md | 17 ++-- src/Crawl/CrawlStartParams.php | 79 +++++++++++------ src/Crawl/CrawlStartParams/Rules.php | 16 ++-- src/Feedback/FeedbackSubmitParams.php | 37 +++++--- .../GenerateSchemaCreateParams.php | 30 +++++-- src/Markdownify/CompletedMarkdownify.php | 37 ++++---- src/Markdownify/MarkdownifyConvertParams.php | 37 +++++--- .../Crawl/CrawlGetResultsResponse.php | 30 ++++--- src/Responses/Crawl/CrawlStartResponse.php | 9 +- src/Responses/Credits/CreditGetResponse.php | 16 ++-- .../Feedback/FeedbackSubmitResponse.php | 30 ++++--- .../CompletedSchemaGenerationResponse.php | 44 ++++++---- .../FailedSchemaGenerationResponse.php | 44 ++++++---- .../GenerateSchemaNewResponse.php | 44 ++++++---- .../Healthz/HealthzCheckResponse.php | 16 ++-- .../FailedMarkdownifyResponse.php | 37 ++++---- .../FailedSearchScraperResponse.php | 51 ++++++----- .../Validate/ValidateAPIKeyResponse.php | 9 +- src/Searchscraper/CompletedSearchScraper.php | 51 ++++++----- .../SearchscraperCreateParams.php | 44 +++++++--- src/Smartscraper/CompletedSmartscraper.php | 44 ++++++---- src/Smartscraper/FailedSmartscraper.php | 44 ++++++---- src/Smartscraper/SmartscraperCreateParams.php | 86 ++++++++++++------- tests/Resources/CrawlTest.php | 6 +- tests/Resources/FeedbackTest.php | 4 +- tests/Resources/GenerateSchemaTest.php | 4 +- tests/Resources/MarkdownifyTest.php | 4 +- tests/Resources/SearchscraperTest.php | 4 +- tests/Resources/SmartscraperTest.php | 4 +- 29 files changed, 531 insertions(+), 347 deletions(-) diff --git a/README.md b/README.md index 84596a4..21a3c3d 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ $client = new Client( environment: "environment_1", ); -$params = SmartscraperCreateParams::from( +$params = SmartscraperCreateParams::with( userPrompt: "Extract the product name, price, and description" ); $completedSmartscraper = $client->smartscraper->create($params); @@ -68,11 +68,11 @@ When the library is unable to connect to the API, or if the API returns a non-su use Scrapegraphai\Errors\APIConnectionError; use Scrapegraphai\Smartscraper\SmartscraperCreateParams; +$params = SmartscraperCreateParams::with( + userPrompt: "Extract the product name, price, and description" +); try { - $params = SmartscraperCreateParams::from( - userPrompt: "Extract the product name, price, and description" - ); - $Smartscraper = $client->smartscraper->create($params); + $Smartscraper = $client->smartscraper->create($params); } catch (APIConnectionError $e) { echo "The server could not be reached", PHP_EOL; var_dump($e->getPrevious()); @@ -117,12 +117,11 @@ use Scrapegraphai\Smartscraper\SmartscraperCreateParams; // Configure the default for all requests: $client = new Client(maxRetries: 0); -$params = SmartscraperCreateParams::from( +$params = SmartscraperCreateParams::with( userPrompt: "Extract the product name, price, and description" ); -// Or, configure per-request: -$result = $client +// Or, configure per-request:$result = $client ->smartscraper ->create($params, new RequestOptions(maxRetries: 5)); ``` @@ -143,7 +142,7 @@ Note: the `extra_` parameters of the same name overrides the documented paramete use Scrapegraphai\RequestOptions; use Scrapegraphai\Smartscraper\SmartscraperCreateParams; -$params = SmartscraperCreateParams::from( +$params = SmartscraperCreateParams::with( userPrompt: "Extract the product name, price, and description" ); $completedSmartscraper = $client diff --git a/src/Crawl/CrawlStartParams.php b/src/Crawl/CrawlStartParams.php index e6de992..124819c 100644 --- a/src/Crawl/CrawlStartParams.php +++ b/src/Crawl/CrawlStartParams.php @@ -83,6 +83,20 @@ final class CrawlStartParams implements BaseModel #[Api(optional: true)] public ?bool $sitemap; + /** + * `new CrawlStartParams()` is missing required properties by the API. + * + * To enforce required parameters use + * ``` + * CrawlStartParams::with(url: ...) + * ``` + * + * Otherwise ensure the following setters are called + * + * ``` + * (new CrawlStartParams)->withURL(...) + * ``` + */ public function __construct() { self::introspect(); @@ -94,7 +108,7 @@ public function __construct() * * You must use named parameters to construct any parameters with a default value. */ - public static function from( + public static function with( string $url, ?int $depth = null, ?bool $extractionMode = null, @@ -124,87 +138,96 @@ public static function from( /** * Starting URL for crawling. */ - public function setURL(string $url): self + public function withURL(string $url): self { - $this->url = $url; + $obj = clone $this; + $obj->url = $url; - return $this; + return $obj; } /** * Maximum crawl depth from starting URL. */ - public function setDepth(int $depth): self + public function withDepth(int $depth): self { - $this->depth = $depth; + $obj = clone $this; + $obj->depth = $depth; - return $this; + return $obj; } /** * Use AI extraction (true) or markdown conversion (false). */ - public function setExtractionMode(bool $extractionMode): self + public function withExtractionMode(bool $extractionMode): self { - $this->extractionMode = $extractionMode; + $obj = clone $this; + $obj->extractionMode = $extractionMode; - return $this; + return $obj; } /** * Maximum number of pages to crawl. */ - public function setMaxPages(int $maxPages): self + public function withMaxPages(int $maxPages): self { - $this->maxPages = $maxPages; + $obj = clone $this; + $obj->maxPages = $maxPages; - return $this; + return $obj; } /** * Extraction prompt (required if extraction_mode is true). */ - public function setPrompt(?string $prompt): self + public function withPrompt(?string $prompt): self { - $this->prompt = $prompt; + $obj = clone $this; + $obj->prompt = $prompt; - return $this; + return $obj; } /** * Enable heavy JavaScript rendering. */ - public function setRenderHeavyJs(bool $renderHeavyJs): self + public function withRenderHeavyJs(bool $renderHeavyJs): self { - $this->renderHeavyJs = $renderHeavyJs; + $obj = clone $this; + $obj->renderHeavyJs = $renderHeavyJs; - return $this; + return $obj; } - public function setRules(Rules $rules): self + public function withRules(Rules $rules): self { - $this->rules = $rules; + $obj = clone $this; + $obj->rules = $rules; - return $this; + return $obj; } /** * Output schema for extraction. */ - public function setSchema(mixed $schema): self + public function withSchema(mixed $schema): self { - $this->schema = $schema; + $obj = clone $this; + $obj->schema = $schema; - return $this; + return $obj; } /** * Use sitemap for crawling. */ - public function setSitemap(bool $sitemap): self + public function withSitemap(bool $sitemap): self { - $this->sitemap = $sitemap; + $obj = clone $this; + $obj->sitemap = $sitemap; - return $this; + return $obj; } } diff --git a/src/Crawl/CrawlStartParams/Rules.php b/src/Crawl/CrawlStartParams/Rules.php index c352317..5a36660 100644 --- a/src/Crawl/CrawlStartParams/Rules.php +++ b/src/Crawl/CrawlStartParams/Rules.php @@ -43,7 +43,7 @@ public function __construct() * * @param null|list $exclude */ - public static function from( + public static function with( ?array $exclude = null, ?bool $sameDomain = null ): self { @@ -60,20 +60,22 @@ public static function from( * * @param list $exclude */ - public function setExclude(array $exclude): self + public function withExclude(array $exclude): self { - $this->exclude = $exclude; + $obj = clone $this; + $obj->exclude = $exclude; - return $this; + return $obj; } /** * Restrict crawling to same domain. */ - public function setSameDomain(bool $sameDomain): self + public function withSameDomain(bool $sameDomain): self { - $this->sameDomain = $sameDomain; + $obj = clone $this; + $obj->sameDomain = $sameDomain; - return $this; + return $obj; } } diff --git a/src/Feedback/FeedbackSubmitParams.php b/src/Feedback/FeedbackSubmitParams.php index f7d25ef..6a6969a 100644 --- a/src/Feedback/FeedbackSubmitParams.php +++ b/src/Feedback/FeedbackSubmitParams.php @@ -39,6 +39,20 @@ final class FeedbackSubmitParams implements BaseModel #[Api('feedback_text', optional: true)] public ?string $feedbackText; + /** + * `new FeedbackSubmitParams()` is missing required properties by the API. + * + * To enforce required parameters use + * ``` + * FeedbackSubmitParams::with(rating: ..., requestID: ...) + * ``` + * + * Otherwise ensure the following setters are called + * + * ``` + * (new FeedbackSubmitParams)->withRating(...)->withRequestID(...) + * ``` + */ public function __construct() { self::introspect(); @@ -50,7 +64,7 @@ public function __construct() * * You must use named parameters to construct any parameters with a default value. */ - public static function from( + public static function with( int $rating, string $requestID, ?string $feedbackText = null @@ -68,30 +82,33 @@ public static function from( /** * Rating score. */ - public function setRating(int $rating): self + public function withRating(int $rating): self { - $this->rating = $rating; + $obj = clone $this; + $obj->rating = $rating; - return $this; + return $obj; } /** * Request to provide feedback for. */ - public function setRequestID(string $requestID): self + public function withRequestID(string $requestID): self { - $this->requestID = $requestID; + $obj = clone $this; + $obj->requestID = $requestID; - return $this; + return $obj; } /** * Optional feedback comments. */ - public function setFeedbackText(?string $feedbackText): self + public function withFeedbackText(?string $feedbackText): self { - $this->feedbackText = $feedbackText; + $obj = clone $this; + $obj->feedbackText = $feedbackText; - return $this; + return $obj; } } diff --git a/src/GenerateSchema/GenerateSchemaCreateParams.php b/src/GenerateSchema/GenerateSchemaCreateParams.php index ff0158c..e0dd7ae 100644 --- a/src/GenerateSchema/GenerateSchemaCreateParams.php +++ b/src/GenerateSchema/GenerateSchemaCreateParams.php @@ -32,6 +32,20 @@ final class GenerateSchemaCreateParams implements BaseModel #[Api('existing_schema', optional: true)] public mixed $existingSchema; + /** + * `new GenerateSchemaCreateParams()` is missing required properties by the API. + * + * To enforce required parameters use + * ``` + * GenerateSchemaCreateParams::with(userPrompt: ...) + * ``` + * + * Otherwise ensure the following setters are called + * + * ``` + * (new GenerateSchemaCreateParams)->withUserPrompt(...) + * ``` + */ public function __construct() { self::introspect(); @@ -43,7 +57,7 @@ public function __construct() * * You must use named parameters to construct any parameters with a default value. */ - public static function from( + public static function with( string $userPrompt, mixed $existingSchema = null ): self { @@ -59,20 +73,22 @@ public static function from( /** * Natural language description of desired schema. */ - public function setUserPrompt(string $userPrompt): self + public function withUserPrompt(string $userPrompt): self { - $this->userPrompt = $userPrompt; + $obj = clone $this; + $obj->userPrompt = $userPrompt; - return $this; + return $obj; } /** * Existing schema to modify or extend. */ - public function setExistingSchema(mixed $existingSchema): self + public function withExistingSchema(mixed $existingSchema): self { - $this->existingSchema = $existingSchema; + $obj = clone $this; + $obj->existingSchema = $existingSchema; - return $this; + return $obj; } } diff --git a/src/Markdownify/CompletedMarkdownify.php b/src/Markdownify/CompletedMarkdownify.php index 3c535a8..a21ce7e 100644 --- a/src/Markdownify/CompletedMarkdownify.php +++ b/src/Markdownify/CompletedMarkdownify.php @@ -54,7 +54,7 @@ public function __construct() * * @param null|Status::* $status */ - public static function from( + public static function with( ?string $error = null, ?string $requestID = null, ?string $result = null, @@ -72,44 +72,49 @@ public static function from( return $obj; } - public function setError(string $error): self + public function withError(string $error): self { - $this->error = $error; + $obj = clone $this; + $obj->error = $error; - return $this; + return $obj; } - public function setRequestID(string $requestID): self + public function withRequestID(string $requestID): self { - $this->requestID = $requestID; + $obj = clone $this; + $obj->requestID = $requestID; - return $this; + return $obj; } /** * Markdown content. */ - public function setResult(?string $result): self + public function withResult(?string $result): self { - $this->result = $result; + $obj = clone $this; + $obj->result = $result; - return $this; + return $obj; } /** * @param Status::* $status */ - public function setStatus(string $status): self + public function withStatus(string $status): self { - $this->status = $status; + $obj = clone $this; + $obj->status = $status; - return $this; + return $obj; } - public function setWebsiteURL(string $websiteURL): self + public function withWebsiteURL(string $websiteURL): self { - $this->websiteURL = $websiteURL; + $obj = clone $this; + $obj->websiteURL = $websiteURL; - return $this; + return $obj; } } diff --git a/src/Markdownify/MarkdownifyConvertParams.php b/src/Markdownify/MarkdownifyConvertParams.php index 74eec44..ad56545 100644 --- a/src/Markdownify/MarkdownifyConvertParams.php +++ b/src/Markdownify/MarkdownifyConvertParams.php @@ -41,6 +41,20 @@ final class MarkdownifyConvertParams implements BaseModel #[Api(type: new ListOf('string'), optional: true)] public ?array $steps; + /** + * `new MarkdownifyConvertParams()` is missing required properties by the API. + * + * To enforce required parameters use + * ``` + * MarkdownifyConvertParams::with(websiteURL: ...) + * ``` + * + * Otherwise ensure the following setters are called + * + * ``` + * (new MarkdownifyConvertParams)->withWebsiteURL(...) + * ``` + */ public function __construct() { self::introspect(); @@ -55,7 +69,7 @@ public function __construct() * @param null|array $headers * @param null|list $steps */ - public static function from( + public static function with( string $websiteURL, ?array $headers = null, ?array $steps = null @@ -73,21 +87,23 @@ public static function from( /** * URL to convert to markdown. */ - public function setWebsiteURL(string $websiteURL): self + public function withWebsiteURL(string $websiteURL): self { - $this->websiteURL = $websiteURL; + $obj = clone $this; + $obj->websiteURL = $websiteURL; - return $this; + return $obj; } /** * @param array $headers */ - public function setHeaders(array $headers): self + public function withHeaders(array $headers): self { - $this->headers = $headers; + $obj = clone $this; + $obj->headers = $headers; - return $this; + return $obj; } /** @@ -95,10 +111,11 @@ public function setHeaders(array $headers): self * * @param list $steps */ - public function setSteps(array $steps): self + public function withSteps(array $steps): self { - $this->steps = $steps; + $obj = clone $this; + $obj->steps = $steps; - return $this; + return $obj; } } diff --git a/src/Responses/Crawl/CrawlGetResultsResponse.php b/src/Responses/Crawl/CrawlGetResultsResponse.php index 6c6f4f2..c3ef9c6 100644 --- a/src/Responses/Crawl/CrawlGetResultsResponse.php +++ b/src/Responses/Crawl/CrawlGetResultsResponse.php @@ -57,7 +57,7 @@ public function __construct() * @param null|mixed|string $result * @param null|Status::* $status */ - public static function from( + public static function with( mixed $result = null, ?string $status = null, ?string $taskID = null, @@ -78,37 +78,41 @@ public static function from( * * @param mixed|string $result */ - public function setResult(mixed $result): self + public function withResult(mixed $result): self { - $this->result = $result; + $obj = clone $this; + $obj->result = $result; - return $this; + return $obj; } /** * @param Status::* $status */ - public function setStatus(string $status): self + public function withStatus(string $status): self { - $this->status = $status; + $obj = clone $this; + $obj->status = $status; - return $this; + return $obj; } - public function setTaskID(string $taskID): self + public function withTaskID(string $taskID): self { - $this->taskID = $taskID; + $obj = clone $this; + $obj->taskID = $taskID; - return $this; + return $obj; } /** * Error traceback for failed tasks. */ - public function setTraceback(?string $traceback): self + public function withTraceback(?string $traceback): self { - $this->traceback = $traceback; + $obj = clone $this; + $obj->traceback = $traceback; - return $this; + return $obj; } } diff --git a/src/Responses/Crawl/CrawlStartResponse.php b/src/Responses/Crawl/CrawlStartResponse.php index 74277a9..7b3d0c2 100644 --- a/src/Responses/Crawl/CrawlStartResponse.php +++ b/src/Responses/Crawl/CrawlStartResponse.php @@ -32,7 +32,7 @@ public function __construct() * * You must use named parameters to construct any parameters with a default value. */ - public static function from(?string $taskID = null): self + public static function with(?string $taskID = null): self { $obj = new self; @@ -44,10 +44,11 @@ public static function from(?string $taskID = null): self /** * Celery task identifier. */ - public function setTaskID(string $taskID): self + public function withTaskID(string $taskID): self { - $this->taskID = $taskID; + $obj = clone $this; + $obj->taskID = $taskID; - return $this; + return $obj; } } diff --git a/src/Responses/Credits/CreditGetResponse.php b/src/Responses/Credits/CreditGetResponse.php index 6e388c8..565120a 100644 --- a/src/Responses/Credits/CreditGetResponse.php +++ b/src/Responses/Credits/CreditGetResponse.php @@ -40,7 +40,7 @@ public function __construct() * * You must use named parameters to construct any parameters with a default value. */ - public static function from( + public static function with( ?int $remainingCredits = null, ?int $totalCreditsUsed = null ): self { @@ -55,20 +55,22 @@ public static function from( /** * Number of credits remaining. */ - public function setRemainingCredits(int $remainingCredits): self + public function withRemainingCredits(int $remainingCredits): self { - $this->remainingCredits = $remainingCredits; + $obj = clone $this; + $obj->remainingCredits = $remainingCredits; - return $this; + return $obj; } /** * Total credits consumed. */ - public function setTotalCreditsUsed(int $totalCreditsUsed): self + public function withTotalCreditsUsed(int $totalCreditsUsed): self { - $this->totalCreditsUsed = $totalCreditsUsed; + $obj = clone $this; + $obj->totalCreditsUsed = $totalCreditsUsed; - return $this; + return $obj; } } diff --git a/src/Responses/Feedback/FeedbackSubmitResponse.php b/src/Responses/Feedback/FeedbackSubmitResponse.php index d7e6791..bf7ac15 100644 --- a/src/Responses/Feedback/FeedbackSubmitResponse.php +++ b/src/Responses/Feedback/FeedbackSubmitResponse.php @@ -43,7 +43,7 @@ public function __construct() * * You must use named parameters to construct any parameters with a default value. */ - public static function from( + public static function with( ?string $feedbackID = null, ?\DateTimeInterface $feedbackTimestamp = null, ?string $message = null, @@ -59,32 +59,36 @@ public static function from( return $obj; } - public function setFeedbackID(string $feedbackID): self + public function withFeedbackID(string $feedbackID): self { - $this->feedbackID = $feedbackID; + $obj = clone $this; + $obj->feedbackID = $feedbackID; - return $this; + return $obj; } - public function setFeedbackTimestamp( + public function withFeedbackTimestamp( \DateTimeInterface $feedbackTimestamp ): self { - $this->feedbackTimestamp = $feedbackTimestamp; + $obj = clone $this; + $obj->feedbackTimestamp = $feedbackTimestamp; - return $this; + return $obj; } - public function setMessage(string $message): self + public function withMessage(string $message): self { - $this->message = $message; + $obj = clone $this; + $obj->message = $message; - return $this; + return $obj; } - public function setRequestID(string $requestID): self + public function withRequestID(string $requestID): self { - $this->requestID = $requestID; + $obj = clone $this; + $obj->requestID = $requestID; - return $this; + return $obj; } } diff --git a/src/Responses/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse.php b/src/Responses/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse.php index c1cd234..a8bdc70 100644 --- a/src/Responses/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse.php +++ b/src/Responses/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse.php @@ -55,7 +55,7 @@ public function __construct() * * @param null|Status::* $status */ - public static function from( + public static function with( ?string $error = null, mixed $generatedSchema = null, ?string $refinedPrompt = null, @@ -75,48 +75,54 @@ public static function from( return $obj; } - public function setError(?string $error): self + public function withError(?string $error): self { - $this->error = $error; + $obj = clone $this; + $obj->error = $error; - return $this; + return $obj; } - public function setGeneratedSchema(mixed $generatedSchema): self + public function withGeneratedSchema(mixed $generatedSchema): self { - $this->generatedSchema = $generatedSchema; + $obj = clone $this; + $obj->generatedSchema = $generatedSchema; - return $this; + return $obj; } - public function setRefinedPrompt(string $refinedPrompt): self + public function withRefinedPrompt(string $refinedPrompt): self { - $this->refinedPrompt = $refinedPrompt; + $obj = clone $this; + $obj->refinedPrompt = $refinedPrompt; - return $this; + return $obj; } - public function setRequestID(string $requestID): self + public function withRequestID(string $requestID): self { - $this->requestID = $requestID; + $obj = clone $this; + $obj->requestID = $requestID; - return $this; + return $obj; } /** * @param Status::* $status */ - public function setStatus(string $status): self + public function withStatus(string $status): self { - $this->status = $status; + $obj = clone $this; + $obj->status = $status; - return $this; + return $obj; } - public function setUserPrompt(string $userPrompt): self + public function withUserPrompt(string $userPrompt): self { - $this->userPrompt = $userPrompt; + $obj = clone $this; + $obj->userPrompt = $userPrompt; - return $this; + return $obj; } } diff --git a/src/Responses/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse.php b/src/Responses/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse.php index 9aec21d..9fdd2f8 100644 --- a/src/Responses/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse.php +++ b/src/Responses/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse.php @@ -55,7 +55,7 @@ public function __construct() * * @param null|Status::* $status */ - public static function from( + public static function with( ?string $error = null, mixed $generatedSchema = null, ?string $refinedPrompt = null, @@ -75,48 +75,54 @@ public static function from( return $obj; } - public function setError(string $error): self + public function withError(string $error): self { - $this->error = $error; + $obj = clone $this; + $obj->error = $error; - return $this; + return $obj; } - public function setGeneratedSchema(mixed $generatedSchema): self + public function withGeneratedSchema(mixed $generatedSchema): self { - $this->generatedSchema = $generatedSchema; + $obj = clone $this; + $obj->generatedSchema = $generatedSchema; - return $this; + return $obj; } - public function setRefinedPrompt(?string $refinedPrompt): self + public function withRefinedPrompt(?string $refinedPrompt): self { - $this->refinedPrompt = $refinedPrompt; + $obj = clone $this; + $obj->refinedPrompt = $refinedPrompt; - return $this; + return $obj; } - public function setRequestID(string $requestID): self + public function withRequestID(string $requestID): self { - $this->requestID = $requestID; + $obj = clone $this; + $obj->requestID = $requestID; - return $this; + return $obj; } /** * @param Status::* $status */ - public function setStatus(string $status): self + public function withStatus(string $status): self { - $this->status = $status; + $obj = clone $this; + $obj->status = $status; - return $this; + return $obj; } - public function setUserPrompt(string $userPrompt): self + public function withUserPrompt(string $userPrompt): self { - $this->userPrompt = $userPrompt; + $obj = clone $this; + $obj->userPrompt = $userPrompt; - return $this; + return $obj; } } diff --git a/src/Responses/GenerateSchema/GenerateSchemaNewResponse.php b/src/Responses/GenerateSchema/GenerateSchemaNewResponse.php index 6e30b10..8b2fa6f 100644 --- a/src/Responses/GenerateSchema/GenerateSchemaNewResponse.php +++ b/src/Responses/GenerateSchema/GenerateSchemaNewResponse.php @@ -61,7 +61,7 @@ public function __construct() * * @param null|Status::* $status */ - public static function from( + public static function with( ?string $error = null, mixed $generatedSchema = null, ?string $refinedPrompt = null, @@ -81,54 +81,60 @@ public static function from( return $obj; } - public function setError(?string $error): self + public function withError(?string $error): self { - $this->error = $error; + $obj = clone $this; + $obj->error = $error; - return $this; + return $obj; } /** * Generated JSON schema. */ - public function setGeneratedSchema(mixed $generatedSchema): self + public function withGeneratedSchema(mixed $generatedSchema): self { - $this->generatedSchema = $generatedSchema; + $obj = clone $this; + $obj->generatedSchema = $generatedSchema; - return $this; + return $obj; } /** * Enhanced search prompt generated from user input. */ - public function setRefinedPrompt(string $refinedPrompt): self + public function withRefinedPrompt(string $refinedPrompt): self { - $this->refinedPrompt = $refinedPrompt; + $obj = clone $this; + $obj->refinedPrompt = $refinedPrompt; - return $this; + return $obj; } - public function setRequestID(string $requestID): self + public function withRequestID(string $requestID): self { - $this->requestID = $requestID; + $obj = clone $this; + $obj->requestID = $requestID; - return $this; + return $obj; } /** * @param Status::* $status */ - public function setStatus(string $status): self + public function withStatus(string $status): self { - $this->status = $status; + $obj = clone $this; + $obj->status = $status; - return $this; + return $obj; } - public function setUserPrompt(string $userPrompt): self + public function withUserPrompt(string $userPrompt): self { - $this->userPrompt = $userPrompt; + $obj = clone $this; + $obj->userPrompt = $userPrompt; - return $this; + return $obj; } } diff --git a/src/Responses/Healthz/HealthzCheckResponse.php b/src/Responses/Healthz/HealthzCheckResponse.php index 4439c13..b2a64ae 100644 --- a/src/Responses/Healthz/HealthzCheckResponse.php +++ b/src/Responses/Healthz/HealthzCheckResponse.php @@ -38,7 +38,7 @@ public function __construct() * * @param null|array $services */ - public static function from( + public static function with( ?array $services = null, ?string $status = null ): self { @@ -53,17 +53,19 @@ public static function from( /** * @param array $services */ - public function setServices(array $services): self + public function withServices(array $services): self { - $this->services = $services; + $obj = clone $this; + $obj->services = $services; - return $this; + return $obj; } - public function setStatus(string $status): self + public function withStatus(string $status): self { - $this->status = $status; + $obj = clone $this; + $obj->status = $status; - return $this; + return $obj; } } diff --git a/src/Responses/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse.php b/src/Responses/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse.php index 11f2a2f..7b74da0 100644 --- a/src/Responses/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse.php +++ b/src/Responses/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse.php @@ -51,7 +51,7 @@ public function __construct() * * @param null|Status::* $status */ - public static function from( + public static function with( ?string $error = null, ?string $requestID = null, ?string $result = null, @@ -69,41 +69,46 @@ public static function from( return $obj; } - public function setError(string $error): self + public function withError(string $error): self { - $this->error = $error; + $obj = clone $this; + $obj->error = $error; - return $this; + return $obj; } - public function setRequestID(string $requestID): self + public function withRequestID(string $requestID): self { - $this->requestID = $requestID; + $obj = clone $this; + $obj->requestID = $requestID; - return $this; + return $obj; } - public function setResult(?string $result): self + public function withResult(?string $result): self { - $this->result = $result; + $obj = clone $this; + $obj->result = $result; - return $this; + return $obj; } /** * @param Status::* $status */ - public function setStatus(string $status): self + public function withStatus(string $status): self { - $this->status = $status; + $obj = clone $this; + $obj->status = $status; - return $this; + return $obj; } - public function setWebsiteURL(string $websiteURL): self + public function withWebsiteURL(string $websiteURL): self { - $this->websiteURL = $websiteURL; + $obj = clone $this; + $obj->websiteURL = $websiteURL; - return $this; + return $obj; } } diff --git a/src/Responses/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse.php b/src/Responses/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse.php index a2f0ba1..91f8b3b 100644 --- a/src/Responses/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse.php +++ b/src/Responses/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse.php @@ -62,7 +62,7 @@ public function __construct() * @param null|list $referenceURLs * @param null|Status::* $status */ - public static function from( + public static function with( ?string $error = null, ?int $numResults = null, ?array $referenceURLs = null, @@ -84,58 +84,65 @@ public static function from( return $obj; } - public function setError(string $error): self + public function withError(string $error): self { - $this->error = $error; + $obj = clone $this; + $obj->error = $error; - return $this; + return $obj; } - public function setNumResults(int $numResults): self + public function withNumResults(int $numResults): self { - $this->numResults = $numResults; + $obj = clone $this; + $obj->numResults = $numResults; - return $this; + return $obj; } /** * @param list $referenceURLs */ - public function setReferenceURLs(array $referenceURLs): self + public function withReferenceURLs(array $referenceURLs): self { - $this->referenceURLs = $referenceURLs; + $obj = clone $this; + $obj->referenceURLs = $referenceURLs; - return $this; + return $obj; } - public function setRequestID(string $requestID): self + public function withRequestID(string $requestID): self { - $this->requestID = $requestID; + $obj = clone $this; + $obj->requestID = $requestID; - return $this; + return $obj; } - public function setResult(mixed $result): self + public function withResult(mixed $result): self { - $this->result = $result; + $obj = clone $this; + $obj->result = $result; - return $this; + return $obj; } /** * @param Status::* $status */ - public function setStatus(string $status): self + public function withStatus(string $status): self { - $this->status = $status; + $obj = clone $this; + $obj->status = $status; - return $this; + return $obj; } - public function setUserPrompt(string $userPrompt): self + public function withUserPrompt(string $userPrompt): self { - $this->userPrompt = $userPrompt; + $obj = clone $this; + $obj->userPrompt = $userPrompt; - return $this; + return $obj; } } diff --git a/src/Responses/Validate/ValidateAPIKeyResponse.php b/src/Responses/Validate/ValidateAPIKeyResponse.php index bed9c37..70a752e 100644 --- a/src/Responses/Validate/ValidateAPIKeyResponse.php +++ b/src/Responses/Validate/ValidateAPIKeyResponse.php @@ -29,7 +29,7 @@ public function __construct() * * You must use named parameters to construct any parameters with a default value. */ - public static function from(?string $email = null): self + public static function with(?string $email = null): self { $obj = new self; @@ -38,10 +38,11 @@ public static function from(?string $email = null): self return $obj; } - public function setEmail(string $email): self + public function withEmail(string $email): self { - $this->email = $email; + $obj = clone $this; + $obj->email = $email; - return $this; + return $obj; } } diff --git a/src/Searchscraper/CompletedSearchScraper.php b/src/Searchscraper/CompletedSearchScraper.php index b6fe395..3653667 100644 --- a/src/Searchscraper/CompletedSearchScraper.php +++ b/src/Searchscraper/CompletedSearchScraper.php @@ -69,7 +69,7 @@ public function __construct() * @param null|list $referenceURLs * @param null|Status::* $status */ - public static function from( + public static function with( ?string $error = null, ?int $numResults = null, ?array $referenceURLs = null, @@ -91,18 +91,20 @@ public static function from( return $obj; } - public function setError(?string $error): self + public function withError(?string $error): self { - $this->error = $error; + $obj = clone $this; + $obj->error = $error; - return $this; + return $obj; } - public function setNumResults(int $numResults): self + public function withNumResults(int $numResults): self { - $this->numResults = $numResults; + $obj = clone $this; + $obj->numResults = $numResults; - return $this; + return $obj; } /** @@ -110,44 +112,49 @@ public function setNumResults(int $numResults): self * * @param list $referenceURLs */ - public function setReferenceURLs(array $referenceURLs): self + public function withReferenceURLs(array $referenceURLs): self { - $this->referenceURLs = $referenceURLs; + $obj = clone $this; + $obj->referenceURLs = $referenceURLs; - return $this; + return $obj; } - public function setRequestID(string $requestID): self + public function withRequestID(string $requestID): self { - $this->requestID = $requestID; + $obj = clone $this; + $obj->requestID = $requestID; - return $this; + return $obj; } /** * Merged results from all scraped websites. */ - public function setResult(mixed $result): self + public function withResult(mixed $result): self { - $this->result = $result; + $obj = clone $this; + $obj->result = $result; - return $this; + return $obj; } /** * @param Status::* $status */ - public function setStatus(string $status): self + public function withStatus(string $status): self { - $this->status = $status; + $obj = clone $this; + $obj->status = $status; - return $this; + return $obj; } - public function setUserPrompt(string $userPrompt): self + public function withUserPrompt(string $userPrompt): self { - $this->userPrompt = $userPrompt; + $obj = clone $this; + $obj->userPrompt = $userPrompt; - return $this; + return $obj; } } diff --git a/src/Searchscraper/SearchscraperCreateParams.php b/src/Searchscraper/SearchscraperCreateParams.php index 7901869..15e808b 100644 --- a/src/Searchscraper/SearchscraperCreateParams.php +++ b/src/Searchscraper/SearchscraperCreateParams.php @@ -48,6 +48,20 @@ final class SearchscraperCreateParams implements BaseModel #[Api('output_schema', optional: true)] public mixed $outputSchema; + /** + * `new SearchscraperCreateParams()` is missing required properties by the API. + * + * To enforce required parameters use + * ``` + * SearchscraperCreateParams::with(userPrompt: ...) + * ``` + * + * Otherwise ensure the following setters are called + * + * ``` + * (new SearchscraperCreateParams)->withUserPrompt(...) + * ``` + */ public function __construct() { self::introspect(); @@ -61,7 +75,7 @@ public function __construct() * * @param null|array $headers */ - public static function from( + public static function with( string $userPrompt, ?array $headers = null, ?int $numResults = null, @@ -81,40 +95,44 @@ public static function from( /** * Search query and extraction instruction. */ - public function setUserPrompt(string $userPrompt): self + public function withUserPrompt(string $userPrompt): self { - $this->userPrompt = $userPrompt; + $obj = clone $this; + $obj->userPrompt = $userPrompt; - return $this; + return $obj; } /** * @param array $headers */ - public function setHeaders(array $headers): self + public function withHeaders(array $headers): self { - $this->headers = $headers; + $obj = clone $this; + $obj->headers = $headers; - return $this; + return $obj; } /** * Number of websites to scrape from search results. */ - public function setNumResults(int $numResults): self + public function withNumResults(int $numResults): self { - $this->numResults = $numResults; + $obj = clone $this; + $obj->numResults = $numResults; - return $this; + return $obj; } /** * JSON schema for structured output. */ - public function setOutputSchema(mixed $outputSchema): self + public function withOutputSchema(mixed $outputSchema): self { - $this->outputSchema = $outputSchema; + $obj = clone $this; + $obj->outputSchema = $outputSchema; - return $this; + return $obj; } } diff --git a/src/Smartscraper/CompletedSmartscraper.php b/src/Smartscraper/CompletedSmartscraper.php index 288bc66..43874ee 100644 --- a/src/Smartscraper/CompletedSmartscraper.php +++ b/src/Smartscraper/CompletedSmartscraper.php @@ -68,7 +68,7 @@ public function __construct() * * @param null|Status::* $status */ - public static function from( + public static function with( ?string $error = null, ?string $requestID = null, mixed $result = null, @@ -91,31 +91,34 @@ public static function from( /** * Error message (empty on success). */ - public function setError(string $error): self + public function withError(string $error): self { - $this->error = $error; + $obj = clone $this; + $obj->error = $error; - return $this; + return $obj; } /** * Unique request identifier. */ - public function setRequestID(string $requestID): self + public function withRequestID(string $requestID): self { - $this->requestID = $requestID; + $obj = clone $this; + $obj->requestID = $requestID; - return $this; + return $obj; } /** * Extracted data based on prompt/schema. */ - public function setResult(mixed $result): self + public function withResult(mixed $result): self { - $this->result = $result; + $obj = clone $this; + $obj->result = $result; - return $this; + return $obj; } /** @@ -123,24 +126,27 @@ public function setResult(mixed $result): self * * @param Status::* $status */ - public function setStatus(string $status): self + public function withStatus(string $status): self { - $this->status = $status; + $obj = clone $this; + $obj->status = $status; - return $this; + return $obj; } - public function setUserPrompt(string $userPrompt): self + public function withUserPrompt(string $userPrompt): self { - $this->userPrompt = $userPrompt; + $obj = clone $this; + $obj->userPrompt = $userPrompt; - return $this; + return $obj; } - public function setWebsiteURL(?string $websiteURL): self + public function withWebsiteURL(?string $websiteURL): self { - $this->websiteURL = $websiteURL; + $obj = clone $this; + $obj->websiteURL = $websiteURL; - return $this; + return $obj; } } diff --git a/src/Smartscraper/FailedSmartscraper.php b/src/Smartscraper/FailedSmartscraper.php index 7bd42e0..5b5cb31 100644 --- a/src/Smartscraper/FailedSmartscraper.php +++ b/src/Smartscraper/FailedSmartscraper.php @@ -58,7 +58,7 @@ public function __construct() * * @param null|Status::* $status */ - public static function from( + public static function with( ?string $error = null, ?string $requestID = null, mixed $result = null, @@ -81,48 +81,54 @@ public static function from( /** * Error description. */ - public function setError(string $error): self + public function withError(string $error): self { - $this->error = $error; + $obj = clone $this; + $obj->error = $error; - return $this; + return $obj; } - public function setRequestID(string $requestID): self + public function withRequestID(string $requestID): self { - $this->requestID = $requestID; + $obj = clone $this; + $obj->requestID = $requestID; - return $this; + return $obj; } - public function setResult(mixed $result): self + public function withResult(mixed $result): self { - $this->result = $result; + $obj = clone $this; + $obj->result = $result; - return $this; + return $obj; } /** * @param Status::* $status */ - public function setStatus(string $status): self + public function withStatus(string $status): self { - $this->status = $status; + $obj = clone $this; + $obj->status = $status; - return $this; + return $obj; } - public function setUserPrompt(string $userPrompt): self + public function withUserPrompt(string $userPrompt): self { - $this->userPrompt = $userPrompt; + $obj = clone $this; + $obj->userPrompt = $userPrompt; - return $this; + return $obj; } - public function setWebsiteURL(?string $websiteURL): self + public function withWebsiteURL(?string $websiteURL): self { - $this->websiteURL = $websiteURL; + $obj = clone $this; + $obj->websiteURL = $websiteURL; - return $this; + return $obj; } } diff --git a/src/Smartscraper/SmartscraperCreateParams.php b/src/Smartscraper/SmartscraperCreateParams.php index 77f2885..0bbd525 100644 --- a/src/Smartscraper/SmartscraperCreateParams.php +++ b/src/Smartscraper/SmartscraperCreateParams.php @@ -99,6 +99,20 @@ final class SmartscraperCreateParams implements BaseModel #[Api('website_url', optional: true)] public ?string $websiteURL; + /** + * `new SmartscraperCreateParams()` is missing required properties by the API. + * + * To enforce required parameters use + * ``` + * SmartscraperCreateParams::with(userPrompt: ...) + * ``` + * + * Otherwise ensure the following setters are called + * + * ``` + * (new SmartscraperCreateParams)->withUserPrompt(...) + * ``` + */ public function __construct() { self::introspect(); @@ -114,7 +128,7 @@ public function __construct() * @param null|array $headers * @param null|list $steps */ - public static function from( + public static function with( string $userPrompt, ?array $cookies = null, ?array $headers = null, @@ -146,11 +160,12 @@ public static function from( /** * Extraction instruction for the LLM. */ - public function setUserPrompt(string $userPrompt): self + public function withUserPrompt(string $userPrompt): self { - $this->userPrompt = $userPrompt; + $obj = clone $this; + $obj->userPrompt = $userPrompt; - return $this; + return $obj; } /** @@ -158,11 +173,12 @@ public function setUserPrompt(string $userPrompt): self * * @param array $cookies */ - public function setCookies(array $cookies): self + public function withCookies(array $cookies): self { - $this->cookies = $cookies; + $obj = clone $this; + $obj->cookies = $cookies; - return $this; + return $obj; } /** @@ -170,41 +186,45 @@ public function setCookies(array $cookies): self * * @param array $headers */ - public function setHeaders(array $headers): self + public function withHeaders(array $headers): self { - $this->headers = $headers; + $obj = clone $this; + $obj->headers = $headers; - return $this; + return $obj; } /** * Number of infinite scroll operations to perform. */ - public function setNumberOfScrolls(int $numberOfScrolls): self + public function withNumberOfScrolls(int $numberOfScrolls): self { - $this->numberOfScrolls = $numberOfScrolls; + $obj = clone $this; + $obj->numberOfScrolls = $numberOfScrolls; - return $this; + return $obj; } /** * JSON schema defining the expected output structure. */ - public function setOutputSchema(mixed $outputSchema): self + public function withOutputSchema(mixed $outputSchema): self { - $this->outputSchema = $outputSchema; + $obj = clone $this; + $obj->outputSchema = $outputSchema; - return $this; + return $obj; } /** * Enable heavy JavaScript rendering. */ - public function setRenderHeavyJs(bool $renderHeavyJs): self + public function withRenderHeavyJs(bool $renderHeavyJs): self { - $this->renderHeavyJs = $renderHeavyJs; + $obj = clone $this; + $obj->renderHeavyJs = $renderHeavyJs; - return $this; + return $obj; } /** @@ -212,40 +232,44 @@ public function setRenderHeavyJs(bool $renderHeavyJs): self * * @param list $steps */ - public function setSteps(array $steps): self + public function withSteps(array $steps): self { - $this->steps = $steps; + $obj = clone $this; + $obj->steps = $steps; - return $this; + return $obj; } /** * Number of pages to process for pagination. */ - public function setTotalPages(int $totalPages): self + public function withTotalPages(int $totalPages): self { - $this->totalPages = $totalPages; + $obj = clone $this; + $obj->totalPages = $totalPages; - return $this; + return $obj; } /** * HTML content to process (max 2MB, mutually exclusive with website_url). */ - public function setWebsiteHTML(string $websiteHTML): self + public function withWebsiteHTML(string $websiteHTML): self { - $this->websiteHTML = $websiteHTML; + $obj = clone $this; + $obj->websiteHTML = $websiteHTML; - return $this; + return $obj; } /** * URL to scrape (mutually exclusive with website_html). */ - public function setWebsiteURL(string $websiteURL): self + public function withWebsiteURL(string $websiteURL): self { - $this->websiteURL = $websiteURL; + $obj = clone $this; + $obj->websiteURL = $websiteURL; - return $this; + return $obj; } } diff --git a/tests/Resources/CrawlTest.php b/tests/Resources/CrawlTest.php index 97139dd..5a7b86b 100644 --- a/tests/Resources/CrawlTest.php +++ b/tests/Resources/CrawlTest.php @@ -47,7 +47,7 @@ public function testStart(): void $this->markTestSkipped('Prism tests are disabled'); } - $params = CrawlStartParams::from(url: 'https://example.com'); + $params = CrawlStartParams::with(url: 'https://example.com'); $result = $this->client->crawl->start($params); $this->assertTrue(true); // @phpstan-ignore-line @@ -60,14 +60,14 @@ public function testStartWithOptionalParams(): void $this->markTestSkipped('Prism tests are disabled'); } - $params = CrawlStartParams::from( + $params = CrawlStartParams::with( url: 'https://example.com', depth: 0, extractionMode: true, maxPages: 1, prompt: 'prompt', renderHeavyJs: true, - rules: (new Rules)->setExclude(['string'])->setSameDomain(true), + rules: (new Rules)->withExclude(['string'])->withSameDomain(true), schema: (object) [], sitemap: true, ); diff --git a/tests/Resources/FeedbackTest.php b/tests/Resources/FeedbackTest.php index 2da6474..b570214 100644 --- a/tests/Resources/FeedbackTest.php +++ b/tests/Resources/FeedbackTest.php @@ -34,7 +34,7 @@ public function testSubmit(): void $this->markTestSkipped('Prism tests are disabled'); } - $params = FeedbackSubmitParams::from( + $params = FeedbackSubmitParams::with( rating: 0, requestID: '182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e' ); @@ -50,7 +50,7 @@ public function testSubmitWithOptionalParams(): void $this->markTestSkipped('Prism tests are disabled'); } - $params = FeedbackSubmitParams::from( + $params = FeedbackSubmitParams::with( rating: 0, requestID: '182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e', feedbackText: 'feedback_text', diff --git a/tests/Resources/GenerateSchemaTest.php b/tests/Resources/GenerateSchemaTest.php index a148898..c1ec19e 100644 --- a/tests/Resources/GenerateSchemaTest.php +++ b/tests/Resources/GenerateSchemaTest.php @@ -34,7 +34,7 @@ public function testCreate(): void $this->markTestSkipped('Prism tests are disabled'); } - $params = GenerateSchemaCreateParams::from( + $params = GenerateSchemaCreateParams::with( userPrompt: 'Create a schema for product information including name, price, and reviews', ); $result = $this->client->generateSchema->create($params); @@ -49,7 +49,7 @@ public function testCreateWithOptionalParams(): void $this->markTestSkipped('Prism tests are disabled'); } - $params = GenerateSchemaCreateParams::from( + $params = GenerateSchemaCreateParams::with( userPrompt: 'Create a schema for product information including name, price, and reviews', existingSchema: (object) [], ); diff --git a/tests/Resources/MarkdownifyTest.php b/tests/Resources/MarkdownifyTest.php index 0b7866d..d1c897a 100644 --- a/tests/Resources/MarkdownifyTest.php +++ b/tests/Resources/MarkdownifyTest.php @@ -34,7 +34,7 @@ public function testConvert(): void $this->markTestSkipped('Prism tests are disabled'); } - $params = MarkdownifyConvertParams::from(websiteURL: 'https://example.com'); + $params = MarkdownifyConvertParams::with(websiteURL: 'https://example.com'); $result = $this->client->markdownify->convert($params); $this->assertTrue(true); // @phpstan-ignore-line @@ -47,7 +47,7 @@ public function testConvertWithOptionalParams(): void $this->markTestSkipped('Prism tests are disabled'); } - $params = MarkdownifyConvertParams::from( + $params = MarkdownifyConvertParams::with( websiteURL: 'https://example.com', headers: ['foo' => 'string'], steps: ['string'], diff --git a/tests/Resources/SearchscraperTest.php b/tests/Resources/SearchscraperTest.php index d355d3c..a3673ae 100644 --- a/tests/Resources/SearchscraperTest.php +++ b/tests/Resources/SearchscraperTest.php @@ -34,7 +34,7 @@ public function testCreate(): void $this->markTestSkipped('Prism tests are disabled'); } - $params = SearchscraperCreateParams::from( + $params = SearchscraperCreateParams::with( userPrompt: 'Find the latest AI news and extract headlines and summaries' ); $result = $this->client->searchscraper->create($params); @@ -49,7 +49,7 @@ public function testCreateWithOptionalParams(): void $this->markTestSkipped('Prism tests are disabled'); } - $params = SearchscraperCreateParams::from( + $params = SearchscraperCreateParams::with( userPrompt: 'Find the latest AI news and extract headlines and summaries', headers: ['foo' => 'string'], numResults: 3, diff --git a/tests/Resources/SmartscraperTest.php b/tests/Resources/SmartscraperTest.php index 2ae80d5..06fa829 100644 --- a/tests/Resources/SmartscraperTest.php +++ b/tests/Resources/SmartscraperTest.php @@ -34,7 +34,7 @@ public function testCreate(): void $this->markTestSkipped('Prism tests are disabled'); } - $params = SmartscraperCreateParams::from( + $params = SmartscraperCreateParams::with( userPrompt: 'Extract the product name, price, and description' ); $result = $this->client->smartscraper->create($params); @@ -49,7 +49,7 @@ public function testCreateWithOptionalParams(): void $this->markTestSkipped('Prism tests are disabled'); } - $params = SmartscraperCreateParams::from( + $params = SmartscraperCreateParams::with( userPrompt: 'Extract the product name, price, and description', cookies: ['foo' => 'string'], headers: ['foo' => 'string'], From b3eab16fb85eafac7eba187d23d9fc9a5678fd15 Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Wed, 20 Aug 2025 04:05:48 +0000 Subject: [PATCH 03/44] feat(client): improve error handling --- README.md | 4 ++-- src/Core/BaseClient.php | 8 ++++---- src/Errors/APIError.php | 2 +- src/Errors/APIStatusError.php | 26 +++++++++++++++++--------- src/Errors/Error.php | 2 +- 5 files changed, 25 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 21a3c3d..26be0bd 100644 --- a/README.md +++ b/README.md @@ -53,8 +53,8 @@ $client = new Client( $params = SmartscraperCreateParams::with( userPrompt: "Extract the product name, price, and description" ); -$completedSmartscraper = $client->smartscraper->create($params); +$completedSmartscraper = $client->smartscraper->create($params); var_dump($completedSmartscraper->request_id); ``` @@ -80,7 +80,7 @@ try { echo "A 429 status code was received; we should back off a bit.", PHP_EOL; } catch (APIStatusError $e) { echo "Another non-200-range status code was received", PHP_EOL; - var_dump($e->status); + echo $e->getMessage(); } ``` diff --git a/src/Core/BaseClient.php b/src/Core/BaseClient.php index 04e3a10..3657161 100644 --- a/src/Core/BaseClient.php +++ b/src/Core/BaseClient.php @@ -26,7 +26,7 @@ class BaseClient protected RequestFactoryInterface $requestFactory; - protected ClientInterface $requester; + protected ClientInterface $transporter; /** * @param array|string> $headers @@ -41,7 +41,7 @@ public function __construct( $this->requestFactory = Psr17FactoryDiscovery::findRequestFactory(); $this->baseUrl = $this->uriFactory->createUri($baseUrl); - $this->requester = Psr18ClientDiscovery::find(); + $this->transporter = Psr18ClientDiscovery::find(); } /** @@ -158,7 +158,7 @@ protected function sendRequest( int $redirectCount, ): ResponseInterface { $req = Util::withSetBody($this->streamFactory, req: $req, body: $data); - $rsp = $this->requester->sendRequest($req); + $rsp = $this->transporter->sendRequest($req); $code = $rsp->getStatusCode(); if ($code >= 300 && $code < 400) { @@ -172,7 +172,7 @@ protected function sendRequest( } if ($code >= 400 && $code < 500) { - throw APIStatusError::from(null, request: $req, response: $rsp); + throw APIStatusError::from(request: $req, response: $rsp); } if ($code >= 500 && $retryCount < $opts->maxRetries) { diff --git a/src/Errors/APIError.php b/src/Errors/APIError.php index 29ec839..de673ce 100644 --- a/src/Errors/APIError.php +++ b/src/Errors/APIError.php @@ -18,6 +18,6 @@ public function __construct( ?\Throwable $previous = null, string $message = '', ) { - parent::__construct(message: 'response: '.$message.PHP_EOL.'request: '.$request->getBody()->__toString(), previous: $previous); + parent::__construct(message: $message, previous: $previous); } } diff --git a/src/Errors/APIStatusError.php b/src/Errors/APIStatusError.php index 5de7126..e6ceead 100644 --- a/src/Errors/APIStatusError.php +++ b/src/Errors/APIStatusError.php @@ -4,7 +4,7 @@ use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; -use Scrapegraphai\Core\Util; +use Psr\Http\Message\StreamInterface; class APIStatusError extends APIError { @@ -14,7 +14,6 @@ class APIStatusError extends APIError public ?int $status; public function __construct( - public mixed $body, public RequestInterface $request, ResponseInterface $response, ?\Throwable $previous = null, @@ -22,15 +21,19 @@ public function __construct( ) { $this->response = $response; $this->status = $response->getStatusCode(); - $message |= json_encode( - ['status' => $this->status, 'body' => $body], - flags: Util::JSON_ENCODE_FLAGS, - ); - parent::__construct(request: $request, message: $message, previous: $previous); + + $summary = 'Status: '.$this->status.PHP_EOL + .'Response Body: '.self::fmtBody($response->getBody()).PHP_EOL + .'Request Body: '.self::fmtBody($request->getBody()).PHP_EOL; + + if ('' != $message) { + $summary .= $message.PHP_EOL.$summary; + } + + parent::__construct(request: $request, message: $summary, previous: $previous); } public static function from( - mixed $body, RequestInterface $request, ResponseInterface $response ): self { @@ -48,6 +51,11 @@ public static function from( default => APIStatusError::class }; - return new $cls(body: $body, request: $request, response: $response); + return new $cls(request: $request, response: $response); + } + + private static function fmtBody(StreamInterface $body): string + { + return json_encode(json_decode($body->__toString() ?: ''), JSON_PRETTY_PRINT) ?: ''; } } diff --git a/src/Errors/Error.php b/src/Errors/Error.php index d606576..2eb393d 100644 --- a/src/Errors/Error.php +++ b/src/Errors/Error.php @@ -9,6 +9,6 @@ class Error extends \Exception public function __construct(string $message, int $code = 0, ?\Throwable $previous = null) { - parent::__construct($this::DESC.' '.$message, $code, $previous); + parent::__construct($this::DESC.PHP_EOL.$message, $code, $previous); } } From d191c29f3e1889640e16911918041bd3850fd4e8 Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Wed, 20 Aug 2025 04:14:59 +0000 Subject: [PATCH 04/44] feat(client): add streaming --- src/Core/Adapters/GeneratorStream.php | 107 ------------------------- src/Core/Contracts/CloseableStream.php | 19 +++++ src/Core/Util.php | 4 +- 3 files changed, 21 insertions(+), 109 deletions(-) delete mode 100644 src/Core/Adapters/GeneratorStream.php create mode 100644 src/Core/Contracts/CloseableStream.php diff --git a/src/Core/Adapters/GeneratorStream.php b/src/Core/Adapters/GeneratorStream.php deleted file mode 100644 index 052701b..0000000 --- a/src/Core/Adapters/GeneratorStream.php +++ /dev/null @@ -1,107 +0,0 @@ - $st - */ - public function __construct(private \Generator $st) {} - - public function __toString(): string - { - try { - return $this->getContents(); - } catch (\Throwable) { - return ''; - } - } - - public function getSize(): ?int - { - return null; - } - - public function eof(): bool - { - return !strlen($this->buf) && !$this->st->valid(); - } - - public function close(): void - { - $ex = new class() extends \Exception {}; - - try { - $this->st->throw(new $ex); - } catch (\Throwable) { - } - } - - public function detach(): null - { - $this->buf = ''; - $this->close(); - - return null; - } - - public function tell(): int - { - return $this->pos; - } - - public function rewind(): void - { - $this->buf = ''; - $this->st->rewind(); - } - - public function isSeekable(): bool - { - return false; - } - - public function seek(int $offset, int $whence = SEEK_SET): void {} - - public function isWritable(): bool - { - return false; - } - - public function write(string $string): int - { - return 0; - } - - public function isReadable(): bool - { - return !$this->eof(); - } - - public function read(int $length): string - { - return ''; - } - - public function getContents(): string - { - foreach ($this->st as $chunk) { - $this->buf .= $chunk; - } - - return $this->buf; - } - - public function getMetadata(?string $key = null): mixed - { - return null; - } -} diff --git a/src/Core/Contracts/CloseableStream.php b/src/Core/Contracts/CloseableStream.php new file mode 100644 index 0000000..c7d5635 --- /dev/null +++ b/src/Core/Contracts/CloseableStream.php @@ -0,0 +1,19 @@ + + */ +interface CloseableStream extends \IteratorAggregate +{ + /** + * Manually force the stream to close early. + * Iterating through will automatically close as well. + */ + public function close(): void; +} diff --git a/src/Core/Util.php b/src/Core/Util.php index 6f64612..040fd25 100644 --- a/src/Core/Util.php +++ b/src/Core/Util.php @@ -265,13 +265,13 @@ public static function decodeLines(\Iterator $stream): \Iterator /** * @param \Iterator $lines * - * @return \Iterator< + * @return \Generator< * array{ * event?: null|string, data?: null|string, id?: null|string, retry?: null|int * }, * > */ - public static function decodeSSE(\Iterator $lines): \Iterator + public static function decodeSSE(\Iterator $lines): \Generator { $blank = ['event' => null, 'data' => null, 'id' => null, 'retry' => null]; $acc = []; From 2a938ad3e8e2658d53813570165fca69ebca554d Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Wed, 20 Aug 2025 04:20:44 +0000 Subject: [PATCH 05/44] feat(client): use named parameters in methods --- README.md | 54 ++++++++++---------- src/Contracts/CrawlContract.php | 33 +++++++----- src/Contracts/FeedbackContract.php | 11 ++-- src/Contracts/GenerateSchemaContract.php | 9 ++-- src/Contracts/MarkdownifyContract.php | 11 ++-- src/Contracts/SearchscraperContract.php | 16 +++--- src/Contracts/SmartscraperContract.php | 34 +++++++----- src/Crawl/CrawlService.php | 46 +++++++++++------ src/Feedback/FeedbackService.php | 20 +++++--- src/GenerateSchema/GenerateSchemaService.php | 14 ++--- src/Markdownify/MarkdownifyService.php | 14 ++--- src/Searchscraper/SearchscraperService.php | 24 +++++---- src/Smartscraper/SmartscraperService.php | 48 +++++++++++------ tests/Resources/CrawlTest.php | 7 +-- tests/Resources/FeedbackTest.php | 7 +-- tests/Resources/GenerateSchemaTest.php | 15 ++---- tests/Resources/MarkdownifyTest.php | 17 +++--- tests/Resources/SearchscraperTest.php | 15 ++---- tests/Resources/SmartscraperTest.php | 15 ++---- 19 files changed, 222 insertions(+), 188 deletions(-) diff --git a/README.md b/README.md index 26be0bd..f5a9396 100644 --- a/README.md +++ b/README.md @@ -39,25 +39,33 @@ To use this package, install via Composer by adding the following to your applic ## Usage +This library uses named parameters to specify optional arguments. +Parameters with a default value must be set by name. + ```php smartscraper->create( userPrompt: "Extract the product name, price, and description" ); -$completedSmartscraper = $client->smartscraper->create($params); var_dump($completedSmartscraper->request_id); ``` +## Value Objects + +It is recommended to use the `with` constructor `Dog::with(name: "Joey")` +and named parameters to initialize value objects. + +However builders are provided as well `(new Dog)->withName("Joey")`. + ### Handling errors When the library is unable to connect to the API, or if the API returns a non-success status code (i.e., 4xx or 5xx response), a subclass of `Scrapegraphai\Errors\APIError` will be thrown: @@ -66,21 +74,19 @@ When the library is unable to connect to the API, or if the API returns a non-su smartscraper->create($params); + $completedSmartscraper = $client->smartscraper->create( + userPrompt: "Extract the product name, price, and description" + ); } catch (APIConnectionError $e) { - echo "The server could not be reached", PHP_EOL; - var_dump($e->getPrevious()); + echo "The server could not be reached", PHP_EOL; + var_dump($e->getPrevious()); } catch (RateLimitError $_) { - echo "A 429 status code was received; we should back off a bit.", PHP_EOL; + echo "A 429 status code was received; we should back off a bit.", PHP_EOL; } catch (APIStatusError $e) { - echo "Another non-200-range status code was received", PHP_EOL; - echo $e->getMessage(); + echo "Another non-200-range status code was received", PHP_EOL; + echo $e->getMessage(); } ``` @@ -113,17 +119,15 @@ You can use the `max_retries` option to configure or disable this: use Scrapegraphai\Client; use Scrapegraphai\RequestOptions; -use Scrapegraphai\Smartscraper\SmartscraperCreateParams; // Configure the default for all requests: $client = new Client(maxRetries: 0); -$params = SmartscraperCreateParams::with( - userPrompt: "Extract the product name, price, and description" -); -// Or, configure per-request:$result = $client - ->smartscraper - ->create($params, new RequestOptions(maxRetries: 5)); +// Or, configure per-request: +$result = $client->smartscraper->create( + userPrompt: "Extract the product name, price, and description", + new RequestOptions(maxRetries: 5), +); ``` ## Advanced concepts @@ -140,15 +144,9 @@ Note: the `extra_` parameters of the same name overrides the documented paramete smartscraper - ->create( - $params, +$completedSmartscraper = $client->smartscraper->create( + userPrompt: "Extract the product name, price, and description", new RequestOptions( extraQueryParams: ["my_query_parameter" => "value"], extraBodyParams: ["my_body_parameter" => "value"], diff --git a/src/Contracts/CrawlContract.php b/src/Contracts/CrawlContract.php index beeb74e..d450583 100644 --- a/src/Contracts/CrawlContract.php +++ b/src/Contracts/CrawlContract.php @@ -4,7 +4,6 @@ namespace Scrapegraphai\Contracts; -use Scrapegraphai\Crawl\CrawlStartParams; use Scrapegraphai\Crawl\CrawlStartParams\Rules; use Scrapegraphai\RequestOptions; use Scrapegraphai\Responses\Crawl\CrawlGetResultsResponse; @@ -18,20 +17,26 @@ public function retrieveResults( ): CrawlGetResultsResponse; /** - * @param array{ - * url: string, - * depth?: int, - * extractionMode?: bool, - * maxPages?: int, - * prompt?: null|string, - * renderHeavyJs?: bool, - * rules?: Rules, - * schema?: mixed, - * sitemap?: bool, - * }|CrawlStartParams $params + * @param string $url Starting URL for crawling + * @param int $depth Maximum crawl depth from starting URL + * @param bool $extractionMode Use AI extraction (true) or markdown conversion (false) + * @param int $maxPages Maximum number of pages to crawl + * @param null|string $prompt Extraction prompt (required if extraction_mode is true) + * @param bool $renderHeavyJs Enable heavy JavaScript rendering + * @param Rules $rules + * @param mixed $schema Output schema for extraction + * @param bool $sitemap Use sitemap for crawling */ public function start( - array|CrawlStartParams $params, - ?RequestOptions $requestOptions = null + $url, + $depth = null, + $extractionMode = null, + $maxPages = null, + $prompt = null, + $renderHeavyJs = null, + $rules = null, + $schema = null, + $sitemap = null, + ?RequestOptions $requestOptions = null, ): CrawlStartResponse; } diff --git a/src/Contracts/FeedbackContract.php b/src/Contracts/FeedbackContract.php index d6d9947..f984247 100644 --- a/src/Contracts/FeedbackContract.php +++ b/src/Contracts/FeedbackContract.php @@ -4,19 +4,20 @@ namespace Scrapegraphai\Contracts; -use Scrapegraphai\Feedback\FeedbackSubmitParams; use Scrapegraphai\RequestOptions; use Scrapegraphai\Responses\Feedback\FeedbackSubmitResponse; interface FeedbackContract { /** - * @param array{ - * rating: int, requestID: string, feedbackText?: null|string - * }|FeedbackSubmitParams $params + * @param int $rating Rating score + * @param string $requestID Request to provide feedback for + * @param null|string $feedbackText Optional feedback comments */ public function submit( - array|FeedbackSubmitParams $params, + $rating, + $requestID, + $feedbackText = null, ?RequestOptions $requestOptions = null, ): FeedbackSubmitResponse; } diff --git a/src/Contracts/GenerateSchemaContract.php b/src/Contracts/GenerateSchemaContract.php index fd3f522..5970bcb 100644 --- a/src/Contracts/GenerateSchemaContract.php +++ b/src/Contracts/GenerateSchemaContract.php @@ -4,7 +4,6 @@ namespace Scrapegraphai\Contracts; -use Scrapegraphai\GenerateSchema\GenerateSchemaCreateParams; use Scrapegraphai\RequestOptions; use Scrapegraphai\Responses\GenerateSchema\GenerateSchemaGetResponse\CompletedSchemaGenerationResponse; use Scrapegraphai\Responses\GenerateSchema\GenerateSchemaGetResponse\FailedSchemaGenerationResponse; @@ -13,12 +12,12 @@ interface GenerateSchemaContract { /** - * @param array{ - * userPrompt: string, existingSchema?: mixed - * }|GenerateSchemaCreateParams $params + * @param string $userPrompt Natural language description of desired schema + * @param mixed $existingSchema Existing schema to modify or extend */ public function create( - array|GenerateSchemaCreateParams $params, + $userPrompt, + $existingSchema = null, ?RequestOptions $requestOptions = null, ): GenerateSchemaNewResponse; diff --git a/src/Contracts/MarkdownifyContract.php b/src/Contracts/MarkdownifyContract.php index 8737c95..2cbba13 100644 --- a/src/Contracts/MarkdownifyContract.php +++ b/src/Contracts/MarkdownifyContract.php @@ -5,19 +5,20 @@ namespace Scrapegraphai\Contracts; use Scrapegraphai\Markdownify\CompletedMarkdownify; -use Scrapegraphai\Markdownify\MarkdownifyConvertParams; use Scrapegraphai\RequestOptions; use Scrapegraphai\Responses\Markdownify\MarkdownifyGetStatusResponse\FailedMarkdownifyResponse; interface MarkdownifyContract { /** - * @param array{ - * websiteURL: string, headers?: array, steps?: list - * }|MarkdownifyConvertParams $params + * @param string $websiteURL URL to convert to markdown + * @param array $headers + * @param list $steps Interaction steps before conversion */ public function convert( - array|MarkdownifyConvertParams $params, + $websiteURL, + $headers = null, + $steps = null, ?RequestOptions $requestOptions = null, ): CompletedMarkdownify; diff --git a/src/Contracts/SearchscraperContract.php b/src/Contracts/SearchscraperContract.php index 2f7a099..82dac5f 100644 --- a/src/Contracts/SearchscraperContract.php +++ b/src/Contracts/SearchscraperContract.php @@ -7,20 +7,20 @@ use Scrapegraphai\RequestOptions; use Scrapegraphai\Responses\Searchscraper\SearchscraperGetStatusResponse\FailedSearchScraperResponse; use Scrapegraphai\Searchscraper\CompletedSearchScraper; -use Scrapegraphai\Searchscraper\SearchscraperCreateParams; interface SearchscraperContract { /** - * @param array{ - * userPrompt: string, - * headers?: array, - * numResults?: int, - * outputSchema?: mixed, - * }|SearchscraperCreateParams $params + * @param string $userPrompt Search query and extraction instruction + * @param array $headers + * @param int $numResults Number of websites to scrape from search results + * @param mixed $outputSchema JSON schema for structured output */ public function create( - array|SearchscraperCreateParams $params, + $userPrompt, + $headers = null, + $numResults = null, + $outputSchema = null, ?RequestOptions $requestOptions = null, ): CompletedSearchScraper; diff --git a/src/Contracts/SmartscraperContract.php b/src/Contracts/SmartscraperContract.php index 9b99f80..4df2546 100644 --- a/src/Contracts/SmartscraperContract.php +++ b/src/Contracts/SmartscraperContract.php @@ -7,26 +7,32 @@ use Scrapegraphai\RequestOptions; use Scrapegraphai\Smartscraper\CompletedSmartscraper; use Scrapegraphai\Smartscraper\FailedSmartscraper; -use Scrapegraphai\Smartscraper\SmartscraperCreateParams; interface SmartscraperContract { /** - * @param array{ - * userPrompt: string, - * cookies?: array, - * headers?: array, - * numberOfScrolls?: int, - * outputSchema?: mixed, - * renderHeavyJs?: bool, - * steps?: list, - * totalPages?: int, - * websiteHTML?: string, - * websiteURL?: string, - * }|SmartscraperCreateParams $params + * @param string $userPrompt Extraction instruction for the LLM + * @param array $cookies Cookies to include in the request + * @param array $headers HTTP headers to include in the request + * @param int $numberOfScrolls Number of infinite scroll operations to perform + * @param mixed $outputSchema JSON schema defining the expected output structure + * @param bool $renderHeavyJs Enable heavy JavaScript rendering + * @param list $steps Website interaction steps (e.g., clicking buttons) + * @param int $totalPages Number of pages to process for pagination + * @param string $websiteHTML HTML content to process (max 2MB, mutually exclusive with website_url) + * @param string $websiteURL URL to scrape (mutually exclusive with website_html) */ public function create( - array|SmartscraperCreateParams $params, + $userPrompt, + $cookies = null, + $headers = null, + $numberOfScrolls = null, + $outputSchema = null, + $renderHeavyJs = null, + $steps = null, + $totalPages = null, + $websiteHTML = null, + $websiteURL = null, ?RequestOptions $requestOptions = null, ): CompletedSmartscraper; diff --git a/src/Crawl/CrawlService.php b/src/Crawl/CrawlService.php index b1f7254..c9ddf10 100644 --- a/src/Crawl/CrawlService.php +++ b/src/Crawl/CrawlService.php @@ -38,25 +38,41 @@ public function retrieveResults( * Supports both AI extraction mode and markdown conversion mode. * Returns a task ID for async processing. * - * @param array{ - * url: string, - * depth?: int, - * extractionMode?: bool, - * maxPages?: int, - * prompt?: null|string, - * renderHeavyJs?: bool, - * rules?: Rules, - * schema?: mixed, - * sitemap?: bool, - * }|CrawlStartParams $params + * @param string $url Starting URL for crawling + * @param int $depth Maximum crawl depth from starting URL + * @param bool $extractionMode Use AI extraction (true) or markdown conversion (false) + * @param int $maxPages Maximum number of pages to crawl + * @param null|string $prompt Extraction prompt (required if extraction_mode is true) + * @param bool $renderHeavyJs Enable heavy JavaScript rendering + * @param Rules $rules + * @param mixed $schema Output schema for extraction + * @param bool $sitemap Use sitemap for crawling */ public function start( - array|CrawlStartParams $params, - ?RequestOptions $requestOptions = null + $url, + $depth = null, + $extractionMode = null, + $maxPages = null, + $prompt = null, + $renderHeavyJs = null, + $rules = null, + $schema = null, + $sitemap = null, + ?RequestOptions $requestOptions = null, ): CrawlStartResponse { [$parsed, $options] = CrawlStartParams::parseRequest( - $params, - $requestOptions + [ + 'url' => $url, + 'depth' => $depth, + 'extractionMode' => $extractionMode, + 'maxPages' => $maxPages, + 'prompt' => $prompt, + 'renderHeavyJs' => $renderHeavyJs, + 'rules' => $rules, + 'schema' => $schema, + 'sitemap' => $sitemap, + ], + $requestOptions, ); $resp = $this->client->request( method: 'post', diff --git a/src/Feedback/FeedbackService.php b/src/Feedback/FeedbackService.php index 86ad79e..cdf73a2 100644 --- a/src/Feedback/FeedbackService.php +++ b/src/Feedback/FeedbackService.php @@ -17,17 +17,23 @@ public function __construct(private Client $client) {} /** * Submit feedback for a specific request. * - * @param array{ - * rating: int, requestID: string, feedbackText?: null|string - * }|FeedbackSubmitParams $params + * @param int $rating Rating score + * @param string $requestID Request to provide feedback for + * @param null|string $feedbackText Optional feedback comments */ public function submit( - array|FeedbackSubmitParams $params, - ?RequestOptions $requestOptions = null + $rating, + $requestID, + $feedbackText = null, + ?RequestOptions $requestOptions = null, ): FeedbackSubmitResponse { [$parsed, $options] = FeedbackSubmitParams::parseRequest( - $params, - $requestOptions + [ + 'rating' => $rating, + 'requestID' => $requestID, + 'feedbackText' => $feedbackText, + ], + $requestOptions, ); $resp = $this->client->request( method: 'post', diff --git a/src/GenerateSchema/GenerateSchemaService.php b/src/GenerateSchema/GenerateSchemaService.php index 45bfe33..d46dc2a 100644 --- a/src/GenerateSchema/GenerateSchemaService.php +++ b/src/GenerateSchema/GenerateSchemaService.php @@ -21,17 +21,17 @@ public function __construct(private Client $client) {} * Generate or modify JSON schemas based on natural language descriptions. * Can create new schemas or extend existing ones. * - * @param array{ - * userPrompt: string, existingSchema?: mixed - * }|GenerateSchemaCreateParams $params + * @param string $userPrompt Natural language description of desired schema + * @param mixed $existingSchema Existing schema to modify or extend */ public function create( - array|GenerateSchemaCreateParams $params, - ?RequestOptions $requestOptions = null, + $userPrompt, + $existingSchema = null, + ?RequestOptions $requestOptions = null ): GenerateSchemaNewResponse { [$parsed, $options] = GenerateSchemaCreateParams::parseRequest( - $params, - $requestOptions + ['userPrompt' => $userPrompt, 'existingSchema' => $existingSchema], + $requestOptions, ); $resp = $this->client->request( method: 'post', diff --git a/src/Markdownify/MarkdownifyService.php b/src/Markdownify/MarkdownifyService.php index 4c88a29..a0387e6 100644 --- a/src/Markdownify/MarkdownifyService.php +++ b/src/Markdownify/MarkdownifyService.php @@ -18,17 +18,19 @@ public function __construct(private Client $client) {} /** * Convert web page content to clean Markdown format. * - * @param array{ - * websiteURL: string, headers?: array, steps?: list - * }|MarkdownifyConvertParams $params + * @param string $websiteURL URL to convert to markdown + * @param array $headers + * @param list $steps Interaction steps before conversion */ public function convert( - array|MarkdownifyConvertParams $params, + $websiteURL, + $headers = null, + $steps = null, ?RequestOptions $requestOptions = null, ): CompletedMarkdownify { [$parsed, $options] = MarkdownifyConvertParams::parseRequest( - $params, - $requestOptions + ['websiteURL' => $websiteURL, 'headers' => $headers, 'steps' => $steps], + $requestOptions, ); $resp = $this->client->request( method: 'post', diff --git a/src/Searchscraper/SearchscraperService.php b/src/Searchscraper/SearchscraperService.php index 45e5efe..43b9e52 100644 --- a/src/Searchscraper/SearchscraperService.php +++ b/src/Searchscraper/SearchscraperService.php @@ -19,20 +19,26 @@ public function __construct(private Client $client) {} * Performs web search, selects relevant URLs, and extracts structured data from multiple websites. * Uses LLM to refine search queries and merge results from different sources. * - * @param array{ - * userPrompt: string, - * headers?: array, - * numResults?: int, - * outputSchema?: mixed, - * }|SearchscraperCreateParams $params + * @param string $userPrompt Search query and extraction instruction + * @param array $headers + * @param int $numResults Number of websites to scrape from search results + * @param mixed $outputSchema JSON schema for structured output */ public function create( - array|SearchscraperCreateParams $params, + $userPrompt, + $headers = null, + $numResults = null, + $outputSchema = null, ?RequestOptions $requestOptions = null, ): CompletedSearchScraper { [$parsed, $options] = SearchscraperCreateParams::parseRequest( - $params, - $requestOptions + [ + 'userPrompt' => $userPrompt, + 'headers' => $headers, + 'numResults' => $numResults, + 'outputSchema' => $outputSchema, + ], + $requestOptions, ); $resp = $this->client->request( method: 'post', diff --git a/src/Smartscraper/SmartscraperService.php b/src/Smartscraper/SmartscraperService.php index 09f6806..135fcfb 100644 --- a/src/Smartscraper/SmartscraperService.php +++ b/src/Smartscraper/SmartscraperService.php @@ -19,26 +19,44 @@ public function __construct(private Client $client) {} * Main scraping endpoint with LLM-powered content analysis. Supports various fetching providers, * infinite scrolling, pagination, and custom output schemas. * - * @param array{ - * userPrompt: string, - * cookies?: array, - * headers?: array, - * numberOfScrolls?: int, - * outputSchema?: mixed, - * renderHeavyJs?: bool, - * steps?: list, - * totalPages?: int, - * websiteHTML?: string, - * websiteURL?: string, - * }|SmartscraperCreateParams $params + * @param string $userPrompt Extraction instruction for the LLM + * @param array $cookies Cookies to include in the request + * @param array $headers HTTP headers to include in the request + * @param int $numberOfScrolls Number of infinite scroll operations to perform + * @param mixed $outputSchema JSON schema defining the expected output structure + * @param bool $renderHeavyJs Enable heavy JavaScript rendering + * @param list $steps Website interaction steps (e.g., clicking buttons) + * @param int $totalPages Number of pages to process for pagination + * @param string $websiteHTML HTML content to process (max 2MB, mutually exclusive with website_url) + * @param string $websiteURL URL to scrape (mutually exclusive with website_html) */ public function create( - array|SmartscraperCreateParams $params, + $userPrompt, + $cookies = null, + $headers = null, + $numberOfScrolls = null, + $outputSchema = null, + $renderHeavyJs = null, + $steps = null, + $totalPages = null, + $websiteHTML = null, + $websiteURL = null, ?RequestOptions $requestOptions = null, ): CompletedSmartscraper { [$parsed, $options] = SmartscraperCreateParams::parseRequest( - $params, - $requestOptions + [ + 'userPrompt' => $userPrompt, + 'cookies' => $cookies, + 'headers' => $headers, + 'numberOfScrolls' => $numberOfScrolls, + 'outputSchema' => $outputSchema, + 'renderHeavyJs' => $renderHeavyJs, + 'steps' => $steps, + 'totalPages' => $totalPages, + 'websiteHTML' => $websiteHTML, + 'websiteURL' => $websiteURL, + ], + $requestOptions, ); $resp = $this->client->request( method: 'post', diff --git a/tests/Resources/CrawlTest.php b/tests/Resources/CrawlTest.php index 5a7b86b..ac83ae1 100644 --- a/tests/Resources/CrawlTest.php +++ b/tests/Resources/CrawlTest.php @@ -6,7 +6,6 @@ use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use Scrapegraphai\Client; -use Scrapegraphai\Crawl\CrawlStartParams; use Scrapegraphai\Crawl\CrawlStartParams\Rules; use Tests\UnsupportedMockTests; @@ -47,8 +46,7 @@ public function testStart(): void $this->markTestSkipped('Prism tests are disabled'); } - $params = CrawlStartParams::with(url: 'https://example.com'); - $result = $this->client->crawl->start($params); + $result = $this->client->crawl->start(url: 'https://example.com'); $this->assertTrue(true); // @phpstan-ignore-line } @@ -60,7 +58,7 @@ public function testStartWithOptionalParams(): void $this->markTestSkipped('Prism tests are disabled'); } - $params = CrawlStartParams::with( + $result = $this->client->crawl->start( url: 'https://example.com', depth: 0, extractionMode: true, @@ -71,7 +69,6 @@ public function testStartWithOptionalParams(): void schema: (object) [], sitemap: true, ); - $result = $this->client->crawl->start($params); $this->assertTrue(true); // @phpstan-ignore-line } diff --git a/tests/Resources/FeedbackTest.php b/tests/Resources/FeedbackTest.php index b570214..2fa387c 100644 --- a/tests/Resources/FeedbackTest.php +++ b/tests/Resources/FeedbackTest.php @@ -6,7 +6,6 @@ use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use Scrapegraphai\Client; -use Scrapegraphai\Feedback\FeedbackSubmitParams; use Tests\UnsupportedMockTests; /** @@ -34,11 +33,10 @@ public function testSubmit(): void $this->markTestSkipped('Prism tests are disabled'); } - $params = FeedbackSubmitParams::with( + $result = $this->client->feedback->submit( rating: 0, requestID: '182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e' ); - $result = $this->client->feedback->submit($params); $this->assertTrue(true); // @phpstan-ignore-line } @@ -50,12 +48,11 @@ public function testSubmitWithOptionalParams(): void $this->markTestSkipped('Prism tests are disabled'); } - $params = FeedbackSubmitParams::with( + $result = $this->client->feedback->submit( rating: 0, requestID: '182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e', feedbackText: 'feedback_text', ); - $result = $this->client->feedback->submit($params); $this->assertTrue(true); // @phpstan-ignore-line } diff --git a/tests/Resources/GenerateSchemaTest.php b/tests/Resources/GenerateSchemaTest.php index c1ec19e..3c0a545 100644 --- a/tests/Resources/GenerateSchemaTest.php +++ b/tests/Resources/GenerateSchemaTest.php @@ -6,7 +6,6 @@ use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use Scrapegraphai\Client; -use Scrapegraphai\GenerateSchema\GenerateSchemaCreateParams; use Tests\UnsupportedMockTests; /** @@ -34,10 +33,9 @@ public function testCreate(): void $this->markTestSkipped('Prism tests are disabled'); } - $params = GenerateSchemaCreateParams::with( + $result = $this->client->generateSchema->create( userPrompt: 'Create a schema for product information including name, price, and reviews', ); - $result = $this->client->generateSchema->create($params); $this->assertTrue(true); // @phpstan-ignore-line } @@ -49,11 +47,10 @@ public function testCreateWithOptionalParams(): void $this->markTestSkipped('Prism tests are disabled'); } - $params = GenerateSchemaCreateParams::with( + $result = $this->client->generateSchema->create( userPrompt: 'Create a schema for product information including name, price, and reviews', existingSchema: (object) [], ); - $result = $this->client->generateSchema->create($params); $this->assertTrue(true); // @phpstan-ignore-line } @@ -65,11 +62,9 @@ public function testRetrieve(): void $this->markTestSkipped('Prism tests are disabled'); } - $result = $this - ->client - ->generateSchema - ->retrieve('182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e') - ; + $result = $this->client->generateSchema->retrieve( + '182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e' + ); $this->assertTrue(true); // @phpstan-ignore-line } diff --git a/tests/Resources/MarkdownifyTest.php b/tests/Resources/MarkdownifyTest.php index d1c897a..cdeacc8 100644 --- a/tests/Resources/MarkdownifyTest.php +++ b/tests/Resources/MarkdownifyTest.php @@ -6,7 +6,6 @@ use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use Scrapegraphai\Client; -use Scrapegraphai\Markdownify\MarkdownifyConvertParams; use Tests\UnsupportedMockTests; /** @@ -34,8 +33,9 @@ public function testConvert(): void $this->markTestSkipped('Prism tests are disabled'); } - $params = MarkdownifyConvertParams::with(websiteURL: 'https://example.com'); - $result = $this->client->markdownify->convert($params); + $result = $this->client->markdownify->convert( + websiteURL: 'https://example.com' + ); $this->assertTrue(true); // @phpstan-ignore-line } @@ -47,12 +47,11 @@ public function testConvertWithOptionalParams(): void $this->markTestSkipped('Prism tests are disabled'); } - $params = MarkdownifyConvertParams::with( + $result = $this->client->markdownify->convert( websiteURL: 'https://example.com', headers: ['foo' => 'string'], steps: ['string'], ); - $result = $this->client->markdownify->convert($params); $this->assertTrue(true); // @phpstan-ignore-line } @@ -64,11 +63,9 @@ public function testRetrieveStatus(): void $this->markTestSkipped('Prism tests are disabled'); } - $result = $this - ->client - ->markdownify - ->retrieveStatus('182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e') - ; + $result = $this->client->markdownify->retrieveStatus( + '182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e' + ); $this->assertTrue(true); // @phpstan-ignore-line } diff --git a/tests/Resources/SearchscraperTest.php b/tests/Resources/SearchscraperTest.php index a3673ae..a2bb574 100644 --- a/tests/Resources/SearchscraperTest.php +++ b/tests/Resources/SearchscraperTest.php @@ -6,7 +6,6 @@ use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use Scrapegraphai\Client; -use Scrapegraphai\Searchscraper\SearchscraperCreateParams; use Tests\UnsupportedMockTests; /** @@ -34,10 +33,9 @@ public function testCreate(): void $this->markTestSkipped('Prism tests are disabled'); } - $params = SearchscraperCreateParams::with( + $result = $this->client->searchscraper->create( userPrompt: 'Find the latest AI news and extract headlines and summaries' ); - $result = $this->client->searchscraper->create($params); $this->assertTrue(true); // @phpstan-ignore-line } @@ -49,13 +47,12 @@ public function testCreateWithOptionalParams(): void $this->markTestSkipped('Prism tests are disabled'); } - $params = SearchscraperCreateParams::with( + $result = $this->client->searchscraper->create( userPrompt: 'Find the latest AI news and extract headlines and summaries', headers: ['foo' => 'string'], numResults: 3, outputSchema: (object) [], ); - $result = $this->client->searchscraper->create($params); $this->assertTrue(true); // @phpstan-ignore-line } @@ -67,11 +64,9 @@ public function testRetrieveStatus(): void $this->markTestSkipped('Prism tests are disabled'); } - $result = $this - ->client - ->searchscraper - ->retrieveStatus('182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e') - ; + $result = $this->client->searchscraper->retrieveStatus( + '182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e' + ); $this->assertTrue(true); // @phpstan-ignore-line } diff --git a/tests/Resources/SmartscraperTest.php b/tests/Resources/SmartscraperTest.php index 06fa829..350aaf9 100644 --- a/tests/Resources/SmartscraperTest.php +++ b/tests/Resources/SmartscraperTest.php @@ -6,7 +6,6 @@ use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use Scrapegraphai\Client; -use Scrapegraphai\Smartscraper\SmartscraperCreateParams; use Tests\UnsupportedMockTests; /** @@ -34,10 +33,9 @@ public function testCreate(): void $this->markTestSkipped('Prism tests are disabled'); } - $params = SmartscraperCreateParams::with( + $result = $this->client->smartscraper->create( userPrompt: 'Extract the product name, price, and description' ); - $result = $this->client->smartscraper->create($params); $this->assertTrue(true); // @phpstan-ignore-line } @@ -49,7 +47,7 @@ public function testCreateWithOptionalParams(): void $this->markTestSkipped('Prism tests are disabled'); } - $params = SmartscraperCreateParams::with( + $result = $this->client->smartscraper->create( userPrompt: 'Extract the product name, price, and description', cookies: ['foo' => 'string'], headers: ['foo' => 'string'], @@ -61,7 +59,6 @@ public function testCreateWithOptionalParams(): void websiteHTML: 'website_html', websiteURL: 'https://example.com/product', ); - $result = $this->client->smartscraper->create($params); $this->assertTrue(true); // @phpstan-ignore-line } @@ -73,11 +70,9 @@ public function testRetrieve(): void $this->markTestSkipped('Prism tests are disabled'); } - $result = $this - ->client - ->smartscraper - ->retrieve('182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e') - ; + $result = $this->client->smartscraper->retrieve( + '182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e' + ); $this->assertTrue(true); // @phpstan-ignore-line } From 1affe59d73390c6012d1f5319db5d3dc61876bab Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Wed, 20 Aug 2025 13:41:56 +0000 Subject: [PATCH 06/44] codegen metadata --- .stats.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.stats.yml b/.stats.yml index 561b1cc..6804ffb 100644 --- a/.stats.yml +++ b/.stats.yml @@ -1,4 +1,4 @@ configured_endpoints: 15 -openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/scrapegraphai%2Fscrapegraphai-969ebada41127057e4cda129b2e7206224743b5c7fd33aa8ae062ff71b775ac9.yml -openapi_spec_hash: 2b2c2c684e6f6885398efca5f2b1f854 +openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/scrapegraphai%2Fscrapegraphai-633fdeab6abaefbe666099e8f86ce6b2acc9dacff1c33a80813bb04e8e437229.yml +openapi_spec_hash: f41ec90694ca8e7233bd20cc7ff1afbf config_hash: 6889576ba0fdc14f2c71cea09a60a0f6 From c38da939ecdc0ee7a092a58faf41e63a92b61b11 Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Thu, 21 Aug 2025 03:55:08 +0000 Subject: [PATCH 07/44] chore: readme improvements --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f5a9396..1fd2f5a 100644 --- a/README.md +++ b/README.md @@ -61,10 +61,10 @@ var_dump($completedSmartscraper->request_id); ## Value Objects -It is recommended to use the `with` constructor `Dog::with(name: "Joey")` +It is recommended to use the static `with` constructor `Dog::with(name: "Joey")` and named parameters to initialize value objects. -However builders are provided as well `(new Dog)->withName("Joey")`. +However, builders are also provided `(new Dog)->withName("Joey")`. ### Handling errors From c55bbd74aa39be1cf5ab1997fa140d4e92f96de3 Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Thu, 21 Aug 2025 04:03:55 +0000 Subject: [PATCH 08/44] feat(php): rename internal types --- README.md | 2 +- src/Core/Concerns/Page.php | 22 ------------------- src/Core/Concerns/{Enum.php => SdkEnum.php} | 5 ++++- src/Core/Concerns/{Model.php => SdkModel.php} | 2 +- .../Concerns/{Params.php => SdkParams.php} | 2 +- src/Core/Concerns/{Union.php => SdkUnion.php} | 2 +- src/Core/Contracts/BasePage.php | 16 +++++++++----- src/Core/Pagination/AbstractPage.php | 4 ++-- src/Crawl/CrawlStartParams.php | 8 +++---- src/Crawl/CrawlStartParams/Rules.php | 4 ++-- src/Feedback/FeedbackSubmitParams.php | 8 +++---- .../GenerateSchemaCreateParams.php | 8 +++---- src/Markdownify/CompletedMarkdownify.php | 4 ++-- .../CompletedMarkdownify/Status.php | 4 ++-- src/Markdownify/MarkdownifyConvertParams.php | 8 +++---- .../Crawl/CrawlGetResultsResponse.php | 4 ++-- .../Crawl/CrawlGetResultsResponse/Result.php | 4 ++-- .../Crawl/CrawlGetResultsResponse/Status.php | 4 ++-- src/Responses/Crawl/CrawlStartResponse.php | 4 ++-- src/Responses/Credits/CreditGetResponse.php | 4 ++-- .../Feedback/FeedbackSubmitResponse.php | 4 ++-- .../GenerateSchemaGetResponse.php | 4 ++-- .../CompletedSchemaGenerationResponse.php | 4 ++-- .../Status.php | 4 ++-- .../FailedSchemaGenerationResponse.php | 4 ++-- .../FailedSchemaGenerationResponse/Status.php | 4 ++-- .../GenerateSchemaNewResponse.php | 4 ++-- .../GenerateSchemaNewResponse/Status.php | 4 ++-- .../Healthz/HealthzCheckResponse.php | 4 ++-- .../MarkdownifyGetStatusResponse.php | 4 ++-- .../FailedMarkdownifyResponse.php | 4 ++-- .../FailedMarkdownifyResponse/Status.php | 4 ++-- .../SearchscraperGetStatusResponse.php | 4 ++-- .../FailedSearchScraperResponse.php | 4 ++-- .../FailedSearchScraperResponse/Status.php | 4 ++-- .../Smartscraper/SmartscraperGetResponse.php | 4 ++-- .../Smartscraper/SmartscraperListResponse.php | 4 ++-- .../Validate/ValidateAPIKeyResponse.php | 4 ++-- src/Searchscraper/CompletedSearchScraper.php | 4 ++-- .../CompletedSearchScraper/Status.php | 4 ++-- .../SearchscraperCreateParams.php | 8 +++---- src/Smartscraper/CompletedSmartscraper.php | 4 ++-- .../CompletedSmartscraper/Status.php | 4 ++-- src/Smartscraper/FailedSmartscraper.php | 4 ++-- .../FailedSmartscraper/Status.php | 4 ++-- src/Smartscraper/SmartscraperCreateParams.php | 8 +++---- tests/Core/TestModel.php | 4 ++-- 47 files changed, 111 insertions(+), 124 deletions(-) delete mode 100644 src/Core/Concerns/Page.php rename src/Core/Concerns/{Enum.php => SdkEnum.php} (95%) rename src/Core/Concerns/{Model.php => SdkModel.php} (99%) rename src/Core/Concerns/{Params.php => SdkParams.php} (98%) rename src/Core/Concerns/{Union.php => SdkUnion.php} (98%) diff --git a/README.md b/README.md index 1fd2f5a..b5b8596 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ $completedSmartscraper = $client->smartscraper->create( var_dump($completedSmartscraper->request_id); ``` -## Value Objects +### Value Objects It is recommended to use the static `with` constructor `Dog::with(name: "Joey")` and named parameters to initialize value objects. diff --git a/src/Core/Concerns/Page.php b/src/Core/Concerns/Page.php deleted file mode 100644 index a355f9d..0000000 --- a/src/Core/Concerns/Page.php +++ /dev/null @@ -1,22 +0,0 @@ -|self $params diff --git a/src/Core/Concerns/Union.php b/src/Core/Concerns/SdkUnion.php similarity index 98% rename from src/Core/Concerns/Union.php rename to src/Core/Concerns/SdkUnion.php index e34110a..cd54e03 100644 --- a/src/Core/Concerns/Union.php +++ b/src/Core/Concerns/SdkUnion.php @@ -11,7 +11,7 @@ /** * @internal */ -trait Union +trait SdkUnion { private static Converter $converter; diff --git a/src/Core/Contracts/BasePage.php b/src/Core/Contracts/BasePage.php index ab70baa..3032525 100644 --- a/src/Core/Contracts/BasePage.php +++ b/src/Core/Contracts/BasePage.php @@ -4,13 +4,19 @@ namespace Scrapegraphai\Core\Contracts; +use Psr\Http\Message\ResponseInterface; +use Scrapegraphai\Core\BaseClient; +use Scrapegraphai\Core\Pagination\PageRequestOptions; + /** * @internal */ -interface BasePage extends \Stringable +interface BasePage { - /** - * @return \Traversable - */ - public function pagingEachItem(): \Traversable; + public function __construct( + BaseClient $client, + PageRequestOptions $options, + ResponseInterface $response, + mixed $body, + ); } diff --git a/src/Core/Pagination/AbstractPage.php b/src/Core/Pagination/AbstractPage.php index 4c4ca93..2206812 100644 --- a/src/Core/Pagination/AbstractPage.php +++ b/src/Core/Pagination/AbstractPage.php @@ -6,7 +6,7 @@ use Psr\Http\Message\ResponseInterface; use Scrapegraphai\Core\BaseClient; -use Scrapegraphai\Core\Concerns\Page; +use Scrapegraphai\Core\Contracts\BasePage; use Scrapegraphai\Errors\Error; /** @@ -16,7 +16,7 @@ * * @implements \IteratorAggregate */ -abstract class AbstractPage implements \IteratorAggregate, Page +abstract class AbstractPage implements \IteratorAggregate, BasePage { public function __construct( protected BaseClient $client, diff --git a/src/Crawl/CrawlStartParams.php b/src/Crawl/CrawlStartParams.php index 124819c..bc35e45 100644 --- a/src/Crawl/CrawlStartParams.php +++ b/src/Crawl/CrawlStartParams.php @@ -5,8 +5,8 @@ namespace Scrapegraphai\Crawl; use Scrapegraphai\Core\Attributes\Api; -use Scrapegraphai\Core\Concerns\Model; -use Scrapegraphai\Core\Concerns\Params; +use Scrapegraphai\Core\Concerns\SdkModel; +use Scrapegraphai\Core\Concerns\SdkParams; use Scrapegraphai\Core\Contracts\BaseModel; use Scrapegraphai\Crawl\CrawlStartParams\Rules; @@ -29,8 +29,8 @@ */ final class CrawlStartParams implements BaseModel { - use Model; - use Params; + use SdkModel; + use SdkParams; /** * Starting URL for crawling. diff --git a/src/Crawl/CrawlStartParams/Rules.php b/src/Crawl/CrawlStartParams/Rules.php index 5a36660..13f1a84 100644 --- a/src/Crawl/CrawlStartParams/Rules.php +++ b/src/Crawl/CrawlStartParams/Rules.php @@ -5,7 +5,7 @@ namespace Scrapegraphai\Crawl\CrawlStartParams; use Scrapegraphai\Core\Attributes\Api; -use Scrapegraphai\Core\Concerns\Model; +use Scrapegraphai\Core\Concerns\SdkModel; use Scrapegraphai\Core\Contracts\BaseModel; use Scrapegraphai\Core\Conversion\ListOf; @@ -14,7 +14,7 @@ */ final class Rules implements BaseModel { - use Model; + use SdkModel; /** * URL patterns to exclude from crawling. diff --git a/src/Feedback/FeedbackSubmitParams.php b/src/Feedback/FeedbackSubmitParams.php index 6a6969a..cfb50a6 100644 --- a/src/Feedback/FeedbackSubmitParams.php +++ b/src/Feedback/FeedbackSubmitParams.php @@ -5,8 +5,8 @@ namespace Scrapegraphai\Feedback; use Scrapegraphai\Core\Attributes\Api; -use Scrapegraphai\Core\Concerns\Model; -use Scrapegraphai\Core\Concerns\Params; +use Scrapegraphai\Core\Concerns\SdkModel; +use Scrapegraphai\Core\Concerns\SdkParams; use Scrapegraphai\Core\Contracts\BaseModel; /** @@ -18,8 +18,8 @@ */ final class FeedbackSubmitParams implements BaseModel { - use Model; - use Params; + use SdkModel; + use SdkParams; /** * Rating score. diff --git a/src/GenerateSchema/GenerateSchemaCreateParams.php b/src/GenerateSchema/GenerateSchemaCreateParams.php index e0dd7ae..2a40332 100644 --- a/src/GenerateSchema/GenerateSchemaCreateParams.php +++ b/src/GenerateSchema/GenerateSchemaCreateParams.php @@ -5,8 +5,8 @@ namespace Scrapegraphai\GenerateSchema; use Scrapegraphai\Core\Attributes\Api; -use Scrapegraphai\Core\Concerns\Model; -use Scrapegraphai\Core\Concerns\Params; +use Scrapegraphai\Core\Concerns\SdkModel; +use Scrapegraphai\Core\Concerns\SdkParams; use Scrapegraphai\Core\Contracts\BaseModel; /** @@ -17,8 +17,8 @@ */ final class GenerateSchemaCreateParams implements BaseModel { - use Model; - use Params; + use SdkModel; + use SdkParams; /** * Natural language description of desired schema. diff --git a/src/Markdownify/CompletedMarkdownify.php b/src/Markdownify/CompletedMarkdownify.php index a21ce7e..ddeda71 100644 --- a/src/Markdownify/CompletedMarkdownify.php +++ b/src/Markdownify/CompletedMarkdownify.php @@ -5,7 +5,7 @@ namespace Scrapegraphai\Markdownify; use Scrapegraphai\Core\Attributes\Api; -use Scrapegraphai\Core\Concerns\Model; +use Scrapegraphai\Core\Concerns\SdkModel; use Scrapegraphai\Core\Contracts\BaseModel; use Scrapegraphai\Markdownify\CompletedMarkdownify\Status; @@ -20,7 +20,7 @@ */ final class CompletedMarkdownify implements BaseModel { - use Model; + use SdkModel; #[Api(optional: true)] public ?string $error; diff --git a/src/Markdownify/CompletedMarkdownify/Status.php b/src/Markdownify/CompletedMarkdownify/Status.php index d6e8ce9..98291b9 100644 --- a/src/Markdownify/CompletedMarkdownify/Status.php +++ b/src/Markdownify/CompletedMarkdownify/Status.php @@ -4,7 +4,7 @@ namespace Scrapegraphai\Markdownify\CompletedMarkdownify; -use Scrapegraphai\Core\Concerns\Enum; +use Scrapegraphai\Core\Concerns\SdkEnum; use Scrapegraphai\Core\Conversion\Contracts\ConverterSource; /** @@ -12,7 +12,7 @@ */ final class Status implements ConverterSource { - use Enum; + use SdkEnum; public const QUEUED = 'queued'; diff --git a/src/Markdownify/MarkdownifyConvertParams.php b/src/Markdownify/MarkdownifyConvertParams.php index ad56545..070d14a 100644 --- a/src/Markdownify/MarkdownifyConvertParams.php +++ b/src/Markdownify/MarkdownifyConvertParams.php @@ -5,8 +5,8 @@ namespace Scrapegraphai\Markdownify; use Scrapegraphai\Core\Attributes\Api; -use Scrapegraphai\Core\Concerns\Model; -use Scrapegraphai\Core\Concerns\Params; +use Scrapegraphai\Core\Concerns\SdkModel; +use Scrapegraphai\Core\Concerns\SdkParams; use Scrapegraphai\Core\Contracts\BaseModel; use Scrapegraphai\Core\Conversion\ListOf; use Scrapegraphai\Core\Conversion\MapOf; @@ -20,8 +20,8 @@ */ final class MarkdownifyConvertParams implements BaseModel { - use Model; - use Params; + use SdkModel; + use SdkParams; /** * URL to convert to markdown. diff --git a/src/Responses/Crawl/CrawlGetResultsResponse.php b/src/Responses/Crawl/CrawlGetResultsResponse.php index c3ef9c6..16fd146 100644 --- a/src/Responses/Crawl/CrawlGetResultsResponse.php +++ b/src/Responses/Crawl/CrawlGetResultsResponse.php @@ -5,7 +5,7 @@ namespace Scrapegraphai\Responses\Crawl; use Scrapegraphai\Core\Attributes\Api; -use Scrapegraphai\Core\Concerns\Model; +use Scrapegraphai\Core\Concerns\SdkModel; use Scrapegraphai\Core\Contracts\BaseModel; use Scrapegraphai\Responses\Crawl\CrawlGetResultsResponse\Result; use Scrapegraphai\Responses\Crawl\CrawlGetResultsResponse\Status; @@ -20,7 +20,7 @@ */ final class CrawlGetResultsResponse implements BaseModel { - use Model; + use SdkModel; /** * Successful crawl results. diff --git a/src/Responses/Crawl/CrawlGetResultsResponse/Result.php b/src/Responses/Crawl/CrawlGetResultsResponse/Result.php index 0982820..e38cd2f 100644 --- a/src/Responses/Crawl/CrawlGetResultsResponse/Result.php +++ b/src/Responses/Crawl/CrawlGetResultsResponse/Result.php @@ -4,7 +4,7 @@ namespace Scrapegraphai\Responses\Crawl\CrawlGetResultsResponse; -use Scrapegraphai\Core\Concerns\Union; +use Scrapegraphai\Core\Concerns\SdkUnion; use Scrapegraphai\Core\Conversion\Contracts\Converter; use Scrapegraphai\Core\Conversion\Contracts\ConverterSource; @@ -15,7 +15,7 @@ */ final class Result implements ConverterSource { - use Union; + use SdkUnion; /** * @return array $services */ #[Api(type: new MapOf('string'), optional: true)] diff --git a/src/Responses/Markdownify/MarkdownifyGetStatusResponse.php b/src/Responses/Markdownify/MarkdownifyGetStatusResponse.php index d5b9236..49c9109 100644 --- a/src/Responses/Markdownify/MarkdownifyGetStatusResponse.php +++ b/src/Responses/Markdownify/MarkdownifyGetStatusResponse.php @@ -4,7 +4,7 @@ namespace Scrapegraphai\Responses\Markdownify; -use Scrapegraphai\Core\Concerns\Union; +use Scrapegraphai\Core\Concerns\SdkUnion; use Scrapegraphai\Core\Conversion\Contracts\Converter; use Scrapegraphai\Core\Conversion\Contracts\ConverterSource; use Scrapegraphai\Markdownify\CompletedMarkdownify; @@ -15,7 +15,7 @@ */ final class MarkdownifyGetStatusResponse implements ConverterSource { - use Union; + use SdkUnion; /** * @return array Date: Thu, 21 Aug 2025 04:05:01 +0000 Subject: [PATCH 09/44] fix(client): elide null named parameters --- README.md | 1 - src/Client.php | 18 ++++---- src/Core/Util.php | 17 +++++++ src/{Crawl => Services}/CrawlService.php | 40 ++++++++++++----- src/{Credits => Services}/CreditsService.php | 2 +- .../FeedbackService.php | 18 +++++--- .../GenerateSchemaService.php | 10 +++-- src/{Healthz => Services}/HealthzService.php | 2 +- .../MarkdownifyService.php | 13 ++++-- .../SearchscraperService.php | 24 ++++++---- .../SmartscraperService.php | 45 +++++++++++++------ .../ValidateService.php | 2 +- tests/Resources/CrawlTest.php | 13 +----- tests/Resources/FeedbackTest.php | 3 +- tests/Resources/GenerateSchemaTest.php | 1 - tests/Resources/MarkdownifyTest.php | 4 +- tests/Resources/SearchscraperTest.php | 5 +-- tests/Resources/SmartscraperTest.php | 11 +---- 18 files changed, 138 insertions(+), 91 deletions(-) rename src/{Crawl => Services}/CrawlService.php (75%) rename src/{Credits => Services}/CreditsService.php (95%) rename src/{Feedback => Services}/FeedbackService.php (75%) rename src/{GenerateSchema => Services}/GenerateSchemaService.php (86%) rename src/{Healthz => Services}/HealthzService.php (95%) rename src/{Markdownify => Services}/MarkdownifyService.php (82%) rename src/{Searchscraper => Services}/SearchscraperService.php (79%) rename src/{Smartscraper => Services}/SmartscraperService.php (75%) rename src/{Validate => Services}/ValidateService.php (95%) diff --git a/README.md b/README.md index b5b8596..8a49fb1 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,6 @@ $client = new Client( $completedSmartscraper = $client->smartscraper->create( userPrompt: "Extract the product name, price, and description" ); - var_dump($completedSmartscraper->request_id); ``` diff --git a/src/Client.php b/src/Client.php index daf0fc9..410fd16 100644 --- a/src/Client.php +++ b/src/Client.php @@ -5,15 +5,15 @@ namespace Scrapegraphai; use Scrapegraphai\Core\BaseClient; -use Scrapegraphai\Crawl\CrawlService; -use Scrapegraphai\Credits\CreditsService; -use Scrapegraphai\Feedback\FeedbackService; -use Scrapegraphai\GenerateSchema\GenerateSchemaService; -use Scrapegraphai\Healthz\HealthzService; -use Scrapegraphai\Markdownify\MarkdownifyService; -use Scrapegraphai\Searchscraper\SearchscraperService; -use Scrapegraphai\Smartscraper\SmartscraperService; -use Scrapegraphai\Validate\ValidateService; +use Scrapegraphai\Services\CrawlService; +use Scrapegraphai\Services\CreditsService; +use Scrapegraphai\Services\FeedbackService; +use Scrapegraphai\Services\GenerateSchemaService; +use Scrapegraphai\Services\HealthzService; +use Scrapegraphai\Services\MarkdownifyService; +use Scrapegraphai\Services\SearchscraperService; +use Scrapegraphai\Services\SmartscraperService; +use Scrapegraphai\Services\ValidateService; class Client extends BaseClient { diff --git a/src/Core/Util.php b/src/Core/Util.php index 040fd25..309dbca 100644 --- a/src/Core/Util.php +++ b/src/Core/Util.php @@ -349,6 +349,23 @@ public static function decodeContent(MessageInterface $rsp): mixed return self::streamIterator($body); } + /** + * @param array $arr + * @param list $keys + * + * @return array + */ + public static function array_filter_null(array $arr, array $keys): array + { + foreach ($keys as $key) { + if (array_key_exists($key, $arr) && is_null($arr[$key])) { + unset($arr[$key]); + } + } + + return $arr; + } + /** * @param list $closing * diff --git a/src/Crawl/CrawlService.php b/src/Services/CrawlService.php similarity index 75% rename from src/Crawl/CrawlService.php rename to src/Services/CrawlService.php index c9ddf10..a101418 100644 --- a/src/Crawl/CrawlService.php +++ b/src/Services/CrawlService.php @@ -2,11 +2,13 @@ declare(strict_types=1); -namespace Scrapegraphai\Crawl; +namespace Scrapegraphai\Services; use Scrapegraphai\Client; use Scrapegraphai\Contracts\CrawlContract; use Scrapegraphai\Core\Conversion; +use Scrapegraphai\Core\Util; +use Scrapegraphai\Crawl\CrawlStartParams; use Scrapegraphai\Crawl\CrawlStartParams\Rules; use Scrapegraphai\RequestOptions; use Scrapegraphai\Responses\Crawl\CrawlGetResultsResponse; @@ -60,19 +62,33 @@ public function start( $sitemap = null, ?RequestOptions $requestOptions = null, ): CrawlStartResponse { - [$parsed, $options] = CrawlStartParams::parseRequest( + $args = [ + 'url' => $url, + 'depth' => $depth, + 'extractionMode' => $extractionMode, + 'maxPages' => $maxPages, + 'prompt' => $prompt, + 'renderHeavyJs' => $renderHeavyJs, + 'rules' => $rules, + 'schema' => $schema, + 'sitemap' => $sitemap, + ]; + $args = Util::array_filter_null( + $args, [ - 'url' => $url, - 'depth' => $depth, - 'extractionMode' => $extractionMode, - 'maxPages' => $maxPages, - 'prompt' => $prompt, - 'renderHeavyJs' => $renderHeavyJs, - 'rules' => $rules, - 'schema' => $schema, - 'sitemap' => $sitemap, + 'depth', + 'extractionMode', + 'maxPages', + 'prompt', + 'renderHeavyJs', + 'rules', + 'schema', + 'sitemap', ], - $requestOptions, + ); + [$parsed, $options] = CrawlStartParams::parseRequest( + $args, + $requestOptions ); $resp = $this->client->request( method: 'post', diff --git a/src/Credits/CreditsService.php b/src/Services/CreditsService.php similarity index 95% rename from src/Credits/CreditsService.php rename to src/Services/CreditsService.php index 0384910..b52d91f 100644 --- a/src/Credits/CreditsService.php +++ b/src/Services/CreditsService.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Scrapegraphai\Credits; +namespace Scrapegraphai\Services; use Scrapegraphai\Client; use Scrapegraphai\Contracts\CreditsContract; diff --git a/src/Feedback/FeedbackService.php b/src/Services/FeedbackService.php similarity index 75% rename from src/Feedback/FeedbackService.php rename to src/Services/FeedbackService.php index cdf73a2..672e4c7 100644 --- a/src/Feedback/FeedbackService.php +++ b/src/Services/FeedbackService.php @@ -2,11 +2,13 @@ declare(strict_types=1); -namespace Scrapegraphai\Feedback; +namespace Scrapegraphai\Services; use Scrapegraphai\Client; use Scrapegraphai\Contracts\FeedbackContract; use Scrapegraphai\Core\Conversion; +use Scrapegraphai\Core\Util; +use Scrapegraphai\Feedback\FeedbackSubmitParams; use Scrapegraphai\RequestOptions; use Scrapegraphai\Responses\Feedback\FeedbackSubmitResponse; @@ -27,13 +29,15 @@ public function submit( $feedbackText = null, ?RequestOptions $requestOptions = null, ): FeedbackSubmitResponse { + $args = [ + 'rating' => $rating, + 'requestID' => $requestID, + 'feedbackText' => $feedbackText, + ]; + $args = Util::array_filter_null($args, ['feedbackText']); [$parsed, $options] = FeedbackSubmitParams::parseRequest( - [ - 'rating' => $rating, - 'requestID' => $requestID, - 'feedbackText' => $feedbackText, - ], - $requestOptions, + $args, + $requestOptions ); $resp = $this->client->request( method: 'post', diff --git a/src/GenerateSchema/GenerateSchemaService.php b/src/Services/GenerateSchemaService.php similarity index 86% rename from src/GenerateSchema/GenerateSchemaService.php rename to src/Services/GenerateSchemaService.php index d46dc2a..b0a8b44 100644 --- a/src/GenerateSchema/GenerateSchemaService.php +++ b/src/Services/GenerateSchemaService.php @@ -2,11 +2,13 @@ declare(strict_types=1); -namespace Scrapegraphai\GenerateSchema; +namespace Scrapegraphai\Services; use Scrapegraphai\Client; use Scrapegraphai\Contracts\GenerateSchemaContract; use Scrapegraphai\Core\Conversion; +use Scrapegraphai\Core\Util; +use Scrapegraphai\GenerateSchema\GenerateSchemaCreateParams; use Scrapegraphai\RequestOptions; use Scrapegraphai\Responses\GenerateSchema\GenerateSchemaGetResponse; use Scrapegraphai\Responses\GenerateSchema\GenerateSchemaGetResponse\CompletedSchemaGenerationResponse; @@ -29,9 +31,11 @@ public function create( $existingSchema = null, ?RequestOptions $requestOptions = null ): GenerateSchemaNewResponse { + $args = ['userPrompt' => $userPrompt, 'existingSchema' => $existingSchema]; + $args = Util::array_filter_null($args, ['existingSchema']); [$parsed, $options] = GenerateSchemaCreateParams::parseRequest( - ['userPrompt' => $userPrompt, 'existingSchema' => $existingSchema], - $requestOptions, + $args, + $requestOptions ); $resp = $this->client->request( method: 'post', diff --git a/src/Healthz/HealthzService.php b/src/Services/HealthzService.php similarity index 95% rename from src/Healthz/HealthzService.php rename to src/Services/HealthzService.php index 9b919de..4d7f3ab 100644 --- a/src/Healthz/HealthzService.php +++ b/src/Services/HealthzService.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Scrapegraphai\Healthz; +namespace Scrapegraphai\Services; use Scrapegraphai\Client; use Scrapegraphai\Contracts\HealthzContract; diff --git a/src/Markdownify/MarkdownifyService.php b/src/Services/MarkdownifyService.php similarity index 82% rename from src/Markdownify/MarkdownifyService.php rename to src/Services/MarkdownifyService.php index a0387e6..70d9111 100644 --- a/src/Markdownify/MarkdownifyService.php +++ b/src/Services/MarkdownifyService.php @@ -2,11 +2,14 @@ declare(strict_types=1); -namespace Scrapegraphai\Markdownify; +namespace Scrapegraphai\Services; use Scrapegraphai\Client; use Scrapegraphai\Contracts\MarkdownifyContract; use Scrapegraphai\Core\Conversion; +use Scrapegraphai\Core\Util; +use Scrapegraphai\Markdownify\CompletedMarkdownify; +use Scrapegraphai\Markdownify\MarkdownifyConvertParams; use Scrapegraphai\RequestOptions; use Scrapegraphai\Responses\Markdownify\MarkdownifyGetStatusResponse; use Scrapegraphai\Responses\Markdownify\MarkdownifyGetStatusResponse\FailedMarkdownifyResponse; @@ -28,9 +31,13 @@ public function convert( $steps = null, ?RequestOptions $requestOptions = null, ): CompletedMarkdownify { + $args = [ + 'websiteURL' => $websiteURL, 'headers' => $headers, 'steps' => $steps, + ]; + $args = Util::array_filter_null($args, ['headers', 'steps']); [$parsed, $options] = MarkdownifyConvertParams::parseRequest( - ['websiteURL' => $websiteURL, 'headers' => $headers, 'steps' => $steps], - $requestOptions, + $args, + $requestOptions ); $resp = $this->client->request( method: 'post', diff --git a/src/Searchscraper/SearchscraperService.php b/src/Services/SearchscraperService.php similarity index 79% rename from src/Searchscraper/SearchscraperService.php rename to src/Services/SearchscraperService.php index 43b9e52..e37ff3a 100644 --- a/src/Searchscraper/SearchscraperService.php +++ b/src/Services/SearchscraperService.php @@ -2,14 +2,17 @@ declare(strict_types=1); -namespace Scrapegraphai\Searchscraper; +namespace Scrapegraphai\Services; use Scrapegraphai\Client; use Scrapegraphai\Contracts\SearchscraperContract; use Scrapegraphai\Core\Conversion; +use Scrapegraphai\Core\Util; use Scrapegraphai\RequestOptions; use Scrapegraphai\Responses\Searchscraper\SearchscraperGetStatusResponse; use Scrapegraphai\Responses\Searchscraper\SearchscraperGetStatusResponse\FailedSearchScraperResponse; +use Scrapegraphai\Searchscraper\CompletedSearchScraper; +use Scrapegraphai\Searchscraper\SearchscraperCreateParams; final class SearchscraperService implements SearchscraperContract { @@ -31,14 +34,19 @@ public function create( $outputSchema = null, ?RequestOptions $requestOptions = null, ): CompletedSearchScraper { + $args = [ + 'userPrompt' => $userPrompt, + 'headers' => $headers, + 'numResults' => $numResults, + 'outputSchema' => $outputSchema, + ]; + $args = Util::array_filter_null( + $args, + ['headers', 'numResults', 'outputSchema'] + ); [$parsed, $options] = SearchscraperCreateParams::parseRequest( - [ - 'userPrompt' => $userPrompt, - 'headers' => $headers, - 'numResults' => $numResults, - 'outputSchema' => $outputSchema, - ], - $requestOptions, + $args, + $requestOptions ); $resp = $this->client->request( method: 'post', diff --git a/src/Smartscraper/SmartscraperService.php b/src/Services/SmartscraperService.php similarity index 75% rename from src/Smartscraper/SmartscraperService.php rename to src/Services/SmartscraperService.php index 135fcfb..846113a 100644 --- a/src/Smartscraper/SmartscraperService.php +++ b/src/Services/SmartscraperService.php @@ -2,14 +2,18 @@ declare(strict_types=1); -namespace Scrapegraphai\Smartscraper; +namespace Scrapegraphai\Services; use Scrapegraphai\Client; use Scrapegraphai\Contracts\SmartscraperContract; use Scrapegraphai\Core\Conversion; +use Scrapegraphai\Core\Util; use Scrapegraphai\RequestOptions; use Scrapegraphai\Responses\Smartscraper\SmartscraperGetResponse; use Scrapegraphai\Responses\Smartscraper\SmartscraperListResponse; +use Scrapegraphai\Smartscraper\CompletedSmartscraper; +use Scrapegraphai\Smartscraper\FailedSmartscraper; +use Scrapegraphai\Smartscraper\SmartscraperCreateParams; final class SmartscraperService implements SmartscraperContract { @@ -43,20 +47,35 @@ public function create( $websiteURL = null, ?RequestOptions $requestOptions = null, ): CompletedSmartscraper { - [$parsed, $options] = SmartscraperCreateParams::parseRequest( + $args = [ + 'userPrompt' => $userPrompt, + 'cookies' => $cookies, + 'headers' => $headers, + 'numberOfScrolls' => $numberOfScrolls, + 'outputSchema' => $outputSchema, + 'renderHeavyJs' => $renderHeavyJs, + 'steps' => $steps, + 'totalPages' => $totalPages, + 'websiteHTML' => $websiteHTML, + 'websiteURL' => $websiteURL, + ]; + $args = Util::array_filter_null( + $args, [ - 'userPrompt' => $userPrompt, - 'cookies' => $cookies, - 'headers' => $headers, - 'numberOfScrolls' => $numberOfScrolls, - 'outputSchema' => $outputSchema, - 'renderHeavyJs' => $renderHeavyJs, - 'steps' => $steps, - 'totalPages' => $totalPages, - 'websiteHTML' => $websiteHTML, - 'websiteURL' => $websiteURL, + 'cookies', + 'headers', + 'numberOfScrolls', + 'outputSchema', + 'renderHeavyJs', + 'steps', + 'totalPages', + 'websiteHTML', + 'websiteURL', ], - $requestOptions, + ); + [$parsed, $options] = SmartscraperCreateParams::parseRequest( + $args, + $requestOptions ); $resp = $this->client->request( method: 'post', diff --git a/src/Validate/ValidateService.php b/src/Services/ValidateService.php similarity index 95% rename from src/Validate/ValidateService.php rename to src/Services/ValidateService.php index e40031a..31ec544 100644 --- a/src/Validate/ValidateService.php +++ b/src/Services/ValidateService.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Scrapegraphai\Validate; +namespace Scrapegraphai\Services; use Scrapegraphai\Client; use Scrapegraphai\Contracts\ValidateContract; diff --git a/tests/Resources/CrawlTest.php b/tests/Resources/CrawlTest.php index ac83ae1..c46a37f 100644 --- a/tests/Resources/CrawlTest.php +++ b/tests/Resources/CrawlTest.php @@ -6,7 +6,6 @@ use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use Scrapegraphai\Client; -use Scrapegraphai\Crawl\CrawlStartParams\Rules; use Tests\UnsupportedMockTests; /** @@ -58,17 +57,7 @@ public function testStartWithOptionalParams(): void $this->markTestSkipped('Prism tests are disabled'); } - $result = $this->client->crawl->start( - url: 'https://example.com', - depth: 0, - extractionMode: true, - maxPages: 1, - prompt: 'prompt', - renderHeavyJs: true, - rules: (new Rules)->withExclude(['string'])->withSameDomain(true), - schema: (object) [], - sitemap: true, - ); + $result = $this->client->crawl->start(url: 'https://example.com'); $this->assertTrue(true); // @phpstan-ignore-line } diff --git a/tests/Resources/FeedbackTest.php b/tests/Resources/FeedbackTest.php index 2fa387c..1659f58 100644 --- a/tests/Resources/FeedbackTest.php +++ b/tests/Resources/FeedbackTest.php @@ -50,8 +50,7 @@ public function testSubmitWithOptionalParams(): void $result = $this->client->feedback->submit( rating: 0, - requestID: '182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e', - feedbackText: 'feedback_text', + requestID: '182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e' ); $this->assertTrue(true); // @phpstan-ignore-line diff --git a/tests/Resources/GenerateSchemaTest.php b/tests/Resources/GenerateSchemaTest.php index 3c0a545..69cd3ba 100644 --- a/tests/Resources/GenerateSchemaTest.php +++ b/tests/Resources/GenerateSchemaTest.php @@ -49,7 +49,6 @@ public function testCreateWithOptionalParams(): void $result = $this->client->generateSchema->create( userPrompt: 'Create a schema for product information including name, price, and reviews', - existingSchema: (object) [], ); $this->assertTrue(true); // @phpstan-ignore-line diff --git a/tests/Resources/MarkdownifyTest.php b/tests/Resources/MarkdownifyTest.php index cdeacc8..d60d5fd 100644 --- a/tests/Resources/MarkdownifyTest.php +++ b/tests/Resources/MarkdownifyTest.php @@ -48,9 +48,7 @@ public function testConvertWithOptionalParams(): void } $result = $this->client->markdownify->convert( - websiteURL: 'https://example.com', - headers: ['foo' => 'string'], - steps: ['string'], + websiteURL: 'https://example.com' ); $this->assertTrue(true); // @phpstan-ignore-line diff --git a/tests/Resources/SearchscraperTest.php b/tests/Resources/SearchscraperTest.php index a2bb574..70491fc 100644 --- a/tests/Resources/SearchscraperTest.php +++ b/tests/Resources/SearchscraperTest.php @@ -48,10 +48,7 @@ public function testCreateWithOptionalParams(): void } $result = $this->client->searchscraper->create( - userPrompt: 'Find the latest AI news and extract headlines and summaries', - headers: ['foo' => 'string'], - numResults: 3, - outputSchema: (object) [], + userPrompt: 'Find the latest AI news and extract headlines and summaries' ); $this->assertTrue(true); // @phpstan-ignore-line diff --git a/tests/Resources/SmartscraperTest.php b/tests/Resources/SmartscraperTest.php index 350aaf9..803a2f3 100644 --- a/tests/Resources/SmartscraperTest.php +++ b/tests/Resources/SmartscraperTest.php @@ -48,16 +48,7 @@ public function testCreateWithOptionalParams(): void } $result = $this->client->smartscraper->create( - userPrompt: 'Extract the product name, price, and description', - cookies: ['foo' => 'string'], - headers: ['foo' => 'string'], - numberOfScrolls: 0, - outputSchema: (object) [], - renderHeavyJs: true, - steps: ['string'], - totalPages: 1, - websiteHTML: 'website_html', - websiteURL: 'https://example.com/product', + userPrompt: 'Extract the product name, price, and description' ); $this->assertTrue(true); // @phpstan-ignore-line From 0cd048d4ce590110cdf01edc1649d4ef2e707a6d Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Thu, 21 Aug 2025 04:09:11 +0000 Subject: [PATCH 10/44] chore: intuitively order union types --- .php-cs-fixer.dist.php | 8 +++- src/Contracts/CrawlContract.php | 2 +- src/Contracts/FeedbackContract.php | 2 +- src/Core/Attributes/Api.php | 16 ++++---- src/Core/BaseClient.php | 38 +++++++++---------- src/Core/Concerns/SdkParams.php | 6 +-- src/Core/Conversion/Concerns/ArrayOf.php | 8 ++-- src/Core/Conversion/EnumOf.php | 2 +- src/Core/Conversion/PropertyInfo.php | 4 +- src/Core/Conversion/UnionOf.php | 2 +- src/Core/Util.php | 20 +++++----- src/Crawl/CrawlStartParams/Rules.php | 4 +- src/Markdownify/CompletedMarkdownify.php | 4 +- src/Markdownify/MarkdownifyConvertParams.php | 8 ++-- src/RequestOptions.php | 34 ++++++++--------- .../Crawl/CrawlGetResultsResponse.php | 8 ++-- .../Crawl/CrawlGetResultsResponse/Result.php | 4 +- .../GenerateSchemaGetResponse.php | 4 +- .../CompletedSchemaGenerationResponse.php | 4 +- .../FailedSchemaGenerationResponse.php | 4 +- .../GenerateSchemaNewResponse.php | 4 +- .../Healthz/HealthzCheckResponse.php | 4 +- .../MarkdownifyGetStatusResponse.php | 4 +- .../FailedMarkdownifyResponse.php | 4 +- .../SearchscraperGetStatusResponse.php | 4 +- .../FailedSearchScraperResponse.php | 8 ++-- .../Smartscraper/SmartscraperGetResponse.php | 4 +- .../Smartscraper/SmartscraperListResponse.php | 4 +- src/Searchscraper/CompletedSearchScraper.php | 8 ++-- .../SearchscraperCreateParams.php | 4 +- src/Services/CrawlService.php | 2 +- src/Services/FeedbackService.php | 2 +- src/Smartscraper/CompletedSmartscraper.php | 4 +- src/Smartscraper/FailedSmartscraper.php | 4 +- src/Smartscraper/SmartscraperCreateParams.php | 12 +++--- tests/Core/TestModel.php | 4 +- 36 files changed, 132 insertions(+), 126 deletions(-) diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index 1e5b181..a2b062b 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -7,5 +7,11 @@ return (new Config()) ->setParallelConfig(ParallelConfigFactory::detect()) ->setFinder(Finder::create()->in([__DIR__.'/src', __DIR__.'/tests'])) - ->setRules(['@PhpCsFixer' => true, 'phpdoc_align' => false, 'new_with_parentheses' => ['named_class' => false]]) + ->setRules([ + '@PhpCsFixer' => true, + 'phpdoc_align' => false, + 'new_with_parentheses' => ['named_class' => false], + 'ordered_types' => ['null_adjustment' => 'always_last', 'sort_algorithm' => 'none'], + 'phpdoc_types_order' => ['null_adjustment' => 'always_last', 'sort_algorithm' => 'none'], + ]) ; diff --git a/src/Contracts/CrawlContract.php b/src/Contracts/CrawlContract.php index d450583..dc2c648 100644 --- a/src/Contracts/CrawlContract.php +++ b/src/Contracts/CrawlContract.php @@ -21,7 +21,7 @@ public function retrieveResults( * @param int $depth Maximum crawl depth from starting URL * @param bool $extractionMode Use AI extraction (true) or markdown conversion (false) * @param int $maxPages Maximum number of pages to crawl - * @param null|string $prompt Extraction prompt (required if extraction_mode is true) + * @param string|null $prompt Extraction prompt (required if extraction_mode is true) * @param bool $renderHeavyJs Enable heavy JavaScript rendering * @param Rules $rules * @param mixed $schema Output schema for extraction diff --git a/src/Contracts/FeedbackContract.php b/src/Contracts/FeedbackContract.php index f984247..4008f17 100644 --- a/src/Contracts/FeedbackContract.php +++ b/src/Contracts/FeedbackContract.php @@ -12,7 +12,7 @@ interface FeedbackContract /** * @param int $rating Rating score * @param string $requestID Request to provide feedback for - * @param null|string $feedbackText Optional feedback comments + * @param string|null $feedbackText Optional feedback comments */ public function submit( $rating, diff --git a/src/Core/Attributes/Api.php b/src/Core/Attributes/Api.php index a8b0d69..9583df3 100644 --- a/src/Core/Attributes/Api.php +++ b/src/Core/Attributes/Api.php @@ -14,20 +14,20 @@ final class Api { /** - * @var null|class-string|Converter|string + * @var class-string|Converter|string|null */ - public readonly null|Converter|string $type; + public readonly Converter|string|null $type; /** - * @param null|class-string|Converter|string $type - * @param null|class-string|Converter $enum - * @param null|class-string|Converter|string $union + * @param class-string|Converter|string|null $type + * @param class-string|Converter|null $enum + * @param class-string|Converter|string|null $union */ public function __construct( public readonly ?string $apiName = null, - null|Converter|string $type = null, - null|Converter|string $enum = null, - null|Converter|string $union = null, + Converter|string|null $type = null, + Converter|string|null $enum = null, + Converter|string|null $union = null, public readonly bool $nullable = false, public readonly bool $optional = false, ) { diff --git a/src/Core/BaseClient.php b/src/Core/BaseClient.php index 3657161..38db270 100644 --- a/src/Core/BaseClient.php +++ b/src/Core/BaseClient.php @@ -29,7 +29,7 @@ class BaseClient protected ClientInterface $transporter; /** - * @param array|string> $headers + * @param array|null> $headers */ public function __construct( protected array $headers, @@ -45,13 +45,13 @@ public function __construct( } /** - * @param list|string $path + * @param string|list $path * @param array $query * @param array $headers */ public function request( string $method, - array|string $path, + string|array $path, array $query = [], array $headers = [], mixed $body = null, @@ -88,27 +88,27 @@ protected function authHeaders(): array } /** - * @param list|string $path + * @param string|list $path * @param array $query - * @param array|string> $headers - * @param null|array{ - * timeout?: null|float, - * maxRetries?: null|int, - * initialRetryDelay?: null|float, - * maxRetryDelay?: null|float, - * extraHeaders?: null|list, - * extraQueryParams?: null|list, - * extraBodyParams?: null|list, - * }|RequestOptions $opts + * @param array|null> $headers + * @param array{ + * timeout?: float|null, + * maxRetries?: int|null, + * initialRetryDelay?: float|null, + * maxRetryDelay?: float|null, + * extraHeaders?: list|null, + * extraQueryParams?: list|null, + * extraBodyParams?: list|null, + * }|RequestOptions|null $opts * * @return array{RequestInterface, RequestOptions} */ protected function buildRequest( string $method, - array|string $path, + string|array $path, array $query, array $headers, - null|array|RequestOptions $opts, + array|RequestOptions|null $opts, ): array { $opts = [...$this->options->__serialize(), ...RequestOptions::parse($opts)->__serialize()]; $options = new RequestOptions(...$opts); @@ -119,7 +119,7 @@ protected function buildRequest( $mergedQuery = array_merge_recursive($query, $options->extraQueryParams); $uri = Util::joinUri($this->baseUrl, path: $parsedPath, query: $mergedQuery); - /** @var array|string> $mergedHeaders */ + /** @var array> $mergedHeaders */ $mergedHeaders = [...$this->headers, ...$this->authHeaders(), ...$headers, @@ -146,9 +146,9 @@ protected function followRedirect( } /** - * @param null|array|bool|float|int|resource|string|\Traversable< + * @param bool|int|float|string|array|resource|\Traversable< * mixed - * > $data + * >|null $data */ protected function sendRequest( RequestInterface $req, diff --git a/src/Core/Concerns/SdkParams.php b/src/Core/Concerns/SdkParams.php index e7192eb..256f357 100644 --- a/src/Core/Concerns/SdkParams.php +++ b/src/Core/Concerns/SdkParams.php @@ -14,8 +14,8 @@ trait SdkParams { /** - * @param null|array|self $params - * @param null|array|RequestOptions $options + * @param array|self|null $params + * @param array|RequestOptions|null $options * * @return array{array, array{ * timeout: float, @@ -27,7 +27,7 @@ trait SdkParams * extraBodyParams: list, * }} */ - public static function parseRequest(null|array|self $params, null|array|RequestOptions $options): array + public static function parseRequest(array|self|null $params, array|RequestOptions|null $options): array { $converter = self::converter(); $state = new DumpState; diff --git a/src/Core/Conversion/Concerns/ArrayOf.php b/src/Core/Conversion/Concerns/ArrayOf.php index ef71a03..46ac460 100644 --- a/src/Core/Conversion/Concerns/ArrayOf.php +++ b/src/Core/Conversion/Concerns/ArrayOf.php @@ -15,12 +15,12 @@ */ trait ArrayOf { - private readonly null|Converter|ConverterSource|string $type; + private readonly Converter|ConverterSource|string|null $type; public function __construct( - null|Converter|ConverterSource|string $type = null, - null|Converter|ConverterSource|string $enum = null, - null|Converter|ConverterSource|string $union = null, + Converter|ConverterSource|string|null $type = null, + Converter|ConverterSource|string|null $enum = null, + Converter|ConverterSource|string|null $union = null, private readonly bool $nullable = false, ) { $this->type = $type ?? $enum ?? $union; diff --git a/src/Core/Conversion/EnumOf.php b/src/Core/Conversion/EnumOf.php index 501159e..19a71bc 100644 --- a/src/Core/Conversion/EnumOf.php +++ b/src/Core/Conversion/EnumOf.php @@ -15,7 +15,7 @@ final class EnumOf implements Converter private readonly string $type; /** - * @param list $members + * @param list $members */ public function __construct(private readonly array $members) { diff --git a/src/Core/Conversion/PropertyInfo.php b/src/Core/Conversion/PropertyInfo.php index 7098314..f19391a 100644 --- a/src/Core/Conversion/PropertyInfo.php +++ b/src/Core/Conversion/PropertyInfo.php @@ -46,9 +46,9 @@ public function __construct(public readonly \ReflectionProperty $property) } /** - * @param null|array|Converter|ConverterSource|\ReflectionType|string $type + * @param array|Converter|ConverterSource|\ReflectionType|string|null $type */ - private static function parse(null|array|Converter|ConverterSource|\ReflectionType|string $type): Converter|ConverterSource|string + private static function parse(array|Converter|ConverterSource|\ReflectionType|string|null $type): Converter|ConverterSource|string { if (is_string($type) || $type instanceof Converter) { return $type; diff --git a/src/Core/Conversion/UnionOf.php b/src/Core/Conversion/UnionOf.php index c302fc0..1ef8a1b 100644 --- a/src/Core/Conversion/UnionOf.php +++ b/src/Core/Conversion/UnionOf.php @@ -77,7 +77,7 @@ public function dump(mixed $value, DumpState $state): mixed private function resolveVariant( mixed $value, - ): null|Converter|ConverterSource|string { + ): Converter|ConverterSource|string|null { if ($value instanceof BaseModel) { return $value::class; } diff --git a/src/Core/Util.php b/src/Core/Util.php index 309dbca..0bfa110 100644 --- a/src/Core/Util.php +++ b/src/Core/Util.php @@ -45,11 +45,11 @@ public static function array_transform_keys(array $array, array $map): array } /** - * @param callable|int|list|string $key + * @param string|int|list|callable $key */ public static function dig( mixed $array, - array|callable|int|string $key + string|int|array|callable $key ): mixed { if (is_callable($key)) { return $key($array); @@ -71,9 +71,9 @@ public static function dig( } /** - * @param list|string $path + * @param string|list $path */ - public static function parsePath(array|string $path): string + public static function parsePath(string|array $path): string { if (is_string($path)) { return $path; @@ -124,7 +124,7 @@ public static function joinUri( } /** - * @param array|string> $headers + * @param array|null> $headers */ public static function withSetHeaders( RequestInterface $req, @@ -165,9 +165,9 @@ public static function streamIterator(StreamInterface $stream): \Iterator } /** - * @param null|array|bool|float|int|resource|string|\Traversable< + * @param bool|int|float|string|array|resource|\Traversable< * mixed - * > $body + * >|null $body * * @return array{string, \Generator} */ @@ -202,9 +202,9 @@ public static function encodeMultipartStreaming(mixed $body): array } /** - * @param null|array|bool|float|int|resource|string|\Traversable< + * @param bool|int|float|string|array|resource|\Traversable< * mixed - * > $body + * >|null $body */ public static function withSetBody( StreamFactoryInterface $factory, @@ -267,7 +267,7 @@ public static function decodeLines(\Iterator $stream): \Iterator * * @return \Generator< * array{ - * event?: null|string, data?: null|string, id?: null|string, retry?: null|int + * event?: string|null, data?: string|null, id?: string|null, retry?: int|null * }, * > */ diff --git a/src/Crawl/CrawlStartParams/Rules.php b/src/Crawl/CrawlStartParams/Rules.php index 13f1a84..2f8cf04 100644 --- a/src/Crawl/CrawlStartParams/Rules.php +++ b/src/Crawl/CrawlStartParams/Rules.php @@ -19,7 +19,7 @@ final class Rules implements BaseModel /** * URL patterns to exclude from crawling. * - * @var null|list $exclude + * @var list|null $exclude */ #[Api(type: new ListOf('string'), optional: true)] public ?array $exclude; @@ -41,7 +41,7 @@ public function __construct() * * You must use named parameters to construct any parameters with a default value. * - * @param null|list $exclude + * @param list|null $exclude */ public static function with( ?array $exclude = null, diff --git a/src/Markdownify/CompletedMarkdownify.php b/src/Markdownify/CompletedMarkdownify.php index ddeda71..45e31fe 100644 --- a/src/Markdownify/CompletedMarkdownify.php +++ b/src/Markdownify/CompletedMarkdownify.php @@ -34,7 +34,7 @@ final class CompletedMarkdownify implements BaseModel #[Api(optional: true)] public ?string $result; - /** @var null|Status::* $status */ + /** @var Status::*|null $status */ #[Api(enum: Status::class, optional: true)] public ?string $status; @@ -52,7 +52,7 @@ public function __construct() * * You must use named parameters to construct any parameters with a default value. * - * @param null|Status::* $status + * @param Status::*|null $status */ public static function with( ?string $error = null, diff --git a/src/Markdownify/MarkdownifyConvertParams.php b/src/Markdownify/MarkdownifyConvertParams.php index 070d14a..df4adc2 100644 --- a/src/Markdownify/MarkdownifyConvertParams.php +++ b/src/Markdownify/MarkdownifyConvertParams.php @@ -29,14 +29,14 @@ final class MarkdownifyConvertParams implements BaseModel #[Api('website_url')] public string $websiteURL; - /** @var null|array $headers */ + /** @var array|null $headers */ #[Api(type: new MapOf('string'), optional: true)] public ?array $headers; /** * Interaction steps before conversion. * - * @var null|list $steps + * @var list|null $steps */ #[Api(type: new ListOf('string'), optional: true)] public ?array $steps; @@ -66,8 +66,8 @@ public function __construct() * * You must use named parameters to construct any parameters with a default value. * - * @param null|array $headers - * @param null|list $steps + * @param array|null $headers + * @param list|null $steps */ public static function with( string $websiteURL, diff --git a/src/RequestOptions.php b/src/RequestOptions.php index 801bed6..2a541ce 100644 --- a/src/RequestOptions.php +++ b/src/RequestOptions.php @@ -55,13 +55,13 @@ public function __serialize(): array /** * @param array{ - * timeout?: null|float, - * maxRetries?: null|int, - * initialRetryDelay?: null|float, - * maxRetryDelay?: null|float, - * extraHeaders?: null|list, - * extraQueryParams?: null|list, - * extraBodyParams?: null|list, + * timeout?: float|null, + * maxRetries?: int|null, + * initialRetryDelay?: float|null, + * maxRetryDelay?: float|null, + * extraHeaders?: list|null, + * extraQueryParams?: list|null, + * extraBodyParams?: list|null, * } $data */ public function __unserialize(array $data): void @@ -88,17 +88,17 @@ public function __unserialize(array $data): void } /** - * @param null|array{ - * timeout?: null|float, - * maxRetries?: null|int, - * initialRetryDelay?: null|float, - * maxRetryDelay?: null|float, - * extraHeaders?: null|list, - * extraQueryParams?: null|list, - * extraBodyParams?: null|list, - * }|RequestOptions $options + * @param array{ + * timeout?: float|null, + * maxRetries?: int|null, + * initialRetryDelay?: float|null, + * maxRetryDelay?: float|null, + * extraHeaders?: list|null, + * extraQueryParams?: list|null, + * extraBodyParams?: list|null, + * }|RequestOptions|null $options */ - public static function parse(null|array|RequestOptions $options): self + public static function parse(array|RequestOptions|null $options): self { if (is_null($options)) { return new self; diff --git a/src/Responses/Crawl/CrawlGetResultsResponse.php b/src/Responses/Crawl/CrawlGetResultsResponse.php index 16fd146..ddc58f2 100644 --- a/src/Responses/Crawl/CrawlGetResultsResponse.php +++ b/src/Responses/Crawl/CrawlGetResultsResponse.php @@ -25,12 +25,12 @@ final class CrawlGetResultsResponse implements BaseModel /** * Successful crawl results. * - * @var null|mixed|string $result + * @var mixed|string|null $result */ #[Api(union: Result::class, optional: true)] public mixed $result; - /** @var null|Status::* $status */ + /** @var Status::*|null $status */ #[Api(enum: Status::class, optional: true)] public ?string $status; @@ -54,8 +54,8 @@ public function __construct() * * You must use named parameters to construct any parameters with a default value. * - * @param null|mixed|string $result - * @param null|Status::* $status + * @param mixed|string|null $result + * @param Status::*|null $status */ public static function with( mixed $result = null, diff --git a/src/Responses/Crawl/CrawlGetResultsResponse/Result.php b/src/Responses/Crawl/CrawlGetResultsResponse/Result.php index e38cd2f..5386262 100644 --- a/src/Responses/Crawl/CrawlGetResultsResponse/Result.php +++ b/src/Responses/Crawl/CrawlGetResultsResponse/Result.php @@ -18,8 +18,8 @@ final class Result implements ConverterSource use SdkUnion; /** - * @return array|list + * @return list|array */ public static function variants(): array { diff --git a/src/Responses/GenerateSchema/GenerateSchemaGetResponse.php b/src/Responses/GenerateSchema/GenerateSchemaGetResponse.php index eb9dca8..313dfed 100644 --- a/src/Responses/GenerateSchema/GenerateSchemaGetResponse.php +++ b/src/Responses/GenerateSchema/GenerateSchemaGetResponse.php @@ -18,8 +18,8 @@ final class GenerateSchemaGetResponse implements ConverterSource use SdkUnion; /** - * @return array|list + * @return list|array */ public static function variants(): array { diff --git a/src/Responses/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse.php b/src/Responses/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse.php index 439cfbc..8921b86 100644 --- a/src/Responses/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse.php +++ b/src/Responses/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse.php @@ -35,7 +35,7 @@ final class CompletedSchemaGenerationResponse implements BaseModel #[Api('request_id', optional: true)] public ?string $requestID; - /** @var null|Status::* $status */ + /** @var Status::*|null $status */ #[Api(enum: Status::class, optional: true)] public ?string $status; @@ -53,7 +53,7 @@ public function __construct() * * You must use named parameters to construct any parameters with a default value. * - * @param null|Status::* $status + * @param Status::*|null $status */ public static function with( ?string $error = null, diff --git a/src/Responses/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse.php b/src/Responses/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse.php index fbb97ba..7c2f453 100644 --- a/src/Responses/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse.php +++ b/src/Responses/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse.php @@ -35,7 +35,7 @@ final class FailedSchemaGenerationResponse implements BaseModel #[Api('request_id', optional: true)] public ?string $requestID; - /** @var null|Status::* $status */ + /** @var Status::*|null $status */ #[Api(enum: Status::class, optional: true)] public ?string $status; @@ -53,7 +53,7 @@ public function __construct() * * You must use named parameters to construct any parameters with a default value. * - * @param null|Status::* $status + * @param Status::*|null $status */ public static function with( ?string $error = null, diff --git a/src/Responses/GenerateSchema/GenerateSchemaNewResponse.php b/src/Responses/GenerateSchema/GenerateSchemaNewResponse.php index b7fb018..68a1794 100644 --- a/src/Responses/GenerateSchema/GenerateSchemaNewResponse.php +++ b/src/Responses/GenerateSchema/GenerateSchemaNewResponse.php @@ -41,7 +41,7 @@ final class GenerateSchemaNewResponse implements BaseModel #[Api('request_id', optional: true)] public ?string $requestID; - /** @var null|Status::* $status */ + /** @var Status::*|null $status */ #[Api(enum: Status::class, optional: true)] public ?string $status; @@ -59,7 +59,7 @@ public function __construct() * * You must use named parameters to construct any parameters with a default value. * - * @param null|Status::* $status + * @param Status::*|null $status */ public static function with( ?string $error = null, diff --git a/src/Responses/Healthz/HealthzCheckResponse.php b/src/Responses/Healthz/HealthzCheckResponse.php index 12bceee..5d5a424 100644 --- a/src/Responses/Healthz/HealthzCheckResponse.php +++ b/src/Responses/Healthz/HealthzCheckResponse.php @@ -18,7 +18,7 @@ final class HealthzCheckResponse implements BaseModel { use SdkModel; - /** @var null|array $services */ + /** @var array|null $services */ #[Api(type: new MapOf('string'), optional: true)] public ?array $services; @@ -36,7 +36,7 @@ public function __construct() * * You must use named parameters to construct any parameters with a default value. * - * @param null|array $services + * @param array|null $services */ public static function with( ?array $services = null, diff --git a/src/Responses/Markdownify/MarkdownifyGetStatusResponse.php b/src/Responses/Markdownify/MarkdownifyGetStatusResponse.php index 49c9109..a73676d 100644 --- a/src/Responses/Markdownify/MarkdownifyGetStatusResponse.php +++ b/src/Responses/Markdownify/MarkdownifyGetStatusResponse.php @@ -18,8 +18,8 @@ final class MarkdownifyGetStatusResponse implements ConverterSource use SdkUnion; /** - * @return array|list + * @return list|array */ public static function variants(): array { diff --git a/src/Responses/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse.php b/src/Responses/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse.php index 7df90b6..0e7d1a5 100644 --- a/src/Responses/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse.php +++ b/src/Responses/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse.php @@ -31,7 +31,7 @@ final class FailedMarkdownifyResponse implements BaseModel #[Api(optional: true)] public ?string $result; - /** @var null|Status::* $status */ + /** @var Status::*|null $status */ #[Api(enum: Status::class, optional: true)] public ?string $status; @@ -49,7 +49,7 @@ public function __construct() * * You must use named parameters to construct any parameters with a default value. * - * @param null|Status::* $status + * @param Status::*|null $status */ public static function with( ?string $error = null, diff --git a/src/Responses/Searchscraper/SearchscraperGetStatusResponse.php b/src/Responses/Searchscraper/SearchscraperGetStatusResponse.php index 4591bb4..783a3a3 100644 --- a/src/Responses/Searchscraper/SearchscraperGetStatusResponse.php +++ b/src/Responses/Searchscraper/SearchscraperGetStatusResponse.php @@ -18,8 +18,8 @@ final class SearchscraperGetStatusResponse implements ConverterSource use SdkUnion; /** - * @return array|list + * @return list|array */ public static function variants(): array { diff --git a/src/Responses/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse.php b/src/Responses/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse.php index fd9c708..2ccd206 100644 --- a/src/Responses/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse.php +++ b/src/Responses/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse.php @@ -31,7 +31,7 @@ final class FailedSearchScraperResponse implements BaseModel #[Api('num_results', optional: true)] public ?int $numResults; - /** @var null|list $referenceURLs */ + /** @var list|null $referenceURLs */ #[Api('reference_urls', type: new ListOf('string'), optional: true)] public ?array $referenceURLs; @@ -41,7 +41,7 @@ final class FailedSearchScraperResponse implements BaseModel #[Api(optional: true)] public mixed $result; - /** @var null|Status::* $status */ + /** @var Status::*|null $status */ #[Api(enum: Status::class, optional: true)] public ?string $status; @@ -59,8 +59,8 @@ public function __construct() * * You must use named parameters to construct any parameters with a default value. * - * @param null|list $referenceURLs - * @param null|Status::* $status + * @param list|null $referenceURLs + * @param Status::*|null $status */ public static function with( ?string $error = null, diff --git a/src/Responses/Smartscraper/SmartscraperGetResponse.php b/src/Responses/Smartscraper/SmartscraperGetResponse.php index f086e17..47411dc 100644 --- a/src/Responses/Smartscraper/SmartscraperGetResponse.php +++ b/src/Responses/Smartscraper/SmartscraperGetResponse.php @@ -18,8 +18,8 @@ final class SmartscraperGetResponse implements ConverterSource use SdkUnion; /** - * @return array|list + * @return list|array */ public static function variants(): array { diff --git a/src/Responses/Smartscraper/SmartscraperListResponse.php b/src/Responses/Smartscraper/SmartscraperListResponse.php index fdfe01b..a0c7a15 100644 --- a/src/Responses/Smartscraper/SmartscraperListResponse.php +++ b/src/Responses/Smartscraper/SmartscraperListResponse.php @@ -18,8 +18,8 @@ final class SmartscraperListResponse implements ConverterSource use SdkUnion; /** - * @return array|list + * @return list|array */ public static function variants(): array { diff --git a/src/Searchscraper/CompletedSearchScraper.php b/src/Searchscraper/CompletedSearchScraper.php index d17e0aa..fe3bbd0 100644 --- a/src/Searchscraper/CompletedSearchScraper.php +++ b/src/Searchscraper/CompletedSearchScraper.php @@ -34,7 +34,7 @@ final class CompletedSearchScraper implements BaseModel /** * URLs of sources used. * - * @var null|list $referenceURLs + * @var list|null $referenceURLs */ #[Api('reference_urls', type: new ListOf('string'), optional: true)] public ?array $referenceURLs; @@ -48,7 +48,7 @@ final class CompletedSearchScraper implements BaseModel #[Api(optional: true)] public mixed $result; - /** @var null|Status::* $status */ + /** @var Status::*|null $status */ #[Api(enum: Status::class, optional: true)] public ?string $status; @@ -66,8 +66,8 @@ public function __construct() * * You must use named parameters to construct any parameters with a default value. * - * @param null|list $referenceURLs - * @param null|Status::* $status + * @param list|null $referenceURLs + * @param Status::*|null $status */ public static function with( ?string $error = null, diff --git a/src/Searchscraper/SearchscraperCreateParams.php b/src/Searchscraper/SearchscraperCreateParams.php index d6bb188..2092d5d 100644 --- a/src/Searchscraper/SearchscraperCreateParams.php +++ b/src/Searchscraper/SearchscraperCreateParams.php @@ -32,7 +32,7 @@ final class SearchscraperCreateParams implements BaseModel #[Api('user_prompt')] public string $userPrompt; - /** @var null|array $headers */ + /** @var array|null $headers */ #[Api(type: new MapOf('string'), optional: true)] public ?array $headers; @@ -73,7 +73,7 @@ public function __construct() * * You must use named parameters to construct any parameters with a default value. * - * @param null|array $headers + * @param array|null $headers */ public static function with( string $userPrompt, diff --git a/src/Services/CrawlService.php b/src/Services/CrawlService.php index a101418..45e81d7 100644 --- a/src/Services/CrawlService.php +++ b/src/Services/CrawlService.php @@ -44,7 +44,7 @@ public function retrieveResults( * @param int $depth Maximum crawl depth from starting URL * @param bool $extractionMode Use AI extraction (true) or markdown conversion (false) * @param int $maxPages Maximum number of pages to crawl - * @param null|string $prompt Extraction prompt (required if extraction_mode is true) + * @param string|null $prompt Extraction prompt (required if extraction_mode is true) * @param bool $renderHeavyJs Enable heavy JavaScript rendering * @param Rules $rules * @param mixed $schema Output schema for extraction diff --git a/src/Services/FeedbackService.php b/src/Services/FeedbackService.php index 672e4c7..e40f052 100644 --- a/src/Services/FeedbackService.php +++ b/src/Services/FeedbackService.php @@ -21,7 +21,7 @@ public function __construct(private Client $client) {} * * @param int $rating Rating score * @param string $requestID Request to provide feedback for - * @param null|string $feedbackText Optional feedback comments + * @param string|null $feedbackText Optional feedback comments */ public function submit( $rating, diff --git a/src/Smartscraper/CompletedSmartscraper.php b/src/Smartscraper/CompletedSmartscraper.php index 4d73839..432ff4d 100644 --- a/src/Smartscraper/CompletedSmartscraper.php +++ b/src/Smartscraper/CompletedSmartscraper.php @@ -44,7 +44,7 @@ final class CompletedSmartscraper implements BaseModel /** * Processing status. * - * @var null|Status::* $status + * @var Status::*|null $status */ #[Api(enum: Status::class, optional: true)] public ?string $status; @@ -66,7 +66,7 @@ public function __construct() * * You must use named parameters to construct any parameters with a default value. * - * @param null|Status::* $status + * @param Status::*|null $status */ public static function with( ?string $error = null, diff --git a/src/Smartscraper/FailedSmartscraper.php b/src/Smartscraper/FailedSmartscraper.php index 9112c25..b2ec95b 100644 --- a/src/Smartscraper/FailedSmartscraper.php +++ b/src/Smartscraper/FailedSmartscraper.php @@ -35,7 +35,7 @@ final class FailedSmartscraper implements BaseModel #[Api(optional: true)] public mixed $result; - /** @var null|Status::* $status */ + /** @var Status::*|null $status */ #[Api(enum: Status::class, optional: true)] public ?string $status; @@ -56,7 +56,7 @@ public function __construct() * * You must use named parameters to construct any parameters with a default value. * - * @param null|Status::* $status + * @param Status::*|null $status */ public static function with( ?string $error = null, diff --git a/src/Smartscraper/SmartscraperCreateParams.php b/src/Smartscraper/SmartscraperCreateParams.php index 0d40932..4471ddf 100644 --- a/src/Smartscraper/SmartscraperCreateParams.php +++ b/src/Smartscraper/SmartscraperCreateParams.php @@ -42,7 +42,7 @@ final class SmartscraperCreateParams implements BaseModel /** * Cookies to include in the request. * - * @var null|array $cookies + * @var array|null $cookies */ #[Api(type: new MapOf('string'), optional: true)] public ?array $cookies; @@ -50,7 +50,7 @@ final class SmartscraperCreateParams implements BaseModel /** * HTTP headers to include in the request. * - * @var null|array $headers + * @var array|null $headers */ #[Api(type: new MapOf('string'), optional: true)] public ?array $headers; @@ -76,7 +76,7 @@ final class SmartscraperCreateParams implements BaseModel /** * Website interaction steps (e.g., clicking buttons). * - * @var null|list $steps + * @var list|null $steps */ #[Api(type: new ListOf('string'), optional: true)] public ?array $steps; @@ -124,9 +124,9 @@ public function __construct() * * You must use named parameters to construct any parameters with a default value. * - * @param null|array $cookies - * @param null|array $headers - * @param null|list $steps + * @param array|null $cookies + * @param array|null $headers + * @param list|null $steps */ public static function with( string $userPrompt, diff --git a/tests/Core/TestModel.php b/tests/Core/TestModel.php index 7c701f8..6e07607 100644 --- a/tests/Core/TestModel.php +++ b/tests/Core/TestModel.php @@ -19,7 +19,7 @@ class TestModel implements BaseModel #[Api('age_years')] public int $ageYears; - /** @var null|list */ + /** @var list|null */ #[Api(optional: true)] public ?array $friends; @@ -27,7 +27,7 @@ class TestModel implements BaseModel public ?string $owner; /** - * @param null|list $friends + * @param list|null $friends */ public function __construct( string $name, From 586c9548945e0522a71f888803bc3c9e7804ccba Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Sat, 23 Aug 2025 03:35:25 +0000 Subject: [PATCH 11/44] chore: remove type aliases --- src/Crawl/CrawlStartParams.php | 12 ------------ src/Crawl/CrawlStartParams/Rules.php | 3 --- src/Feedback/FeedbackSubmitParams.php | 4 ---- src/GenerateSchema/GenerateSchemaCreateParams.php | 2 -- src/Markdownify/CompletedMarkdownify.php | 9 --------- src/Markdownify/CompletedMarkdownify/Status.php | 3 --- src/Markdownify/MarkdownifyConvertParams.php | 4 ---- src/Responses/Crawl/CrawlGetResultsResponse.php | 8 -------- .../Crawl/CrawlGetResultsResponse/Result.php | 2 -- .../Crawl/CrawlGetResultsResponse/Status.php | 3 --- src/Responses/Crawl/CrawlStartResponse.php | 3 --- src/Responses/Credits/CreditGetResponse.php | 5 ----- src/Responses/Feedback/FeedbackSubmitResponse.php | 8 -------- .../GenerateSchema/GenerateSchemaGetResponse.php | 3 --- .../CompletedSchemaGenerationResponse.php | 10 ---------- .../CompletedSchemaGenerationResponse/Status.php | 3 --- .../FailedSchemaGenerationResponse.php | 10 ---------- .../FailedSchemaGenerationResponse/Status.php | 3 --- .../GenerateSchema/GenerateSchemaNewResponse.php | 10 ---------- .../GenerateSchemaNewResponse/Status.php | 3 --- src/Responses/Healthz/HealthzCheckResponse.php | 5 ----- .../Markdownify/MarkdownifyGetStatusResponse.php | 3 --- .../FailedMarkdownifyResponse.php | 9 --------- .../FailedMarkdownifyResponse/Status.php | 3 --- .../SearchscraperGetStatusResponse.php | 3 --- .../FailedSearchScraperResponse.php | 11 ----------- .../FailedSearchScraperResponse/Status.php | 3 --- .../Smartscraper/SmartscraperGetResponse.php | 3 --- .../Smartscraper/SmartscraperListResponse.php | 3 --- src/Responses/Validate/ValidateAPIKeyResponse.php | 3 --- src/Searchscraper/CompletedSearchScraper.php | 11 ----------- src/Searchscraper/CompletedSearchScraper/Status.php | 3 --- src/Searchscraper/SearchscraperCreateParams.php | 7 ------- src/Smartscraper/CompletedSmartscraper.php | 10 ---------- src/Smartscraper/CompletedSmartscraper/Status.php | 2 -- src/Smartscraper/FailedSmartscraper.php | 10 ---------- src/Smartscraper/FailedSmartscraper/Status.php | 3 --- src/Smartscraper/SmartscraperCreateParams.php | 13 ------------- 38 files changed, 213 deletions(-) diff --git a/src/Crawl/CrawlStartParams.php b/src/Crawl/CrawlStartParams.php index bc35e45..d90d2fb 100644 --- a/src/Crawl/CrawlStartParams.php +++ b/src/Crawl/CrawlStartParams.php @@ -14,18 +14,6 @@ * Initiate comprehensive website crawling with sitemap support. * Supports both AI extraction mode and markdown conversion mode. * Returns a task ID for async processing. - * - * @phpstan-type start_params = array{ - * url: string, - * depth?: int, - * extractionMode?: bool, - * maxPages?: int, - * prompt?: string|null, - * renderHeavyJs?: bool, - * rules?: Rules, - * schema?: mixed, - * sitemap?: bool, - * } */ final class CrawlStartParams implements BaseModel { diff --git a/src/Crawl/CrawlStartParams/Rules.php b/src/Crawl/CrawlStartParams/Rules.php index 2f8cf04..c497764 100644 --- a/src/Crawl/CrawlStartParams/Rules.php +++ b/src/Crawl/CrawlStartParams/Rules.php @@ -9,9 +9,6 @@ use Scrapegraphai\Core\Contracts\BaseModel; use Scrapegraphai\Core\Conversion\ListOf; -/** - * @phpstan-type rules_alias = array{exclude?: list, sameDomain?: bool} - */ final class Rules implements BaseModel { use SdkModel; diff --git a/src/Feedback/FeedbackSubmitParams.php b/src/Feedback/FeedbackSubmitParams.php index cfb50a6..d0afd9c 100644 --- a/src/Feedback/FeedbackSubmitParams.php +++ b/src/Feedback/FeedbackSubmitParams.php @@ -11,10 +11,6 @@ /** * Submit feedback for a specific request. - * - * @phpstan-type submit_params = array{ - * rating: int, requestID: string, feedbackText?: string|null - * } */ final class FeedbackSubmitParams implements BaseModel { diff --git a/src/GenerateSchema/GenerateSchemaCreateParams.php b/src/GenerateSchema/GenerateSchemaCreateParams.php index 2a40332..fd35093 100644 --- a/src/GenerateSchema/GenerateSchemaCreateParams.php +++ b/src/GenerateSchema/GenerateSchemaCreateParams.php @@ -12,8 +12,6 @@ /** * Generate or modify JSON schemas based on natural language descriptions. * Can create new schemas or extend existing ones. - * - * @phpstan-type create_params = array{userPrompt: string, existingSchema?: mixed} */ final class GenerateSchemaCreateParams implements BaseModel { diff --git a/src/Markdownify/CompletedMarkdownify.php b/src/Markdownify/CompletedMarkdownify.php index 45e31fe..a30af97 100644 --- a/src/Markdownify/CompletedMarkdownify.php +++ b/src/Markdownify/CompletedMarkdownify.php @@ -9,15 +9,6 @@ use Scrapegraphai\Core\Contracts\BaseModel; use Scrapegraphai\Markdownify\CompletedMarkdownify\Status; -/** - * @phpstan-type completed_markdownify_alias = array{ - * error?: string, - * requestID?: string, - * result?: string|null, - * status?: Status::*, - * websiteURL?: string, - * } - */ final class CompletedMarkdownify implements BaseModel { use SdkModel; diff --git a/src/Markdownify/CompletedMarkdownify/Status.php b/src/Markdownify/CompletedMarkdownify/Status.php index 98291b9..8087933 100644 --- a/src/Markdownify/CompletedMarkdownify/Status.php +++ b/src/Markdownify/CompletedMarkdownify/Status.php @@ -7,9 +7,6 @@ use Scrapegraphai\Core\Concerns\SdkEnum; use Scrapegraphai\Core\Conversion\Contracts\ConverterSource; -/** - * @phpstan-type status_alias = Status::* - */ final class Status implements ConverterSource { use SdkEnum; diff --git a/src/Markdownify/MarkdownifyConvertParams.php b/src/Markdownify/MarkdownifyConvertParams.php index df4adc2..cf7bd96 100644 --- a/src/Markdownify/MarkdownifyConvertParams.php +++ b/src/Markdownify/MarkdownifyConvertParams.php @@ -13,10 +13,6 @@ /** * Convert web page content to clean Markdown format. - * - * @phpstan-type convert_params = array{ - * websiteURL: string, headers?: array, steps?: list - * } */ final class MarkdownifyConvertParams implements BaseModel { diff --git a/src/Responses/Crawl/CrawlGetResultsResponse.php b/src/Responses/Crawl/CrawlGetResultsResponse.php index ddc58f2..10b6d39 100644 --- a/src/Responses/Crawl/CrawlGetResultsResponse.php +++ b/src/Responses/Crawl/CrawlGetResultsResponse.php @@ -10,14 +10,6 @@ use Scrapegraphai\Responses\Crawl\CrawlGetResultsResponse\Result; use Scrapegraphai\Responses\Crawl\CrawlGetResultsResponse\Status; -/** - * @phpstan-type crawl_get_results_response_alias = array{ - * result?: mixed|string, - * status?: Status::*, - * taskID?: string, - * traceback?: string|null, - * } - */ final class CrawlGetResultsResponse implements BaseModel { use SdkModel; diff --git a/src/Responses/Crawl/CrawlGetResultsResponse/Result.php b/src/Responses/Crawl/CrawlGetResultsResponse/Result.php index 5386262..4c36957 100644 --- a/src/Responses/Crawl/CrawlGetResultsResponse/Result.php +++ b/src/Responses/Crawl/CrawlGetResultsResponse/Result.php @@ -10,8 +10,6 @@ /** * Successful crawl results. - * - * @phpstan-type result_alias = mixed|string */ final class Result implements ConverterSource { diff --git a/src/Responses/Crawl/CrawlGetResultsResponse/Status.php b/src/Responses/Crawl/CrawlGetResultsResponse/Status.php index 9318b1c..02e3650 100644 --- a/src/Responses/Crawl/CrawlGetResultsResponse/Status.php +++ b/src/Responses/Crawl/CrawlGetResultsResponse/Status.php @@ -7,9 +7,6 @@ use Scrapegraphai\Core\Concerns\SdkEnum; use Scrapegraphai\Core\Conversion\Contracts\ConverterSource; -/** - * @phpstan-type status_alias = Status::* - */ final class Status implements ConverterSource { use SdkEnum; diff --git a/src/Responses/Crawl/CrawlStartResponse.php b/src/Responses/Crawl/CrawlStartResponse.php index d247fcc..94e64e4 100644 --- a/src/Responses/Crawl/CrawlStartResponse.php +++ b/src/Responses/Crawl/CrawlStartResponse.php @@ -8,9 +8,6 @@ use Scrapegraphai\Core\Concerns\SdkModel; use Scrapegraphai\Core\Contracts\BaseModel; -/** - * @phpstan-type crawl_start_response_alias = array{taskID?: string} - */ final class CrawlStartResponse implements BaseModel { use SdkModel; diff --git a/src/Responses/Credits/CreditGetResponse.php b/src/Responses/Credits/CreditGetResponse.php index 88a8bf5..00e0c60 100644 --- a/src/Responses/Credits/CreditGetResponse.php +++ b/src/Responses/Credits/CreditGetResponse.php @@ -8,11 +8,6 @@ use Scrapegraphai\Core\Concerns\SdkModel; use Scrapegraphai\Core\Contracts\BaseModel; -/** - * @phpstan-type credit_get_response_alias = array{ - * remainingCredits?: int, totalCreditsUsed?: int - * } - */ final class CreditGetResponse implements BaseModel { use SdkModel; diff --git a/src/Responses/Feedback/FeedbackSubmitResponse.php b/src/Responses/Feedback/FeedbackSubmitResponse.php index 540e3c4..dc71c86 100644 --- a/src/Responses/Feedback/FeedbackSubmitResponse.php +++ b/src/Responses/Feedback/FeedbackSubmitResponse.php @@ -8,14 +8,6 @@ use Scrapegraphai\Core\Concerns\SdkModel; use Scrapegraphai\Core\Contracts\BaseModel; -/** - * @phpstan-type feedback_submit_response_alias = array{ - * feedbackID?: string, - * feedbackTimestamp?: \DateTimeInterface, - * message?: string, - * requestID?: string, - * } - */ final class FeedbackSubmitResponse implements BaseModel { use SdkModel; diff --git a/src/Responses/GenerateSchema/GenerateSchemaGetResponse.php b/src/Responses/GenerateSchema/GenerateSchemaGetResponse.php index 313dfed..100b707 100644 --- a/src/Responses/GenerateSchema/GenerateSchemaGetResponse.php +++ b/src/Responses/GenerateSchema/GenerateSchemaGetResponse.php @@ -10,9 +10,6 @@ use Scrapegraphai\Responses\GenerateSchema\GenerateSchemaGetResponse\CompletedSchemaGenerationResponse; use Scrapegraphai\Responses\GenerateSchema\GenerateSchemaGetResponse\FailedSchemaGenerationResponse; -/** - * @phpstan-type generate_schema_get_response_alias = CompletedSchemaGenerationResponse|FailedSchemaGenerationResponse - */ final class GenerateSchemaGetResponse implements ConverterSource { use SdkUnion; diff --git a/src/Responses/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse.php b/src/Responses/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse.php index 8921b86..730378f 100644 --- a/src/Responses/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse.php +++ b/src/Responses/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse.php @@ -9,16 +9,6 @@ use Scrapegraphai\Core\Contracts\BaseModel; use Scrapegraphai\Responses\GenerateSchema\GenerateSchemaGetResponse\CompletedSchemaGenerationResponse\Status; -/** - * @phpstan-type completed_schema_generation_response_alias = array{ - * error?: string|null, - * generatedSchema?: mixed, - * refinedPrompt?: string, - * requestID?: string, - * status?: Status::*, - * userPrompt?: string, - * } - */ final class CompletedSchemaGenerationResponse implements BaseModel { use SdkModel; diff --git a/src/Responses/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse/Status.php b/src/Responses/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse/Status.php index 2ab7597..e6a3e96 100644 --- a/src/Responses/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse/Status.php +++ b/src/Responses/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse/Status.php @@ -7,9 +7,6 @@ use Scrapegraphai\Core\Concerns\SdkEnum; use Scrapegraphai\Core\Conversion\Contracts\ConverterSource; -/** - * @phpstan-type status_alias = Status::* - */ final class Status implements ConverterSource { use SdkEnum; diff --git a/src/Responses/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse.php b/src/Responses/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse.php index 7c2f453..6138e56 100644 --- a/src/Responses/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse.php +++ b/src/Responses/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse.php @@ -9,16 +9,6 @@ use Scrapegraphai\Core\Contracts\BaseModel; use Scrapegraphai\Responses\GenerateSchema\GenerateSchemaGetResponse\FailedSchemaGenerationResponse\Status; -/** - * @phpstan-type failed_schema_generation_response_alias = array{ - * error?: string, - * generatedSchema?: mixed, - * refinedPrompt?: string|null, - * requestID?: string, - * status?: Status::*, - * userPrompt?: string, - * } - */ final class FailedSchemaGenerationResponse implements BaseModel { use SdkModel; diff --git a/src/Responses/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse/Status.php b/src/Responses/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse/Status.php index 08afa4a..f67b258 100644 --- a/src/Responses/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse/Status.php +++ b/src/Responses/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse/Status.php @@ -7,9 +7,6 @@ use Scrapegraphai\Core\Concerns\SdkEnum; use Scrapegraphai\Core\Conversion\Contracts\ConverterSource; -/** - * @phpstan-type status_alias = Status::* - */ final class Status implements ConverterSource { use SdkEnum; diff --git a/src/Responses/GenerateSchema/GenerateSchemaNewResponse.php b/src/Responses/GenerateSchema/GenerateSchemaNewResponse.php index 68a1794..b544c89 100644 --- a/src/Responses/GenerateSchema/GenerateSchemaNewResponse.php +++ b/src/Responses/GenerateSchema/GenerateSchemaNewResponse.php @@ -9,16 +9,6 @@ use Scrapegraphai\Core\Contracts\BaseModel; use Scrapegraphai\Responses\GenerateSchema\GenerateSchemaNewResponse\Status; -/** - * @phpstan-type generate_schema_new_response_alias = array{ - * error?: string|null, - * generatedSchema?: mixed, - * refinedPrompt?: string, - * requestID?: string, - * status?: Status::*, - * userPrompt?: string, - * } - */ final class GenerateSchemaNewResponse implements BaseModel { use SdkModel; diff --git a/src/Responses/GenerateSchema/GenerateSchemaNewResponse/Status.php b/src/Responses/GenerateSchema/GenerateSchemaNewResponse/Status.php index 589a5d2..eecd235 100644 --- a/src/Responses/GenerateSchema/GenerateSchemaNewResponse/Status.php +++ b/src/Responses/GenerateSchema/GenerateSchemaNewResponse/Status.php @@ -7,9 +7,6 @@ use Scrapegraphai\Core\Concerns\SdkEnum; use Scrapegraphai\Core\Conversion\Contracts\ConverterSource; -/** - * @phpstan-type status_alias = Status::* - */ final class Status implements ConverterSource { use SdkEnum; diff --git a/src/Responses/Healthz/HealthzCheckResponse.php b/src/Responses/Healthz/HealthzCheckResponse.php index 5d5a424..ad643b5 100644 --- a/src/Responses/Healthz/HealthzCheckResponse.php +++ b/src/Responses/Healthz/HealthzCheckResponse.php @@ -9,11 +9,6 @@ use Scrapegraphai\Core\Contracts\BaseModel; use Scrapegraphai\Core\Conversion\MapOf; -/** - * @phpstan-type healthz_check_response_alias = array{ - * services?: array, status?: string - * } - */ final class HealthzCheckResponse implements BaseModel { use SdkModel; diff --git a/src/Responses/Markdownify/MarkdownifyGetStatusResponse.php b/src/Responses/Markdownify/MarkdownifyGetStatusResponse.php index a73676d..14061e8 100644 --- a/src/Responses/Markdownify/MarkdownifyGetStatusResponse.php +++ b/src/Responses/Markdownify/MarkdownifyGetStatusResponse.php @@ -10,9 +10,6 @@ use Scrapegraphai\Markdownify\CompletedMarkdownify; use Scrapegraphai\Responses\Markdownify\MarkdownifyGetStatusResponse\FailedMarkdownifyResponse; -/** - * @phpstan-type markdownify_get_status_response_alias = CompletedMarkdownify|FailedMarkdownifyResponse - */ final class MarkdownifyGetStatusResponse implements ConverterSource { use SdkUnion; diff --git a/src/Responses/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse.php b/src/Responses/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse.php index 0e7d1a5..a3d2982 100644 --- a/src/Responses/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse.php +++ b/src/Responses/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse.php @@ -9,15 +9,6 @@ use Scrapegraphai\Core\Contracts\BaseModel; use Scrapegraphai\Responses\Markdownify\MarkdownifyGetStatusResponse\FailedMarkdownifyResponse\Status; -/** - * @phpstan-type failed_markdownify_response_alias = array{ - * error?: string, - * requestID?: string, - * result?: string|null, - * status?: Status::*, - * websiteURL?: string, - * } - */ final class FailedMarkdownifyResponse implements BaseModel { use SdkModel; diff --git a/src/Responses/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse/Status.php b/src/Responses/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse/Status.php index 106d2b8..e173831 100644 --- a/src/Responses/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse/Status.php +++ b/src/Responses/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse/Status.php @@ -7,9 +7,6 @@ use Scrapegraphai\Core\Concerns\SdkEnum; use Scrapegraphai\Core\Conversion\Contracts\ConverterSource; -/** - * @phpstan-type status_alias = Status::* - */ final class Status implements ConverterSource { use SdkEnum; diff --git a/src/Responses/Searchscraper/SearchscraperGetStatusResponse.php b/src/Responses/Searchscraper/SearchscraperGetStatusResponse.php index 783a3a3..67bf387 100644 --- a/src/Responses/Searchscraper/SearchscraperGetStatusResponse.php +++ b/src/Responses/Searchscraper/SearchscraperGetStatusResponse.php @@ -10,9 +10,6 @@ use Scrapegraphai\Responses\Searchscraper\SearchscraperGetStatusResponse\FailedSearchScraperResponse; use Scrapegraphai\Searchscraper\CompletedSearchScraper; -/** - * @phpstan-type searchscraper_get_status_response_alias = CompletedSearchScraper|FailedSearchScraperResponse - */ final class SearchscraperGetStatusResponse implements ConverterSource { use SdkUnion; diff --git a/src/Responses/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse.php b/src/Responses/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse.php index 2ccd206..2f6e3e8 100644 --- a/src/Responses/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse.php +++ b/src/Responses/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse.php @@ -10,17 +10,6 @@ use Scrapegraphai\Core\Conversion\ListOf; use Scrapegraphai\Responses\Searchscraper\SearchscraperGetStatusResponse\FailedSearchScraperResponse\Status; -/** - * @phpstan-type failed_search_scraper_response_alias = array{ - * error?: string, - * numResults?: int, - * referenceURLs?: list, - * requestID?: string, - * result?: mixed, - * status?: Status::*, - * userPrompt?: string, - * } - */ final class FailedSearchScraperResponse implements BaseModel { use SdkModel; diff --git a/src/Responses/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse/Status.php b/src/Responses/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse/Status.php index d013879..3c15568 100644 --- a/src/Responses/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse/Status.php +++ b/src/Responses/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse/Status.php @@ -7,9 +7,6 @@ use Scrapegraphai\Core\Concerns\SdkEnum; use Scrapegraphai\Core\Conversion\Contracts\ConverterSource; -/** - * @phpstan-type status_alias = Status::* - */ final class Status implements ConverterSource { use SdkEnum; diff --git a/src/Responses/Smartscraper/SmartscraperGetResponse.php b/src/Responses/Smartscraper/SmartscraperGetResponse.php index 47411dc..5af25f8 100644 --- a/src/Responses/Smartscraper/SmartscraperGetResponse.php +++ b/src/Responses/Smartscraper/SmartscraperGetResponse.php @@ -10,9 +10,6 @@ use Scrapegraphai\Smartscraper\CompletedSmartscraper; use Scrapegraphai\Smartscraper\FailedSmartscraper; -/** - * @phpstan-type smartscraper_get_response_alias = CompletedSmartscraper|FailedSmartscraper - */ final class SmartscraperGetResponse implements ConverterSource { use SdkUnion; diff --git a/src/Responses/Smartscraper/SmartscraperListResponse.php b/src/Responses/Smartscraper/SmartscraperListResponse.php index a0c7a15..5e20795 100644 --- a/src/Responses/Smartscraper/SmartscraperListResponse.php +++ b/src/Responses/Smartscraper/SmartscraperListResponse.php @@ -10,9 +10,6 @@ use Scrapegraphai\Smartscraper\CompletedSmartscraper; use Scrapegraphai\Smartscraper\FailedSmartscraper; -/** - * @phpstan-type smartscraper_list_response_alias = CompletedSmartscraper|FailedSmartscraper - */ final class SmartscraperListResponse implements ConverterSource { use SdkUnion; diff --git a/src/Responses/Validate/ValidateAPIKeyResponse.php b/src/Responses/Validate/ValidateAPIKeyResponse.php index 03d46f5..bf40f3b 100644 --- a/src/Responses/Validate/ValidateAPIKeyResponse.php +++ b/src/Responses/Validate/ValidateAPIKeyResponse.php @@ -8,9 +8,6 @@ use Scrapegraphai\Core\Concerns\SdkModel; use Scrapegraphai\Core\Contracts\BaseModel; -/** - * @phpstan-type validate_api_key_response_alias = array{email?: string} - */ final class ValidateAPIKeyResponse implements BaseModel { use SdkModel; diff --git a/src/Searchscraper/CompletedSearchScraper.php b/src/Searchscraper/CompletedSearchScraper.php index fe3bbd0..ab89ece 100644 --- a/src/Searchscraper/CompletedSearchScraper.php +++ b/src/Searchscraper/CompletedSearchScraper.php @@ -10,17 +10,6 @@ use Scrapegraphai\Core\Conversion\ListOf; use Scrapegraphai\Searchscraper\CompletedSearchScraper\Status; -/** - * @phpstan-type completed_search_scraper_alias = array{ - * error?: string|null, - * numResults?: int, - * referenceURLs?: list, - * requestID?: string, - * result?: mixed, - * status?: Status::*, - * userPrompt?: string, - * } - */ final class CompletedSearchScraper implements BaseModel { use SdkModel; diff --git a/src/Searchscraper/CompletedSearchScraper/Status.php b/src/Searchscraper/CompletedSearchScraper/Status.php index 67a8243..68e49ff 100644 --- a/src/Searchscraper/CompletedSearchScraper/Status.php +++ b/src/Searchscraper/CompletedSearchScraper/Status.php @@ -7,9 +7,6 @@ use Scrapegraphai\Core\Concerns\SdkEnum; use Scrapegraphai\Core\Conversion\Contracts\ConverterSource; -/** - * @phpstan-type status_alias = Status::* - */ final class Status implements ConverterSource { use SdkEnum; diff --git a/src/Searchscraper/SearchscraperCreateParams.php b/src/Searchscraper/SearchscraperCreateParams.php index 2092d5d..98ed298 100644 --- a/src/Searchscraper/SearchscraperCreateParams.php +++ b/src/Searchscraper/SearchscraperCreateParams.php @@ -13,13 +13,6 @@ /** * Performs web search, selects relevant URLs, and extracts structured data from multiple websites. * Uses LLM to refine search queries and merge results from different sources. - * - * @phpstan-type create_params = array{ - * userPrompt: string, - * headers?: array, - * numResults?: int, - * outputSchema?: mixed, - * } */ final class SearchscraperCreateParams implements BaseModel { diff --git a/src/Smartscraper/CompletedSmartscraper.php b/src/Smartscraper/CompletedSmartscraper.php index 432ff4d..fbc7baa 100644 --- a/src/Smartscraper/CompletedSmartscraper.php +++ b/src/Smartscraper/CompletedSmartscraper.php @@ -9,16 +9,6 @@ use Scrapegraphai\Core\Contracts\BaseModel; use Scrapegraphai\Smartscraper\CompletedSmartscraper\Status; -/** - * @phpstan-type completed_smartscraper_alias = array{ - * error?: string, - * requestID?: string, - * result?: mixed, - * status?: Status::*, - * userPrompt?: string, - * websiteURL?: string|null, - * } - */ final class CompletedSmartscraper implements BaseModel { use SdkModel; diff --git a/src/Smartscraper/CompletedSmartscraper/Status.php b/src/Smartscraper/CompletedSmartscraper/Status.php index bd39cbb..1b66e4b 100644 --- a/src/Smartscraper/CompletedSmartscraper/Status.php +++ b/src/Smartscraper/CompletedSmartscraper/Status.php @@ -9,8 +9,6 @@ /** * Processing status. - * - * @phpstan-type status_alias = Status::* */ final class Status implements ConverterSource { diff --git a/src/Smartscraper/FailedSmartscraper.php b/src/Smartscraper/FailedSmartscraper.php index b2ec95b..32380e8 100644 --- a/src/Smartscraper/FailedSmartscraper.php +++ b/src/Smartscraper/FailedSmartscraper.php @@ -9,16 +9,6 @@ use Scrapegraphai\Core\Contracts\BaseModel; use Scrapegraphai\Smartscraper\FailedSmartscraper\Status; -/** - * @phpstan-type failed_smartscraper_alias = array{ - * error?: string, - * requestID?: string, - * result?: mixed, - * status?: Status::*, - * userPrompt?: string, - * websiteURL?: string|null, - * } - */ final class FailedSmartscraper implements BaseModel { use SdkModel; diff --git a/src/Smartscraper/FailedSmartscraper/Status.php b/src/Smartscraper/FailedSmartscraper/Status.php index 0f4f020..ef18e98 100644 --- a/src/Smartscraper/FailedSmartscraper/Status.php +++ b/src/Smartscraper/FailedSmartscraper/Status.php @@ -7,9 +7,6 @@ use Scrapegraphai\Core\Concerns\SdkEnum; use Scrapegraphai\Core\Conversion\Contracts\ConverterSource; -/** - * @phpstan-type status_alias = Status::* - */ final class Status implements ConverterSource { use SdkEnum; diff --git a/src/Smartscraper/SmartscraperCreateParams.php b/src/Smartscraper/SmartscraperCreateParams.php index 4471ddf..a280ed0 100644 --- a/src/Smartscraper/SmartscraperCreateParams.php +++ b/src/Smartscraper/SmartscraperCreateParams.php @@ -14,19 +14,6 @@ /** * Main scraping endpoint with LLM-powered content analysis. Supports various fetching providers, * infinite scrolling, pagination, and custom output schemas. - * - * @phpstan-type create_params = array{ - * userPrompt: string, - * cookies?: array, - * headers?: array, - * numberOfScrolls?: int, - * outputSchema?: mixed, - * renderHeavyJs?: bool, - * steps?: list, - * totalPages?: int, - * websiteHTML?: string, - * websiteURL?: string, - * } */ final class SmartscraperCreateParams implements BaseModel { From 585d282dfdc9c34081e6cb44c0c83256037be12f Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Sun, 24 Aug 2025 02:14:51 +0000 Subject: [PATCH 12/44] chore: improve model annotations --- src/Core/Attributes/Api.php | 7 ++++++- src/Core/BaseClient.php | 5 ++--- src/Core/Util.php | 10 ++++------ src/Crawl/CrawlStartParams.php | 4 ++-- src/Crawl/CrawlStartParams/Rules.php | 5 ++--- src/Feedback/FeedbackSubmitParams.php | 2 +- src/GenerateSchema/GenerateSchemaCreateParams.php | 2 +- src/Markdownify/CompletedMarkdownify.php | 4 ++-- src/Markdownify/MarkdownifyConvertParams.php | 10 ++++------ src/Responses/Crawl/CrawlGetResultsResponse.php | 6 +++--- .../CompletedSchemaGenerationResponse.php | 4 ++-- .../FailedSchemaGenerationResponse.php | 6 +++--- .../GenerateSchema/GenerateSchemaNewResponse.php | 4 ++-- src/Responses/Healthz/HealthzCheckResponse.php | 5 ++--- .../FailedMarkdownifyResponse.php | 4 ++-- .../FailedSearchScraperResponse.php | 9 ++++----- src/Searchscraper/CompletedSearchScraper.php | 9 ++++----- src/Searchscraper/SearchscraperCreateParams.php | 5 ++--- src/Smartscraper/CompletedSmartscraper.php | 6 +++--- src/Smartscraper/FailedSmartscraper.php | 6 +++--- src/Smartscraper/SmartscraperCreateParams.php | 14 ++++++-------- 21 files changed, 60 insertions(+), 67 deletions(-) diff --git a/src/Core/Attributes/Api.php b/src/Core/Attributes/Api.php index 9583df3..a1ec512 100644 --- a/src/Core/Attributes/Api.php +++ b/src/Core/Attributes/Api.php @@ -6,6 +6,8 @@ use Scrapegraphai\Core\Conversion\Contracts\Converter; use Scrapegraphai\Core\Conversion\Contracts\ConverterSource; +use Scrapegraphai\Core\Conversion\ListOf; +use Scrapegraphai\Core\Conversion\MapOf; /** * @internal @@ -22,15 +24,18 @@ final class Api * @param class-string|Converter|string|null $type * @param class-string|Converter|null $enum * @param class-string|Converter|string|null $union + * @param class-string|Converter|string|null $list */ public function __construct( public readonly ?string $apiName = null, Converter|string|null $type = null, Converter|string|null $enum = null, Converter|string|null $union = null, + Converter|string|null $list = null, + Converter|string|null $map = null, public readonly bool $nullable = false, public readonly bool $optional = false, ) { - $this->type = $type ?? $enum ?? $union; + $this->type = $type ?? $enum ?? $union ?? ($list ? new ListOf($list) : ($map ? new MapOf($map) : null)); } } diff --git a/src/Core/BaseClient.php b/src/Core/BaseClient.php index 38db270..29e5b92 100644 --- a/src/Core/BaseClient.php +++ b/src/Core/BaseClient.php @@ -146,9 +146,8 @@ protected function followRedirect( } /** - * @param bool|int|float|string|array|resource|\Traversable< - * mixed - * >|null $data + * @param bool|int|float|string|resource|\Traversable|array|null $data */ protected function sendRequest( RequestInterface $req, diff --git a/src/Core/Util.php b/src/Core/Util.php index 0bfa110..88c9be0 100644 --- a/src/Core/Util.php +++ b/src/Core/Util.php @@ -165,9 +165,8 @@ public static function streamIterator(StreamInterface $stream): \Iterator } /** - * @param bool|int|float|string|array|resource|\Traversable< - * mixed - * >|null $body + * @param bool|int|float|string|resource|\Traversable|array|null $body * * @return array{string, \Generator} */ @@ -202,9 +201,8 @@ public static function encodeMultipartStreaming(mixed $body): array } /** - * @param bool|int|float|string|array|resource|\Traversable< - * mixed - * >|null $body + * @param bool|int|float|string|resource|\Traversable|array|null $body */ public static function withSetBody( StreamFactoryInterface $factory, diff --git a/src/Crawl/CrawlStartParams.php b/src/Crawl/CrawlStartParams.php index d90d2fb..3bbcb52 100644 --- a/src/Crawl/CrawlStartParams.php +++ b/src/Crawl/CrawlStartParams.php @@ -47,7 +47,7 @@ final class CrawlStartParams implements BaseModel /** * Extraction prompt (required if extraction_mode is true). */ - #[Api(optional: true)] + #[Api(nullable: true, optional: true)] public ?string $prompt; /** @@ -62,7 +62,7 @@ final class CrawlStartParams implements BaseModel /** * Output schema for extraction. */ - #[Api(optional: true)] + #[Api(nullable: true, optional: true)] public mixed $schema; /** diff --git a/src/Crawl/CrawlStartParams/Rules.php b/src/Crawl/CrawlStartParams/Rules.php index c497764..a73af47 100644 --- a/src/Crawl/CrawlStartParams/Rules.php +++ b/src/Crawl/CrawlStartParams/Rules.php @@ -7,7 +7,6 @@ use Scrapegraphai\Core\Attributes\Api; use Scrapegraphai\Core\Concerns\SdkModel; use Scrapegraphai\Core\Contracts\BaseModel; -use Scrapegraphai\Core\Conversion\ListOf; final class Rules implements BaseModel { @@ -18,7 +17,7 @@ final class Rules implements BaseModel * * @var list|null $exclude */ - #[Api(type: new ListOf('string'), optional: true)] + #[Api(list: 'string', optional: true)] public ?array $exclude; /** @@ -38,7 +37,7 @@ public function __construct() * * You must use named parameters to construct any parameters with a default value. * - * @param list|null $exclude + * @param list $exclude */ public static function with( ?array $exclude = null, diff --git a/src/Feedback/FeedbackSubmitParams.php b/src/Feedback/FeedbackSubmitParams.php index d0afd9c..e7372b6 100644 --- a/src/Feedback/FeedbackSubmitParams.php +++ b/src/Feedback/FeedbackSubmitParams.php @@ -32,7 +32,7 @@ final class FeedbackSubmitParams implements BaseModel /** * Optional feedback comments. */ - #[Api('feedback_text', optional: true)] + #[Api('feedback_text', nullable: true, optional: true)] public ?string $feedbackText; /** diff --git a/src/GenerateSchema/GenerateSchemaCreateParams.php b/src/GenerateSchema/GenerateSchemaCreateParams.php index fd35093..eed8204 100644 --- a/src/GenerateSchema/GenerateSchemaCreateParams.php +++ b/src/GenerateSchema/GenerateSchemaCreateParams.php @@ -27,7 +27,7 @@ final class GenerateSchemaCreateParams implements BaseModel /** * Existing schema to modify or extend. */ - #[Api('existing_schema', optional: true)] + #[Api('existing_schema', nullable: true, optional: true)] public mixed $existingSchema; /** diff --git a/src/Markdownify/CompletedMarkdownify.php b/src/Markdownify/CompletedMarkdownify.php index a30af97..ab99144 100644 --- a/src/Markdownify/CompletedMarkdownify.php +++ b/src/Markdownify/CompletedMarkdownify.php @@ -22,7 +22,7 @@ final class CompletedMarkdownify implements BaseModel /** * Markdown content. */ - #[Api(optional: true)] + #[Api(nullable: true, optional: true)] public ?string $result; /** @var Status::*|null $status */ @@ -43,7 +43,7 @@ public function __construct() * * You must use named parameters to construct any parameters with a default value. * - * @param Status::*|null $status + * @param Status::* $status */ public static function with( ?string $error = null, diff --git a/src/Markdownify/MarkdownifyConvertParams.php b/src/Markdownify/MarkdownifyConvertParams.php index cf7bd96..9699ef2 100644 --- a/src/Markdownify/MarkdownifyConvertParams.php +++ b/src/Markdownify/MarkdownifyConvertParams.php @@ -8,8 +8,6 @@ use Scrapegraphai\Core\Concerns\SdkModel; use Scrapegraphai\Core\Concerns\SdkParams; use Scrapegraphai\Core\Contracts\BaseModel; -use Scrapegraphai\Core\Conversion\ListOf; -use Scrapegraphai\Core\Conversion\MapOf; /** * Convert web page content to clean Markdown format. @@ -26,7 +24,7 @@ final class MarkdownifyConvertParams implements BaseModel public string $websiteURL; /** @var array|null $headers */ - #[Api(type: new MapOf('string'), optional: true)] + #[Api(map: 'string', optional: true)] public ?array $headers; /** @@ -34,7 +32,7 @@ final class MarkdownifyConvertParams implements BaseModel * * @var list|null $steps */ - #[Api(type: new ListOf('string'), optional: true)] + #[Api(list: 'string', optional: true)] public ?array $steps; /** @@ -62,8 +60,8 @@ public function __construct() * * You must use named parameters to construct any parameters with a default value. * - * @param array|null $headers - * @param list|null $steps + * @param array $headers + * @param list $steps */ public static function with( string $websiteURL, diff --git a/src/Responses/Crawl/CrawlGetResultsResponse.php b/src/Responses/Crawl/CrawlGetResultsResponse.php index 10b6d39..6efb80d 100644 --- a/src/Responses/Crawl/CrawlGetResultsResponse.php +++ b/src/Responses/Crawl/CrawlGetResultsResponse.php @@ -32,7 +32,7 @@ final class CrawlGetResultsResponse implements BaseModel /** * Error traceback for failed tasks. */ - #[Api(optional: true)] + #[Api(nullable: true, optional: true)] public ?string $traceback; public function __construct() @@ -46,8 +46,8 @@ public function __construct() * * You must use named parameters to construct any parameters with a default value. * - * @param mixed|string|null $result - * @param Status::*|null $status + * @param mixed|string $result + * @param Status::* $status */ public static function with( mixed $result = null, diff --git a/src/Responses/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse.php b/src/Responses/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse.php index 730378f..d788ecc 100644 --- a/src/Responses/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse.php +++ b/src/Responses/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse.php @@ -13,7 +13,7 @@ final class CompletedSchemaGenerationResponse implements BaseModel { use SdkModel; - #[Api(optional: true)] + #[Api(nullable: true, optional: true)] public ?string $error; #[Api('generated_schema', optional: true)] @@ -43,7 +43,7 @@ public function __construct() * * You must use named parameters to construct any parameters with a default value. * - * @param Status::*|null $status + * @param Status::* $status */ public static function with( ?string $error = null, diff --git a/src/Responses/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse.php b/src/Responses/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse.php index 6138e56..2703192 100644 --- a/src/Responses/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse.php +++ b/src/Responses/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse.php @@ -16,10 +16,10 @@ final class FailedSchemaGenerationResponse implements BaseModel #[Api(optional: true)] public ?string $error; - #[Api('generated_schema', optional: true)] + #[Api('generated_schema', nullable: true, optional: true)] public mixed $generatedSchema; - #[Api('refined_prompt', optional: true)] + #[Api('refined_prompt', nullable: true, optional: true)] public ?string $refinedPrompt; #[Api('request_id', optional: true)] @@ -43,7 +43,7 @@ public function __construct() * * You must use named parameters to construct any parameters with a default value. * - * @param Status::*|null $status + * @param Status::* $status */ public static function with( ?string $error = null, diff --git a/src/Responses/GenerateSchema/GenerateSchemaNewResponse.php b/src/Responses/GenerateSchema/GenerateSchemaNewResponse.php index b544c89..8dbe968 100644 --- a/src/Responses/GenerateSchema/GenerateSchemaNewResponse.php +++ b/src/Responses/GenerateSchema/GenerateSchemaNewResponse.php @@ -13,7 +13,7 @@ final class GenerateSchemaNewResponse implements BaseModel { use SdkModel; - #[Api(optional: true)] + #[Api(nullable: true, optional: true)] public ?string $error; /** @@ -49,7 +49,7 @@ public function __construct() * * You must use named parameters to construct any parameters with a default value. * - * @param Status::*|null $status + * @param Status::* $status */ public static function with( ?string $error = null, diff --git a/src/Responses/Healthz/HealthzCheckResponse.php b/src/Responses/Healthz/HealthzCheckResponse.php index ad643b5..4828008 100644 --- a/src/Responses/Healthz/HealthzCheckResponse.php +++ b/src/Responses/Healthz/HealthzCheckResponse.php @@ -7,14 +7,13 @@ use Scrapegraphai\Core\Attributes\Api; use Scrapegraphai\Core\Concerns\SdkModel; use Scrapegraphai\Core\Contracts\BaseModel; -use Scrapegraphai\Core\Conversion\MapOf; final class HealthzCheckResponse implements BaseModel { use SdkModel; /** @var array|null $services */ - #[Api(type: new MapOf('string'), optional: true)] + #[Api(map: 'string', optional: true)] public ?array $services; #[Api(optional: true)] @@ -31,7 +30,7 @@ public function __construct() * * You must use named parameters to construct any parameters with a default value. * - * @param array|null $services + * @param array $services */ public static function with( ?array $services = null, diff --git a/src/Responses/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse.php b/src/Responses/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse.php index a3d2982..2a1a00e 100644 --- a/src/Responses/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse.php +++ b/src/Responses/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse.php @@ -19,7 +19,7 @@ final class FailedMarkdownifyResponse implements BaseModel #[Api('request_id', optional: true)] public ?string $requestID; - #[Api(optional: true)] + #[Api(nullable: true, optional: true)] public ?string $result; /** @var Status::*|null $status */ @@ -40,7 +40,7 @@ public function __construct() * * You must use named parameters to construct any parameters with a default value. * - * @param Status::*|null $status + * @param Status::* $status */ public static function with( ?string $error = null, diff --git a/src/Responses/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse.php b/src/Responses/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse.php index 2f6e3e8..31972dc 100644 --- a/src/Responses/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse.php +++ b/src/Responses/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse.php @@ -7,7 +7,6 @@ use Scrapegraphai\Core\Attributes\Api; use Scrapegraphai\Core\Concerns\SdkModel; use Scrapegraphai\Core\Contracts\BaseModel; -use Scrapegraphai\Core\Conversion\ListOf; use Scrapegraphai\Responses\Searchscraper\SearchscraperGetStatusResponse\FailedSearchScraperResponse\Status; final class FailedSearchScraperResponse implements BaseModel @@ -21,13 +20,13 @@ final class FailedSearchScraperResponse implements BaseModel public ?int $numResults; /** @var list|null $referenceURLs */ - #[Api('reference_urls', type: new ListOf('string'), optional: true)] + #[Api('reference_urls', list: 'string', optional: true)] public ?array $referenceURLs; #[Api('request_id', optional: true)] public ?string $requestID; - #[Api(optional: true)] + #[Api(nullable: true, optional: true)] public mixed $result; /** @var Status::*|null $status */ @@ -48,8 +47,8 @@ public function __construct() * * You must use named parameters to construct any parameters with a default value. * - * @param list|null $referenceURLs - * @param Status::*|null $status + * @param list $referenceURLs + * @param Status::* $status */ public static function with( ?string $error = null, diff --git a/src/Searchscraper/CompletedSearchScraper.php b/src/Searchscraper/CompletedSearchScraper.php index ab89ece..701b23e 100644 --- a/src/Searchscraper/CompletedSearchScraper.php +++ b/src/Searchscraper/CompletedSearchScraper.php @@ -7,14 +7,13 @@ use Scrapegraphai\Core\Attributes\Api; use Scrapegraphai\Core\Concerns\SdkModel; use Scrapegraphai\Core\Contracts\BaseModel; -use Scrapegraphai\Core\Conversion\ListOf; use Scrapegraphai\Searchscraper\CompletedSearchScraper\Status; final class CompletedSearchScraper implements BaseModel { use SdkModel; - #[Api(optional: true)] + #[Api(nullable: true, optional: true)] public ?string $error; #[Api('num_results', optional: true)] @@ -25,7 +24,7 @@ final class CompletedSearchScraper implements BaseModel * * @var list|null $referenceURLs */ - #[Api('reference_urls', type: new ListOf('string'), optional: true)] + #[Api('reference_urls', list: 'string', optional: true)] public ?array $referenceURLs; #[Api('request_id', optional: true)] @@ -55,8 +54,8 @@ public function __construct() * * You must use named parameters to construct any parameters with a default value. * - * @param list|null $referenceURLs - * @param Status::*|null $status + * @param list $referenceURLs + * @param Status::* $status */ public static function with( ?string $error = null, diff --git a/src/Searchscraper/SearchscraperCreateParams.php b/src/Searchscraper/SearchscraperCreateParams.php index 98ed298..4dd94ee 100644 --- a/src/Searchscraper/SearchscraperCreateParams.php +++ b/src/Searchscraper/SearchscraperCreateParams.php @@ -8,7 +8,6 @@ use Scrapegraphai\Core\Concerns\SdkModel; use Scrapegraphai\Core\Concerns\SdkParams; use Scrapegraphai\Core\Contracts\BaseModel; -use Scrapegraphai\Core\Conversion\MapOf; /** * Performs web search, selects relevant URLs, and extracts structured data from multiple websites. @@ -26,7 +25,7 @@ final class SearchscraperCreateParams implements BaseModel public string $userPrompt; /** @var array|null $headers */ - #[Api(type: new MapOf('string'), optional: true)] + #[Api(map: 'string', optional: true)] public ?array $headers; /** @@ -66,7 +65,7 @@ public function __construct() * * You must use named parameters to construct any parameters with a default value. * - * @param array|null $headers + * @param array $headers */ public static function with( string $userPrompt, diff --git a/src/Smartscraper/CompletedSmartscraper.php b/src/Smartscraper/CompletedSmartscraper.php index fbc7baa..684284b 100644 --- a/src/Smartscraper/CompletedSmartscraper.php +++ b/src/Smartscraper/CompletedSmartscraper.php @@ -28,7 +28,7 @@ final class CompletedSmartscraper implements BaseModel /** * Extracted data based on prompt/schema. */ - #[Api(optional: true)] + #[Api(nullable: true, optional: true)] public mixed $result; /** @@ -42,7 +42,7 @@ final class CompletedSmartscraper implements BaseModel #[Api('user_prompt', optional: true)] public ?string $userPrompt; - #[Api('website_url', optional: true)] + #[Api('website_url', nullable: true, optional: true)] public ?string $websiteURL; public function __construct() @@ -56,7 +56,7 @@ public function __construct() * * You must use named parameters to construct any parameters with a default value. * - * @param Status::*|null $status + * @param Status::* $status */ public static function with( ?string $error = null, diff --git a/src/Smartscraper/FailedSmartscraper.php b/src/Smartscraper/FailedSmartscraper.php index 32380e8..5d2411d 100644 --- a/src/Smartscraper/FailedSmartscraper.php +++ b/src/Smartscraper/FailedSmartscraper.php @@ -22,7 +22,7 @@ final class FailedSmartscraper implements BaseModel #[Api('request_id', optional: true)] public ?string $requestID; - #[Api(optional: true)] + #[Api(nullable: true, optional: true)] public mixed $result; /** @var Status::*|null $status */ @@ -32,7 +32,7 @@ final class FailedSmartscraper implements BaseModel #[Api('user_prompt', optional: true)] public ?string $userPrompt; - #[Api('website_url', optional: true)] + #[Api('website_url', nullable: true, optional: true)] public ?string $websiteURL; public function __construct() @@ -46,7 +46,7 @@ public function __construct() * * You must use named parameters to construct any parameters with a default value. * - * @param Status::*|null $status + * @param Status::* $status */ public static function with( ?string $error = null, diff --git a/src/Smartscraper/SmartscraperCreateParams.php b/src/Smartscraper/SmartscraperCreateParams.php index a280ed0..44867ef 100644 --- a/src/Smartscraper/SmartscraperCreateParams.php +++ b/src/Smartscraper/SmartscraperCreateParams.php @@ -8,8 +8,6 @@ use Scrapegraphai\Core\Concerns\SdkModel; use Scrapegraphai\Core\Concerns\SdkParams; use Scrapegraphai\Core\Contracts\BaseModel; -use Scrapegraphai\Core\Conversion\ListOf; -use Scrapegraphai\Core\Conversion\MapOf; /** * Main scraping endpoint with LLM-powered content analysis. Supports various fetching providers, @@ -31,7 +29,7 @@ final class SmartscraperCreateParams implements BaseModel * * @var array|null $cookies */ - #[Api(type: new MapOf('string'), optional: true)] + #[Api(map: 'string', optional: true)] public ?array $cookies; /** @@ -39,7 +37,7 @@ final class SmartscraperCreateParams implements BaseModel * * @var array|null $headers */ - #[Api(type: new MapOf('string'), optional: true)] + #[Api(map: 'string', optional: true)] public ?array $headers; /** @@ -65,7 +63,7 @@ final class SmartscraperCreateParams implements BaseModel * * @var list|null $steps */ - #[Api(type: new ListOf('string'), optional: true)] + #[Api(list: 'string', optional: true)] public ?array $steps; /** @@ -111,9 +109,9 @@ public function __construct() * * You must use named parameters to construct any parameters with a default value. * - * @param array|null $cookies - * @param array|null $headers - * @param list|null $steps + * @param array $cookies + * @param array $headers + * @param list $steps */ public static function with( string $userPrompt, From f19a57af690dba72f269a940930da3efd35d4b2c Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Tue, 26 Aug 2025 03:30:07 +0000 Subject: [PATCH 13/44] feat(php): differentiate null and omit --- composer.json | 1 + src/Contracts/CrawlContract.php | 18 ++++---- src/Contracts/FeedbackContract.php | 4 +- src/Contracts/GenerateSchemaContract.php | 4 +- src/Contracts/MarkdownifyContract.php | 6 ++- src/Contracts/SearchscraperContract.php | 8 ++-- src/Contracts/SmartscraperContract.php | 20 +++++---- src/Core/Omit.php | 7 +++ src/Core/Omittable.php | 13 ++++++ src/Core/Util.php | 11 +---- src/Services/CrawlService.php | 49 +++++++++------------ src/Services/FeedbackService.php | 17 +++++--- src/Services/GenerateSchemaService.php | 9 ++-- src/Services/MarkdownifyService.php | 13 +++--- src/Services/SearchscraperService.php | 24 +++++------ src/Services/SmartscraperService.php | 54 ++++++++++-------------- 16 files changed, 136 insertions(+), 122 deletions(-) create mode 100644 src/Core/Omit.php create mode 100644 src/Core/Omittable.php diff --git a/composer.json b/composer.json index 17df3ea..bf2d0a8 100644 --- a/composer.json +++ b/composer.json @@ -2,6 +2,7 @@ "$schema": "https://getcomposer.org/schema.json", "autoload": { "files": [ + "src/Core/Omit.php", "src/Client.php" ], "psr-4": { diff --git a/src/Contracts/CrawlContract.php b/src/Contracts/CrawlContract.php index dc2c648..da8ee2e 100644 --- a/src/Contracts/CrawlContract.php +++ b/src/Contracts/CrawlContract.php @@ -9,6 +9,8 @@ use Scrapegraphai\Responses\Crawl\CrawlGetResultsResponse; use Scrapegraphai\Responses\Crawl\CrawlStartResponse; +use const Scrapegraphai\Core\OMIT as omit; + interface CrawlContract { public function retrieveResults( @@ -29,14 +31,14 @@ public function retrieveResults( */ public function start( $url, - $depth = null, - $extractionMode = null, - $maxPages = null, - $prompt = null, - $renderHeavyJs = null, - $rules = null, - $schema = null, - $sitemap = null, + $depth = omit, + $extractionMode = omit, + $maxPages = omit, + $prompt = omit, + $renderHeavyJs = omit, + $rules = omit, + $schema = omit, + $sitemap = omit, ?RequestOptions $requestOptions = null, ): CrawlStartResponse; } diff --git a/src/Contracts/FeedbackContract.php b/src/Contracts/FeedbackContract.php index 4008f17..71c0167 100644 --- a/src/Contracts/FeedbackContract.php +++ b/src/Contracts/FeedbackContract.php @@ -7,6 +7,8 @@ use Scrapegraphai\RequestOptions; use Scrapegraphai\Responses\Feedback\FeedbackSubmitResponse; +use const Scrapegraphai\Core\OMIT as omit; + interface FeedbackContract { /** @@ -17,7 +19,7 @@ interface FeedbackContract public function submit( $rating, $requestID, - $feedbackText = null, + $feedbackText = omit, ?RequestOptions $requestOptions = null, ): FeedbackSubmitResponse; } diff --git a/src/Contracts/GenerateSchemaContract.php b/src/Contracts/GenerateSchemaContract.php index 5970bcb..38293f1 100644 --- a/src/Contracts/GenerateSchemaContract.php +++ b/src/Contracts/GenerateSchemaContract.php @@ -9,6 +9,8 @@ use Scrapegraphai\Responses\GenerateSchema\GenerateSchemaGetResponse\FailedSchemaGenerationResponse; use Scrapegraphai\Responses\GenerateSchema\GenerateSchemaNewResponse; +use const Scrapegraphai\Core\OMIT as omit; + interface GenerateSchemaContract { /** @@ -17,7 +19,7 @@ interface GenerateSchemaContract */ public function create( $userPrompt, - $existingSchema = null, + $existingSchema = omit, ?RequestOptions $requestOptions = null, ): GenerateSchemaNewResponse; diff --git a/src/Contracts/MarkdownifyContract.php b/src/Contracts/MarkdownifyContract.php index 2cbba13..8b748ee 100644 --- a/src/Contracts/MarkdownifyContract.php +++ b/src/Contracts/MarkdownifyContract.php @@ -8,6 +8,8 @@ use Scrapegraphai\RequestOptions; use Scrapegraphai\Responses\Markdownify\MarkdownifyGetStatusResponse\FailedMarkdownifyResponse; +use const Scrapegraphai\Core\OMIT as omit; + interface MarkdownifyContract { /** @@ -17,8 +19,8 @@ interface MarkdownifyContract */ public function convert( $websiteURL, - $headers = null, - $steps = null, + $headers = omit, + $steps = omit, ?RequestOptions $requestOptions = null, ): CompletedMarkdownify; diff --git a/src/Contracts/SearchscraperContract.php b/src/Contracts/SearchscraperContract.php index 82dac5f..4f2b1fa 100644 --- a/src/Contracts/SearchscraperContract.php +++ b/src/Contracts/SearchscraperContract.php @@ -8,6 +8,8 @@ use Scrapegraphai\Responses\Searchscraper\SearchscraperGetStatusResponse\FailedSearchScraperResponse; use Scrapegraphai\Searchscraper\CompletedSearchScraper; +use const Scrapegraphai\Core\OMIT as omit; + interface SearchscraperContract { /** @@ -18,9 +20,9 @@ interface SearchscraperContract */ public function create( $userPrompt, - $headers = null, - $numResults = null, - $outputSchema = null, + $headers = omit, + $numResults = omit, + $outputSchema = omit, ?RequestOptions $requestOptions = null, ): CompletedSearchScraper; diff --git a/src/Contracts/SmartscraperContract.php b/src/Contracts/SmartscraperContract.php index 4df2546..cc8c137 100644 --- a/src/Contracts/SmartscraperContract.php +++ b/src/Contracts/SmartscraperContract.php @@ -8,6 +8,8 @@ use Scrapegraphai\Smartscraper\CompletedSmartscraper; use Scrapegraphai\Smartscraper\FailedSmartscraper; +use const Scrapegraphai\Core\OMIT as omit; + interface SmartscraperContract { /** @@ -24,15 +26,15 @@ interface SmartscraperContract */ public function create( $userPrompt, - $cookies = null, - $headers = null, - $numberOfScrolls = null, - $outputSchema = null, - $renderHeavyJs = null, - $steps = null, - $totalPages = null, - $websiteHTML = null, - $websiteURL = null, + $cookies = omit, + $headers = omit, + $numberOfScrolls = omit, + $outputSchema = omit, + $renderHeavyJs = omit, + $steps = omit, + $totalPages = omit, + $websiteHTML = omit, + $websiteURL = omit, ?RequestOptions $requestOptions = null, ): CompletedSmartscraper; diff --git a/src/Core/Omit.php b/src/Core/Omit.php new file mode 100644 index 0000000..4de928c --- /dev/null +++ b/src/Core/Omit.php @@ -0,0 +1,7 @@ + $arr - * @param list $keys * * @return array */ - public static function array_filter_null(array $arr, array $keys): array + public static function array_filter_omit(array $arr): array { - foreach ($keys as $key) { - if (array_key_exists($key, $arr) && is_null($arr[$key])) { - unset($arr[$key]); - } - } - - return $arr; + return array_filter($arr, fn ($v, $_) => OMIT !== $v, ARRAY_FILTER_USE_BOTH); } /** diff --git a/src/Services/CrawlService.php b/src/Services/CrawlService.php index 45e81d7..1ca4fe1 100644 --- a/src/Services/CrawlService.php +++ b/src/Services/CrawlService.php @@ -14,6 +14,8 @@ use Scrapegraphai\Responses\Crawl\CrawlGetResultsResponse; use Scrapegraphai\Responses\Crawl\CrawlStartResponse; +use const Scrapegraphai\Core\OMIT as omit; + final class CrawlService implements CrawlContract { public function __construct(private Client $client) {} @@ -52,38 +54,27 @@ public function retrieveResults( */ public function start( $url, - $depth = null, - $extractionMode = null, - $maxPages = null, - $prompt = null, - $renderHeavyJs = null, - $rules = null, - $schema = null, - $sitemap = null, + $depth = omit, + $extractionMode = omit, + $maxPages = omit, + $prompt = omit, + $renderHeavyJs = omit, + $rules = omit, + $schema = omit, + $sitemap = omit, ?RequestOptions $requestOptions = null, ): CrawlStartResponse { - $args = [ - 'url' => $url, - 'depth' => $depth, - 'extractionMode' => $extractionMode, - 'maxPages' => $maxPages, - 'prompt' => $prompt, - 'renderHeavyJs' => $renderHeavyJs, - 'rules' => $rules, - 'schema' => $schema, - 'sitemap' => $sitemap, - ]; - $args = Util::array_filter_null( - $args, + $args = Util::array_filter_omit( [ - 'depth', - 'extractionMode', - 'maxPages', - 'prompt', - 'renderHeavyJs', - 'rules', - 'schema', - 'sitemap', + 'url' => $url, + 'depth' => $depth, + 'extractionMode' => $extractionMode, + 'maxPages' => $maxPages, + 'prompt' => $prompt, + 'renderHeavyJs' => $renderHeavyJs, + 'rules' => $rules, + 'schema' => $schema, + 'sitemap' => $sitemap, ], ); [$parsed, $options] = CrawlStartParams::parseRequest( diff --git a/src/Services/FeedbackService.php b/src/Services/FeedbackService.php index e40f052..0ebf684 100644 --- a/src/Services/FeedbackService.php +++ b/src/Services/FeedbackService.php @@ -12,6 +12,8 @@ use Scrapegraphai\RequestOptions; use Scrapegraphai\Responses\Feedback\FeedbackSubmitResponse; +use const Scrapegraphai\Core\OMIT as omit; + final class FeedbackService implements FeedbackContract { public function __construct(private Client $client) {} @@ -26,15 +28,16 @@ public function __construct(private Client $client) {} public function submit( $rating, $requestID, - $feedbackText = null, + $feedbackText = omit, ?RequestOptions $requestOptions = null, ): FeedbackSubmitResponse { - $args = [ - 'rating' => $rating, - 'requestID' => $requestID, - 'feedbackText' => $feedbackText, - ]; - $args = Util::array_filter_null($args, ['feedbackText']); + $args = Util::array_filter_omit( + [ + 'rating' => $rating, + 'requestID' => $requestID, + 'feedbackText' => $feedbackText, + ], + ); [$parsed, $options] = FeedbackSubmitParams::parseRequest( $args, $requestOptions diff --git a/src/Services/GenerateSchemaService.php b/src/Services/GenerateSchemaService.php index b0a8b44..d7d9d20 100644 --- a/src/Services/GenerateSchemaService.php +++ b/src/Services/GenerateSchemaService.php @@ -15,6 +15,8 @@ use Scrapegraphai\Responses\GenerateSchema\GenerateSchemaGetResponse\FailedSchemaGenerationResponse; use Scrapegraphai\Responses\GenerateSchema\GenerateSchemaNewResponse; +use const Scrapegraphai\Core\OMIT as omit; + final class GenerateSchemaService implements GenerateSchemaContract { public function __construct(private Client $client) {} @@ -28,11 +30,12 @@ public function __construct(private Client $client) {} */ public function create( $userPrompt, - $existingSchema = null, + $existingSchema = omit, ?RequestOptions $requestOptions = null ): GenerateSchemaNewResponse { - $args = ['userPrompt' => $userPrompt, 'existingSchema' => $existingSchema]; - $args = Util::array_filter_null($args, ['existingSchema']); + $args = Util::array_filter_omit( + ['userPrompt' => $userPrompt, 'existingSchema' => $existingSchema] + ); [$parsed, $options] = GenerateSchemaCreateParams::parseRequest( $args, $requestOptions diff --git a/src/Services/MarkdownifyService.php b/src/Services/MarkdownifyService.php index 70d9111..5a6b869 100644 --- a/src/Services/MarkdownifyService.php +++ b/src/Services/MarkdownifyService.php @@ -14,6 +14,8 @@ use Scrapegraphai\Responses\Markdownify\MarkdownifyGetStatusResponse; use Scrapegraphai\Responses\Markdownify\MarkdownifyGetStatusResponse\FailedMarkdownifyResponse; +use const Scrapegraphai\Core\OMIT as omit; + final class MarkdownifyService implements MarkdownifyContract { public function __construct(private Client $client) {} @@ -27,14 +29,13 @@ public function __construct(private Client $client) {} */ public function convert( $websiteURL, - $headers = null, - $steps = null, + $headers = omit, + $steps = omit, ?RequestOptions $requestOptions = null, ): CompletedMarkdownify { - $args = [ - 'websiteURL' => $websiteURL, 'headers' => $headers, 'steps' => $steps, - ]; - $args = Util::array_filter_null($args, ['headers', 'steps']); + $args = Util::array_filter_omit( + ['websiteURL' => $websiteURL, 'headers' => $headers, 'steps' => $steps] + ); [$parsed, $options] = MarkdownifyConvertParams::parseRequest( $args, $requestOptions diff --git a/src/Services/SearchscraperService.php b/src/Services/SearchscraperService.php index e37ff3a..ba15ff2 100644 --- a/src/Services/SearchscraperService.php +++ b/src/Services/SearchscraperService.php @@ -14,6 +14,8 @@ use Scrapegraphai\Searchscraper\CompletedSearchScraper; use Scrapegraphai\Searchscraper\SearchscraperCreateParams; +use const Scrapegraphai\Core\OMIT as omit; + final class SearchscraperService implements SearchscraperContract { public function __construct(private Client $client) {} @@ -29,20 +31,18 @@ public function __construct(private Client $client) {} */ public function create( $userPrompt, - $headers = null, - $numResults = null, - $outputSchema = null, + $headers = omit, + $numResults = omit, + $outputSchema = omit, ?RequestOptions $requestOptions = null, ): CompletedSearchScraper { - $args = [ - 'userPrompt' => $userPrompt, - 'headers' => $headers, - 'numResults' => $numResults, - 'outputSchema' => $outputSchema, - ]; - $args = Util::array_filter_null( - $args, - ['headers', 'numResults', 'outputSchema'] + $args = Util::array_filter_omit( + [ + 'userPrompt' => $userPrompt, + 'headers' => $headers, + 'numResults' => $numResults, + 'outputSchema' => $outputSchema, + ], ); [$parsed, $options] = SearchscraperCreateParams::parseRequest( $args, diff --git a/src/Services/SmartscraperService.php b/src/Services/SmartscraperService.php index 846113a..80d7909 100644 --- a/src/Services/SmartscraperService.php +++ b/src/Services/SmartscraperService.php @@ -15,6 +15,8 @@ use Scrapegraphai\Smartscraper\FailedSmartscraper; use Scrapegraphai\Smartscraper\SmartscraperCreateParams; +use const Scrapegraphai\Core\OMIT as omit; + final class SmartscraperService implements SmartscraperContract { public function __construct(private Client $client) {} @@ -36,41 +38,29 @@ public function __construct(private Client $client) {} */ public function create( $userPrompt, - $cookies = null, - $headers = null, - $numberOfScrolls = null, - $outputSchema = null, - $renderHeavyJs = null, - $steps = null, - $totalPages = null, - $websiteHTML = null, - $websiteURL = null, + $cookies = omit, + $headers = omit, + $numberOfScrolls = omit, + $outputSchema = omit, + $renderHeavyJs = omit, + $steps = omit, + $totalPages = omit, + $websiteHTML = omit, + $websiteURL = omit, ?RequestOptions $requestOptions = null, ): CompletedSmartscraper { - $args = [ - 'userPrompt' => $userPrompt, - 'cookies' => $cookies, - 'headers' => $headers, - 'numberOfScrolls' => $numberOfScrolls, - 'outputSchema' => $outputSchema, - 'renderHeavyJs' => $renderHeavyJs, - 'steps' => $steps, - 'totalPages' => $totalPages, - 'websiteHTML' => $websiteHTML, - 'websiteURL' => $websiteURL, - ]; - $args = Util::array_filter_null( - $args, + $args = Util::array_filter_omit( [ - 'cookies', - 'headers', - 'numberOfScrolls', - 'outputSchema', - 'renderHeavyJs', - 'steps', - 'totalPages', - 'websiteHTML', - 'websiteURL', + 'userPrompt' => $userPrompt, + 'cookies' => $cookies, + 'headers' => $headers, + 'numberOfScrolls' => $numberOfScrolls, + 'outputSchema' => $outputSchema, + 'renderHeavyJs' => $renderHeavyJs, + 'steps' => $steps, + 'totalPages' => $totalPages, + 'websiteHTML' => $websiteHTML, + 'websiteURL' => $websiteURL, ], ); [$parsed, $options] = SmartscraperCreateParams::parseRequest( From 25165664e3d3c7fd4f11f72589f89baa1305eb56 Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Tue, 26 Aug 2025 03:35:37 +0000 Subject: [PATCH 14/44] fix: streaming internals --- src/Core/Attributes/Api.php | 3 ++- src/Core/Contracts/{CloseableStream.php => BaseStream.php} | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) rename src/Core/Contracts/{CloseableStream.php => BaseStream.php} (73%) diff --git a/src/Core/Attributes/Api.php b/src/Core/Attributes/Api.php index a1ec512..a24e309 100644 --- a/src/Core/Attributes/Api.php +++ b/src/Core/Attributes/Api.php @@ -23,8 +23,9 @@ final class Api /** * @param class-string|Converter|string|null $type * @param class-string|Converter|null $enum - * @param class-string|Converter|string|null $union + * @param class-string|Converter|null $union * @param class-string|Converter|string|null $list + * @param class-string|Converter|string|null $map */ public function __construct( public readonly ?string $apiName = null, diff --git a/src/Core/Contracts/CloseableStream.php b/src/Core/Contracts/BaseStream.php similarity index 73% rename from src/Core/Contracts/CloseableStream.php rename to src/Core/Contracts/BaseStream.php index c7d5635..41ddb83 100644 --- a/src/Core/Contracts/CloseableStream.php +++ b/src/Core/Contracts/BaseStream.php @@ -7,9 +7,9 @@ /** * @template TInner * - * @extends \IteratorAggregate + * @extends \IteratorAggregate */ -interface CloseableStream extends \IteratorAggregate +interface BaseStream extends \IteratorAggregate { /** * Manually force the stream to close early. From 713582082c5123e8152bbdb5896926ae58496095 Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Tue, 26 Aug 2025 03:39:45 +0000 Subject: [PATCH 15/44] feat(refactor)!: clean up pagination, errors, as well as request methods --- README.md | 2 +- composer.json | 2 +- src/{Core/Omittable.php => Core.php} | 2 + src/Core/BaseClient.php | 2 +- src/Core/Concerns/SdkParams.php | 4 +- src/Core/Contracts/BasePage.php | 21 +++++- src/{ => Core}/Errors/APIConnectionError.php | 2 +- src/{ => Core}/Errors/APIError.php | 4 +- src/{ => Core}/Errors/APIStatusError.php | 2 +- src/{ => Core}/Errors/APITimeoutError.php | 2 +- src/{ => Core}/Errors/AuthenticationError.php | 2 +- src/{ => Core}/Errors/BadRequestError.php | 2 +- src/{ => Core}/Errors/ConflictError.php | 2 +- src/{ => Core}/Errors/InternalServerError.php | 2 +- src/{ => Core}/Errors/NotFoundError.php | 2 +- .../Errors/PermissionDeniedError.php | 2 +- src/{ => Core}/Errors/RateLimitError.php | 2 +- .../Errors/ScrapegraphaiError.php} | 4 +- .../Errors/UnprocessableEntityError.php | 2 +- src/Core/Omit.php | 7 -- src/Core/Pagination/AbstractPage.php | 20 +++--- src/Core/Util.php | 72 +++++++++---------- src/Services/CrawlService.php | 8 +-- src/Services/FeedbackService.php | 8 +-- src/Services/GenerateSchemaService.php | 8 +-- src/Services/MarkdownifyService.php | 8 +-- src/Services/SearchscraperService.php | 8 +-- src/Services/SmartscraperService.php | 8 +-- 28 files changed, 100 insertions(+), 110 deletions(-) rename src/{Core/Omittable.php => Core.php} (79%) rename src/{ => Core}/Errors/APIConnectionError.php (79%) rename src/{ => Core}/Errors/APIError.php (84%) rename src/{ => Core}/Errors/APIStatusError.php (98%) rename src/{ => Core}/Errors/APITimeoutError.php (92%) rename src/{ => Core}/Errors/AuthenticationError.php (80%) rename src/{ => Core}/Errors/BadRequestError.php (79%) rename src/{ => Core}/Errors/ConflictError.php (78%) rename src/{ => Core}/Errors/InternalServerError.php (80%) rename src/{ => Core}/Errors/NotFoundError.php (78%) rename src/{ => Core}/Errors/PermissionDeniedError.php (80%) rename src/{ => Core}/Errors/RateLimitError.php (79%) rename src/{Errors/Error.php => Core/Errors/ScrapegraphaiError.php} (76%) rename src/{ => Core}/Errors/UnprocessableEntityError.php (81%) delete mode 100644 src/Core/Omit.php diff --git a/README.md b/README.md index 8a49fb1..f833e98 100644 --- a/README.md +++ b/README.md @@ -72,7 +72,7 @@ When the library is unable to connect to the API, or if the API returns a non-su ```php smartscraper->create( diff --git a/composer.json b/composer.json index bf2d0a8..056aa80 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "$schema": "https://getcomposer.org/schema.json", "autoload": { "files": [ - "src/Core/Omit.php", + "src/Core.php", "src/Client.php" ], "psr-4": { diff --git a/src/Core/Omittable.php b/src/Core.php similarity index 79% rename from src/Core/Omittable.php rename to src/Core.php index fd201b8..441d490 100644 --- a/src/Core/Omittable.php +++ b/src/Core.php @@ -11,3 +11,5 @@ enum Omittable { case OMIT; } + +const OMIT = Omittable::OMIT; diff --git a/src/Core/BaseClient.php b/src/Core/BaseClient.php index 29e5b92..c1473a3 100644 --- a/src/Core/BaseClient.php +++ b/src/Core/BaseClient.php @@ -13,7 +13,7 @@ use Psr\Http\Message\StreamFactoryInterface; use Psr\Http\Message\UriFactoryInterface; use Psr\Http\Message\UriInterface; -use Scrapegraphai\Errors\APIStatusError; +use Scrapegraphai\Core\Errors\APIStatusError; use Scrapegraphai\RequestOptions; class BaseClient diff --git a/src/Core/Concerns/SdkParams.php b/src/Core/Concerns/SdkParams.php index 256f357..b75e3a7 100644 --- a/src/Core/Concerns/SdkParams.php +++ b/src/Core/Concerns/SdkParams.php @@ -6,6 +6,7 @@ use Scrapegraphai\Core\Conversion; use Scrapegraphai\Core\Conversion\DumpState; +use Scrapegraphai\Core\Util; use Scrapegraphai\RequestOptions; /** @@ -29,9 +30,10 @@ trait SdkParams */ public static function parseRequest(array|self|null $params, array|RequestOptions|null $options): array { + $value = is_array($params) ? Util::array_filter_omit($params) : $params; $converter = self::converter(); $state = new DumpState; - $dumped = (array) Conversion::dump($converter, value: $params, state: $state); + $dumped = (array) Conversion::dump($converter, value: $value, state: $state); $opts = RequestOptions::parse($options); // @phpstan-ignore-line if (!$state->canRetry) { diff --git a/src/Core/Contracts/BasePage.php b/src/Core/Contracts/BasePage.php index 3032525..c0d3d9f 100644 --- a/src/Core/Contracts/BasePage.php +++ b/src/Core/Contracts/BasePage.php @@ -9,14 +9,31 @@ use Scrapegraphai\Core\Pagination\PageRequestOptions; /** - * @internal + * @template Item + * + * @extends \IteratorAggregate */ -interface BasePage +interface BasePage extends \IteratorAggregate { + /** + * @internal + */ public function __construct( BaseClient $client, PageRequestOptions $options, ResponseInterface $response, mixed $body, ); + + public function hasNextPage(): bool; + + /** + * @return list + */ + public function getPaginatedItems(): array; + + /** + * @return static + */ + public function getNextPage(): static; } diff --git a/src/Errors/APIConnectionError.php b/src/Core/Errors/APIConnectionError.php similarity index 79% rename from src/Errors/APIConnectionError.php rename to src/Core/Errors/APIConnectionError.php index 35e1581..13539ee 100644 --- a/src/Errors/APIConnectionError.php +++ b/src/Core/Errors/APIConnectionError.php @@ -1,6 +1,6 @@ + * @implements BasePage */ -abstract class AbstractPage implements \IteratorAggregate, BasePage +abstract class AbstractPage implements BasePage { public function __construct( protected BaseClient $client, @@ -25,8 +25,6 @@ public function __construct( protected mixed $body, ) {} - abstract public function nextPageRequestOptions(): ?PageRequestOptions; - /** * @return list */ @@ -49,13 +47,13 @@ public function hasNextPage(): bool * * @return static of AbstractPage * - * @throws Error + * @throws APIStatusError */ public function getNextPage(): static { $nextOptions = $this->nextPageRequestOptions(); if (!$nextOptions) { - throw new Error( + throw new \RuntimeException( 'No next page expected; please check `.hasNextPage()` before calling `.getNextPage()`.' ); } @@ -78,7 +76,7 @@ public function getNextPage(): static * * @return \Generator */ - public function iterPages(): \Generator + public function getIterator(): \Generator { $page = $this; @@ -95,12 +93,14 @@ public function iterPages(): \Generator * * @return \Generator */ - public function getIterator(): \Generator + public function pagingEachItem(): \Generator { - foreach ($this->iterPages() as $page) { + foreach ($this as $page) { foreach ($page->getPaginatedItems() as $item) { yield $item; } } } + + abstract protected function nextPageRequestOptions(): ?PageRequestOptions; } diff --git a/src/Core/Util.php b/src/Core/Util.php index 1bac668..51576c5 100644 --- a/src/Core/Util.php +++ b/src/Core/Util.php @@ -164,42 +164,6 @@ public static function streamIterator(StreamInterface $stream): \Iterator } } - /** - * @param bool|int|float|string|resource|\Traversable|array|null $body - * - * @return array{string, \Generator} - */ - public static function encodeMultipartStreaming(mixed $body): array - { - $boundary = rtrim(strtr(base64_encode(random_bytes(60)), '+/', '-_'), '='); - $gen = (function () use ($boundary, $body) { - $closing = []; - - try { - if (is_array($body) || is_object($body)) { - foreach ((array) $body as $key => $val) { - foreach (static::writeMultipartChunk(boundary: $boundary, key: $key, val: $val, closing: $closing) as $chunk) { - yield $chunk; - } - } - } else { - foreach (static::writeMultipartChunk(boundary: $boundary, key: null, val: $body, closing: $closing) as $chunk) { - yield $chunk; - } - } - - yield "--{$boundary}--\r\n"; - } finally { - foreach ($closing as $c) { - $c(); - } - } - })(); - - return [$boundary, $gen]; - } - /** * @param bool|int|float|string|resource|\Traversable|array|null $body @@ -415,4 +379,40 @@ private static function writeMultipartChunk( yield $chunk; } } + + /** + * @param bool|int|float|string|resource|\Traversable|array|null $body + * + * @return array{string, \Generator} + */ + private static function encodeMultipartStreaming(mixed $body): array + { + $boundary = rtrim(strtr(base64_encode(random_bytes(60)), '+/', '-_'), '='); + $gen = (function () use ($boundary, $body) { + $closing = []; + + try { + if (is_array($body) || is_object($body)) { + foreach ((array) $body as $key => $val) { + foreach (static::writeMultipartChunk(boundary: $boundary, key: $key, val: $val, closing: $closing) as $chunk) { + yield $chunk; + } + } + } else { + foreach (static::writeMultipartChunk(boundary: $boundary, key: null, val: $body, closing: $closing) as $chunk) { + yield $chunk; + } + } + + yield "--{$boundary}--\r\n"; + } finally { + foreach ($closing as $c) { + $c(); + } + } + })(); + + return [$boundary, $gen]; + } } diff --git a/src/Services/CrawlService.php b/src/Services/CrawlService.php index 1ca4fe1..9e96c5a 100644 --- a/src/Services/CrawlService.php +++ b/src/Services/CrawlService.php @@ -7,7 +7,6 @@ use Scrapegraphai\Client; use Scrapegraphai\Contracts\CrawlContract; use Scrapegraphai\Core\Conversion; -use Scrapegraphai\Core\Util; use Scrapegraphai\Crawl\CrawlStartParams; use Scrapegraphai\Crawl\CrawlStartParams\Rules; use Scrapegraphai\RequestOptions; @@ -64,7 +63,7 @@ public function start( $sitemap = omit, ?RequestOptions $requestOptions = null, ): CrawlStartResponse { - $args = Util::array_filter_omit( + [$parsed, $options] = CrawlStartParams::parseRequest( [ 'url' => $url, 'depth' => $depth, @@ -76,10 +75,7 @@ public function start( 'schema' => $schema, 'sitemap' => $sitemap, ], - ); - [$parsed, $options] = CrawlStartParams::parseRequest( - $args, - $requestOptions + $requestOptions, ); $resp = $this->client->request( method: 'post', diff --git a/src/Services/FeedbackService.php b/src/Services/FeedbackService.php index 0ebf684..4039b7d 100644 --- a/src/Services/FeedbackService.php +++ b/src/Services/FeedbackService.php @@ -7,7 +7,6 @@ use Scrapegraphai\Client; use Scrapegraphai\Contracts\FeedbackContract; use Scrapegraphai\Core\Conversion; -use Scrapegraphai\Core\Util; use Scrapegraphai\Feedback\FeedbackSubmitParams; use Scrapegraphai\RequestOptions; use Scrapegraphai\Responses\Feedback\FeedbackSubmitResponse; @@ -31,16 +30,13 @@ public function submit( $feedbackText = omit, ?RequestOptions $requestOptions = null, ): FeedbackSubmitResponse { - $args = Util::array_filter_omit( + [$parsed, $options] = FeedbackSubmitParams::parseRequest( [ 'rating' => $rating, 'requestID' => $requestID, 'feedbackText' => $feedbackText, ], - ); - [$parsed, $options] = FeedbackSubmitParams::parseRequest( - $args, - $requestOptions + $requestOptions, ); $resp = $this->client->request( method: 'post', diff --git a/src/Services/GenerateSchemaService.php b/src/Services/GenerateSchemaService.php index d7d9d20..4d6a29e 100644 --- a/src/Services/GenerateSchemaService.php +++ b/src/Services/GenerateSchemaService.php @@ -7,7 +7,6 @@ use Scrapegraphai\Client; use Scrapegraphai\Contracts\GenerateSchemaContract; use Scrapegraphai\Core\Conversion; -use Scrapegraphai\Core\Util; use Scrapegraphai\GenerateSchema\GenerateSchemaCreateParams; use Scrapegraphai\RequestOptions; use Scrapegraphai\Responses\GenerateSchema\GenerateSchemaGetResponse; @@ -33,12 +32,9 @@ public function create( $existingSchema = omit, ?RequestOptions $requestOptions = null ): GenerateSchemaNewResponse { - $args = Util::array_filter_omit( - ['userPrompt' => $userPrompt, 'existingSchema' => $existingSchema] - ); [$parsed, $options] = GenerateSchemaCreateParams::parseRequest( - $args, - $requestOptions + ['userPrompt' => $userPrompt, 'existingSchema' => $existingSchema], + $requestOptions, ); $resp = $this->client->request( method: 'post', diff --git a/src/Services/MarkdownifyService.php b/src/Services/MarkdownifyService.php index 5a6b869..301cf27 100644 --- a/src/Services/MarkdownifyService.php +++ b/src/Services/MarkdownifyService.php @@ -7,7 +7,6 @@ use Scrapegraphai\Client; use Scrapegraphai\Contracts\MarkdownifyContract; use Scrapegraphai\Core\Conversion; -use Scrapegraphai\Core\Util; use Scrapegraphai\Markdownify\CompletedMarkdownify; use Scrapegraphai\Markdownify\MarkdownifyConvertParams; use Scrapegraphai\RequestOptions; @@ -33,12 +32,9 @@ public function convert( $steps = omit, ?RequestOptions $requestOptions = null, ): CompletedMarkdownify { - $args = Util::array_filter_omit( - ['websiteURL' => $websiteURL, 'headers' => $headers, 'steps' => $steps] - ); [$parsed, $options] = MarkdownifyConvertParams::parseRequest( - $args, - $requestOptions + ['websiteURL' => $websiteURL, 'headers' => $headers, 'steps' => $steps], + $requestOptions, ); $resp = $this->client->request( method: 'post', diff --git a/src/Services/SearchscraperService.php b/src/Services/SearchscraperService.php index ba15ff2..a6270c8 100644 --- a/src/Services/SearchscraperService.php +++ b/src/Services/SearchscraperService.php @@ -7,7 +7,6 @@ use Scrapegraphai\Client; use Scrapegraphai\Contracts\SearchscraperContract; use Scrapegraphai\Core\Conversion; -use Scrapegraphai\Core\Util; use Scrapegraphai\RequestOptions; use Scrapegraphai\Responses\Searchscraper\SearchscraperGetStatusResponse; use Scrapegraphai\Responses\Searchscraper\SearchscraperGetStatusResponse\FailedSearchScraperResponse; @@ -36,17 +35,14 @@ public function create( $outputSchema = omit, ?RequestOptions $requestOptions = null, ): CompletedSearchScraper { - $args = Util::array_filter_omit( + [$parsed, $options] = SearchscraperCreateParams::parseRequest( [ 'userPrompt' => $userPrompt, 'headers' => $headers, 'numResults' => $numResults, 'outputSchema' => $outputSchema, ], - ); - [$parsed, $options] = SearchscraperCreateParams::parseRequest( - $args, - $requestOptions + $requestOptions, ); $resp = $this->client->request( method: 'post', diff --git a/src/Services/SmartscraperService.php b/src/Services/SmartscraperService.php index 80d7909..d2de744 100644 --- a/src/Services/SmartscraperService.php +++ b/src/Services/SmartscraperService.php @@ -7,7 +7,6 @@ use Scrapegraphai\Client; use Scrapegraphai\Contracts\SmartscraperContract; use Scrapegraphai\Core\Conversion; -use Scrapegraphai\Core\Util; use Scrapegraphai\RequestOptions; use Scrapegraphai\Responses\Smartscraper\SmartscraperGetResponse; use Scrapegraphai\Responses\Smartscraper\SmartscraperListResponse; @@ -49,7 +48,7 @@ public function create( $websiteURL = omit, ?RequestOptions $requestOptions = null, ): CompletedSmartscraper { - $args = Util::array_filter_omit( + [$parsed, $options] = SmartscraperCreateParams::parseRequest( [ 'userPrompt' => $userPrompt, 'cookies' => $cookies, @@ -62,10 +61,7 @@ public function create( 'websiteHTML' => $websiteHTML, 'websiteURL' => $websiteURL, ], - ); - [$parsed, $options] = SmartscraperCreateParams::parseRequest( - $args, - $requestOptions + $requestOptions, ); $resp = $this->client->request( method: 'post', From 8c75ed0c279a15e9051ac891e69a5fa2c9e7be30 Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Tue, 26 Aug 2025 03:42:16 +0000 Subject: [PATCH 16/44] feat(refactor)!: namespacing cleanup --- src/Client.php | 18 +++++++++--------- .../ServiceContracts}/CrawlContract.php | 6 +++--- .../ServiceContracts}/CreditsContract.php | 4 ++-- .../ServiceContracts}/FeedbackContract.php | 4 ++-- .../GenerateSchemaContract.php | 8 ++++---- .../ServiceContracts}/HealthzContract.php | 4 ++-- .../ServiceContracts}/MarkdownifyContract.php | 4 ++-- .../SearchscraperContract.php | 4 ++-- .../ServiceContracts}/SmartscraperContract.php | 2 +- .../ServiceContracts}/ValidateContract.php | 4 ++-- src/{ => Core}/Services/CrawlService.php | 8 ++++---- src/{ => Core}/Services/CreditsService.php | 6 +++--- src/{ => Core}/Services/FeedbackService.php | 6 +++--- .../Services/GenerateSchemaService.php | 12 ++++++------ src/{ => Core}/Services/HealthzService.php | 6 +++--- src/{ => Core}/Services/MarkdownifyService.php | 8 ++++---- .../Services/SearchscraperService.php | 8 ++++---- .../Services/SmartscraperService.php | 8 ++++---- src/{ => Core}/Services/ValidateService.php | 6 +++--- .../Crawl/CrawlGetResultsResponse.php | 6 +++--- .../Crawl/CrawlGetResultsResponse/Result.php | 2 +- .../Crawl/CrawlGetResultsResponse/Status.php | 2 +- .../Crawl/CrawlStartResponse.php | 2 +- .../Credits/CreditGetResponse.php | 2 +- .../Feedback/FeedbackSubmitResponse.php | 2 +- .../GenerateSchemaGetResponse.php | 6 +++--- .../CompletedSchemaGenerationResponse.php | 4 ++-- .../Status.php | 2 +- .../FailedSchemaGenerationResponse.php | 4 ++-- .../FailedSchemaGenerationResponse}/Status.php | 2 +- .../GenerateSchemaNewResponse.php | 4 ++-- .../GenerateSchemaNewResponse/Status.php | 2 +- .../Healthz/HealthzCheckResponse.php | 2 +- .../MarkdownifyGetStatusResponse.php | 5 ++--- .../FailedMarkdownifyResponse.php | 4 ++-- .../FailedMarkdownifyResponse}/Status.php | 2 +- .../SearchscraperGetStatusResponse.php | 5 ++--- .../FailedSearchScraperResponse.php | 4 ++-- .../FailedSearchScraperResponse/Status.php | 2 +- .../Smartscraper/SmartscraperGetResponse.php | 4 +--- .../Smartscraper/SmartscraperListResponse.php | 4 +--- .../Validate/ValidateAPIKeyResponse.php | 2 +- tests/{Resources => Services}/CrawlTest.php | 2 +- tests/{Resources => Services}/CreditsTest.php | 2 +- tests/{Resources => Services}/FeedbackTest.php | 2 +- .../GenerateSchemaTest.php | 2 +- tests/{Resources => Services}/HealthzTest.php | 2 +- .../MarkdownifyTest.php | 2 +- .../SearchscraperTest.php | 2 +- .../SmartscraperTest.php | 2 +- tests/{Resources => Services}/ValidateTest.php | 2 +- 51 files changed, 106 insertions(+), 112 deletions(-) rename src/{Contracts => Core/ServiceContracts}/CrawlContract.php (89%) rename src/{Contracts => Core/ServiceContracts}/CreditsContract.php (68%) rename src/{Contracts => Core/ServiceContracts}/FeedbackContract.php (83%) rename src/{Contracts => Core/ServiceContracts}/GenerateSchemaContract.php (68%) rename src/{Contracts => Core/ServiceContracts}/HealthzContract.php (68%) rename src/{Contracts => Core/ServiceContracts}/MarkdownifyContract.php (84%) rename src/{Contracts => Core/ServiceContracts}/SearchscraperContract.php (86%) rename src/{Contracts => Core/ServiceContracts}/SmartscraperContract.php (97%) rename src/{Contracts => Core/ServiceContracts}/ValidateContract.php (67%) rename src/{ => Core}/Services/CrawlService.php (93%) rename src/{ => Core}/Services/CreditsService.php (83%) rename src/{ => Core}/Services/FeedbackService.php (89%) rename src/{ => Core}/Services/GenerateSchemaService.php (81%) rename src/{ => Core}/Services/HealthzService.php (82%) rename src/{ => Core}/Services/MarkdownifyService.php (88%) rename src/{ => Core}/Services/SearchscraperService.php (89%) rename src/{ => Core}/Services/SmartscraperService.php (94%) rename src/{ => Core}/Services/ValidateService.php (82%) rename src/{Responses => }/Crawl/CrawlGetResultsResponse.php (93%) rename src/{Responses => }/Crawl/CrawlGetResultsResponse/Result.php (88%) rename src/{Responses => }/Crawl/CrawlGetResultsResponse/Status.php (86%) rename src/{Responses => }/Crawl/CrawlStartResponse.php (96%) rename src/{Responses => }/Credits/CreditGetResponse.php (97%) rename src/{Responses => }/Feedback/FeedbackSubmitResponse.php (97%) rename src/{Responses => }/GenerateSchema/GenerateSchemaGetResponse.php (70%) rename src/{Responses => }/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse.php (93%) rename src/{Responses => }/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse/Status.php (69%) rename src/{Responses => }/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse.php (93%) rename src/{Responses/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse => GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse}/Status.php (70%) rename src/{Responses => }/GenerateSchema/GenerateSchemaNewResponse.php (95%) rename src/{Responses => }/GenerateSchema/GenerateSchemaNewResponse/Status.php (76%) rename src/{Responses => }/Healthz/HealthzCheckResponse.php (97%) rename src/{Responses => }/Markdownify/MarkdownifyGetStatusResponse.php (73%) rename src/{Responses => }/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse.php (92%) rename src/{Responses/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse => Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse}/Status.php (69%) rename src/{Responses => }/Searchscraper/SearchscraperGetStatusResponse.php (72%) rename src/{Responses => }/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse.php (94%) rename src/{Responses => }/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse/Status.php (69%) rename src/{Responses => }/Smartscraper/SmartscraperGetResponse.php (77%) rename src/{Responses => }/Smartscraper/SmartscraperListResponse.php (77%) rename src/{Responses => }/Validate/ValidateAPIKeyResponse.php (95%) rename tests/{Resources => Services}/CrawlTest.php (98%) rename tests/{Resources => Services}/CreditsTest.php (96%) rename tests/{Resources => Services}/FeedbackTest.php (98%) rename tests/{Resources => Services}/GenerateSchemaTest.php (98%) rename tests/{Resources => Services}/HealthzTest.php (96%) rename tests/{Resources => Services}/MarkdownifyTest.php (98%) rename tests/{Resources => Services}/SearchscraperTest.php (98%) rename tests/{Resources => Services}/SmartscraperTest.php (98%) rename tests/{Resources => Services}/ValidateTest.php (96%) diff --git a/src/Client.php b/src/Client.php index 410fd16..637fa6c 100644 --- a/src/Client.php +++ b/src/Client.php @@ -5,15 +5,15 @@ namespace Scrapegraphai; use Scrapegraphai\Core\BaseClient; -use Scrapegraphai\Services\CrawlService; -use Scrapegraphai\Services\CreditsService; -use Scrapegraphai\Services\FeedbackService; -use Scrapegraphai\Services\GenerateSchemaService; -use Scrapegraphai\Services\HealthzService; -use Scrapegraphai\Services\MarkdownifyService; -use Scrapegraphai\Services\SearchscraperService; -use Scrapegraphai\Services\SmartscraperService; -use Scrapegraphai\Services\ValidateService; +use Scrapegraphai\Core\Services\CrawlService; +use Scrapegraphai\Core\Services\CreditsService; +use Scrapegraphai\Core\Services\FeedbackService; +use Scrapegraphai\Core\Services\GenerateSchemaService; +use Scrapegraphai\Core\Services\HealthzService; +use Scrapegraphai\Core\Services\MarkdownifyService; +use Scrapegraphai\Core\Services\SearchscraperService; +use Scrapegraphai\Core\Services\SmartscraperService; +use Scrapegraphai\Core\Services\ValidateService; class Client extends BaseClient { diff --git a/src/Contracts/CrawlContract.php b/src/Core/ServiceContracts/CrawlContract.php similarity index 89% rename from src/Contracts/CrawlContract.php rename to src/Core/ServiceContracts/CrawlContract.php index da8ee2e..fb071c1 100644 --- a/src/Contracts/CrawlContract.php +++ b/src/Core/ServiceContracts/CrawlContract.php @@ -2,12 +2,12 @@ declare(strict_types=1); -namespace Scrapegraphai\Contracts; +namespace Scrapegraphai\Core\ServiceContracts; +use Scrapegraphai\Crawl\CrawlGetResultsResponse; use Scrapegraphai\Crawl\CrawlStartParams\Rules; +use Scrapegraphai\Crawl\CrawlStartResponse; use Scrapegraphai\RequestOptions; -use Scrapegraphai\Responses\Crawl\CrawlGetResultsResponse; -use Scrapegraphai\Responses\Crawl\CrawlStartResponse; use const Scrapegraphai\Core\OMIT as omit; diff --git a/src/Contracts/CreditsContract.php b/src/Core/ServiceContracts/CreditsContract.php similarity index 68% rename from src/Contracts/CreditsContract.php rename to src/Core/ServiceContracts/CreditsContract.php index 39219ae..8c5d8ad 100644 --- a/src/Contracts/CreditsContract.php +++ b/src/Core/ServiceContracts/CreditsContract.php @@ -2,10 +2,10 @@ declare(strict_types=1); -namespace Scrapegraphai\Contracts; +namespace Scrapegraphai\Core\ServiceContracts; +use Scrapegraphai\Credits\CreditGetResponse; use Scrapegraphai\RequestOptions; -use Scrapegraphai\Responses\Credits\CreditGetResponse; interface CreditsContract { diff --git a/src/Contracts/FeedbackContract.php b/src/Core/ServiceContracts/FeedbackContract.php similarity index 83% rename from src/Contracts/FeedbackContract.php rename to src/Core/ServiceContracts/FeedbackContract.php index 71c0167..5ce3f88 100644 --- a/src/Contracts/FeedbackContract.php +++ b/src/Core/ServiceContracts/FeedbackContract.php @@ -2,10 +2,10 @@ declare(strict_types=1); -namespace Scrapegraphai\Contracts; +namespace Scrapegraphai\Core\ServiceContracts; +use Scrapegraphai\Feedback\FeedbackSubmitResponse; use Scrapegraphai\RequestOptions; -use Scrapegraphai\Responses\Feedback\FeedbackSubmitResponse; use const Scrapegraphai\Core\OMIT as omit; diff --git a/src/Contracts/GenerateSchemaContract.php b/src/Core/ServiceContracts/GenerateSchemaContract.php similarity index 68% rename from src/Contracts/GenerateSchemaContract.php rename to src/Core/ServiceContracts/GenerateSchemaContract.php index 38293f1..cd83760 100644 --- a/src/Contracts/GenerateSchemaContract.php +++ b/src/Core/ServiceContracts/GenerateSchemaContract.php @@ -2,12 +2,12 @@ declare(strict_types=1); -namespace Scrapegraphai\Contracts; +namespace Scrapegraphai\Core\ServiceContracts; +use Scrapegraphai\GenerateSchema\GenerateSchemaGetResponse\CompletedSchemaGenerationResponse; +use Scrapegraphai\GenerateSchema\GenerateSchemaGetResponse\FailedSchemaGenerationResponse; +use Scrapegraphai\GenerateSchema\GenerateSchemaNewResponse; use Scrapegraphai\RequestOptions; -use Scrapegraphai\Responses\GenerateSchema\GenerateSchemaGetResponse\CompletedSchemaGenerationResponse; -use Scrapegraphai\Responses\GenerateSchema\GenerateSchemaGetResponse\FailedSchemaGenerationResponse; -use Scrapegraphai\Responses\GenerateSchema\GenerateSchemaNewResponse; use const Scrapegraphai\Core\OMIT as omit; diff --git a/src/Contracts/HealthzContract.php b/src/Core/ServiceContracts/HealthzContract.php similarity index 68% rename from src/Contracts/HealthzContract.php rename to src/Core/ServiceContracts/HealthzContract.php index 8b8f1f0..9db899e 100644 --- a/src/Contracts/HealthzContract.php +++ b/src/Core/ServiceContracts/HealthzContract.php @@ -2,10 +2,10 @@ declare(strict_types=1); -namespace Scrapegraphai\Contracts; +namespace Scrapegraphai\Core\ServiceContracts; +use Scrapegraphai\Healthz\HealthzCheckResponse; use Scrapegraphai\RequestOptions; -use Scrapegraphai\Responses\Healthz\HealthzCheckResponse; interface HealthzContract { diff --git a/src/Contracts/MarkdownifyContract.php b/src/Core/ServiceContracts/MarkdownifyContract.php similarity index 84% rename from src/Contracts/MarkdownifyContract.php rename to src/Core/ServiceContracts/MarkdownifyContract.php index 8b748ee..ca08fda 100644 --- a/src/Contracts/MarkdownifyContract.php +++ b/src/Core/ServiceContracts/MarkdownifyContract.php @@ -2,11 +2,11 @@ declare(strict_types=1); -namespace Scrapegraphai\Contracts; +namespace Scrapegraphai\Core\ServiceContracts; use Scrapegraphai\Markdownify\CompletedMarkdownify; +use Scrapegraphai\Markdownify\MarkdownifyGetStatusResponse\FailedMarkdownifyResponse; use Scrapegraphai\RequestOptions; -use Scrapegraphai\Responses\Markdownify\MarkdownifyGetStatusResponse\FailedMarkdownifyResponse; use const Scrapegraphai\Core\OMIT as omit; diff --git a/src/Contracts/SearchscraperContract.php b/src/Core/ServiceContracts/SearchscraperContract.php similarity index 86% rename from src/Contracts/SearchscraperContract.php rename to src/Core/ServiceContracts/SearchscraperContract.php index 4f2b1fa..871933a 100644 --- a/src/Contracts/SearchscraperContract.php +++ b/src/Core/ServiceContracts/SearchscraperContract.php @@ -2,11 +2,11 @@ declare(strict_types=1); -namespace Scrapegraphai\Contracts; +namespace Scrapegraphai\Core\ServiceContracts; use Scrapegraphai\RequestOptions; -use Scrapegraphai\Responses\Searchscraper\SearchscraperGetStatusResponse\FailedSearchScraperResponse; use Scrapegraphai\Searchscraper\CompletedSearchScraper; +use Scrapegraphai\Searchscraper\SearchscraperGetStatusResponse\FailedSearchScraperResponse; use const Scrapegraphai\Core\OMIT as omit; diff --git a/src/Contracts/SmartscraperContract.php b/src/Core/ServiceContracts/SmartscraperContract.php similarity index 97% rename from src/Contracts/SmartscraperContract.php rename to src/Core/ServiceContracts/SmartscraperContract.php index cc8c137..3effb72 100644 --- a/src/Contracts/SmartscraperContract.php +++ b/src/Core/ServiceContracts/SmartscraperContract.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Scrapegraphai\Contracts; +namespace Scrapegraphai\Core\ServiceContracts; use Scrapegraphai\RequestOptions; use Scrapegraphai\Smartscraper\CompletedSmartscraper; diff --git a/src/Contracts/ValidateContract.php b/src/Core/ServiceContracts/ValidateContract.php similarity index 67% rename from src/Contracts/ValidateContract.php rename to src/Core/ServiceContracts/ValidateContract.php index 3be846f..48c6742 100644 --- a/src/Contracts/ValidateContract.php +++ b/src/Core/ServiceContracts/ValidateContract.php @@ -2,10 +2,10 @@ declare(strict_types=1); -namespace Scrapegraphai\Contracts; +namespace Scrapegraphai\Core\ServiceContracts; use Scrapegraphai\RequestOptions; -use Scrapegraphai\Responses\Validate\ValidateAPIKeyResponse; +use Scrapegraphai\Validate\ValidateAPIKeyResponse; interface ValidateContract { diff --git a/src/Services/CrawlService.php b/src/Core/Services/CrawlService.php similarity index 93% rename from src/Services/CrawlService.php rename to src/Core/Services/CrawlService.php index 9e96c5a..844d0bf 100644 --- a/src/Services/CrawlService.php +++ b/src/Core/Services/CrawlService.php @@ -2,16 +2,16 @@ declare(strict_types=1); -namespace Scrapegraphai\Services; +namespace Scrapegraphai\Core\Services; use Scrapegraphai\Client; -use Scrapegraphai\Contracts\CrawlContract; use Scrapegraphai\Core\Conversion; +use Scrapegraphai\Core\ServiceContracts\CrawlContract; +use Scrapegraphai\Crawl\CrawlGetResultsResponse; use Scrapegraphai\Crawl\CrawlStartParams; use Scrapegraphai\Crawl\CrawlStartParams\Rules; +use Scrapegraphai\Crawl\CrawlStartResponse; use Scrapegraphai\RequestOptions; -use Scrapegraphai\Responses\Crawl\CrawlGetResultsResponse; -use Scrapegraphai\Responses\Crawl\CrawlStartResponse; use const Scrapegraphai\Core\OMIT as omit; diff --git a/src/Services/CreditsService.php b/src/Core/Services/CreditsService.php similarity index 83% rename from src/Services/CreditsService.php rename to src/Core/Services/CreditsService.php index b52d91f..f6735b6 100644 --- a/src/Services/CreditsService.php +++ b/src/Core/Services/CreditsService.php @@ -2,13 +2,13 @@ declare(strict_types=1); -namespace Scrapegraphai\Services; +namespace Scrapegraphai\Core\Services; use Scrapegraphai\Client; -use Scrapegraphai\Contracts\CreditsContract; use Scrapegraphai\Core\Conversion; +use Scrapegraphai\Core\ServiceContracts\CreditsContract; +use Scrapegraphai\Credits\CreditGetResponse; use Scrapegraphai\RequestOptions; -use Scrapegraphai\Responses\Credits\CreditGetResponse; final class CreditsService implements CreditsContract { diff --git a/src/Services/FeedbackService.php b/src/Core/Services/FeedbackService.php similarity index 89% rename from src/Services/FeedbackService.php rename to src/Core/Services/FeedbackService.php index 4039b7d..dcda1da 100644 --- a/src/Services/FeedbackService.php +++ b/src/Core/Services/FeedbackService.php @@ -2,14 +2,14 @@ declare(strict_types=1); -namespace Scrapegraphai\Services; +namespace Scrapegraphai\Core\Services; use Scrapegraphai\Client; -use Scrapegraphai\Contracts\FeedbackContract; use Scrapegraphai\Core\Conversion; +use Scrapegraphai\Core\ServiceContracts\FeedbackContract; use Scrapegraphai\Feedback\FeedbackSubmitParams; +use Scrapegraphai\Feedback\FeedbackSubmitResponse; use Scrapegraphai\RequestOptions; -use Scrapegraphai\Responses\Feedback\FeedbackSubmitResponse; use const Scrapegraphai\Core\OMIT as omit; diff --git a/src/Services/GenerateSchemaService.php b/src/Core/Services/GenerateSchemaService.php similarity index 81% rename from src/Services/GenerateSchemaService.php rename to src/Core/Services/GenerateSchemaService.php index 4d6a29e..dc15807 100644 --- a/src/Services/GenerateSchemaService.php +++ b/src/Core/Services/GenerateSchemaService.php @@ -2,17 +2,17 @@ declare(strict_types=1); -namespace Scrapegraphai\Services; +namespace Scrapegraphai\Core\Services; use Scrapegraphai\Client; -use Scrapegraphai\Contracts\GenerateSchemaContract; use Scrapegraphai\Core\Conversion; +use Scrapegraphai\Core\ServiceContracts\GenerateSchemaContract; use Scrapegraphai\GenerateSchema\GenerateSchemaCreateParams; +use Scrapegraphai\GenerateSchema\GenerateSchemaGetResponse; +use Scrapegraphai\GenerateSchema\GenerateSchemaGetResponse\CompletedSchemaGenerationResponse; +use Scrapegraphai\GenerateSchema\GenerateSchemaGetResponse\FailedSchemaGenerationResponse; +use Scrapegraphai\GenerateSchema\GenerateSchemaNewResponse; use Scrapegraphai\RequestOptions; -use Scrapegraphai\Responses\GenerateSchema\GenerateSchemaGetResponse; -use Scrapegraphai\Responses\GenerateSchema\GenerateSchemaGetResponse\CompletedSchemaGenerationResponse; -use Scrapegraphai\Responses\GenerateSchema\GenerateSchemaGetResponse\FailedSchemaGenerationResponse; -use Scrapegraphai\Responses\GenerateSchema\GenerateSchemaNewResponse; use const Scrapegraphai\Core\OMIT as omit; diff --git a/src/Services/HealthzService.php b/src/Core/Services/HealthzService.php similarity index 82% rename from src/Services/HealthzService.php rename to src/Core/Services/HealthzService.php index 4d7f3ab..03a6ccd 100644 --- a/src/Services/HealthzService.php +++ b/src/Core/Services/HealthzService.php @@ -2,13 +2,13 @@ declare(strict_types=1); -namespace Scrapegraphai\Services; +namespace Scrapegraphai\Core\Services; use Scrapegraphai\Client; -use Scrapegraphai\Contracts\HealthzContract; use Scrapegraphai\Core\Conversion; +use Scrapegraphai\Core\ServiceContracts\HealthzContract; +use Scrapegraphai\Healthz\HealthzCheckResponse; use Scrapegraphai\RequestOptions; -use Scrapegraphai\Responses\Healthz\HealthzCheckResponse; final class HealthzService implements HealthzContract { diff --git a/src/Services/MarkdownifyService.php b/src/Core/Services/MarkdownifyService.php similarity index 88% rename from src/Services/MarkdownifyService.php rename to src/Core/Services/MarkdownifyService.php index 301cf27..9fc5bf2 100644 --- a/src/Services/MarkdownifyService.php +++ b/src/Core/Services/MarkdownifyService.php @@ -2,16 +2,16 @@ declare(strict_types=1); -namespace Scrapegraphai\Services; +namespace Scrapegraphai\Core\Services; use Scrapegraphai\Client; -use Scrapegraphai\Contracts\MarkdownifyContract; use Scrapegraphai\Core\Conversion; +use Scrapegraphai\Core\ServiceContracts\MarkdownifyContract; use Scrapegraphai\Markdownify\CompletedMarkdownify; use Scrapegraphai\Markdownify\MarkdownifyConvertParams; +use Scrapegraphai\Markdownify\MarkdownifyGetStatusResponse; +use Scrapegraphai\Markdownify\MarkdownifyGetStatusResponse\FailedMarkdownifyResponse; use Scrapegraphai\RequestOptions; -use Scrapegraphai\Responses\Markdownify\MarkdownifyGetStatusResponse; -use Scrapegraphai\Responses\Markdownify\MarkdownifyGetStatusResponse\FailedMarkdownifyResponse; use const Scrapegraphai\Core\OMIT as omit; diff --git a/src/Services/SearchscraperService.php b/src/Core/Services/SearchscraperService.php similarity index 89% rename from src/Services/SearchscraperService.php rename to src/Core/Services/SearchscraperService.php index a6270c8..d3c5ccf 100644 --- a/src/Services/SearchscraperService.php +++ b/src/Core/Services/SearchscraperService.php @@ -2,16 +2,16 @@ declare(strict_types=1); -namespace Scrapegraphai\Services; +namespace Scrapegraphai\Core\Services; use Scrapegraphai\Client; -use Scrapegraphai\Contracts\SearchscraperContract; use Scrapegraphai\Core\Conversion; +use Scrapegraphai\Core\ServiceContracts\SearchscraperContract; use Scrapegraphai\RequestOptions; -use Scrapegraphai\Responses\Searchscraper\SearchscraperGetStatusResponse; -use Scrapegraphai\Responses\Searchscraper\SearchscraperGetStatusResponse\FailedSearchScraperResponse; use Scrapegraphai\Searchscraper\CompletedSearchScraper; use Scrapegraphai\Searchscraper\SearchscraperCreateParams; +use Scrapegraphai\Searchscraper\SearchscraperGetStatusResponse; +use Scrapegraphai\Searchscraper\SearchscraperGetStatusResponse\FailedSearchScraperResponse; use const Scrapegraphai\Core\OMIT as omit; diff --git a/src/Services/SmartscraperService.php b/src/Core/Services/SmartscraperService.php similarity index 94% rename from src/Services/SmartscraperService.php rename to src/Core/Services/SmartscraperService.php index d2de744..d8536b5 100644 --- a/src/Services/SmartscraperService.php +++ b/src/Core/Services/SmartscraperService.php @@ -2,17 +2,17 @@ declare(strict_types=1); -namespace Scrapegraphai\Services; +namespace Scrapegraphai\Core\Services; use Scrapegraphai\Client; -use Scrapegraphai\Contracts\SmartscraperContract; use Scrapegraphai\Core\Conversion; +use Scrapegraphai\Core\ServiceContracts\SmartscraperContract; use Scrapegraphai\RequestOptions; -use Scrapegraphai\Responses\Smartscraper\SmartscraperGetResponse; -use Scrapegraphai\Responses\Smartscraper\SmartscraperListResponse; use Scrapegraphai\Smartscraper\CompletedSmartscraper; use Scrapegraphai\Smartscraper\FailedSmartscraper; use Scrapegraphai\Smartscraper\SmartscraperCreateParams; +use Scrapegraphai\Smartscraper\SmartscraperGetResponse; +use Scrapegraphai\Smartscraper\SmartscraperListResponse; use const Scrapegraphai\Core\OMIT as omit; diff --git a/src/Services/ValidateService.php b/src/Core/Services/ValidateService.php similarity index 82% rename from src/Services/ValidateService.php rename to src/Core/Services/ValidateService.php index 31ec544..4259642 100644 --- a/src/Services/ValidateService.php +++ b/src/Core/Services/ValidateService.php @@ -2,13 +2,13 @@ declare(strict_types=1); -namespace Scrapegraphai\Services; +namespace Scrapegraphai\Core\Services; use Scrapegraphai\Client; -use Scrapegraphai\Contracts\ValidateContract; use Scrapegraphai\Core\Conversion; +use Scrapegraphai\Core\ServiceContracts\ValidateContract; use Scrapegraphai\RequestOptions; -use Scrapegraphai\Responses\Validate\ValidateAPIKeyResponse; +use Scrapegraphai\Validate\ValidateAPIKeyResponse; final class ValidateService implements ValidateContract { diff --git a/src/Responses/Crawl/CrawlGetResultsResponse.php b/src/Crawl/CrawlGetResultsResponse.php similarity index 93% rename from src/Responses/Crawl/CrawlGetResultsResponse.php rename to src/Crawl/CrawlGetResultsResponse.php index 6efb80d..ed4b322 100644 --- a/src/Responses/Crawl/CrawlGetResultsResponse.php +++ b/src/Crawl/CrawlGetResultsResponse.php @@ -2,13 +2,13 @@ declare(strict_types=1); -namespace Scrapegraphai\Responses\Crawl; +namespace Scrapegraphai\Crawl; use Scrapegraphai\Core\Attributes\Api; use Scrapegraphai\Core\Concerns\SdkModel; use Scrapegraphai\Core\Contracts\BaseModel; -use Scrapegraphai\Responses\Crawl\CrawlGetResultsResponse\Result; -use Scrapegraphai\Responses\Crawl\CrawlGetResultsResponse\Status; +use Scrapegraphai\Crawl\CrawlGetResultsResponse\Result; +use Scrapegraphai\Crawl\CrawlGetResultsResponse\Status; final class CrawlGetResultsResponse implements BaseModel { diff --git a/src/Responses/Crawl/CrawlGetResultsResponse/Result.php b/src/Crawl/CrawlGetResultsResponse/Result.php similarity index 88% rename from src/Responses/Crawl/CrawlGetResultsResponse/Result.php rename to src/Crawl/CrawlGetResultsResponse/Result.php index 4c36957..4ace754 100644 --- a/src/Responses/Crawl/CrawlGetResultsResponse/Result.php +++ b/src/Crawl/CrawlGetResultsResponse/Result.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Scrapegraphai\Responses\Crawl\CrawlGetResultsResponse; +namespace Scrapegraphai\Crawl\CrawlGetResultsResponse; use Scrapegraphai\Core\Concerns\SdkUnion; use Scrapegraphai\Core\Conversion\Contracts\Converter; diff --git a/src/Responses/Crawl/CrawlGetResultsResponse/Status.php b/src/Crawl/CrawlGetResultsResponse/Status.php similarity index 86% rename from src/Responses/Crawl/CrawlGetResultsResponse/Status.php rename to src/Crawl/CrawlGetResultsResponse/Status.php index 02e3650..47c9438 100644 --- a/src/Responses/Crawl/CrawlGetResultsResponse/Status.php +++ b/src/Crawl/CrawlGetResultsResponse/Status.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Scrapegraphai\Responses\Crawl\CrawlGetResultsResponse; +namespace Scrapegraphai\Crawl\CrawlGetResultsResponse; use Scrapegraphai\Core\Concerns\SdkEnum; use Scrapegraphai\Core\Conversion\Contracts\ConverterSource; diff --git a/src/Responses/Crawl/CrawlStartResponse.php b/src/Crawl/CrawlStartResponse.php similarity index 96% rename from src/Responses/Crawl/CrawlStartResponse.php rename to src/Crawl/CrawlStartResponse.php index 94e64e4..878c758 100644 --- a/src/Responses/Crawl/CrawlStartResponse.php +++ b/src/Crawl/CrawlStartResponse.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Scrapegraphai\Responses\Crawl; +namespace Scrapegraphai\Crawl; use Scrapegraphai\Core\Attributes\Api; use Scrapegraphai\Core\Concerns\SdkModel; diff --git a/src/Responses/Credits/CreditGetResponse.php b/src/Credits/CreditGetResponse.php similarity index 97% rename from src/Responses/Credits/CreditGetResponse.php rename to src/Credits/CreditGetResponse.php index 00e0c60..9da136b 100644 --- a/src/Responses/Credits/CreditGetResponse.php +++ b/src/Credits/CreditGetResponse.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Scrapegraphai\Responses\Credits; +namespace Scrapegraphai\Credits; use Scrapegraphai\Core\Attributes\Api; use Scrapegraphai\Core\Concerns\SdkModel; diff --git a/src/Responses/Feedback/FeedbackSubmitResponse.php b/src/Feedback/FeedbackSubmitResponse.php similarity index 97% rename from src/Responses/Feedback/FeedbackSubmitResponse.php rename to src/Feedback/FeedbackSubmitResponse.php index dc71c86..77a94d1 100644 --- a/src/Responses/Feedback/FeedbackSubmitResponse.php +++ b/src/Feedback/FeedbackSubmitResponse.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Scrapegraphai\Responses\Feedback; +namespace Scrapegraphai\Feedback; use Scrapegraphai\Core\Attributes\Api; use Scrapegraphai\Core\Concerns\SdkModel; diff --git a/src/Responses/GenerateSchema/GenerateSchemaGetResponse.php b/src/GenerateSchema/GenerateSchemaGetResponse.php similarity index 70% rename from src/Responses/GenerateSchema/GenerateSchemaGetResponse.php rename to src/GenerateSchema/GenerateSchemaGetResponse.php index 100b707..4718cf2 100644 --- a/src/Responses/GenerateSchema/GenerateSchemaGetResponse.php +++ b/src/GenerateSchema/GenerateSchemaGetResponse.php @@ -2,13 +2,13 @@ declare(strict_types=1); -namespace Scrapegraphai\Responses\GenerateSchema; +namespace Scrapegraphai\GenerateSchema; use Scrapegraphai\Core\Concerns\SdkUnion; use Scrapegraphai\Core\Conversion\Contracts\Converter; use Scrapegraphai\Core\Conversion\Contracts\ConverterSource; -use Scrapegraphai\Responses\GenerateSchema\GenerateSchemaGetResponse\CompletedSchemaGenerationResponse; -use Scrapegraphai\Responses\GenerateSchema\GenerateSchemaGetResponse\FailedSchemaGenerationResponse; +use Scrapegraphai\GenerateSchema\GenerateSchemaGetResponse\CompletedSchemaGenerationResponse; +use Scrapegraphai\GenerateSchema\GenerateSchemaGetResponse\FailedSchemaGenerationResponse; final class GenerateSchemaGetResponse implements ConverterSource { diff --git a/src/Responses/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse.php b/src/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse.php similarity index 93% rename from src/Responses/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse.php rename to src/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse.php index d788ecc..b80b094 100644 --- a/src/Responses/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse.php +++ b/src/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse.php @@ -2,12 +2,12 @@ declare(strict_types=1); -namespace Scrapegraphai\Responses\GenerateSchema\GenerateSchemaGetResponse; +namespace Scrapegraphai\GenerateSchema\GenerateSchemaGetResponse; use Scrapegraphai\Core\Attributes\Api; use Scrapegraphai\Core\Concerns\SdkModel; use Scrapegraphai\Core\Contracts\BaseModel; -use Scrapegraphai\Responses\GenerateSchema\GenerateSchemaGetResponse\CompletedSchemaGenerationResponse\Status; +use Scrapegraphai\GenerateSchema\GenerateSchemaGetResponse\CompletedSchemaGenerationResponse\Status; final class CompletedSchemaGenerationResponse implements BaseModel { diff --git a/src/Responses/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse/Status.php b/src/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse/Status.php similarity index 69% rename from src/Responses/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse/Status.php rename to src/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse/Status.php index e6a3e96..f862250 100644 --- a/src/Responses/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse/Status.php +++ b/src/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse/Status.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Scrapegraphai\Responses\GenerateSchema\GenerateSchemaGetResponse\CompletedSchemaGenerationResponse; +namespace Scrapegraphai\GenerateSchema\GenerateSchemaGetResponse\CompletedSchemaGenerationResponse; use Scrapegraphai\Core\Concerns\SdkEnum; use Scrapegraphai\Core\Conversion\Contracts\ConverterSource; diff --git a/src/Responses/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse.php b/src/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse.php similarity index 93% rename from src/Responses/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse.php rename to src/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse.php index 2703192..e6ec7d7 100644 --- a/src/Responses/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse.php +++ b/src/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse.php @@ -2,12 +2,12 @@ declare(strict_types=1); -namespace Scrapegraphai\Responses\GenerateSchema\GenerateSchemaGetResponse; +namespace Scrapegraphai\GenerateSchema\GenerateSchemaGetResponse; use Scrapegraphai\Core\Attributes\Api; use Scrapegraphai\Core\Concerns\SdkModel; use Scrapegraphai\Core\Contracts\BaseModel; -use Scrapegraphai\Responses\GenerateSchema\GenerateSchemaGetResponse\FailedSchemaGenerationResponse\Status; +use Scrapegraphai\GenerateSchema\GenerateSchemaGetResponse\FailedSchemaGenerationResponse\Status; final class FailedSchemaGenerationResponse implements BaseModel { diff --git a/src/Responses/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse/Status.php b/src/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse/Status.php similarity index 70% rename from src/Responses/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse/Status.php rename to src/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse/Status.php index e173831..07d8023 100644 --- a/src/Responses/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse/Status.php +++ b/src/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse/Status.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Scrapegraphai\Responses\Markdownify\MarkdownifyGetStatusResponse\FailedMarkdownifyResponse; +namespace Scrapegraphai\GenerateSchema\GenerateSchemaGetResponse\FailedSchemaGenerationResponse; use Scrapegraphai\Core\Concerns\SdkEnum; use Scrapegraphai\Core\Conversion\Contracts\ConverterSource; diff --git a/src/Responses/GenerateSchema/GenerateSchemaNewResponse.php b/src/GenerateSchema/GenerateSchemaNewResponse.php similarity index 95% rename from src/Responses/GenerateSchema/GenerateSchemaNewResponse.php rename to src/GenerateSchema/GenerateSchemaNewResponse.php index 8dbe968..184a810 100644 --- a/src/Responses/GenerateSchema/GenerateSchemaNewResponse.php +++ b/src/GenerateSchema/GenerateSchemaNewResponse.php @@ -2,12 +2,12 @@ declare(strict_types=1); -namespace Scrapegraphai\Responses\GenerateSchema; +namespace Scrapegraphai\GenerateSchema; use Scrapegraphai\Core\Attributes\Api; use Scrapegraphai\Core\Concerns\SdkModel; use Scrapegraphai\Core\Contracts\BaseModel; -use Scrapegraphai\Responses\GenerateSchema\GenerateSchemaNewResponse\Status; +use Scrapegraphai\GenerateSchema\GenerateSchemaNewResponse\Status; final class GenerateSchemaNewResponse implements BaseModel { diff --git a/src/Responses/GenerateSchema/GenerateSchemaNewResponse/Status.php b/src/GenerateSchema/GenerateSchemaNewResponse/Status.php similarity index 76% rename from src/Responses/GenerateSchema/GenerateSchemaNewResponse/Status.php rename to src/GenerateSchema/GenerateSchemaNewResponse/Status.php index eecd235..46f448d 100644 --- a/src/Responses/GenerateSchema/GenerateSchemaNewResponse/Status.php +++ b/src/GenerateSchema/GenerateSchemaNewResponse/Status.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Scrapegraphai\Responses\GenerateSchema\GenerateSchemaNewResponse; +namespace Scrapegraphai\GenerateSchema\GenerateSchemaNewResponse; use Scrapegraphai\Core\Concerns\SdkEnum; use Scrapegraphai\Core\Conversion\Contracts\ConverterSource; diff --git a/src/Responses/Healthz/HealthzCheckResponse.php b/src/Healthz/HealthzCheckResponse.php similarity index 97% rename from src/Responses/Healthz/HealthzCheckResponse.php rename to src/Healthz/HealthzCheckResponse.php index 4828008..b2cbcb4 100644 --- a/src/Responses/Healthz/HealthzCheckResponse.php +++ b/src/Healthz/HealthzCheckResponse.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Scrapegraphai\Responses\Healthz; +namespace Scrapegraphai\Healthz; use Scrapegraphai\Core\Attributes\Api; use Scrapegraphai\Core\Concerns\SdkModel; diff --git a/src/Responses/Markdownify/MarkdownifyGetStatusResponse.php b/src/Markdownify/MarkdownifyGetStatusResponse.php similarity index 73% rename from src/Responses/Markdownify/MarkdownifyGetStatusResponse.php rename to src/Markdownify/MarkdownifyGetStatusResponse.php index 14061e8..c8fd76f 100644 --- a/src/Responses/Markdownify/MarkdownifyGetStatusResponse.php +++ b/src/Markdownify/MarkdownifyGetStatusResponse.php @@ -2,13 +2,12 @@ declare(strict_types=1); -namespace Scrapegraphai\Responses\Markdownify; +namespace Scrapegraphai\Markdownify; use Scrapegraphai\Core\Concerns\SdkUnion; use Scrapegraphai\Core\Conversion\Contracts\Converter; use Scrapegraphai\Core\Conversion\Contracts\ConverterSource; -use Scrapegraphai\Markdownify\CompletedMarkdownify; -use Scrapegraphai\Responses\Markdownify\MarkdownifyGetStatusResponse\FailedMarkdownifyResponse; +use Scrapegraphai\Markdownify\MarkdownifyGetStatusResponse\FailedMarkdownifyResponse; final class MarkdownifyGetStatusResponse implements ConverterSource { diff --git a/src/Responses/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse.php b/src/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse.php similarity index 92% rename from src/Responses/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse.php rename to src/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse.php index 2a1a00e..21ab267 100644 --- a/src/Responses/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse.php +++ b/src/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse.php @@ -2,12 +2,12 @@ declare(strict_types=1); -namespace Scrapegraphai\Responses\Markdownify\MarkdownifyGetStatusResponse; +namespace Scrapegraphai\Markdownify\MarkdownifyGetStatusResponse; use Scrapegraphai\Core\Attributes\Api; use Scrapegraphai\Core\Concerns\SdkModel; use Scrapegraphai\Core\Contracts\BaseModel; -use Scrapegraphai\Responses\Markdownify\MarkdownifyGetStatusResponse\FailedMarkdownifyResponse\Status; +use Scrapegraphai\Markdownify\MarkdownifyGetStatusResponse\FailedMarkdownifyResponse\Status; final class FailedMarkdownifyResponse implements BaseModel { diff --git a/src/Responses/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse/Status.php b/src/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse/Status.php similarity index 69% rename from src/Responses/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse/Status.php rename to src/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse/Status.php index f67b258..a8981ab 100644 --- a/src/Responses/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse/Status.php +++ b/src/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse/Status.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Scrapegraphai\Responses\GenerateSchema\GenerateSchemaGetResponse\FailedSchemaGenerationResponse; +namespace Scrapegraphai\Markdownify\MarkdownifyGetStatusResponse\FailedMarkdownifyResponse; use Scrapegraphai\Core\Concerns\SdkEnum; use Scrapegraphai\Core\Conversion\Contracts\ConverterSource; diff --git a/src/Responses/Searchscraper/SearchscraperGetStatusResponse.php b/src/Searchscraper/SearchscraperGetStatusResponse.php similarity index 72% rename from src/Responses/Searchscraper/SearchscraperGetStatusResponse.php rename to src/Searchscraper/SearchscraperGetStatusResponse.php index 67bf387..4d14289 100644 --- a/src/Responses/Searchscraper/SearchscraperGetStatusResponse.php +++ b/src/Searchscraper/SearchscraperGetStatusResponse.php @@ -2,13 +2,12 @@ declare(strict_types=1); -namespace Scrapegraphai\Responses\Searchscraper; +namespace Scrapegraphai\Searchscraper; use Scrapegraphai\Core\Concerns\SdkUnion; use Scrapegraphai\Core\Conversion\Contracts\Converter; use Scrapegraphai\Core\Conversion\Contracts\ConverterSource; -use Scrapegraphai\Responses\Searchscraper\SearchscraperGetStatusResponse\FailedSearchScraperResponse; -use Scrapegraphai\Searchscraper\CompletedSearchScraper; +use Scrapegraphai\Searchscraper\SearchscraperGetStatusResponse\FailedSearchScraperResponse; final class SearchscraperGetStatusResponse implements ConverterSource { diff --git a/src/Responses/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse.php b/src/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse.php similarity index 94% rename from src/Responses/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse.php rename to src/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse.php index 31972dc..40c1303 100644 --- a/src/Responses/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse.php +++ b/src/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse.php @@ -2,12 +2,12 @@ declare(strict_types=1); -namespace Scrapegraphai\Responses\Searchscraper\SearchscraperGetStatusResponse; +namespace Scrapegraphai\Searchscraper\SearchscraperGetStatusResponse; use Scrapegraphai\Core\Attributes\Api; use Scrapegraphai\Core\Concerns\SdkModel; use Scrapegraphai\Core\Contracts\BaseModel; -use Scrapegraphai\Responses\Searchscraper\SearchscraperGetStatusResponse\FailedSearchScraperResponse\Status; +use Scrapegraphai\Searchscraper\SearchscraperGetStatusResponse\FailedSearchScraperResponse\Status; final class FailedSearchScraperResponse implements BaseModel { diff --git a/src/Responses/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse/Status.php b/src/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse/Status.php similarity index 69% rename from src/Responses/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse/Status.php rename to src/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse/Status.php index 3c15568..c1970d9 100644 --- a/src/Responses/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse/Status.php +++ b/src/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse/Status.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Scrapegraphai\Responses\Searchscraper\SearchscraperGetStatusResponse\FailedSearchScraperResponse; +namespace Scrapegraphai\Searchscraper\SearchscraperGetStatusResponse\FailedSearchScraperResponse; use Scrapegraphai\Core\Concerns\SdkEnum; use Scrapegraphai\Core\Conversion\Contracts\ConverterSource; diff --git a/src/Responses/Smartscraper/SmartscraperGetResponse.php b/src/Smartscraper/SmartscraperGetResponse.php similarity index 77% rename from src/Responses/Smartscraper/SmartscraperGetResponse.php rename to src/Smartscraper/SmartscraperGetResponse.php index 5af25f8..474b71d 100644 --- a/src/Responses/Smartscraper/SmartscraperGetResponse.php +++ b/src/Smartscraper/SmartscraperGetResponse.php @@ -2,13 +2,11 @@ declare(strict_types=1); -namespace Scrapegraphai\Responses\Smartscraper; +namespace Scrapegraphai\Smartscraper; use Scrapegraphai\Core\Concerns\SdkUnion; use Scrapegraphai\Core\Conversion\Contracts\Converter; use Scrapegraphai\Core\Conversion\Contracts\ConverterSource; -use Scrapegraphai\Smartscraper\CompletedSmartscraper; -use Scrapegraphai\Smartscraper\FailedSmartscraper; final class SmartscraperGetResponse implements ConverterSource { diff --git a/src/Responses/Smartscraper/SmartscraperListResponse.php b/src/Smartscraper/SmartscraperListResponse.php similarity index 77% rename from src/Responses/Smartscraper/SmartscraperListResponse.php rename to src/Smartscraper/SmartscraperListResponse.php index 5e20795..b776c46 100644 --- a/src/Responses/Smartscraper/SmartscraperListResponse.php +++ b/src/Smartscraper/SmartscraperListResponse.php @@ -2,13 +2,11 @@ declare(strict_types=1); -namespace Scrapegraphai\Responses\Smartscraper; +namespace Scrapegraphai\Smartscraper; use Scrapegraphai\Core\Concerns\SdkUnion; use Scrapegraphai\Core\Conversion\Contracts\Converter; use Scrapegraphai\Core\Conversion\Contracts\ConverterSource; -use Scrapegraphai\Smartscraper\CompletedSmartscraper; -use Scrapegraphai\Smartscraper\FailedSmartscraper; final class SmartscraperListResponse implements ConverterSource { diff --git a/src/Responses/Validate/ValidateAPIKeyResponse.php b/src/Validate/ValidateAPIKeyResponse.php similarity index 95% rename from src/Responses/Validate/ValidateAPIKeyResponse.php rename to src/Validate/ValidateAPIKeyResponse.php index bf40f3b..1d6a417 100644 --- a/src/Responses/Validate/ValidateAPIKeyResponse.php +++ b/src/Validate/ValidateAPIKeyResponse.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Scrapegraphai\Responses\Validate; +namespace Scrapegraphai\Validate; use Scrapegraphai\Core\Attributes\Api; use Scrapegraphai\Core\Concerns\SdkModel; diff --git a/tests/Resources/CrawlTest.php b/tests/Services/CrawlTest.php similarity index 98% rename from tests/Resources/CrawlTest.php rename to tests/Services/CrawlTest.php index c46a37f..b5e68c4 100644 --- a/tests/Resources/CrawlTest.php +++ b/tests/Services/CrawlTest.php @@ -1,6 +1,6 @@ Date: Wed, 27 Aug 2025 04:27:23 +0000 Subject: [PATCH 17/44] chore(internal): refactored internal codepaths --- src/Core/BaseClient.php | 78 +++++++++++++++------ src/Core/Concerns/SdkModel.php | 2 +- src/Core/Contracts/BasePage.php | 18 +++-- src/Core/Contracts/BaseStream.php | 15 ++++ src/Core/Errors/APIStatusError.php | 16 ++--- src/Core/Pagination/AbstractPage.php | 41 +++++------ src/Core/Pagination/PageRequestOptions.php | 73 ------------------- src/Core/Services/CrawlService.php | 20 +++--- src/Core/Services/CreditsService.php | 10 ++- src/Core/Services/FeedbackService.php | 9 ++- src/Core/Services/GenerateSchemaService.php | 16 ++--- src/Core/Services/HealthzService.php | 10 ++- src/Core/Services/MarkdownifyService.php | 19 ++--- src/Core/Services/SearchscraperService.php | 19 ++--- src/Core/Services/SmartscraperService.php | 25 +++---- src/Core/Services/ValidateService.php | 10 ++- src/Core/Util.php | 55 +++++++++++---- src/RequestOptions.php | 6 +- 18 files changed, 210 insertions(+), 232 deletions(-) delete mode 100644 src/Core/Pagination/PageRequestOptions.php diff --git a/src/Core/BaseClient.php b/src/Core/BaseClient.php index c1473a3..fd2f7e2 100644 --- a/src/Core/BaseClient.php +++ b/src/Core/BaseClient.php @@ -13,9 +13,22 @@ use Psr\Http\Message\StreamFactoryInterface; use Psr\Http\Message\UriFactoryInterface; use Psr\Http\Message\UriInterface; +use Scrapegraphai\Core\Contracts\BasePage; +use Scrapegraphai\Core\Contracts\BaseStream; +use Scrapegraphai\Core\Conversion\Contracts\Converter; +use Scrapegraphai\Core\Conversion\Contracts\ConverterSource; use Scrapegraphai\Core\Errors\APIStatusError; use Scrapegraphai\RequestOptions; +/** + * @phpstan-type normalized_request = array{ + * method: string, + * path: string, + * query: array, + * headers: array>, + * body: mixed, + * } + */ class BaseClient { protected UriInterface $baseUrl; @@ -48,6 +61,8 @@ public function __construct( * @param string|list $path * @param array $query * @param array $headers + * @param class-string> $page + * @param class-string> $stream */ public function request( string $method, @@ -55,30 +70,47 @@ public function request( array $query = [], array $headers = [], mixed $body = null, + string|Converter|ConverterSource|null $convert = null, + ?string $page = null, + ?string $stream = null, mixed $options = [], ): mixed { // @phpstan-ignore-next-line - [$req, $opts] = $this->buildRequest(method: $method, path: $path, query: $query, headers: $headers, opts: $options); + [$req, $opts] = $this->buildRequest(method: $method, path: $path, query: $query, headers: $headers, body: $body, opts: $options); + ['method' => $method, 'path' => $uri, 'headers' => $headers] = $req; + + $req = $this->requestFactory->createRequest($method, uri: $uri); + $req = Util::withSetHeaders($req, headers: $headers); // @phpstan-ignore-next-line $rsp = $this->sendRequest($req, data: $body, opts: $opts, redirectCount: 0, retryCount: 0); - if (204 == $rsp->getStatusCode()) { - return null; // Handle 204 No Content + + $decoded = Util::decodeContent($rsp); + + if (!is_null($stream)) { + return new $stream( + convert: $convert, + request: $req, + response: $rsp, + stream: $decoded + ); } - return Util::decodeContent($rsp); - } + if (!is_null($page)) { + return new $page( + convert: $convert, + client: $this, + request: $req, + options: $opts, + data: $decoded, + ); + } - /** - * @template Item - * @template T of Pagination\AbstractPage - * - * @param T $page - */ - public function requestApiList(object $page, RequestOptions $options): ResponseInterface - { - // @phpstan-ignore-next-line - return null; + if (!is_null($convert)) { + return Conversion::coerce($convert, value: $decoded); + } + + return $decoded; } /** @return array */ @@ -91,7 +123,7 @@ protected function authHeaders(): array * @param string|list $path * @param array $query * @param array|null> $headers - * @param array{ + * @param RequestOptions|array{ * timeout?: float|null, * maxRetries?: int|null, * initialRetryDelay?: float|null, @@ -99,16 +131,17 @@ protected function authHeaders(): array * extraHeaders?: list|null, * extraQueryParams?: list|null, * extraBodyParams?: list|null, - * }|RequestOptions|null $opts + * }|null $opts * - * @return array{RequestInterface, RequestOptions} + * @return array{normalized_request, RequestOptions} */ protected function buildRequest( string $method, string|array $path, array $query, array $headers, - array|RequestOptions|null $opts, + mixed $body, + RequestOptions|array|null $opts, ): array { $opts = [...$this->options->__serialize(), ...RequestOptions::parse($opts)->__serialize()]; $options = new RequestOptions(...$opts); @@ -117,16 +150,15 @@ protected function buildRequest( /** @var array $mergedQuery */ $mergedQuery = array_merge_recursive($query, $options->extraQueryParams); - $uri = Util::joinUri($this->baseUrl, path: $parsedPath, query: $mergedQuery); + $uri = Util::joinUri($this->baseUrl, path: $parsedPath, query: $mergedQuery)->__toString(); - /** @var array> $mergedHeaders */ + /** @var array|null> $mergedHeaders */ $mergedHeaders = [...$this->headers, ...$this->authHeaders(), ...$headers, ...$options->extraHeaders, ]; - $req = $this->requestFactory->createRequest(strtoupper($method), uri: $uri); - $req = Util::withSetHeaders($req, headers: $mergedHeaders); + $req = ['method' => strtoupper($method), 'path' => $uri, 'query' => $mergedQuery, 'headers' => $mergedHeaders, 'body' => $body]; return [$req, $options]; } diff --git a/src/Core/Concerns/SdkModel.php b/src/Core/Concerns/SdkModel.php index 18d59f0..3b25583 100644 --- a/src/Core/Concerns/SdkModel.php +++ b/src/Core/Concerns/SdkModel.php @@ -60,7 +60,7 @@ public function __debugInfo(): array */ public function __toString(): string { - return json_encode($this->__debugInfo(), flags: JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) ?: ''; + return Util::prettyEncodeJson($this->__debugInfo()); } /** diff --git a/src/Core/Contracts/BasePage.php b/src/Core/Contracts/BasePage.php index c0d3d9f..1ed30c4 100644 --- a/src/Core/Contracts/BasePage.php +++ b/src/Core/Contracts/BasePage.php @@ -4,9 +4,10 @@ namespace Scrapegraphai\Core\Contracts; -use Psr\Http\Message\ResponseInterface; -use Scrapegraphai\Core\BaseClient; -use Scrapegraphai\Core\Pagination\PageRequestOptions; +use Scrapegraphai\Client; +use Scrapegraphai\Core\Conversion\Contracts\Converter; +use Scrapegraphai\Core\Conversion\Contracts\ConverterSource; +use Scrapegraphai\RequestOptions; /** * @template Item @@ -17,12 +18,15 @@ interface BasePage extends \IteratorAggregate { /** * @internal + * + * @param array $request */ public function __construct( - BaseClient $client, - PageRequestOptions $options, - ResponseInterface $response, - mixed $body, + Converter|ConverterSource|string $convert, + Client $client, + array $request, + RequestOptions $options, + mixed $data, ); public function hasNextPage(): bool; diff --git a/src/Core/Contracts/BaseStream.php b/src/Core/Contracts/BaseStream.php index 41ddb83..b467d7f 100644 --- a/src/Core/Contracts/BaseStream.php +++ b/src/Core/Contracts/BaseStream.php @@ -4,6 +4,11 @@ namespace Scrapegraphai\Core\Contracts; +use Psr\Http\Message\RequestInterface; +use Psr\Http\Message\ResponseInterface; +use Scrapegraphai\Core\Conversion\Contracts\Converter; +use Scrapegraphai\Core\Conversion\Contracts\ConverterSource; + /** * @template TInner * @@ -11,6 +16,16 @@ */ interface BaseStream extends \IteratorAggregate { + /** + * @param \Generator $stream + */ + public function __construct( + Converter|ConverterSource|string $convert, + RequestInterface $request, + ResponseInterface $response, + \Generator $stream, + ); + /** * Manually force the stream to close early. * Iterating through will automatically close as well. diff --git a/src/Core/Errors/APIStatusError.php b/src/Core/Errors/APIStatusError.php index 6d96b27..9390a85 100644 --- a/src/Core/Errors/APIStatusError.php +++ b/src/Core/Errors/APIStatusError.php @@ -4,7 +4,7 @@ use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; -use Psr\Http\Message\StreamInterface; +use Scrapegraphai\Core\Util; class APIStatusError extends APIError { @@ -23,8 +23,8 @@ public function __construct( $this->status = $response->getStatusCode(); $summary = 'Status: '.$this->status.PHP_EOL - .'Response Body: '.self::fmtBody($response->getBody()).PHP_EOL - .'Request Body: '.self::fmtBody($request->getBody()).PHP_EOL; + .'Response Body: '.Util::prettyEncodeJson(Util::decodeJson($response->getBody())).PHP_EOL + .'Request Body: '.Util::prettyEncodeJson(Util::decodeJson($request->getBody())).PHP_EOL; if ('' != $message) { $summary .= $message.PHP_EOL.$summary; @@ -35,7 +35,8 @@ public function __construct( public static function from( RequestInterface $request, - ResponseInterface $response + ResponseInterface $response, + string $message = '' ): self { $status = $response->getStatusCode(); @@ -51,11 +52,6 @@ public static function from( default => APIStatusError::class }; - return new $cls(request: $request, response: $response); - } - - private static function fmtBody(StreamInterface $body): string - { - return json_encode(json_decode($body->__toString() ?: ''), JSON_PRETTY_PRINT) ?: ''; + return new $cls(request: $request, response: $response, message: $message); } } diff --git a/src/Core/Pagination/AbstractPage.php b/src/Core/Pagination/AbstractPage.php index e767100..aa36c66 100644 --- a/src/Core/Pagination/AbstractPage.php +++ b/src/Core/Pagination/AbstractPage.php @@ -4,10 +4,12 @@ namespace Scrapegraphai\Core\Pagination; -use Psr\Http\Message\ResponseInterface; -use Scrapegraphai\Core\BaseClient; +use Scrapegraphai\Client; use Scrapegraphai\Core\Contracts\BasePage; +use Scrapegraphai\Core\Conversion\Contracts\Converter; +use Scrapegraphai\Core\Conversion\Contracts\ConverterSource; use Scrapegraphai\Core\Errors\APIStatusError; +use Scrapegraphai\RequestOptions; /** * @internal @@ -15,14 +17,17 @@ * @template Item * * @implements BasePage + * + * @phpstan-import-type normalized_request from \Scrapegraphai\Core\BaseClient */ abstract class AbstractPage implements BasePage { public function __construct( - protected BaseClient $client, - protected PageRequestOptions $options, - protected ResponseInterface $response, - protected mixed $body, + protected Converter|ConverterSource|string $convert, + protected Client $client, + protected array $request, + protected RequestOptions $options, + protected mixed $data, ) {} /** @@ -37,7 +42,7 @@ public function hasNextPage(): bool return false; } - return null != $this->nextPageRequestOptions(); + return null != $this->nextRequest(); } /** @@ -51,24 +56,17 @@ public function hasNextPage(): bool */ public function getNextPage(): static { - $nextOptions = $this->nextPageRequestOptions(); - if (!$nextOptions) { + $next = $this->nextRequest(); + if (!$next) { throw new \RuntimeException( 'No next page expected; please check `.hasNextPage()` before calling `.getNextPage()`.' ); } - $response = $this->client->requestApiList($this, $nextOptions); - - /** @var static of AbstractPage $nextPage */ - $nextPage = new static( - client: $this->client, - options: $nextOptions, - response: $response, - body: $response->getBody() - ); + [$req, $opts] = $next; - return $nextPage; + // @phpstan-ignore-next-line + return $this->client->request(...$req, convert: $this->convert, page: $this, options: $opts); } /** @@ -102,5 +100,8 @@ public function pagingEachItem(): \Generator } } - abstract protected function nextPageRequestOptions(): ?PageRequestOptions; + /** + * @return array{normalized_request, RequestOptions} + */ + abstract protected function nextRequest(): ?array; } diff --git a/src/Core/Pagination/PageRequestOptions.php b/src/Core/Pagination/PageRequestOptions.php deleted file mode 100644 index 8b780ac..0000000 --- a/src/Core/Pagination/PageRequestOptions.php +++ /dev/null @@ -1,73 +0,0 @@ -client->request( + // @phpstan-ignore-next-line; + return $this->client->request( method: 'get', path: ['crawl/%1$s', $taskID], - options: $requestOptions + options: $requestOptions, + convert: CrawlGetResultsResponse::class, ); - - // @phpstan-ignore-next-line; - return Conversion::coerce(CrawlGetResultsResponse::class, value: $resp); } /** @@ -77,14 +75,14 @@ public function start( ], $requestOptions, ); - $resp = $this->client->request( + + // @phpstan-ignore-next-line; + return $this->client->request( method: 'post', path: 'crawl', body: (object) $parsed, - options: $options + options: $options, + convert: CrawlStartResponse::class, ); - - // @phpstan-ignore-next-line; - return Conversion::coerce(CrawlStartResponse::class, value: $resp); } } diff --git a/src/Core/Services/CreditsService.php b/src/Core/Services/CreditsService.php index f6735b6..9cbbddc 100644 --- a/src/Core/Services/CreditsService.php +++ b/src/Core/Services/CreditsService.php @@ -5,7 +5,6 @@ namespace Scrapegraphai\Core\Services; use Scrapegraphai\Client; -use Scrapegraphai\Core\Conversion; use Scrapegraphai\Core\ServiceContracts\CreditsContract; use Scrapegraphai\Credits\CreditGetResponse; use Scrapegraphai\RequestOptions; @@ -20,13 +19,12 @@ public function __construct(private Client $client) {} public function retrieve( ?RequestOptions $requestOptions = null ): CreditGetResponse { - $resp = $this->client->request( + // @phpstan-ignore-next-line; + return $this->client->request( method: 'get', path: 'credits', - options: $requestOptions + options: $requestOptions, + convert: CreditGetResponse::class, ); - - // @phpstan-ignore-next-line; - return Conversion::coerce(CreditGetResponse::class, value: $resp); } } diff --git a/src/Core/Services/FeedbackService.php b/src/Core/Services/FeedbackService.php index dcda1da..012e76e 100644 --- a/src/Core/Services/FeedbackService.php +++ b/src/Core/Services/FeedbackService.php @@ -5,7 +5,6 @@ namespace Scrapegraphai\Core\Services; use Scrapegraphai\Client; -use Scrapegraphai\Core\Conversion; use Scrapegraphai\Core\ServiceContracts\FeedbackContract; use Scrapegraphai\Feedback\FeedbackSubmitParams; use Scrapegraphai\Feedback\FeedbackSubmitResponse; @@ -38,14 +37,14 @@ public function submit( ], $requestOptions, ); - $resp = $this->client->request( + + // @phpstan-ignore-next-line; + return $this->client->request( method: 'post', path: 'feedback', body: (object) $parsed, options: $options, + convert: FeedbackSubmitResponse::class, ); - - // @phpstan-ignore-next-line; - return Conversion::coerce(FeedbackSubmitResponse::class, value: $resp); } } diff --git a/src/Core/Services/GenerateSchemaService.php b/src/Core/Services/GenerateSchemaService.php index dc15807..a09d4d9 100644 --- a/src/Core/Services/GenerateSchemaService.php +++ b/src/Core/Services/GenerateSchemaService.php @@ -5,7 +5,6 @@ namespace Scrapegraphai\Core\Services; use Scrapegraphai\Client; -use Scrapegraphai\Core\Conversion; use Scrapegraphai\Core\ServiceContracts\GenerateSchemaContract; use Scrapegraphai\GenerateSchema\GenerateSchemaCreateParams; use Scrapegraphai\GenerateSchema\GenerateSchemaGetResponse; @@ -36,15 +35,15 @@ public function create( ['userPrompt' => $userPrompt, 'existingSchema' => $existingSchema], $requestOptions, ); - $resp = $this->client->request( + + // @phpstan-ignore-next-line; + return $this->client->request( method: 'post', path: 'generate_schema', body: (object) $parsed, options: $options, + convert: GenerateSchemaNewResponse::class, ); - - // @phpstan-ignore-next-line; - return Conversion::coerce(GenerateSchemaNewResponse::class, value: $resp); } /** @@ -54,13 +53,12 @@ public function retrieve( string $requestID, ?RequestOptions $requestOptions = null ): CompletedSchemaGenerationResponse|FailedSchemaGenerationResponse { - $resp = $this->client->request( + // @phpstan-ignore-next-line; + return $this->client->request( method: 'get', path: ['generate_schema/%1$s', $requestID], options: $requestOptions, + convert: GenerateSchemaGetResponse::class, ); - - // @phpstan-ignore-next-line; - return Conversion::coerce(GenerateSchemaGetResponse::class, value: $resp); } } diff --git a/src/Core/Services/HealthzService.php b/src/Core/Services/HealthzService.php index 03a6ccd..4285b21 100644 --- a/src/Core/Services/HealthzService.php +++ b/src/Core/Services/HealthzService.php @@ -5,7 +5,6 @@ namespace Scrapegraphai\Core\Services; use Scrapegraphai\Client; -use Scrapegraphai\Core\Conversion; use Scrapegraphai\Core\ServiceContracts\HealthzContract; use Scrapegraphai\Healthz\HealthzCheckResponse; use Scrapegraphai\RequestOptions; @@ -20,13 +19,12 @@ public function __construct(private Client $client) {} public function check( ?RequestOptions $requestOptions = null ): HealthzCheckResponse { - $resp = $this->client->request( + // @phpstan-ignore-next-line; + return $this->client->request( method: 'get', path: 'healthz', - options: $requestOptions + options: $requestOptions, + convert: HealthzCheckResponse::class, ); - - // @phpstan-ignore-next-line; - return Conversion::coerce(HealthzCheckResponse::class, value: $resp); } } diff --git a/src/Core/Services/MarkdownifyService.php b/src/Core/Services/MarkdownifyService.php index 9fc5bf2..427520a 100644 --- a/src/Core/Services/MarkdownifyService.php +++ b/src/Core/Services/MarkdownifyService.php @@ -5,7 +5,6 @@ namespace Scrapegraphai\Core\Services; use Scrapegraphai\Client; -use Scrapegraphai\Core\Conversion; use Scrapegraphai\Core\ServiceContracts\MarkdownifyContract; use Scrapegraphai\Markdownify\CompletedMarkdownify; use Scrapegraphai\Markdownify\MarkdownifyConvertParams; @@ -36,15 +35,15 @@ public function convert( ['websiteURL' => $websiteURL, 'headers' => $headers, 'steps' => $steps], $requestOptions, ); - $resp = $this->client->request( + + // @phpstan-ignore-next-line; + return $this->client->request( method: 'post', path: 'markdownify', body: (object) $parsed, options: $options, + convert: CompletedMarkdownify::class, ); - - // @phpstan-ignore-next-line; - return Conversion::coerce(CompletedMarkdownify::class, value: $resp); } /** @@ -54,16 +53,12 @@ public function retrieveStatus( string $requestID, ?RequestOptions $requestOptions = null ): CompletedMarkdownify|FailedMarkdownifyResponse { - $resp = $this->client->request( + // @phpstan-ignore-next-line; + return $this->client->request( method: 'get', path: ['markdownify/%1$s', $requestID], options: $requestOptions, - ); - - // @phpstan-ignore-next-line; - return Conversion::coerce( - MarkdownifyGetStatusResponse::class, - value: $resp + convert: MarkdownifyGetStatusResponse::class, ); } } diff --git a/src/Core/Services/SearchscraperService.php b/src/Core/Services/SearchscraperService.php index d3c5ccf..da54076 100644 --- a/src/Core/Services/SearchscraperService.php +++ b/src/Core/Services/SearchscraperService.php @@ -5,7 +5,6 @@ namespace Scrapegraphai\Core\Services; use Scrapegraphai\Client; -use Scrapegraphai\Core\Conversion; use Scrapegraphai\Core\ServiceContracts\SearchscraperContract; use Scrapegraphai\RequestOptions; use Scrapegraphai\Searchscraper\CompletedSearchScraper; @@ -44,15 +43,15 @@ public function create( ], $requestOptions, ); - $resp = $this->client->request( + + // @phpstan-ignore-next-line; + return $this->client->request( method: 'post', path: 'searchscraper', body: (object) $parsed, options: $options, + convert: CompletedSearchScraper::class, ); - - // @phpstan-ignore-next-line; - return Conversion::coerce(CompletedSearchScraper::class, value: $resp); } /** @@ -62,16 +61,12 @@ public function retrieveStatus( string $requestID, ?RequestOptions $requestOptions = null ): CompletedSearchScraper|FailedSearchScraperResponse { - $resp = $this->client->request( + // @phpstan-ignore-next-line; + return $this->client->request( method: 'get', path: ['searchscraper/%1$s', $requestID], options: $requestOptions, - ); - - // @phpstan-ignore-next-line; - return Conversion::coerce( - SearchscraperGetStatusResponse::class, - value: $resp + convert: SearchscraperGetStatusResponse::class, ); } } diff --git a/src/Core/Services/SmartscraperService.php b/src/Core/Services/SmartscraperService.php index d8536b5..d7c0ba3 100644 --- a/src/Core/Services/SmartscraperService.php +++ b/src/Core/Services/SmartscraperService.php @@ -5,7 +5,6 @@ namespace Scrapegraphai\Core\Services; use Scrapegraphai\Client; -use Scrapegraphai\Core\Conversion; use Scrapegraphai\Core\ServiceContracts\SmartscraperContract; use Scrapegraphai\RequestOptions; use Scrapegraphai\Smartscraper\CompletedSmartscraper; @@ -63,15 +62,15 @@ public function create( ], $requestOptions, ); - $resp = $this->client->request( + + // @phpstan-ignore-next-line; + return $this->client->request( method: 'post', path: 'smartscraper', body: (object) $parsed, options: $options, + convert: CompletedSmartscraper::class, ); - - // @phpstan-ignore-next-line; - return Conversion::coerce(CompletedSmartscraper::class, value: $resp); } /** @@ -81,14 +80,13 @@ public function retrieve( string $requestID, ?RequestOptions $requestOptions = null ): CompletedSmartscraper|FailedSmartscraper { - $resp = $this->client->request( + // @phpstan-ignore-next-line; + return $this->client->request( method: 'get', path: ['smartscraper/%1$s', $requestID], options: $requestOptions, + convert: SmartscraperGetResponse::class, ); - - // @phpstan-ignore-next-line; - return Conversion::coerce(SmartscraperGetResponse::class, value: $resp); } /** @@ -97,13 +95,12 @@ public function retrieve( public function list( ?RequestOptions $requestOptions = null ): CompletedSmartscraper|FailedSmartscraper { - $resp = $this->client->request( + // @phpstan-ignore-next-line; + return $this->client->request( method: 'get', path: 'smartscraper', - options: $requestOptions + options: $requestOptions, + convert: SmartscraperListResponse::class, ); - - // @phpstan-ignore-next-line; - return Conversion::coerce(SmartscraperListResponse::class, value: $resp); } } diff --git a/src/Core/Services/ValidateService.php b/src/Core/Services/ValidateService.php index 4259642..38f3ed4 100644 --- a/src/Core/Services/ValidateService.php +++ b/src/Core/Services/ValidateService.php @@ -5,7 +5,6 @@ namespace Scrapegraphai\Core\Services; use Scrapegraphai\Client; -use Scrapegraphai\Core\Conversion; use Scrapegraphai\Core\ServiceContracts\ValidateContract; use Scrapegraphai\RequestOptions; use Scrapegraphai\Validate\ValidateAPIKeyResponse; @@ -20,13 +19,12 @@ public function __construct(private Client $client) {} public function apiKey( ?RequestOptions $requestOptions = null ): ValidateAPIKeyResponse { - $resp = $this->client->request( + // @phpstan-ignore-next-line; + return $this->client->request( method: 'get', path: 'validate', - options: $requestOptions + options: $requestOptions, + convert: ValidateAPIKeyResponse::class, ); - - // @phpstan-ignore-next-line; - return Conversion::coerce(ValidateAPIKeyResponse::class, value: $resp); } } diff --git a/src/Core/Util.php b/src/Core/Util.php index 51576c5..d1197bd 100644 --- a/src/Core/Util.php +++ b/src/Core/Util.php @@ -4,8 +4,8 @@ namespace Scrapegraphai\Core; -use Psr\Http\Message\MessageInterface; use Psr\Http\Message\RequestInterface; +use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\StreamFactoryInterface; use Psr\Http\Message\StreamInterface; use Psr\Http\Message\UriInterface; @@ -16,7 +16,9 @@ final class Util public const JSON_ENCODE_FLAGS = JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE; - public const JSON_CONTENT_TYPE = '/application\/json/'; + public const JSON_CONTENT_TYPE = '/^application\/(?:vnd(?:.[^.]+)*+)?json(?!l)/'; + + public const JSONL_CONTENT_TYPE = '/^application\/(:?x-(?:n|l)djson)|(:?(?:x-)?jsonl)/'; /** * @return array @@ -44,6 +46,16 @@ public static function array_transform_keys(array $array, array $map): array return $acc; } + /** + * @param array $arr + * + * @return array + */ + public static function array_filter_omit(array $arr): array + { + return array_filter($arr, fn ($v, $_) => OMIT !== $v, mode: ARRAY_FILTER_USE_BOTH); + } + /** * @param string|int|list|callable $key */ @@ -85,7 +97,7 @@ public static function parsePath(string|array $path): string [$template] = $path; - return sprintf($template, ...array_map('rawurlencode', array_slice($path, 1))); + return sprintf($template, ...array_map('rawurlencode', array: array_slice($path, 1))); } /** @@ -290,18 +302,38 @@ public static function decodeSSE(\Iterator $lines): \Generator } } - public static function decodeContent(MessageInterface $rsp): mixed + public static function decodeJson(string $json): mixed { + return json_decode($json, associative: true, flags: JSON_THROW_ON_ERROR); + } + + public static function decodeContent(ResponseInterface $rsp): mixed + { + if (204 == $rsp->getStatusCode()) { + return null; + } + $content_type = $rsp->getHeaderLine('Content-Type'); $body = $rsp->getBody(); - if (preg_match(self::JSON_CONTENT_TYPE, $content_type)) { + if (preg_match(self::JSON_CONTENT_TYPE, subject: $content_type)) { $json = $body->getContents(); - return json_decode($json, associative: true, flags: JSON_THROW_ON_ERROR); + return self::decodeJson($json); + } + + if (preg_match(self::JSONL_CONTENT_TYPE, subject: $content_type)) { + $it = self::streamIterator($body); + $lines = self::decodeLines($it); + + return (function () use ($lines) { + foreach ($lines as $line) { + yield static::decodeJson($line); + } + })(); } - if (str_contains($content_type, 'text/event-stream')) { + if (str_contains($content_type, needle: 'text/event-stream')) { $it = self::streamIterator($body); $lines = self::decodeLines($it); @@ -311,14 +343,9 @@ public static function decodeContent(MessageInterface $rsp): mixed return self::streamIterator($body); } - /** - * @param array $arr - * - * @return array - */ - public static function array_filter_omit(array $arr): array + public static function prettyEncodeJson(mixed $obj): string { - return array_filter($arr, fn ($v, $_) => OMIT !== $v, ARRAY_FILTER_USE_BOTH); + return json_encode($obj, flags: JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) ?: ''; } /** diff --git a/src/RequestOptions.php b/src/RequestOptions.php index 2a541ce..2ac8632 100644 --- a/src/RequestOptions.php +++ b/src/RequestOptions.php @@ -88,7 +88,7 @@ public function __unserialize(array $data): void } /** - * @param array{ + * @param RequestOptions|array{ * timeout?: float|null, * maxRetries?: int|null, * initialRetryDelay?: float|null, @@ -96,9 +96,9 @@ public function __unserialize(array $data): void * extraHeaders?: list|null, * extraQueryParams?: list|null, * extraBodyParams?: list|null, - * }|RequestOptions|null $options + * }|null $options */ - public static function parse(array|RequestOptions|null $options): self + public static function parse(RequestOptions|array|null $options): self { if (is_null($options)) { return new self; From ecebf37bdf9a4b6ede506820789805c8d4ae4f9e Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Wed, 27 Aug 2025 04:28:32 +0000 Subject: [PATCH 18/44] fix: basic pagination should work --- src/Core.php | 8 +------ src/Core/BaseClient.php | 8 +++---- src/Core/Concerns/SdkModel.php | 3 ++- .../AbstractPage.php => Concerns/SdkPage.php} | 24 +++++++++---------- src/Core/Contracts/BasePage.php | 18 +++++++++++++- src/Core/Conversion/ModelOf.php | 9 ++++--- src/Core/Implementation/Omittable.php | 13 ++++++++++ 7 files changed, 55 insertions(+), 28 deletions(-) rename src/Core/{Pagination/AbstractPage.php => Concerns/SdkPage.php} (84%) create mode 100644 src/Core/Implementation/Omittable.php diff --git a/src/Core.php b/src/Core.php index 441d490..0ddfc22 100644 --- a/src/Core.php +++ b/src/Core.php @@ -4,12 +4,6 @@ namespace Scrapegraphai\Core; -/** - * @internal - */ -enum Omittable -{ - case OMIT; -} +use Scrapegraphai\Core\Implementation\Omittable; const OMIT = Omittable::OMIT; diff --git a/src/Core/BaseClient.php b/src/Core/BaseClient.php index fd2f7e2..dcb8f5e 100644 --- a/src/Core/BaseClient.php +++ b/src/Core/BaseClient.php @@ -79,18 +79,18 @@ public function request( [$req, $opts] = $this->buildRequest(method: $method, path: $path, query: $query, headers: $headers, body: $body, opts: $options); ['method' => $method, 'path' => $uri, 'headers' => $headers] = $req; - $req = $this->requestFactory->createRequest($method, uri: $uri); - $req = Util::withSetHeaders($req, headers: $headers); + $request = $this->requestFactory->createRequest($method, uri: $uri); + $request = Util::withSetHeaders($request, headers: $headers); // @phpstan-ignore-next-line - $rsp = $this->sendRequest($req, data: $body, opts: $opts, redirectCount: 0, retryCount: 0); + $rsp = $this->sendRequest($request, data: $body, opts: $opts, redirectCount: 0, retryCount: 0); $decoded = Util::decodeContent($rsp); if (!is_null($stream)) { return new $stream( convert: $convert, - request: $req, + request: $request, response: $rsp, stream: $decoded ); diff --git a/src/Core/Concerns/SdkModel.php b/src/Core/Concerns/SdkModel.php index 3b25583..ed3152a 100644 --- a/src/Core/Concerns/SdkModel.php +++ b/src/Core/Concerns/SdkModel.php @@ -5,6 +5,7 @@ namespace Scrapegraphai\Core\Concerns; use Scrapegraphai\Core\Contracts\BaseModel; +use Scrapegraphai\Core\Contracts\BasePage; use Scrapegraphai\Core\Conversion; use Scrapegraphai\Core\Conversion\CoerceState; use Scrapegraphai\Core\Conversion\Contracts\Converter; @@ -244,7 +245,7 @@ private function unsetOptionalProperties(): void */ private static function serialize(mixed $value): mixed { - if ($value instanceof BaseModel) { + if ($value instanceof BaseModel || $value instanceof BasePage) { return $value->toArray(); } diff --git a/src/Core/Pagination/AbstractPage.php b/src/Core/Concerns/SdkPage.php similarity index 84% rename from src/Core/Pagination/AbstractPage.php rename to src/Core/Concerns/SdkPage.php index aa36c66..06f0b68 100644 --- a/src/Core/Pagination/AbstractPage.php +++ b/src/Core/Concerns/SdkPage.php @@ -2,10 +2,9 @@ declare(strict_types=1); -namespace Scrapegraphai\Core\Pagination; +namespace Scrapegraphai\Core\Concerns; use Scrapegraphai\Client; -use Scrapegraphai\Core\Contracts\BasePage; use Scrapegraphai\Core\Conversion\Contracts\Converter; use Scrapegraphai\Core\Conversion\Contracts\ConverterSource; use Scrapegraphai\Core\Errors\APIStatusError; @@ -16,19 +15,20 @@ * * @template Item * - * @implements BasePage - * * @phpstan-import-type normalized_request from \Scrapegraphai\Core\BaseClient */ -abstract class AbstractPage implements BasePage +trait SdkPage { - public function __construct( - protected Converter|ConverterSource|string $convert, - protected Client $client, - protected array $request, - protected RequestOptions $options, - protected mixed $data, - ) {} + private Converter|ConverterSource|string $convert; + + private Client $client; + + /** + * normalized_request $request. + */ + private array $request; + + private RequestOptions $options; /** * @return list diff --git a/src/Core/Contracts/BasePage.php b/src/Core/Contracts/BasePage.php index 1ed30c4..e434a07 100644 --- a/src/Core/Contracts/BasePage.php +++ b/src/Core/Contracts/BasePage.php @@ -10,11 +10,14 @@ use Scrapegraphai\RequestOptions; /** + * @internal + * * @template Item * + * @extends \ArrayAccess * @extends \IteratorAggregate */ -interface BasePage extends \IteratorAggregate +interface BasePage extends \ArrayAccess, \JsonSerializable, \Stringable, \IteratorAggregate { /** * @internal @@ -29,6 +32,14 @@ public function __construct( mixed $data, ); + /** + * @return static + */ + public static function fromArray(mixed $data): self; + + /** @return array */ + public function toArray(): array; + public function hasNextPage(): bool; /** @@ -40,4 +51,9 @@ public function getPaginatedItems(): array; * @return static */ public function getNextPage(): static; + + /** + * @return \Generator + */ + public function pagingEachItem(): \Generator; } diff --git a/src/Core/Conversion/ModelOf.php b/src/Core/Conversion/ModelOf.php index 306c717..773a1e2 100644 --- a/src/Core/Conversion/ModelOf.php +++ b/src/Core/Conversion/ModelOf.php @@ -6,6 +6,7 @@ use Scrapegraphai\Core\Attributes\Api; use Scrapegraphai\Core\Contracts\BaseModel; +use Scrapegraphai\Core\Contracts\BasePage; use Scrapegraphai\Core\Conversion; use Scrapegraphai\Core\Conversion\Contracts\Converter; @@ -20,7 +21,7 @@ final class ModelOf implements Converter public readonly array $properties; /** - * @param \ReflectionClass $class + * @param \ReflectionClass> $class */ public function __construct(public readonly \ReflectionClass $class) { @@ -93,8 +94,10 @@ public function coerce(mixed $value, CoerceState $state): mixed /** * @param array $data + * + * @return BaseModel|BasePage */ - public function from(array $data): BaseModel + public function from(array $data): BaseModel|BasePage { $instance = $this->class->newInstanceWithoutConstructor(); $instance->__unserialize($data); // @phpstan-ignore-line @@ -104,7 +107,7 @@ public function from(array $data): BaseModel public function dump(mixed $value, DumpState $state): mixed { - if ($value instanceof BaseModel) { + if ($value instanceof BaseModel || $value instanceof BasePage) { $value = $value->toArray(); } diff --git a/src/Core/Implementation/Omittable.php b/src/Core/Implementation/Omittable.php new file mode 100644 index 0000000..7ec1eb2 --- /dev/null +++ b/src/Core/Implementation/Omittable.php @@ -0,0 +1,13 @@ + Date: Wed, 27 Aug 2025 04:29:51 +0000 Subject: [PATCH 19/44] fix: minor bugs --- src/Core/Errors/APIStatusError.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Core/Errors/APIStatusError.php b/src/Core/Errors/APIStatusError.php index 9390a85..4bcdfdf 100644 --- a/src/Core/Errors/APIStatusError.php +++ b/src/Core/Errors/APIStatusError.php @@ -22,9 +22,7 @@ public function __construct( $this->response = $response; $this->status = $response->getStatusCode(); - $summary = 'Status: '.$this->status.PHP_EOL - .'Response Body: '.Util::prettyEncodeJson(Util::decodeJson($response->getBody())).PHP_EOL - .'Request Body: '.Util::prettyEncodeJson(Util::decodeJson($request->getBody())).PHP_EOL; + $summary = Util::prettyEncodeJson(['status' => $this->status, 'body' => Util::decodeJson($response->getBody())]); if ('' != $message) { $summary .= $message.PHP_EOL.$summary; From d79ffc9ea439c191f0ffed61f2752e7bfe47b00c Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Wed, 27 Aug 2025 04:35:59 +0000 Subject: [PATCH 20/44] feat!: pagination field rename, and basic streaming docs --- README.md | 2 ++ src/Core/Concerns/SdkPage.php | 8 ++++---- src/Core/Contracts/BasePage.php | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index f833e98..d4f16c2 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,7 @@ $client = new Client( $completedSmartscraper = $client->smartscraper->create( userPrompt: "Extract the product name, price, and description" ); + var_dump($completedSmartscraper->request_id); ``` @@ -123,6 +124,7 @@ use Scrapegraphai\RequestOptions; $client = new Client(maxRetries: 0); // Or, configure per-request: + $result = $client->smartscraper->create( userPrompt: "Extract the product name, price, and description", new RequestOptions(maxRetries: 5), diff --git a/src/Core/Concerns/SdkPage.php b/src/Core/Concerns/SdkPage.php index 06f0b68..2adc4e9 100644 --- a/src/Core/Concerns/SdkPage.php +++ b/src/Core/Concerns/SdkPage.php @@ -33,11 +33,11 @@ trait SdkPage /** * @return list */ - abstract public function getPaginatedItems(): array; + abstract public function getItems(): array; public function hasNextPage(): bool { - $items = $this->getPaginatedItems(); + $items = $this->getItems(); if (empty($items)) { return false; } @@ -66,7 +66,7 @@ public function getNextPage(): static [$req, $opts] = $next; // @phpstan-ignore-next-line - return $this->client->request(...$req, convert: $this->convert, page: $this, options: $opts); + return $this->client->request(...$req, convert: $this->convert, page: $this::class, options: $opts); } /** @@ -94,7 +94,7 @@ public function getIterator(): \Generator public function pagingEachItem(): \Generator { foreach ($this as $page) { - foreach ($page->getPaginatedItems() as $item) { + foreach ($page->getItems() as $item) { yield $item; } } diff --git a/src/Core/Contracts/BasePage.php b/src/Core/Contracts/BasePage.php index e434a07..5e08407 100644 --- a/src/Core/Contracts/BasePage.php +++ b/src/Core/Contracts/BasePage.php @@ -45,7 +45,7 @@ public function hasNextPage(): bool; /** * @return list */ - public function getPaginatedItems(): array; + public function getItems(): array; /** * @return static From 205ab97f9f2bc8ca38e9847850163701827777dc Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Wed, 27 Aug 2025 05:12:53 +0000 Subject: [PATCH 21/44] feat: ensure `->toArray()` benefits from structural typing --- src/Core/Concerns/SdkModel.php | 18 ++++++++++++------ src/Core/Concerns/SdkPage.php | 2 +- src/Core/Contracts/BasePage.php | 4 +++- src/Crawl/CrawlGetResultsResponse.php | 9 +++++++++ src/Crawl/CrawlStartParams.php | 13 +++++++++++++ src/Crawl/CrawlStartParams/Rules.php | 6 ++++++ src/Crawl/CrawlStartResponse.php | 4 ++++ src/Credits/CreditGetResponse.php | 6 ++++++ src/Feedback/FeedbackSubmitParams.php | 5 +++++ src/Feedback/FeedbackSubmitResponse.php | 9 +++++++++ .../GenerateSchemaCreateParams.php | 5 +++++ .../CompletedSchemaGenerationResponse.php | 11 +++++++++++ .../FailedSchemaGenerationResponse.php | 11 +++++++++++ .../GenerateSchemaNewResponse.php | 11 +++++++++++ src/Healthz/HealthzCheckResponse.php | 6 ++++++ src/Markdownify/CompletedMarkdownify.php | 10 ++++++++++ src/Markdownify/MarkdownifyConvertParams.php | 5 +++++ .../FailedMarkdownifyResponse.php | 10 ++++++++++ src/Searchscraper/CompletedSearchScraper.php | 12 ++++++++++++ .../SearchscraperCreateParams.php | 8 ++++++++ .../FailedSearchScraperResponse.php | 12 ++++++++++++ src/Smartscraper/CompletedSmartscraper.php | 11 +++++++++++ src/Smartscraper/FailedSmartscraper.php | 11 +++++++++++ src/Smartscraper/SmartscraperCreateParams.php | 14 ++++++++++++++ src/Validate/ValidateAPIKeyResponse.php | 4 ++++ tests/Core/TestModel.php | 1 + 26 files changed, 210 insertions(+), 8 deletions(-) diff --git a/src/Core/Concerns/SdkModel.php b/src/Core/Concerns/SdkModel.php index ed3152a..17ac024 100644 --- a/src/Core/Concerns/SdkModel.php +++ b/src/Core/Concerns/SdkModel.php @@ -14,6 +14,8 @@ /** * @internal + * + * @template-covariant Data of array */ trait SdkModel { @@ -65,13 +67,13 @@ public function __toString(): string } /** + * @internal + * * Magic get is intended to occur when we have manually unset * a native class property, indicating an omitted value, - * or a property overridden with an incongruent type. + * or a property overridden with an incongruent type * * @throws \Exception - * - * @internal */ public function __get(string $key): mixed { @@ -92,10 +94,12 @@ public function __get(string $key): mixed return null; } - /** @return array */ + /** + * @return Data + */ public function toArray(): array { - return $this->__serialize(); + return $this->__serialize(); // @phpstan-ignore-line } /** @@ -200,8 +204,10 @@ public function jsonSerialize(): array /** * @internal + * + * @param array $data */ - public static function fromArray(mixed $data): self + public static function fromArray(array $data): self { return self::converter()->from($data); // @phpstan-ignore-line } diff --git a/src/Core/Concerns/SdkPage.php b/src/Core/Concerns/SdkPage.php index 2adc4e9..8e3093e 100644 --- a/src/Core/Concerns/SdkPage.php +++ b/src/Core/Concerns/SdkPage.php @@ -50,7 +50,7 @@ public function hasNextPage(): bool * Before calling this method, you must check if there is a next page * using {@link hasNextPage()}. * - * @return static of AbstractPage + * @return static of static * * @throws APIStatusError */ diff --git a/src/Core/Contracts/BasePage.php b/src/Core/Contracts/BasePage.php index 5e08407..1dd9b58 100644 --- a/src/Core/Contracts/BasePage.php +++ b/src/Core/Contracts/BasePage.php @@ -33,9 +33,11 @@ public function __construct( ); /** + * @param array $data + * * @return static */ - public static function fromArray(mixed $data): self; + public static function fromArray(array $data): self; /** @return array */ public function toArray(): array; diff --git a/src/Crawl/CrawlGetResultsResponse.php b/src/Crawl/CrawlGetResultsResponse.php index ed4b322..d3bf78b 100644 --- a/src/Crawl/CrawlGetResultsResponse.php +++ b/src/Crawl/CrawlGetResultsResponse.php @@ -10,8 +10,17 @@ use Scrapegraphai\Crawl\CrawlGetResultsResponse\Result; use Scrapegraphai\Crawl\CrawlGetResultsResponse\Status; +/** + * @phpstan-type crawl_get_results_response = array{ + * result?: mixed|string|null, + * status?: Status::*|null, + * taskID?: string|null, + * traceback?: string|null, + * } + */ final class CrawlGetResultsResponse implements BaseModel { + /** @use SdkModel */ use SdkModel; /** diff --git a/src/Crawl/CrawlStartParams.php b/src/Crawl/CrawlStartParams.php index 3bbcb52..07b526f 100644 --- a/src/Crawl/CrawlStartParams.php +++ b/src/Crawl/CrawlStartParams.php @@ -14,9 +14,22 @@ * Initiate comprehensive website crawling with sitemap support. * Supports both AI extraction mode and markdown conversion mode. * Returns a task ID for async processing. + * + * @phpstan-type crawl_start_params = array{ + * url: string, + * depth?: int, + * extractionMode?: bool, + * maxPages?: int, + * prompt?: string|null, + * renderHeavyJs?: bool, + * rules?: Rules, + * schema?: mixed, + * sitemap?: bool, + * } */ final class CrawlStartParams implements BaseModel { + /** @use SdkModel */ use SdkModel; use SdkParams; diff --git a/src/Crawl/CrawlStartParams/Rules.php b/src/Crawl/CrawlStartParams/Rules.php index a73af47..887fd36 100644 --- a/src/Crawl/CrawlStartParams/Rules.php +++ b/src/Crawl/CrawlStartParams/Rules.php @@ -8,8 +8,14 @@ use Scrapegraphai\Core\Concerns\SdkModel; use Scrapegraphai\Core\Contracts\BaseModel; +/** + * @phpstan-type rules_alias = array{ + * exclude?: list|null, sameDomain?: bool|null + * } + */ final class Rules implements BaseModel { + /** @use SdkModel */ use SdkModel; /** diff --git a/src/Crawl/CrawlStartResponse.php b/src/Crawl/CrawlStartResponse.php index 878c758..29b1a44 100644 --- a/src/Crawl/CrawlStartResponse.php +++ b/src/Crawl/CrawlStartResponse.php @@ -8,8 +8,12 @@ use Scrapegraphai\Core\Concerns\SdkModel; use Scrapegraphai\Core\Contracts\BaseModel; +/** + * @phpstan-type crawl_start_response = array{taskID?: string|null} + */ final class CrawlStartResponse implements BaseModel { + /** @use SdkModel */ use SdkModel; /** diff --git a/src/Credits/CreditGetResponse.php b/src/Credits/CreditGetResponse.php index 9da136b..184e0a5 100644 --- a/src/Credits/CreditGetResponse.php +++ b/src/Credits/CreditGetResponse.php @@ -8,8 +8,14 @@ use Scrapegraphai\Core\Concerns\SdkModel; use Scrapegraphai\Core\Contracts\BaseModel; +/** + * @phpstan-type credit_get_response = array{ + * remainingCredits?: int|null, totalCreditsUsed?: int|null + * } + */ final class CreditGetResponse implements BaseModel { + /** @use SdkModel */ use SdkModel; /** diff --git a/src/Feedback/FeedbackSubmitParams.php b/src/Feedback/FeedbackSubmitParams.php index e7372b6..d7f128e 100644 --- a/src/Feedback/FeedbackSubmitParams.php +++ b/src/Feedback/FeedbackSubmitParams.php @@ -11,9 +11,14 @@ /** * Submit feedback for a specific request. + * + * @phpstan-type feedback_submit_params = array{ + * rating: int, requestID: string, feedbackText?: string|null + * } */ final class FeedbackSubmitParams implements BaseModel { + /** @use SdkModel */ use SdkModel; use SdkParams; diff --git a/src/Feedback/FeedbackSubmitResponse.php b/src/Feedback/FeedbackSubmitResponse.php index 77a94d1..0f034eb 100644 --- a/src/Feedback/FeedbackSubmitResponse.php +++ b/src/Feedback/FeedbackSubmitResponse.php @@ -8,8 +8,17 @@ use Scrapegraphai\Core\Concerns\SdkModel; use Scrapegraphai\Core\Contracts\BaseModel; +/** + * @phpstan-type feedback_submit_response = array{ + * feedbackID?: string|null, + * feedbackTimestamp?: \DateTimeInterface|null, + * message?: string|null, + * requestID?: string|null, + * } + */ final class FeedbackSubmitResponse implements BaseModel { + /** @use SdkModel */ use SdkModel; #[Api('feedback_id', optional: true)] diff --git a/src/GenerateSchema/GenerateSchemaCreateParams.php b/src/GenerateSchema/GenerateSchemaCreateParams.php index eed8204..06cc8d1 100644 --- a/src/GenerateSchema/GenerateSchemaCreateParams.php +++ b/src/GenerateSchema/GenerateSchemaCreateParams.php @@ -12,9 +12,14 @@ /** * Generate or modify JSON schemas based on natural language descriptions. * Can create new schemas or extend existing ones. + * + * @phpstan-type generate_schema_create_params = array{ + * userPrompt: string, existingSchema?: mixed + * } */ final class GenerateSchemaCreateParams implements BaseModel { + /** @use SdkModel */ use SdkModel; use SdkParams; diff --git a/src/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse.php b/src/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse.php index b80b094..d13746e 100644 --- a/src/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse.php +++ b/src/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse.php @@ -9,8 +9,19 @@ use Scrapegraphai\Core\Contracts\BaseModel; use Scrapegraphai\GenerateSchema\GenerateSchemaGetResponse\CompletedSchemaGenerationResponse\Status; +/** + * @phpstan-type completed_schema_generation_response = array{ + * error?: string|null, + * generatedSchema?: mixed, + * refinedPrompt?: string|null, + * requestID?: string|null, + * status?: Status::*|null, + * userPrompt?: string|null, + * } + */ final class CompletedSchemaGenerationResponse implements BaseModel { + /** @use SdkModel */ use SdkModel; #[Api(nullable: true, optional: true)] diff --git a/src/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse.php b/src/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse.php index e6ec7d7..8103b5f 100644 --- a/src/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse.php +++ b/src/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse.php @@ -9,8 +9,19 @@ use Scrapegraphai\Core\Contracts\BaseModel; use Scrapegraphai\GenerateSchema\GenerateSchemaGetResponse\FailedSchemaGenerationResponse\Status; +/** + * @phpstan-type failed_schema_generation_response = array{ + * error?: string|null, + * generatedSchema?: mixed, + * refinedPrompt?: string|null, + * requestID?: string|null, + * status?: Status::*|null, + * userPrompt?: string|null, + * } + */ final class FailedSchemaGenerationResponse implements BaseModel { + /** @use SdkModel */ use SdkModel; #[Api(optional: true)] diff --git a/src/GenerateSchema/GenerateSchemaNewResponse.php b/src/GenerateSchema/GenerateSchemaNewResponse.php index 184a810..c4cfe0b 100644 --- a/src/GenerateSchema/GenerateSchemaNewResponse.php +++ b/src/GenerateSchema/GenerateSchemaNewResponse.php @@ -9,8 +9,19 @@ use Scrapegraphai\Core\Contracts\BaseModel; use Scrapegraphai\GenerateSchema\GenerateSchemaNewResponse\Status; +/** + * @phpstan-type generate_schema_new_response = array{ + * error?: string|null, + * generatedSchema?: mixed, + * refinedPrompt?: string|null, + * requestID?: string|null, + * status?: Status::*|null, + * userPrompt?: string|null, + * } + */ final class GenerateSchemaNewResponse implements BaseModel { + /** @use SdkModel */ use SdkModel; #[Api(nullable: true, optional: true)] diff --git a/src/Healthz/HealthzCheckResponse.php b/src/Healthz/HealthzCheckResponse.php index b2cbcb4..81b5b75 100644 --- a/src/Healthz/HealthzCheckResponse.php +++ b/src/Healthz/HealthzCheckResponse.php @@ -8,8 +8,14 @@ use Scrapegraphai\Core\Concerns\SdkModel; use Scrapegraphai\Core\Contracts\BaseModel; +/** + * @phpstan-type healthz_check_response = array{ + * services?: array|null, status?: string|null + * } + */ final class HealthzCheckResponse implements BaseModel { + /** @use SdkModel */ use SdkModel; /** @var array|null $services */ diff --git a/src/Markdownify/CompletedMarkdownify.php b/src/Markdownify/CompletedMarkdownify.php index ab99144..18b57b0 100644 --- a/src/Markdownify/CompletedMarkdownify.php +++ b/src/Markdownify/CompletedMarkdownify.php @@ -9,8 +9,18 @@ use Scrapegraphai\Core\Contracts\BaseModel; use Scrapegraphai\Markdownify\CompletedMarkdownify\Status; +/** + * @phpstan-type completed_markdownify = array{ + * error?: string|null, + * requestID?: string|null, + * result?: string|null, + * status?: Status::*|null, + * websiteURL?: string|null, + * } + */ final class CompletedMarkdownify implements BaseModel { + /** @use SdkModel */ use SdkModel; #[Api(optional: true)] diff --git a/src/Markdownify/MarkdownifyConvertParams.php b/src/Markdownify/MarkdownifyConvertParams.php index 9699ef2..59ba4b7 100644 --- a/src/Markdownify/MarkdownifyConvertParams.php +++ b/src/Markdownify/MarkdownifyConvertParams.php @@ -11,9 +11,14 @@ /** * Convert web page content to clean Markdown format. + * + * @phpstan-type markdownify_convert_params = array{ + * websiteURL: string, headers?: array, steps?: list + * } */ final class MarkdownifyConvertParams implements BaseModel { + /** @use SdkModel */ use SdkModel; use SdkParams; diff --git a/src/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse.php b/src/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse.php index 21ab267..b91e3b4 100644 --- a/src/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse.php +++ b/src/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse.php @@ -9,8 +9,18 @@ use Scrapegraphai\Core\Contracts\BaseModel; use Scrapegraphai\Markdownify\MarkdownifyGetStatusResponse\FailedMarkdownifyResponse\Status; +/** + * @phpstan-type failed_markdownify_response = array{ + * error?: string|null, + * requestID?: string|null, + * result?: string|null, + * status?: Status::*|null, + * websiteURL?: string|null, + * } + */ final class FailedMarkdownifyResponse implements BaseModel { + /** @use SdkModel */ use SdkModel; #[Api(optional: true)] diff --git a/src/Searchscraper/CompletedSearchScraper.php b/src/Searchscraper/CompletedSearchScraper.php index 701b23e..e5571b6 100644 --- a/src/Searchscraper/CompletedSearchScraper.php +++ b/src/Searchscraper/CompletedSearchScraper.php @@ -9,8 +9,20 @@ use Scrapegraphai\Core\Contracts\BaseModel; use Scrapegraphai\Searchscraper\CompletedSearchScraper\Status; +/** + * @phpstan-type completed_search_scraper = array{ + * error?: string|null, + * numResults?: int|null, + * referenceURLs?: list|null, + * requestID?: string|null, + * result?: mixed, + * status?: Status::*|null, + * userPrompt?: string|null, + * } + */ final class CompletedSearchScraper implements BaseModel { + /** @use SdkModel */ use SdkModel; #[Api(nullable: true, optional: true)] diff --git a/src/Searchscraper/SearchscraperCreateParams.php b/src/Searchscraper/SearchscraperCreateParams.php index 4dd94ee..9888016 100644 --- a/src/Searchscraper/SearchscraperCreateParams.php +++ b/src/Searchscraper/SearchscraperCreateParams.php @@ -12,9 +12,17 @@ /** * Performs web search, selects relevant URLs, and extracts structured data from multiple websites. * Uses LLM to refine search queries and merge results from different sources. + * + * @phpstan-type searchscraper_create_params = array{ + * userPrompt: string, + * headers?: array, + * numResults?: int, + * outputSchema?: mixed, + * } */ final class SearchscraperCreateParams implements BaseModel { + /** @use SdkModel */ use SdkModel; use SdkParams; diff --git a/src/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse.php b/src/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse.php index 40c1303..849f4ce 100644 --- a/src/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse.php +++ b/src/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse.php @@ -9,8 +9,20 @@ use Scrapegraphai\Core\Contracts\BaseModel; use Scrapegraphai\Searchscraper\SearchscraperGetStatusResponse\FailedSearchScraperResponse\Status; +/** + * @phpstan-type failed_search_scraper_response = array{ + * error?: string|null, + * numResults?: int|null, + * referenceURLs?: list|null, + * requestID?: string|null, + * result?: mixed, + * status?: Status::*|null, + * userPrompt?: string|null, + * } + */ final class FailedSearchScraperResponse implements BaseModel { + /** @use SdkModel */ use SdkModel; #[Api(optional: true)] diff --git a/src/Smartscraper/CompletedSmartscraper.php b/src/Smartscraper/CompletedSmartscraper.php index 684284b..3d55572 100644 --- a/src/Smartscraper/CompletedSmartscraper.php +++ b/src/Smartscraper/CompletedSmartscraper.php @@ -9,8 +9,19 @@ use Scrapegraphai\Core\Contracts\BaseModel; use Scrapegraphai\Smartscraper\CompletedSmartscraper\Status; +/** + * @phpstan-type completed_smartscraper = array{ + * error?: string|null, + * requestID?: string|null, + * result?: mixed, + * status?: Status::*|null, + * userPrompt?: string|null, + * websiteURL?: string|null, + * } + */ final class CompletedSmartscraper implements BaseModel { + /** @use SdkModel */ use SdkModel; /** diff --git a/src/Smartscraper/FailedSmartscraper.php b/src/Smartscraper/FailedSmartscraper.php index 5d2411d..63461e5 100644 --- a/src/Smartscraper/FailedSmartscraper.php +++ b/src/Smartscraper/FailedSmartscraper.php @@ -9,8 +9,19 @@ use Scrapegraphai\Core\Contracts\BaseModel; use Scrapegraphai\Smartscraper\FailedSmartscraper\Status; +/** + * @phpstan-type failed_smartscraper = array{ + * error?: string|null, + * requestID?: string|null, + * result?: mixed, + * status?: Status::*|null, + * userPrompt?: string|null, + * websiteURL?: string|null, + * } + */ final class FailedSmartscraper implements BaseModel { + /** @use SdkModel */ use SdkModel; /** diff --git a/src/Smartscraper/SmartscraperCreateParams.php b/src/Smartscraper/SmartscraperCreateParams.php index 44867ef..4c1a7dc 100644 --- a/src/Smartscraper/SmartscraperCreateParams.php +++ b/src/Smartscraper/SmartscraperCreateParams.php @@ -12,9 +12,23 @@ /** * Main scraping endpoint with LLM-powered content analysis. Supports various fetching providers, * infinite scrolling, pagination, and custom output schemas. + * + * @phpstan-type smartscraper_create_params = array{ + * userPrompt: string, + * cookies?: array, + * headers?: array, + * numberOfScrolls?: int, + * outputSchema?: mixed, + * renderHeavyJs?: bool, + * steps?: list, + * totalPages?: int, + * websiteHTML?: string, + * websiteURL?: string, + * } */ final class SmartscraperCreateParams implements BaseModel { + /** @use SdkModel */ use SdkModel; use SdkParams; diff --git a/src/Validate/ValidateAPIKeyResponse.php b/src/Validate/ValidateAPIKeyResponse.php index 1d6a417..9288d86 100644 --- a/src/Validate/ValidateAPIKeyResponse.php +++ b/src/Validate/ValidateAPIKeyResponse.php @@ -8,8 +8,12 @@ use Scrapegraphai\Core\Concerns\SdkModel; use Scrapegraphai\Core\Contracts\BaseModel; +/** + * @phpstan-type validate_api_key_response = array{email?: string|null} + */ final class ValidateAPIKeyResponse implements BaseModel { + /** @use SdkModel */ use SdkModel; #[Api(optional: true)] diff --git a/tests/Core/TestModel.php b/tests/Core/TestModel.php index 6e07607..2a119a5 100644 --- a/tests/Core/TestModel.php +++ b/tests/Core/TestModel.php @@ -11,6 +11,7 @@ class TestModel implements BaseModel { + /** @use SdkModel> */ use SdkModel; #[Api] From b568600fb25d39cbd315e98dc8654360383523fe Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Wed, 27 Aug 2025 05:14:55 +0000 Subject: [PATCH 22/44] feat!: rename errors to exceptions --- README.md | 32 +++++++++---------- src/Core/BaseClient.php | 4 +-- src/Core/Concerns/SdkPage.php | 4 +-- src/Core/Errors/AuthenticationError.php | 9 ------ src/Core/Errors/BadRequestError.php | 9 ------ src/Core/Errors/ConflictError.php | 9 ------ src/Core/Errors/InternalServerError.php | 9 ------ src/Core/Errors/NotFoundError.php | 9 ------ src/Core/Errors/PermissionDeniedError.php | 9 ------ src/Core/Errors/RateLimitError.php | 9 ------ src/Core/Errors/UnprocessableEntityError.php | 9 ------ .../APIConnectionException.php} | 4 +-- .../APIException.php} | 4 +-- .../APIStatusException.php} | 22 ++++++------- .../APITimeoutException.php} | 6 ++-- .../Exceptions/AuthenticationException.php | 9 ++++++ src/Core/Exceptions/BadRequestException.php | 9 ++++++ src/Core/Exceptions/ConflictException.php | 9 ++++++ .../Exceptions/InternalServerException.php | 9 ++++++ src/Core/Exceptions/NotFoundException.php | 9 ++++++ .../Exceptions/PermissionDeniedException.php | 9 ++++++ src/Core/Exceptions/RateLimitException.php | 9 ++++++ .../ScrapegraphaiException.php} | 4 +-- .../UnprocessableEntityException.php | 9 ++++++ 24 files changed, 112 insertions(+), 112 deletions(-) delete mode 100644 src/Core/Errors/AuthenticationError.php delete mode 100644 src/Core/Errors/BadRequestError.php delete mode 100644 src/Core/Errors/ConflictError.php delete mode 100644 src/Core/Errors/InternalServerError.php delete mode 100644 src/Core/Errors/NotFoundError.php delete mode 100644 src/Core/Errors/PermissionDeniedError.php delete mode 100644 src/Core/Errors/RateLimitError.php delete mode 100644 src/Core/Errors/UnprocessableEntityError.php rename src/Core/{Errors/APIConnectionError.php => Exceptions/APIConnectionException.php} (52%) rename src/Core/{Errors/APIError.php => Exceptions/APIException.php} (82%) rename src/Core/{Errors/APIStatusError.php => Exceptions/APIStatusException.php} (64%) rename src/Core/{Errors/APITimeoutError.php => Exceptions/APITimeoutException.php} (67%) create mode 100644 src/Core/Exceptions/AuthenticationException.php create mode 100644 src/Core/Exceptions/BadRequestException.php create mode 100644 src/Core/Exceptions/ConflictException.php create mode 100644 src/Core/Exceptions/InternalServerException.php create mode 100644 src/Core/Exceptions/NotFoundException.php create mode 100644 src/Core/Exceptions/PermissionDeniedException.php create mode 100644 src/Core/Exceptions/RateLimitException.php rename src/Core/{Errors/ScrapegraphaiError.php => Exceptions/ScrapegraphaiException.php} (75%) create mode 100644 src/Core/Exceptions/UnprocessableEntityException.php diff --git a/README.md b/README.md index d4f16c2..4891217 100644 --- a/README.md +++ b/README.md @@ -68,18 +68,18 @@ However, builders are also provided `(new Dog)->withName("Joey")`. ### Handling errors -When the library is unable to connect to the API, or if the API returns a non-success status code (i.e., 4xx or 5xx response), a subclass of `Scrapegraphai\Errors\APIError` will be thrown: +When the library is unable to connect to the API, or if the API returns a non-success status code (i.e., 4xx or 5xx response), a subclass of `Scrapegraphai\Core\Exceptions\APIException` will be thrown: ```php smartscraper->create( userPrompt: "Extract the product name, price, and description" ); -} catch (APIConnectionError $e) { +} catch (APIConnectionException $e) { echo "The server could not be reached", PHP_EOL; var_dump($e->getPrevious()); } catch (RateLimitError $_) { @@ -92,19 +92,19 @@ try { Error codes are as follows: -| Cause | Error Type | -| ---------------- | -------------------------- | -| HTTP 400 | `BadRequestError` | -| HTTP 401 | `AuthenticationError` | -| HTTP 403 | `PermissionDeniedError` | -| HTTP 404 | `NotFoundError` | -| HTTP 409 | `ConflictError` | -| HTTP 422 | `UnprocessableEntityError` | -| HTTP 429 | `RateLimitError` | -| HTTP >= 500 | `InternalServerError` | -| Other HTTP error | `APIStatusError` | -| Timeout | `APITimeoutError` | -| Network error | `APIConnectionError` | +| Cause | Error Type | +| ---------------- | ------------------------------ | +| HTTP 400 | `BadRequestException` | +| HTTP 401 | `AuthenticationException` | +| HTTP 403 | `PermissionDeniedException` | +| HTTP 404 | `NotFoundException` | +| HTTP 409 | `ConflictException` | +| HTTP 422 | `UnprocessableEntityException` | +| HTTP 429 | `RateLimitException` | +| HTTP >= 500 | `InternalServerException` | +| Other HTTP error | `APIStatusException` | +| Timeout | `APITimeoutException` | +| Network error | `APIConnectionException` | ### Retries diff --git a/src/Core/BaseClient.php b/src/Core/BaseClient.php index dcb8f5e..b53df31 100644 --- a/src/Core/BaseClient.php +++ b/src/Core/BaseClient.php @@ -17,7 +17,7 @@ use Scrapegraphai\Core\Contracts\BaseStream; use Scrapegraphai\Core\Conversion\Contracts\Converter; use Scrapegraphai\Core\Conversion\Contracts\ConverterSource; -use Scrapegraphai\Core\Errors\APIStatusError; +use Scrapegraphai\Core\Exceptions\APIStatusException; use Scrapegraphai\RequestOptions; /** @@ -203,7 +203,7 @@ protected function sendRequest( } if ($code >= 400 && $code < 500) { - throw APIStatusError::from(request: $req, response: $rsp); + throw APIStatusException::from(request: $req, response: $rsp); } if ($code >= 500 && $retryCount < $opts->maxRetries) { diff --git a/src/Core/Concerns/SdkPage.php b/src/Core/Concerns/SdkPage.php index 8e3093e..bb35fe3 100644 --- a/src/Core/Concerns/SdkPage.php +++ b/src/Core/Concerns/SdkPage.php @@ -7,7 +7,7 @@ use Scrapegraphai\Client; use Scrapegraphai\Core\Conversion\Contracts\Converter; use Scrapegraphai\Core\Conversion\Contracts\ConverterSource; -use Scrapegraphai\Core\Errors\APIStatusError; +use Scrapegraphai\Core\Exceptions\APIStatusException; use Scrapegraphai\RequestOptions; /** @@ -52,7 +52,7 @@ public function hasNextPage(): bool * * @return static of static * - * @throws APIStatusError + * @throws APIStatusException */ public function getNextPage(): static { diff --git a/src/Core/Errors/AuthenticationError.php b/src/Core/Errors/AuthenticationError.php deleted file mode 100644 index 1f5d7af..0000000 --- a/src/Core/Errors/AuthenticationError.php +++ /dev/null @@ -1,9 +0,0 @@ -getStatusCode(); $cls = match (true) { - 400 === $status => BadRequestError::class, - 401 === $status => AuthenticationError::class, - 403 === $status => PermissionDeniedError::class, - 404 === $status => NotFoundError::class, - 409 === $status => ConflictError::class, - 422 === $status => UnprocessableEntityError::class, - 429 === $status => RateLimitError::class, - $status >= 500 => InternalServerError::class, - default => APIStatusError::class + 400 === $status => BadRequestException::class, + 401 === $status => AuthenticationException::class, + 403 === $status => PermissionDeniedException::class, + 404 === $status => NotFoundException::class, + 409 === $status => ConflictException::class, + 422 === $status => UnprocessableEntityException::class, + 429 === $status => RateLimitException::class, + $status >= 500 => InternalServerException::class, + default => APIStatusException::class }; return new $cls(request: $request, response: $response, message: $message); diff --git a/src/Core/Errors/APITimeoutError.php b/src/Core/Exceptions/APITimeoutException.php similarity index 67% rename from src/Core/Errors/APITimeoutError.php rename to src/Core/Exceptions/APITimeoutException.php index 0f164b3..4db21ac 100644 --- a/src/Core/Errors/APITimeoutError.php +++ b/src/Core/Exceptions/APITimeoutException.php @@ -1,13 +1,13 @@ Date: Wed, 27 Aug 2025 05:16:50 +0000 Subject: [PATCH 23/44] fix: add create release workflow --- .github/workflows/release-doctor.yml | 19 +++++++++++++++++++ bin/check-release-environment | 25 +++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 .github/workflows/release-doctor.yml create mode 100644 bin/check-release-environment diff --git a/.github/workflows/release-doctor.yml b/.github/workflows/release-doctor.yml new file mode 100644 index 0000000..22b3397 --- /dev/null +++ b/.github/workflows/release-doctor.yml @@ -0,0 +1,19 @@ +name: Release Doctor +on: + pull_request: + branches: + - main + workflow_dispatch: + +jobs: + release_doctor: + name: release doctor + runs-on: ubuntu-latest + if: github.repository == 'ScrapeGraphAI/scrapegraphai-php' && (github.event_name == 'push' || github.event_name == 'workflow_dispatch' || startsWith(github.head_ref, 'release-please') || github.head_ref == 'next') + + steps: + - uses: actions/checkout@v4 + + - name: Check release environment + run: | + bash ./bin/check-release-environment diff --git a/bin/check-release-environment b/bin/check-release-environment new file mode 100644 index 0000000..cf571b6 --- /dev/null +++ b/bin/check-release-environment @@ -0,0 +1,25 @@ +#!/usr/bin/env bash + +errors=() + +if [ -z "${PACKAGIST_USERNAME}" ]; then + errors+=("The PACKAGIST_USERNAME secret has not been set. Please set it in either this repository's secrets or your organization secrets") +fi + +if [ -z "${PACKAGIST_SAFE_KEY}" ]; then + errors+=("The PACKAGIST_SAFE_KEY secret has not been set. Please set it in either this repository's secrets or your organization secrets") +fi + +lenErrors=${#errors[@]} + +if [[ lenErrors -gt 0 ]]; then + echo -e "Found the following errors in the release environment:\n" + + for error in "${errors[@]}"; do + echo -e "- $error\n" + done + + exit 1 +fi + +echo "The environment is ready to push releases!" From 03a02b472282af3977cab5be1296edee8a8750ce Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Thu, 28 Aug 2025 02:55:35 +0000 Subject: [PATCH 24/44] chore: remove `php-http/multipart-stream-builder` as a required dependency --- composer.json | 8 ++------ src/Core/Concerns/SdkModel.php | 2 -- src/Core/Contracts/BaseStream.php | 2 ++ src/Core/Conversion/UnionOf.php | 3 +++ 4 files changed, 7 insertions(+), 8 deletions(-) diff --git a/composer.json b/composer.json index 056aa80..cd5cb4d 100644 --- a/composer.json +++ b/composer.json @@ -1,10 +1,7 @@ { "$schema": "https://getcomposer.org/schema.json", "autoload": { - "files": [ - "src/Core.php", - "src/Client.php" - ], + "files": ["src/Core.php", "src/Client.php"], "psr-4": { "Scrapegraphai\\": "src/" } @@ -26,13 +23,12 @@ "preferred-install": "dist", "sort-packages": true }, - "license": "APACHE-2.0", "description": "Scrapegraphai PHP SDK", + "license": "APACHE-2.0", "name": "org-placeholder/scrapegraphai", "require": { "php": "^8.1", "php-http/discovery": "^1", - "php-http/multipart-stream-builder": "^1", "psr/http-client": "^1", "psr/http-client-implementation": "^1", "psr/http-factory-implementation": "^1", diff --git a/src/Core/Concerns/SdkModel.php b/src/Core/Concerns/SdkModel.php index 17ac024..88e1f8c 100644 --- a/src/Core/Concerns/SdkModel.php +++ b/src/Core/Concerns/SdkModel.php @@ -203,8 +203,6 @@ public function jsonSerialize(): array } /** - * @internal - * * @param array $data */ public static function fromArray(array $data): self diff --git a/src/Core/Contracts/BaseStream.php b/src/Core/Contracts/BaseStream.php index b467d7f..2f95a2b 100644 --- a/src/Core/Contracts/BaseStream.php +++ b/src/Core/Contracts/BaseStream.php @@ -10,6 +10,8 @@ use Scrapegraphai\Core\Conversion\Contracts\ConverterSource; /** + * @internal + * * @template TInner * * @extends \IteratorAggregate diff --git a/src/Core/Conversion/UnionOf.php b/src/Core/Conversion/UnionOf.php index 1ef8a1b..e350d4e 100644 --- a/src/Core/Conversion/UnionOf.php +++ b/src/Core/Conversion/UnionOf.php @@ -9,6 +9,9 @@ use Scrapegraphai\Core\Conversion\Contracts\Converter; use Scrapegraphai\Core\Conversion\Contracts\ConverterSource; +/** + * @internal + */ final class UnionOf implements Converter { /** From 1cfba5fa543a792ecf6651259309247fa3ce726b Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Thu, 28 Aug 2025 02:56:39 +0000 Subject: [PATCH 25/44] fix: remove inaccurate `license` field in composer.json --- composer.json | 1 - 1 file changed, 1 deletion(-) diff --git a/composer.json b/composer.json index cd5cb4d..396912a 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,6 @@ "sort-packages": true }, "description": "Scrapegraphai PHP SDK", - "license": "APACHE-2.0", "name": "org-placeholder/scrapegraphai", "require": { "php": "^8.1", From 77b2f7692824f3d6d7ff151704174cfe550b1339 Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Thu, 28 Aug 2025 03:00:09 +0000 Subject: [PATCH 26/44] chore(refactor): simplify base page interface --- src/Core/Concerns/SdkModel.php | 43 +++++++++++++++++++++------------ src/Core/Concerns/SdkPage.php | 7 ++++++ src/Core/Contracts/BasePage.php | 13 +--------- src/Core/Conversion/ModelOf.php | 13 ++++------ 4 files changed, 40 insertions(+), 36 deletions(-) diff --git a/src/Core/Concerns/SdkModel.php b/src/Core/Concerns/SdkModel.php index 88e1f8c..6b71d29 100644 --- a/src/Core/Concerns/SdkModel.php +++ b/src/Core/Concerns/SdkModel.php @@ -5,7 +5,6 @@ namespace Scrapegraphai\Core\Concerns; use Scrapegraphai\Core\Contracts\BaseModel; -use Scrapegraphai\Core\Contracts\BasePage; use Scrapegraphai\Core\Conversion; use Scrapegraphai\Core\Conversion\CoerceState; use Scrapegraphai\Core\Conversion\Contracts\Converter; @@ -15,7 +14,7 @@ /** * @internal * - * @template-covariant Data of array + * @template-covariant Shape of array */ trait SdkModel { @@ -41,12 +40,12 @@ public function __serialize(): array /** * @internal * - * @param array $data + * @param array $data */ public function __unserialize(array $data): void { foreach ($data as $key => $value) { - $this->offsetSet($key, value: $value); + $this->offsetSet($key, value: $value); // @phpstan-ignore-line } } @@ -73,12 +72,14 @@ public function __toString(): string * a native class property, indicating an omitted value, * or a property overridden with an incongruent type * + * @return value-of + * * @throws \Exception */ public function __get(string $key): mixed { if (!array_key_exists($key, array: self::$converter->properties)) { - throw new \Exception("Property '{$key}' does not exist in {$this}::class"); + throw new \RuntimeException("Property '{$key}' does not exist in {$this}::class"); } // The unset property was overridden by a value with an incongruent type. @@ -91,11 +92,11 @@ public function __get(string $key): mixed // An optional property which was unset to be omitted from serialized is being accessed. // Return null to match user's expectations. - return null; + return null; // @phpstan-ignore-line } /** - * @return Data + * @return Shape */ public function toArray(): array { @@ -104,6 +105,8 @@ public function toArray(): array /** * @internal + * + * @param key-of $offset */ public function offsetExists(mixed $offset): bool { @@ -130,6 +133,10 @@ public function offsetExists(mixed $offset): bool /** * @internal + * + * @param key-of $offset + * + * @return value-of */ public function &offsetGet(mixed $offset): mixed { @@ -137,19 +144,21 @@ public function &offsetGet(mixed $offset): mixed throw new \InvalidArgumentException; } - if (!$this->offsetExists($offset)) { - return null; + if (!$this->offsetExists($offset)) { // @phpstan-ignore-line + return null; // @phpstan-ignore-line } if (array_key_exists($offset, array: $this->_data)) { - return $this->_data[$offset]; + return $this->_data[$offset]; // @phpstan-ignore-line } - return $this->{$offset}; + return $this->{$offset}; // @phpstan-ignore-line } /** * @internal + * + * @param key-of $offset */ public function offsetSet(mixed $offset, mixed $value): void { @@ -163,9 +172,9 @@ public function offsetSet(mixed $offset, mixed $value): void $coerced = Conversion::coerce($type, value: $value, state: new CoerceState(translateNames: false)); - if (property_exists($this, property: $offset)) { + if (property_exists($this, property: $offset)) { // @phpstan-ignore-line try { - $this->{$offset} = $coerced; + $this->{$offset} = $coerced; // @phpstan-ignore-line unset($this->_data[$offset]); return; @@ -179,6 +188,8 @@ public function offsetSet(mixed $offset, mixed $value): void /** * @internal + * + * @param key-of $offset */ public function offsetUnset(mixed $offset): void { @@ -186,7 +197,7 @@ public function offsetUnset(mixed $offset): void throw new \InvalidArgumentException; } - if (property_exists($this, property: $offset)) { + if (property_exists($this, property: $offset)) { // @phpstan-ignore-line unset($this->{$offset}); } @@ -205,7 +216,7 @@ public function jsonSerialize(): array /** * @param array $data */ - public static function fromArray(array $data): self + public static function fromArray(array $data): static { return self::converter()->from($data); // @phpstan-ignore-line } @@ -249,7 +260,7 @@ private function unsetOptionalProperties(): void */ private static function serialize(mixed $value): mixed { - if ($value instanceof BaseModel || $value instanceof BasePage) { + if ($value instanceof BaseModel) { return $value->toArray(); } diff --git a/src/Core/Concerns/SdkPage.php b/src/Core/Concerns/SdkPage.php index bb35fe3..36f82de 100644 --- a/src/Core/Concerns/SdkPage.php +++ b/src/Core/Concerns/SdkPage.php @@ -100,6 +100,13 @@ public function pagingEachItem(): \Generator } } + /** + * @param array $data + * + * @return static + */ + abstract public static function fromArray(array $data): static; + /** * @return array{normalized_request, RequestOptions} */ diff --git a/src/Core/Contracts/BasePage.php b/src/Core/Contracts/BasePage.php index 1dd9b58..bde0d48 100644 --- a/src/Core/Contracts/BasePage.php +++ b/src/Core/Contracts/BasePage.php @@ -14,10 +14,9 @@ * * @template Item * - * @extends \ArrayAccess * @extends \IteratorAggregate */ -interface BasePage extends \ArrayAccess, \JsonSerializable, \Stringable, \IteratorAggregate +interface BasePage extends \IteratorAggregate { /** * @internal @@ -32,16 +31,6 @@ public function __construct( mixed $data, ); - /** - * @param array $data - * - * @return static - */ - public static function fromArray(array $data): self; - - /** @return array */ - public function toArray(): array; - public function hasNextPage(): bool; /** diff --git a/src/Core/Conversion/ModelOf.php b/src/Core/Conversion/ModelOf.php index 773a1e2..8207dbc 100644 --- a/src/Core/Conversion/ModelOf.php +++ b/src/Core/Conversion/ModelOf.php @@ -6,7 +6,6 @@ use Scrapegraphai\Core\Attributes\Api; use Scrapegraphai\Core\Contracts\BaseModel; -use Scrapegraphai\Core\Contracts\BasePage; use Scrapegraphai\Core\Conversion; use Scrapegraphai\Core\Conversion\Contracts\Converter; @@ -21,7 +20,7 @@ final class ModelOf implements Converter public readonly array $properties; /** - * @param \ReflectionClass> $class + * @param \ReflectionClass $class */ public function __construct(public readonly \ReflectionClass $class) { @@ -89,15 +88,13 @@ public function coerce(mixed $value, CoerceState $state): mixed $acc[$name] = $item; } - return $this->from($acc); + return $this->from($acc); // @phpstan-ignore-line } /** - * @param array $data - * - * @return BaseModel|BasePage + * @param array $data */ - public function from(array $data): BaseModel|BasePage + public function from(array $data): BaseModel { $instance = $this->class->newInstanceWithoutConstructor(); $instance->__unserialize($data); // @phpstan-ignore-line @@ -107,7 +104,7 @@ public function from(array $data): BaseModel|BasePage public function dump(mixed $value, DumpState $state): mixed { - if ($value instanceof BaseModel || $value instanceof BasePage) { + if ($value instanceof BaseModel) { $value = $value->toArray(); } From 3a18c70daa9fa2d26264429cddd2b092bfcd5473 Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Fri, 29 Aug 2025 02:51:02 +0000 Subject: [PATCH 27/44] chore: add additional php doc tags --- .gitignore | 8 +++--- composer.json | 1 + src/Client.php | 27 +++++++++++++++++++ src/Core/ServiceContracts/CrawlContract.php | 5 ++++ src/Core/ServiceContracts/CreditsContract.php | 3 +++ .../ServiceContracts/FeedbackContract.php | 2 ++ .../GenerateSchemaContract.php | 5 ++++ src/Core/ServiceContracts/HealthzContract.php | 3 +++ .../ServiceContracts/MarkdownifyContract.php | 5 ++++ .../SearchscraperContract.php | 5 ++++ .../ServiceContracts/SmartscraperContract.php | 8 ++++++ .../ServiceContracts/ValidateContract.php | 3 +++ src/Core/Services/CrawlService.php | 6 ++++- src/Core/Services/CreditsService.php | 4 ++- src/Core/Services/FeedbackService.php | 4 ++- src/Core/Services/GenerateSchemaService.php | 6 ++++- src/Core/Services/HealthzService.php | 4 ++- src/Core/Services/MarkdownifyService.php | 8 ++++-- src/Core/Services/SearchscraperService.php | 6 ++++- src/Core/Services/SmartscraperService.php | 10 +++++-- src/Core/Services/ValidateService.php | 4 ++- src/Crawl/CrawlStartParams.php | 2 ++ src/Feedback/FeedbackSubmitParams.php | 2 ++ .../GenerateSchemaCreateParams.php | 2 ++ src/Markdownify/MarkdownifyConvertParams.php | 2 ++ .../SearchscraperCreateParams.php | 2 ++ src/Smartscraper/SmartscraperCreateParams.php | 2 ++ 27 files changed, 124 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index 70d76f1..6739884 100644 --- a/.gitignore +++ b/.gitignore @@ -1,11 +1,11 @@ -*.swo -*.swp +composer.lock +docs/ .idea/ .php-cs-fixer.cache .php-cs-fixer.php -.phpdoc/ .phpunit.cache -composer.lock phpunit.xml playground/ +*.swo +*.swp vendor/ diff --git a/composer.json b/composer.json index 396912a..daca4bc 100644 --- a/composer.json +++ b/composer.json @@ -44,6 +44,7 @@ "symfony/http-client": "^7" }, "scripts": { + "build:docs": "curl --etag-save ./vendor/ag.etags --etag-compare ./vendor/ag.etags --create-dirs --remote-name --output-dir ./vendor/bin --no-progress-meter -- https://github.com/ApiGen/ApiGen/releases/latest/download/apigen.phar && php ./vendor/bin/apigen.phar --output docs -- src", "lint": "./scripts/lint", "test": "./scripts/test" } diff --git a/src/Client.php b/src/Client.php index 637fa6c..559e441 100644 --- a/src/Client.php +++ b/src/Client.php @@ -19,22 +19,49 @@ class Client extends BaseClient { public string $apiKey; + /** + * @api + */ public SmartscraperService $smartscraper; + /** + * @api + */ public MarkdownifyService $markdownify; + /** + * @api + */ public SearchscraperService $searchscraper; + /** + * @api + */ public GenerateSchemaService $generateSchema; + /** + * @api + */ public CrawlService $crawl; + /** + * @api + */ public CreditsService $credits; + /** + * @api + */ public ValidateService $validate; + /** + * @api + */ public FeedbackService $feedback; + /** + * @api + */ public HealthzService $healthz; public function __construct(?string $apiKey = null, ?string $baseUrl = null) diff --git a/src/Core/ServiceContracts/CrawlContract.php b/src/Core/ServiceContracts/CrawlContract.php index fb071c1..a005c6b 100644 --- a/src/Core/ServiceContracts/CrawlContract.php +++ b/src/Core/ServiceContracts/CrawlContract.php @@ -13,12 +13,17 @@ interface CrawlContract { + /** + * @api + */ public function retrieveResults( string $taskID, ?RequestOptions $requestOptions = null ): CrawlGetResultsResponse; /** + * @api + * * @param string $url Starting URL for crawling * @param int $depth Maximum crawl depth from starting URL * @param bool $extractionMode Use AI extraction (true) or markdown conversion (false) diff --git a/src/Core/ServiceContracts/CreditsContract.php b/src/Core/ServiceContracts/CreditsContract.php index 8c5d8ad..a5381d0 100644 --- a/src/Core/ServiceContracts/CreditsContract.php +++ b/src/Core/ServiceContracts/CreditsContract.php @@ -9,6 +9,9 @@ interface CreditsContract { + /** + * @api + */ public function retrieve( ?RequestOptions $requestOptions = null ): CreditGetResponse; diff --git a/src/Core/ServiceContracts/FeedbackContract.php b/src/Core/ServiceContracts/FeedbackContract.php index 5ce3f88..b1aa9b6 100644 --- a/src/Core/ServiceContracts/FeedbackContract.php +++ b/src/Core/ServiceContracts/FeedbackContract.php @@ -12,6 +12,8 @@ interface FeedbackContract { /** + * @api + * * @param int $rating Rating score * @param string $requestID Request to provide feedback for * @param string|null $feedbackText Optional feedback comments diff --git a/src/Core/ServiceContracts/GenerateSchemaContract.php b/src/Core/ServiceContracts/GenerateSchemaContract.php index cd83760..41a3110 100644 --- a/src/Core/ServiceContracts/GenerateSchemaContract.php +++ b/src/Core/ServiceContracts/GenerateSchemaContract.php @@ -14,6 +14,8 @@ interface GenerateSchemaContract { /** + * @api + * * @param string $userPrompt Natural language description of desired schema * @param mixed $existingSchema Existing schema to modify or extend */ @@ -23,6 +25,9 @@ public function create( ?RequestOptions $requestOptions = null, ): GenerateSchemaNewResponse; + /** + * @api + */ public function retrieve( string $requestID, ?RequestOptions $requestOptions = null diff --git a/src/Core/ServiceContracts/HealthzContract.php b/src/Core/ServiceContracts/HealthzContract.php index 9db899e..ec42978 100644 --- a/src/Core/ServiceContracts/HealthzContract.php +++ b/src/Core/ServiceContracts/HealthzContract.php @@ -9,6 +9,9 @@ interface HealthzContract { + /** + * @api + */ public function check( ?RequestOptions $requestOptions = null ): HealthzCheckResponse; diff --git a/src/Core/ServiceContracts/MarkdownifyContract.php b/src/Core/ServiceContracts/MarkdownifyContract.php index ca08fda..a2b30ca 100644 --- a/src/Core/ServiceContracts/MarkdownifyContract.php +++ b/src/Core/ServiceContracts/MarkdownifyContract.php @@ -13,6 +13,8 @@ interface MarkdownifyContract { /** + * @api + * * @param string $websiteURL URL to convert to markdown * @param array $headers * @param list $steps Interaction steps before conversion @@ -24,6 +26,9 @@ public function convert( ?RequestOptions $requestOptions = null, ): CompletedMarkdownify; + /** + * @api + */ public function retrieveStatus( string $requestID, ?RequestOptions $requestOptions = null diff --git a/src/Core/ServiceContracts/SearchscraperContract.php b/src/Core/ServiceContracts/SearchscraperContract.php index 871933a..180c248 100644 --- a/src/Core/ServiceContracts/SearchscraperContract.php +++ b/src/Core/ServiceContracts/SearchscraperContract.php @@ -13,6 +13,8 @@ interface SearchscraperContract { /** + * @api + * * @param string $userPrompt Search query and extraction instruction * @param array $headers * @param int $numResults Number of websites to scrape from search results @@ -26,6 +28,9 @@ public function create( ?RequestOptions $requestOptions = null, ): CompletedSearchScraper; + /** + * @api + */ public function retrieveStatus( string $requestID, ?RequestOptions $requestOptions = null diff --git a/src/Core/ServiceContracts/SmartscraperContract.php b/src/Core/ServiceContracts/SmartscraperContract.php index 3effb72..8778595 100644 --- a/src/Core/ServiceContracts/SmartscraperContract.php +++ b/src/Core/ServiceContracts/SmartscraperContract.php @@ -13,6 +13,8 @@ interface SmartscraperContract { /** + * @api + * * @param string $userPrompt Extraction instruction for the LLM * @param array $cookies Cookies to include in the request * @param array $headers HTTP headers to include in the request @@ -38,11 +40,17 @@ public function create( ?RequestOptions $requestOptions = null, ): CompletedSmartscraper; + /** + * @api + */ public function retrieve( string $requestID, ?RequestOptions $requestOptions = null ): CompletedSmartscraper|FailedSmartscraper; + /** + * @api + */ public function list( ?RequestOptions $requestOptions = null ): CompletedSmartscraper|FailedSmartscraper; diff --git a/src/Core/ServiceContracts/ValidateContract.php b/src/Core/ServiceContracts/ValidateContract.php index 48c6742..5c41a7c 100644 --- a/src/Core/ServiceContracts/ValidateContract.php +++ b/src/Core/ServiceContracts/ValidateContract.php @@ -9,6 +9,9 @@ interface ValidateContract { + /** + * @api + */ public function apiKey( ?RequestOptions $requestOptions = null ): ValidateAPIKeyResponse; diff --git a/src/Core/Services/CrawlService.php b/src/Core/Services/CrawlService.php index 14d0c62..1442bbd 100644 --- a/src/Core/Services/CrawlService.php +++ b/src/Core/Services/CrawlService.php @@ -19,7 +19,9 @@ final class CrawlService implements CrawlContract public function __construct(private Client $client) {} /** - * Retrieve the status and results of a crawling job. + * @api + * + * Retrieve the status and results of a crawling job */ public function retrieveResults( string $taskID, @@ -35,6 +37,8 @@ public function retrieveResults( } /** + * @api + * * Initiate comprehensive website crawling with sitemap support. * Supports both AI extraction mode and markdown conversion mode. * Returns a task ID for async processing. diff --git a/src/Core/Services/CreditsService.php b/src/Core/Services/CreditsService.php index 9cbbddc..af7453f 100644 --- a/src/Core/Services/CreditsService.php +++ b/src/Core/Services/CreditsService.php @@ -14,7 +14,9 @@ final class CreditsService implements CreditsContract public function __construct(private Client $client) {} /** - * Retrieve the current credit balance and usage for the authenticated user. + * @api + * + * Retrieve the current credit balance and usage for the authenticated user */ public function retrieve( ?RequestOptions $requestOptions = null diff --git a/src/Core/Services/FeedbackService.php b/src/Core/Services/FeedbackService.php index 012e76e..6f15fd9 100644 --- a/src/Core/Services/FeedbackService.php +++ b/src/Core/Services/FeedbackService.php @@ -17,7 +17,9 @@ final class FeedbackService implements FeedbackContract public function __construct(private Client $client) {} /** - * Submit feedback for a specific request. + * @api + * + * Submit feedback for a specific request * * @param int $rating Rating score * @param string $requestID Request to provide feedback for diff --git a/src/Core/Services/GenerateSchemaService.php b/src/Core/Services/GenerateSchemaService.php index a09d4d9..aa4345b 100644 --- a/src/Core/Services/GenerateSchemaService.php +++ b/src/Core/Services/GenerateSchemaService.php @@ -20,6 +20,8 @@ final class GenerateSchemaService implements GenerateSchemaContract public function __construct(private Client $client) {} /** + * @api + * * Generate or modify JSON schemas based on natural language descriptions. * Can create new schemas or extend existing ones. * @@ -47,7 +49,9 @@ public function create( } /** - * Retrieve the status and results of a schema generation request. + * @api + * + * Retrieve the status and results of a schema generation request */ public function retrieve( string $requestID, diff --git a/src/Core/Services/HealthzService.php b/src/Core/Services/HealthzService.php index 4285b21..0717481 100644 --- a/src/Core/Services/HealthzService.php +++ b/src/Core/Services/HealthzService.php @@ -14,7 +14,9 @@ final class HealthzService implements HealthzContract public function __construct(private Client $client) {} /** - * Check the health status of the service. + * @api + * + * Check the health status of the service */ public function check( ?RequestOptions $requestOptions = null diff --git a/src/Core/Services/MarkdownifyService.php b/src/Core/Services/MarkdownifyService.php index 427520a..04d21c9 100644 --- a/src/Core/Services/MarkdownifyService.php +++ b/src/Core/Services/MarkdownifyService.php @@ -19,7 +19,9 @@ final class MarkdownifyService implements MarkdownifyContract public function __construct(private Client $client) {} /** - * Convert web page content to clean Markdown format. + * @api + * + * Convert web page content to clean Markdown format * * @param string $websiteURL URL to convert to markdown * @param array $headers @@ -47,7 +49,9 @@ public function convert( } /** - * Retrieve the status and results of a markdown conversion. + * @api + * + * Retrieve the status and results of a markdown conversion */ public function retrieveStatus( string $requestID, diff --git a/src/Core/Services/SearchscraperService.php b/src/Core/Services/SearchscraperService.php index da54076..c9d9548 100644 --- a/src/Core/Services/SearchscraperService.php +++ b/src/Core/Services/SearchscraperService.php @@ -19,6 +19,8 @@ final class SearchscraperService implements SearchscraperContract public function __construct(private Client $client) {} /** + * @api + * * Performs web search, selects relevant URLs, and extracts structured data from multiple websites. * Uses LLM to refine search queries and merge results from different sources. * @@ -55,7 +57,9 @@ public function create( } /** - * Retrieve the status and results of a search scraping operation. + * @api + * + * Retrieve the status and results of a search scraping operation */ public function retrieveStatus( string $requestID, diff --git a/src/Core/Services/SmartscraperService.php b/src/Core/Services/SmartscraperService.php index d7c0ba3..91a1a51 100644 --- a/src/Core/Services/SmartscraperService.php +++ b/src/Core/Services/SmartscraperService.php @@ -20,6 +20,8 @@ final class SmartscraperService implements SmartscraperContract public function __construct(private Client $client) {} /** + * @api + * * Main scraping endpoint with LLM-powered content analysis. Supports various fetching providers, * infinite scrolling, pagination, and custom output schemas. * @@ -74,7 +76,9 @@ public function create( } /** - * Retrieve the status and results of a scraping operation. + * @api + * + * Retrieve the status and results of a scraping operation */ public function retrieve( string $requestID, @@ -90,7 +94,9 @@ public function retrieve( } /** - * Retrieve the status and results of a scraping operation. + * @api + * + * Retrieve the status and results of a scraping operation */ public function list( ?RequestOptions $requestOptions = null diff --git a/src/Core/Services/ValidateService.php b/src/Core/Services/ValidateService.php index 38f3ed4..af9946a 100644 --- a/src/Core/Services/ValidateService.php +++ b/src/Core/Services/ValidateService.php @@ -14,7 +14,9 @@ final class ValidateService implements ValidateContract public function __construct(private Client $client) {} /** - * Validate the API key and retrieve associated user email. + * @api + * + * Validate the API key and retrieve associated user email */ public function apiKey( ?RequestOptions $requestOptions = null diff --git a/src/Crawl/CrawlStartParams.php b/src/Crawl/CrawlStartParams.php index 07b526f..8ac17b8 100644 --- a/src/Crawl/CrawlStartParams.php +++ b/src/Crawl/CrawlStartParams.php @@ -15,6 +15,8 @@ * Supports both AI extraction mode and markdown conversion mode. * Returns a task ID for async processing. * + * @see Scrapegraphai\Crawl->start + * * @phpstan-type crawl_start_params = array{ * url: string, * depth?: int, diff --git a/src/Feedback/FeedbackSubmitParams.php b/src/Feedback/FeedbackSubmitParams.php index d7f128e..47b3893 100644 --- a/src/Feedback/FeedbackSubmitParams.php +++ b/src/Feedback/FeedbackSubmitParams.php @@ -12,6 +12,8 @@ /** * Submit feedback for a specific request. * + * @see Scrapegraphai\Feedback->submit + * * @phpstan-type feedback_submit_params = array{ * rating: int, requestID: string, feedbackText?: string|null * } diff --git a/src/GenerateSchema/GenerateSchemaCreateParams.php b/src/GenerateSchema/GenerateSchemaCreateParams.php index 06cc8d1..d2e86de 100644 --- a/src/GenerateSchema/GenerateSchemaCreateParams.php +++ b/src/GenerateSchema/GenerateSchemaCreateParams.php @@ -13,6 +13,8 @@ * Generate or modify JSON schemas based on natural language descriptions. * Can create new schemas or extend existing ones. * + * @see Scrapegraphai\GenerateSchema->create + * * @phpstan-type generate_schema_create_params = array{ * userPrompt: string, existingSchema?: mixed * } diff --git a/src/Markdownify/MarkdownifyConvertParams.php b/src/Markdownify/MarkdownifyConvertParams.php index 59ba4b7..64058e0 100644 --- a/src/Markdownify/MarkdownifyConvertParams.php +++ b/src/Markdownify/MarkdownifyConvertParams.php @@ -12,6 +12,8 @@ /** * Convert web page content to clean Markdown format. * + * @see Scrapegraphai\Markdownify->convert + * * @phpstan-type markdownify_convert_params = array{ * websiteURL: string, headers?: array, steps?: list * } diff --git a/src/Searchscraper/SearchscraperCreateParams.php b/src/Searchscraper/SearchscraperCreateParams.php index 9888016..a5402fd 100644 --- a/src/Searchscraper/SearchscraperCreateParams.php +++ b/src/Searchscraper/SearchscraperCreateParams.php @@ -13,6 +13,8 @@ * Performs web search, selects relevant URLs, and extracts structured data from multiple websites. * Uses LLM to refine search queries and merge results from different sources. * + * @see Scrapegraphai\Searchscraper->create + * * @phpstan-type searchscraper_create_params = array{ * userPrompt: string, * headers?: array, diff --git a/src/Smartscraper/SmartscraperCreateParams.php b/src/Smartscraper/SmartscraperCreateParams.php index 4c1a7dc..4db0ff9 100644 --- a/src/Smartscraper/SmartscraperCreateParams.php +++ b/src/Smartscraper/SmartscraperCreateParams.php @@ -13,6 +13,8 @@ * Main scraping endpoint with LLM-powered content analysis. Supports various fetching providers, * infinite scrolling, pagination, and custom output schemas. * + * @see Scrapegraphai\Smartscraper->create + * * @phpstan-type smartscraper_create_params = array{ * userPrompt: string, * cookies?: array, From 389806624293626a1246486efa870095a959eca5 Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Sat, 30 Aug 2025 02:53:26 +0000 Subject: [PATCH 28/44] chore: refactor request options --- src/Client.php | 11 +- src/Core/BaseClient.php | 52 +++++----- src/Core/Contracts/BasePage.php | 4 +- src/RequestOptions.php | 172 +++++++++++++++++--------------- 4 files changed, 128 insertions(+), 111 deletions(-) diff --git a/src/Client.php b/src/Client.php index 559e441..da42d5c 100644 --- a/src/Client.php +++ b/src/Client.php @@ -4,6 +4,8 @@ namespace Scrapegraphai; +use Http\Discovery\Psr17FactoryDiscovery; +use Http\Discovery\Psr18ClientDiscovery; use Scrapegraphai\Core\BaseClient; use Scrapegraphai\Core\Services\CrawlService; use Scrapegraphai\Core\Services\CreditsService; @@ -72,12 +74,19 @@ public function __construct(?string $apiKey = null, ?string $baseUrl = null) 'SCRAPEGRAPHAI_BASE_URL' ) ?: 'https://api.scrapegraphai.com/v1'; + $options = new RequestOptions( + uriFactory: Psr17FactoryDiscovery::findUriFactory(), + streamFactory: Psr17FactoryDiscovery::findStreamFactory(), + requestFactory: Psr17FactoryDiscovery::findRequestFactory(), + transporter: Psr18ClientDiscovery::find(), + ); + parent::__construct( headers: [ 'Content-Type' => 'application/json', 'Accept' => 'application/json', ], baseUrl: $base, - options: new RequestOptions, + options: $options, ); $this->smartscraper = new SmartscraperService($this); diff --git a/src/Core/BaseClient.php b/src/Core/BaseClient.php index b53df31..2cd40bd 100644 --- a/src/Core/BaseClient.php +++ b/src/Core/BaseClient.php @@ -4,8 +4,6 @@ namespace Scrapegraphai\Core; -use Http\Discovery\Psr17FactoryDiscovery; -use Http\Discovery\Psr18ClientDiscovery; use Psr\Http\Client\ClientInterface; use Psr\Http\Message\RequestFactoryInterface; use Psr\Http\Message\RequestInterface; @@ -33,14 +31,6 @@ class BaseClient { protected UriInterface $baseUrl; - protected UriFactoryInterface $uriFactory; - - protected StreamFactoryInterface $streamFactory; - - protected RequestFactoryInterface $requestFactory; - - protected ClientInterface $transporter; - /** * @param array|null> $headers */ @@ -49,12 +39,8 @@ public function __construct( string $baseUrl, protected RequestOptions $options = new RequestOptions, ) { - $this->uriFactory = Psr17FactoryDiscovery::findUriFactory(); - $this->streamFactory = Psr17FactoryDiscovery::findStreamFactory(); - $this->requestFactory = Psr17FactoryDiscovery::findRequestFactory(); - - $this->baseUrl = $this->uriFactory->createUri($baseUrl); - $this->transporter = Psr18ClientDiscovery::find(); + assert(null !== $this->options->uriFactory); + $this->baseUrl = $this->options->uriFactory->createUri($baseUrl); } /** @@ -63,6 +49,7 @@ public function __construct( * @param array $headers * @param class-string> $page * @param class-string> $stream + * @param RequestOptions|array|null $options */ public function request( string $method, @@ -73,17 +60,18 @@ public function request( string|Converter|ConverterSource|null $convert = null, ?string $page = null, ?string $stream = null, - mixed $options = [], + RequestOptions|array|null $options = [], ): mixed { // @phpstan-ignore-next-line [$req, $opts] = $this->buildRequest(method: $method, path: $path, query: $query, headers: $headers, body: $body, opts: $options); ['method' => $method, 'path' => $uri, 'headers' => $headers] = $req; + assert(null !== $opts->requestFactory); - $request = $this->requestFactory->createRequest($method, uri: $uri); + $request = $opts->requestFactory->createRequest($method, uri: $uri); $request = Util::withSetHeaders($request, headers: $headers); // @phpstan-ignore-next-line - $rsp = $this->sendRequest($request, data: $body, opts: $opts, redirectCount: 0, retryCount: 0); + $rsp = $this->sendRequest($opts, req: $request, data: $body, redirectCount: 0, retryCount: 0); $decoded = Util::decodeContent($rsp); @@ -123,14 +111,18 @@ protected function authHeaders(): array * @param string|list $path * @param array $query * @param array|null> $headers - * @param RequestOptions|array{ + * @param array{ * timeout?: float|null, * maxRetries?: int|null, * initialRetryDelay?: float|null, * maxRetryDelay?: float|null, - * extraHeaders?: list|null, - * extraQueryParams?: list|null, - * extraBodyParams?: list|null, + * extraHeaders?: array|null>|null, + * extraQueryParams?: array|null, + * extraBodyParams?: mixed, + * transporter?: ClientInterface|null, + * uriFactory?: UriFactoryInterface|null, + * streamFactory?: StreamFactoryInterface|null, + * requestFactory?: RequestFactoryInterface|null, * }|null $opts * * @return array{normalized_request, RequestOptions} @@ -143,7 +135,7 @@ protected function buildRequest( mixed $body, RequestOptions|array|null $opts, ): array { - $opts = [...$this->options->__serialize(), ...RequestOptions::parse($opts)->__serialize()]; + $opts = array_merge($this->options->toArray(), RequestOptions::parse($opts)->toArray()); $options = new RequestOptions(...$opts); $parsedPath = Util::parsePath($path); @@ -182,14 +174,16 @@ protected function followRedirect( * mixed,>|null $data */ protected function sendRequest( + RequestOptions $opts, RequestInterface $req, mixed $data, - RequestOptions $opts, int $retryCount, int $redirectCount, ): ResponseInterface { - $req = Util::withSetBody($this->streamFactory, req: $req, body: $data); - $rsp = $this->transporter->sendRequest($req); + assert(null !== $opts->streamFactory && null !== $opts->transporter); + + $req = Util::withSetBody($opts->streamFactory, req: $req, body: $data); + $rsp = $opts->transporter->sendRequest($req); $code = $rsp->getStatusCode(); if ($code >= 300 && $code < 400) { @@ -199,7 +193,7 @@ protected function sendRequest( $req = $this->followRedirect($rsp, req: $req); - return $this->sendRequest($req, data: $data, opts: $opts, retryCount: $retryCount, redirectCount: ++$redirectCount); + return $this->sendRequest($opts, req: $req, data: $data, retryCount: $retryCount, redirectCount: ++$redirectCount); } if ($code >= 400 && $code < 500) { @@ -209,7 +203,7 @@ protected function sendRequest( if ($code >= 500 && $retryCount < $opts->maxRetries) { usleep((int) $opts->initialRetryDelay); - return $this->sendRequest($req, data: $data, opts: $opts, retryCount: ++$retryCount, redirectCount: $redirectCount); + return $this->sendRequest($opts, req: $req, data: $data, retryCount: ++$retryCount, redirectCount: $redirectCount); } return $rsp; diff --git a/src/Core/Contracts/BasePage.php b/src/Core/Contracts/BasePage.php index bde0d48..0829df8 100644 --- a/src/Core/Contracts/BasePage.php +++ b/src/Core/Contracts/BasePage.php @@ -12,6 +12,8 @@ /** * @internal * + * @phpstan-import-type normalized_request from \Scrapegraphai\Core\BaseClient + * * @template Item * * @extends \IteratorAggregate @@ -21,7 +23,7 @@ interface BasePage extends \IteratorAggregate /** * @internal * - * @param array $request + * @param normalized_request $request */ public function __construct( Converter|ConverterSource|string $convert, diff --git a/src/RequestOptions.php b/src/RequestOptions.php index 2ac8632..d455203 100644 --- a/src/RequestOptions.php +++ b/src/RequestOptions.php @@ -4,99 +4,111 @@ namespace Scrapegraphai; -class RequestOptions +use Psr\Http\Client\ClientInterface; +use Psr\Http\Message\RequestFactoryInterface; +use Psr\Http\Message\StreamFactoryInterface; +use Psr\Http\Message\UriFactoryInterface; +use Scrapegraphai\Core\Attributes\Api as Property; +use Scrapegraphai\Core\Concerns\SdkModel; +use Scrapegraphai\Core\Contracts\BaseModel; +use Scrapegraphai\Core\Implementation\Omittable; + +use const Scrapegraphai\Core\OMIT as omit; + +/** + * @phpstan-type request_options = array{ + * timeout?: float|null, + * maxRetries?: int|null, + * initialRetryDelay?: float|null, + * maxRetryDelay?: float|null, + * extraHeaders?: array>|null, + * extraQueryParams?: array|null, + * extraBodyParams?: mixed, + * transporter?: ClientInterface|null, + * uriFactory?: UriFactoryInterface|null, + * streamFactory?: StreamFactoryInterface|null, + * requestFactory?: RequestFactoryInterface|null, + * } + * @phpstan-type request_opts = null|RequestOptions|request_options + */ +final class RequestOptions implements BaseModel { - public const DEFAULT_TIMEOUT = 60; + /** @use SdkModel */ + use SdkModel; - public const DEFAULT_MAX_RETRIES = 2; + #[Property] + public float $timeout = 60; - public const DEFAULT_INITIAL_RETRYDELAY = 0.5; + #[Property] + public int $maxRetries = 2; - public const DEFAULT_MAX_RETRY_DELAY = 8.0; + #[Property] + public float $initialRetryDelay = 0.5; - /** - * @param list $extraHeaders - * @param list $extraQueryParams - * @param list $extraBodyParams - */ - public function __construct( - public float $timeout = self::DEFAULT_TIMEOUT, - public int $maxRetries = self::DEFAULT_MAX_RETRIES, - public float $initialRetryDelay = self::DEFAULT_INITIAL_RETRYDELAY, - public float $maxRetryDelay = self::DEFAULT_MAX_RETRY_DELAY, - public array $extraHeaders = [], - public array $extraQueryParams = [], - public array $extraBodyParams = [], - ) {} + #[Property] + public float $maxRetryDelay = 8.0; - /** - * @return array{ - * timeout: float, - * maxRetries: int, - * initialRetryDelay: float, - * maxRetryDelay: float, - * extraHeaders: list, - * extraQueryParams: list, - * extraBodyParams: list, - * } - */ - public function __serialize(): array - { - return [ - 'timeout' => $this->timeout, - 'maxRetries' => $this->maxRetries, - 'initialRetryDelay' => $this->initialRetryDelay, - 'maxRetryDelay' => $this->maxRetryDelay, - 'extraHeaders' => $this->extraHeaders, - 'extraQueryParams' => $this->extraQueryParams, - 'extraBodyParams' => $this->extraBodyParams, - ]; - } + /** @var array|null> $extraHeaders */ + #[Property] + public array $extraHeaders = []; + + /** @var array $extraQueryParams */ + #[Property] + public array $extraQueryParams = []; + + #[Property] + public mixed $extraBodyParams; + + #[Property(optional: true)] + public ?ClientInterface $transporter; + + #[Property(optional: true)] + public ?UriFactoryInterface $uriFactory; + + #[Property(optional: true)] + public ?StreamFactoryInterface $streamFactory; + + #[Property(optional: true)] + public ?RequestFactoryInterface $requestFactory; /** - * @param array{ - * timeout?: float|null, - * maxRetries?: int|null, - * initialRetryDelay?: float|null, - * maxRetryDelay?: float|null, - * extraHeaders?: list|null, - * extraQueryParams?: list|null, - * extraBodyParams?: list|null, - * } $data + * @param array|null>|null $extraHeaders + * @param array|null $extraQueryParams + * @param mixed|Omittable $extraBodyParams */ - public function __unserialize(array $data): void - { - $this->timeout = $data['timeout'] ?? self::DEFAULT_TIMEOUT; - $this - ->maxRetries = $data['maxRetries'] ?? self::DEFAULT_MAX_RETRIES - ; - $this - ->initialRetryDelay = $data[ - 'initialRetryDelay' - ] ?? self::DEFAULT_INITIAL_RETRYDELAY - ; - $this->maxRetryDelay = $data[ - 'maxRetryDelay' - ] ?? self::DEFAULT_MAX_RETRY_DELAY; - $this->extraHeaders = $data[ - 'extraHeaders' - ] ?? []; - $this->extraQueryParams = $data['extraQueryParams'] ?? []; - $this - ->extraBodyParams = $data['extraBodyParams'] ?? [] + public function __construct( + ?float $timeout = null, + ?int $maxRetries = null, + ?float $initialRetryDelay = null, + ?float $maxRetryDelay = null, + ?array $extraHeaders = null, + ?array $extraQueryParams = null, + mixed $extraBodyParams = omit, + ?ClientInterface $transporter = null, + ?UriFactoryInterface $uriFactory = null, + ?StreamFactoryInterface $streamFactory = null, + ?RequestFactoryInterface $requestFactory = null, + ) { + self::introspect(); + $this->unsetOptionalProperties(); + + null !== $timeout && $this->timeout = $timeout; + null !== $maxRetries && $this->maxRetries = $maxRetries; + null !== $initialRetryDelay && $this + ->initialRetryDelay = $initialRetryDelay ; + null !== $maxRetryDelay && $this->maxRetryDelay = $maxRetryDelay; + null !== $extraHeaders && $this->extraHeaders = $extraHeaders; + null !== $extraQueryParams && $this->extraQueryParams = $extraQueryParams; + omit !== $extraBodyParams && $this->extraBodyParams = $extraBodyParams; + null !== $transporter && $this->transporter = $transporter; + null !== $uriFactory && $this->uriFactory = $uriFactory; + null !== $streamFactory && $this->streamFactory = $streamFactory; + null !== $requestFactory && $this->requestFactory = $requestFactory; } /** - * @param RequestOptions|array{ - * timeout?: float|null, - * maxRetries?: int|null, - * initialRetryDelay?: float|null, - * maxRetryDelay?: float|null, - * extraHeaders?: list|null, - * extraQueryParams?: list|null, - * extraBodyParams?: list|null, - * }|null $options + * @param request_opts|null $options */ public static function parse(RequestOptions|array|null $options): self { From 4a166fcc6584323b68d900e6809453a8d0eb1bcd Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Sat, 30 Aug 2025 02:55:20 +0000 Subject: [PATCH 29/44] chore: simplify model initialization --- src/Core/Concerns/SdkModel.php | 14 +++++--------- src/Core/Concerns/SdkPage.php | 8 ++++++-- src/Core/Services/CrawlService.php | 3 +++ src/Core/Services/CreditsService.php | 3 +++ src/Core/Services/FeedbackService.php | 3 +++ src/Core/Services/GenerateSchemaService.php | 3 +++ src/Core/Services/HealthzService.php | 3 +++ src/Core/Services/MarkdownifyService.php | 3 +++ src/Core/Services/SearchscraperService.php | 3 +++ src/Core/Services/SmartscraperService.php | 3 +++ src/Core/Services/ValidateService.php | 3 +++ src/Crawl/CrawlGetResultsResponse.php | 3 +-- src/Crawl/CrawlStartParams.php | 3 +-- src/Crawl/CrawlStartParams/Rules.php | 3 +-- src/Crawl/CrawlStartResponse.php | 3 +-- src/Credits/CreditGetResponse.php | 3 +-- src/Feedback/FeedbackSubmitParams.php | 3 +-- src/Feedback/FeedbackSubmitResponse.php | 3 +-- src/GenerateSchema/GenerateSchemaCreateParams.php | 3 +-- .../CompletedSchemaGenerationResponse.php | 3 +-- .../FailedSchemaGenerationResponse.php | 3 +-- src/GenerateSchema/GenerateSchemaNewResponse.php | 3 +-- src/Healthz/HealthzCheckResponse.php | 3 +-- src/Markdownify/CompletedMarkdownify.php | 3 +-- src/Markdownify/MarkdownifyConvertParams.php | 3 +-- .../FailedMarkdownifyResponse.php | 3 +-- src/RequestOptions.php | 3 +-- src/Searchscraper/CompletedSearchScraper.php | 3 +-- src/Searchscraper/SearchscraperCreateParams.php | 3 +-- .../FailedSearchScraperResponse.php | 3 +-- src/Smartscraper/CompletedSmartscraper.php | 3 +-- src/Smartscraper/FailedSmartscraper.php | 3 +-- src/Smartscraper/SmartscraperCreateParams.php | 3 +-- src/Validate/ValidateAPIKeyResponse.php | 3 +-- tests/Core/TestModel.php | 5 ++--- 35 files changed, 63 insertions(+), 60 deletions(-) diff --git a/src/Core/Concerns/SdkModel.php b/src/Core/Concerns/SdkModel.php index 6b71d29..fdc45eb 100644 --- a/src/Core/Concerns/SdkModel.php +++ b/src/Core/Concerns/SdkModel.php @@ -50,6 +50,8 @@ public function __unserialize(array $data): void } /** + * @internal + * * @return array */ public function __debugInfo(): array @@ -135,8 +137,6 @@ public function offsetExists(mixed $offset): bool * @internal * * @param key-of $offset - * - * @return value-of */ public function &offsetGet(mixed $offset): mixed { @@ -205,6 +205,8 @@ public function offsetUnset(mixed $offset): void } /** + * @internal + * * @return array */ public function jsonSerialize(): array @@ -238,16 +240,10 @@ public static function converter(): Converter /** * @internal */ - public static function introspect(): void + private function initialize(): void { static::converter(); - } - /** - * @internal - */ - private function unsetOptionalProperties(): void - { foreach (self::$converter->properties as $name => $info) { if ($info->optional) { unset($this->{$name}); diff --git a/src/Core/Concerns/SdkPage.php b/src/Core/Concerns/SdkPage.php index 36f82de..d5f8e01 100644 --- a/src/Core/Concerns/SdkPage.php +++ b/src/Core/Concerns/SdkPage.php @@ -70,7 +70,7 @@ public function getNextPage(): static } /** - * Generator yielding each page (instance of static). + * Iterator yielding each page (instance of static). * * @return \Generator */ @@ -87,7 +87,7 @@ public function getIterator(): \Generator } /** - * Generator yielding each item across all pages. + * Iterator yielding each item across all pages. * * @return \Generator */ @@ -101,6 +101,8 @@ public function pagingEachItem(): \Generator } /** + * @internal + * * @param array $data * * @return static @@ -108,6 +110,8 @@ public function pagingEachItem(): \Generator abstract public static function fromArray(array $data): static; /** + * @internal + * * @return array{normalized_request, RequestOptions} */ abstract protected function nextRequest(): ?array; diff --git a/src/Core/Services/CrawlService.php b/src/Core/Services/CrawlService.php index 1442bbd..e3a48bb 100644 --- a/src/Core/Services/CrawlService.php +++ b/src/Core/Services/CrawlService.php @@ -16,6 +16,9 @@ final class CrawlService implements CrawlContract { + /** + * @internal + */ public function __construct(private Client $client) {} /** diff --git a/src/Core/Services/CreditsService.php b/src/Core/Services/CreditsService.php index af7453f..e69f63c 100644 --- a/src/Core/Services/CreditsService.php +++ b/src/Core/Services/CreditsService.php @@ -11,6 +11,9 @@ final class CreditsService implements CreditsContract { + /** + * @internal + */ public function __construct(private Client $client) {} /** diff --git a/src/Core/Services/FeedbackService.php b/src/Core/Services/FeedbackService.php index 6f15fd9..553f933 100644 --- a/src/Core/Services/FeedbackService.php +++ b/src/Core/Services/FeedbackService.php @@ -14,6 +14,9 @@ final class FeedbackService implements FeedbackContract { + /** + * @internal + */ public function __construct(private Client $client) {} /** diff --git a/src/Core/Services/GenerateSchemaService.php b/src/Core/Services/GenerateSchemaService.php index aa4345b..02566f2 100644 --- a/src/Core/Services/GenerateSchemaService.php +++ b/src/Core/Services/GenerateSchemaService.php @@ -17,6 +17,9 @@ final class GenerateSchemaService implements GenerateSchemaContract { + /** + * @internal + */ public function __construct(private Client $client) {} /** diff --git a/src/Core/Services/HealthzService.php b/src/Core/Services/HealthzService.php index 0717481..d11d8cc 100644 --- a/src/Core/Services/HealthzService.php +++ b/src/Core/Services/HealthzService.php @@ -11,6 +11,9 @@ final class HealthzService implements HealthzContract { + /** + * @internal + */ public function __construct(private Client $client) {} /** diff --git a/src/Core/Services/MarkdownifyService.php b/src/Core/Services/MarkdownifyService.php index 04d21c9..f527084 100644 --- a/src/Core/Services/MarkdownifyService.php +++ b/src/Core/Services/MarkdownifyService.php @@ -16,6 +16,9 @@ final class MarkdownifyService implements MarkdownifyContract { + /** + * @internal + */ public function __construct(private Client $client) {} /** diff --git a/src/Core/Services/SearchscraperService.php b/src/Core/Services/SearchscraperService.php index c9d9548..c474c36 100644 --- a/src/Core/Services/SearchscraperService.php +++ b/src/Core/Services/SearchscraperService.php @@ -16,6 +16,9 @@ final class SearchscraperService implements SearchscraperContract { + /** + * @internal + */ public function __construct(private Client $client) {} /** diff --git a/src/Core/Services/SmartscraperService.php b/src/Core/Services/SmartscraperService.php index 91a1a51..d24ce64 100644 --- a/src/Core/Services/SmartscraperService.php +++ b/src/Core/Services/SmartscraperService.php @@ -17,6 +17,9 @@ final class SmartscraperService implements SmartscraperContract { + /** + * @internal + */ public function __construct(private Client $client) {} /** diff --git a/src/Core/Services/ValidateService.php b/src/Core/Services/ValidateService.php index af9946a..7bb0a7f 100644 --- a/src/Core/Services/ValidateService.php +++ b/src/Core/Services/ValidateService.php @@ -11,6 +11,9 @@ final class ValidateService implements ValidateContract { + /** + * @internal + */ public function __construct(private Client $client) {} /** diff --git a/src/Crawl/CrawlGetResultsResponse.php b/src/Crawl/CrawlGetResultsResponse.php index d3bf78b..54fe5ed 100644 --- a/src/Crawl/CrawlGetResultsResponse.php +++ b/src/Crawl/CrawlGetResultsResponse.php @@ -46,8 +46,7 @@ final class CrawlGetResultsResponse implements BaseModel public function __construct() { - self::introspect(); - $this->unsetOptionalProperties(); + $this->initialize(); } /** diff --git a/src/Crawl/CrawlStartParams.php b/src/Crawl/CrawlStartParams.php index 8ac17b8..03aeca4 100644 --- a/src/Crawl/CrawlStartParams.php +++ b/src/Crawl/CrawlStartParams.php @@ -102,8 +102,7 @@ final class CrawlStartParams implements BaseModel */ public function __construct() { - self::introspect(); - $this->unsetOptionalProperties(); + $this->initialize(); } /** diff --git a/src/Crawl/CrawlStartParams/Rules.php b/src/Crawl/CrawlStartParams/Rules.php index 887fd36..17c3a18 100644 --- a/src/Crawl/CrawlStartParams/Rules.php +++ b/src/Crawl/CrawlStartParams/Rules.php @@ -34,8 +34,7 @@ final class Rules implements BaseModel public function __construct() { - self::introspect(); - $this->unsetOptionalProperties(); + $this->initialize(); } /** diff --git a/src/Crawl/CrawlStartResponse.php b/src/Crawl/CrawlStartResponse.php index 29b1a44..3104d31 100644 --- a/src/Crawl/CrawlStartResponse.php +++ b/src/Crawl/CrawlStartResponse.php @@ -24,8 +24,7 @@ final class CrawlStartResponse implements BaseModel public function __construct() { - self::introspect(); - $this->unsetOptionalProperties(); + $this->initialize(); } /** diff --git a/src/Credits/CreditGetResponse.php b/src/Credits/CreditGetResponse.php index 184e0a5..c6a9c55 100644 --- a/src/Credits/CreditGetResponse.php +++ b/src/Credits/CreditGetResponse.php @@ -32,8 +32,7 @@ final class CreditGetResponse implements BaseModel public function __construct() { - self::introspect(); - $this->unsetOptionalProperties(); + $this->initialize(); } /** diff --git a/src/Feedback/FeedbackSubmitParams.php b/src/Feedback/FeedbackSubmitParams.php index 47b3893..adacb16 100644 --- a/src/Feedback/FeedbackSubmitParams.php +++ b/src/Feedback/FeedbackSubmitParams.php @@ -58,8 +58,7 @@ final class FeedbackSubmitParams implements BaseModel */ public function __construct() { - self::introspect(); - $this->unsetOptionalProperties(); + $this->initialize(); } /** diff --git a/src/Feedback/FeedbackSubmitResponse.php b/src/Feedback/FeedbackSubmitResponse.php index 0f034eb..761da69 100644 --- a/src/Feedback/FeedbackSubmitResponse.php +++ b/src/Feedback/FeedbackSubmitResponse.php @@ -35,8 +35,7 @@ final class FeedbackSubmitResponse implements BaseModel public function __construct() { - self::introspect(); - $this->unsetOptionalProperties(); + $this->initialize(); } /** diff --git a/src/GenerateSchema/GenerateSchemaCreateParams.php b/src/GenerateSchema/GenerateSchemaCreateParams.php index d2e86de..bb2c871 100644 --- a/src/GenerateSchema/GenerateSchemaCreateParams.php +++ b/src/GenerateSchema/GenerateSchemaCreateParams.php @@ -53,8 +53,7 @@ final class GenerateSchemaCreateParams implements BaseModel */ public function __construct() { - self::introspect(); - $this->unsetOptionalProperties(); + $this->initialize(); } /** diff --git a/src/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse.php b/src/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse.php index d13746e..b4f3fbf 100644 --- a/src/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse.php +++ b/src/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse.php @@ -45,8 +45,7 @@ final class CompletedSchemaGenerationResponse implements BaseModel public function __construct() { - self::introspect(); - $this->unsetOptionalProperties(); + $this->initialize(); } /** diff --git a/src/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse.php b/src/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse.php index 8103b5f..1dfdc0c 100644 --- a/src/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse.php +++ b/src/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse.php @@ -45,8 +45,7 @@ final class FailedSchemaGenerationResponse implements BaseModel public function __construct() { - self::introspect(); - $this->unsetOptionalProperties(); + $this->initialize(); } /** diff --git a/src/GenerateSchema/GenerateSchemaNewResponse.php b/src/GenerateSchema/GenerateSchemaNewResponse.php index c4cfe0b..17888c7 100644 --- a/src/GenerateSchema/GenerateSchemaNewResponse.php +++ b/src/GenerateSchema/GenerateSchemaNewResponse.php @@ -51,8 +51,7 @@ final class GenerateSchemaNewResponse implements BaseModel public function __construct() { - self::introspect(); - $this->unsetOptionalProperties(); + $this->initialize(); } /** diff --git a/src/Healthz/HealthzCheckResponse.php b/src/Healthz/HealthzCheckResponse.php index 81b5b75..2160dcf 100644 --- a/src/Healthz/HealthzCheckResponse.php +++ b/src/Healthz/HealthzCheckResponse.php @@ -27,8 +27,7 @@ final class HealthzCheckResponse implements BaseModel public function __construct() { - self::introspect(); - $this->unsetOptionalProperties(); + $this->initialize(); } /** diff --git a/src/Markdownify/CompletedMarkdownify.php b/src/Markdownify/CompletedMarkdownify.php index 18b57b0..752b512 100644 --- a/src/Markdownify/CompletedMarkdownify.php +++ b/src/Markdownify/CompletedMarkdownify.php @@ -44,8 +44,7 @@ final class CompletedMarkdownify implements BaseModel public function __construct() { - self::introspect(); - $this->unsetOptionalProperties(); + $this->initialize(); } /** diff --git a/src/Markdownify/MarkdownifyConvertParams.php b/src/Markdownify/MarkdownifyConvertParams.php index 64058e0..3204aeb 100644 --- a/src/Markdownify/MarkdownifyConvertParams.php +++ b/src/Markdownify/MarkdownifyConvertParams.php @@ -58,8 +58,7 @@ final class MarkdownifyConvertParams implements BaseModel */ public function __construct() { - self::introspect(); - $this->unsetOptionalProperties(); + $this->initialize(); } /** diff --git a/src/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse.php b/src/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse.php index b91e3b4..f15dc7b 100644 --- a/src/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse.php +++ b/src/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse.php @@ -41,8 +41,7 @@ final class FailedMarkdownifyResponse implements BaseModel public function __construct() { - self::introspect(); - $this->unsetOptionalProperties(); + $this->initialize(); } /** diff --git a/src/RequestOptions.php b/src/RequestOptions.php index d455203..a14f915 100644 --- a/src/RequestOptions.php +++ b/src/RequestOptions.php @@ -89,8 +89,7 @@ public function __construct( ?StreamFactoryInterface $streamFactory = null, ?RequestFactoryInterface $requestFactory = null, ) { - self::introspect(); - $this->unsetOptionalProperties(); + $this->initialize(); null !== $timeout && $this->timeout = $timeout; null !== $maxRetries && $this->maxRetries = $maxRetries; diff --git a/src/Searchscraper/CompletedSearchScraper.php b/src/Searchscraper/CompletedSearchScraper.php index e5571b6..8466de0 100644 --- a/src/Searchscraper/CompletedSearchScraper.php +++ b/src/Searchscraper/CompletedSearchScraper.php @@ -57,8 +57,7 @@ final class CompletedSearchScraper implements BaseModel public function __construct() { - self::introspect(); - $this->unsetOptionalProperties(); + $this->initialize(); } /** diff --git a/src/Searchscraper/SearchscraperCreateParams.php b/src/Searchscraper/SearchscraperCreateParams.php index a5402fd..0797d82 100644 --- a/src/Searchscraper/SearchscraperCreateParams.php +++ b/src/Searchscraper/SearchscraperCreateParams.php @@ -66,8 +66,7 @@ final class SearchscraperCreateParams implements BaseModel */ public function __construct() { - self::introspect(); - $this->unsetOptionalProperties(); + $this->initialize(); } /** diff --git a/src/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse.php b/src/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse.php index 849f4ce..545f3a3 100644 --- a/src/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse.php +++ b/src/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse.php @@ -50,8 +50,7 @@ final class FailedSearchScraperResponse implements BaseModel public function __construct() { - self::introspect(); - $this->unsetOptionalProperties(); + $this->initialize(); } /** diff --git a/src/Smartscraper/CompletedSmartscraper.php b/src/Smartscraper/CompletedSmartscraper.php index 3d55572..b016b74 100644 --- a/src/Smartscraper/CompletedSmartscraper.php +++ b/src/Smartscraper/CompletedSmartscraper.php @@ -58,8 +58,7 @@ final class CompletedSmartscraper implements BaseModel public function __construct() { - self::introspect(); - $this->unsetOptionalProperties(); + $this->initialize(); } /** diff --git a/src/Smartscraper/FailedSmartscraper.php b/src/Smartscraper/FailedSmartscraper.php index 63461e5..9a7d219 100644 --- a/src/Smartscraper/FailedSmartscraper.php +++ b/src/Smartscraper/FailedSmartscraper.php @@ -48,8 +48,7 @@ final class FailedSmartscraper implements BaseModel public function __construct() { - self::introspect(); - $this->unsetOptionalProperties(); + $this->initialize(); } /** diff --git a/src/Smartscraper/SmartscraperCreateParams.php b/src/Smartscraper/SmartscraperCreateParams.php index 4db0ff9..3998338 100644 --- a/src/Smartscraper/SmartscraperCreateParams.php +++ b/src/Smartscraper/SmartscraperCreateParams.php @@ -116,8 +116,7 @@ final class SmartscraperCreateParams implements BaseModel */ public function __construct() { - self::introspect(); - $this->unsetOptionalProperties(); + $this->initialize(); } /** diff --git a/src/Validate/ValidateAPIKeyResponse.php b/src/Validate/ValidateAPIKeyResponse.php index 9288d86..6c12fdb 100644 --- a/src/Validate/ValidateAPIKeyResponse.php +++ b/src/Validate/ValidateAPIKeyResponse.php @@ -21,8 +21,7 @@ final class ValidateAPIKeyResponse implements BaseModel public function __construct() { - self::introspect(); - $this->unsetOptionalProperties(); + $this->initialize(); } /** diff --git a/tests/Core/TestModel.php b/tests/Core/TestModel.php index 2a119a5..4435f18 100644 --- a/tests/Core/TestModel.php +++ b/tests/Core/TestModel.php @@ -36,13 +36,12 @@ public function __construct( ?string $owner, ?array $friends = null, ) { + $this->initialize(); + $this->name = $name; $this->ageYears = $ageYears; $this->owner = $owner; - self::introspect(); - $this->unsetOptionalProperties(); - null != $friends && $this->friends = $friends; } } From c2300dcdc4d9370eb060b04ee25601572821f59c Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Sat, 30 Aug 2025 02:56:17 +0000 Subject: [PATCH 30/44] feat!: use builders for RequestOptions --- README.md | 8 +- src/Client.php | 2 +- src/Core.php | 4 +- src/Core/BaseClient.php | 19 +- src/Core/Concerns/SdkParams.php | 23 +-- .../{Omittable.php => Omit.php} | 4 +- src/RequestOptions.php | 164 ++++++++++++++---- 7 files changed, 154 insertions(+), 70 deletions(-) rename src/Core/Implementation/{Omittable.php => Omit.php} (77%) diff --git a/README.md b/README.md index 4891217..d062da8 100644 --- a/README.md +++ b/README.md @@ -112,7 +112,7 @@ Certain errors will be automatically retried 2 times by default, with a short ex Connection errors (for example, due to a network connectivity problem), 408 Request Timeout, 409 Conflict, 429 Rate Limit, >=500 Internal errors, and timeouts will all be retried by default. -You can use the `max_retries` option to configure or disable this: +You can use the `maxRetries` option to configure or disable this: ```php smartscraper->create( userPrompt: "Extract the product name, price, and description", - new RequestOptions(maxRetries: 5), + requestOptions: RequestOptions::with(maxRetries: 5), ); ``` @@ -139,7 +139,7 @@ $result = $client->smartscraper->create( You can send undocumented parameters to any endpoint, and read undocumented response properties, like so: -Note: the `extra_` parameters of the same name overrides the documented parameters. +Note: the `extra*` parameters of the same name overrides the documented parameters. ```php smartscraper->create( userPrompt: "Extract the product name, price, and description", - new RequestOptions( + requestOptions: RequestOptions::with( extraQueryParams: ["my_query_parameter" => "value"], extraBodyParams: ["my_body_parameter" => "value"], extraHeaders: ["my-header" => "value"], diff --git a/src/Client.php b/src/Client.php index da42d5c..b2ef76e 100644 --- a/src/Client.php +++ b/src/Client.php @@ -74,7 +74,7 @@ public function __construct(?string $apiKey = null, ?string $baseUrl = null) 'SCRAPEGRAPHAI_BASE_URL' ) ?: 'https://api.scrapegraphai.com/v1'; - $options = new RequestOptions( + $options = RequestOptions::with( uriFactory: Psr17FactoryDiscovery::findUriFactory(), streamFactory: Psr17FactoryDiscovery::findStreamFactory(), requestFactory: Psr17FactoryDiscovery::findRequestFactory(), diff --git a/src/Core.php b/src/Core.php index 0ddfc22..299a3d2 100644 --- a/src/Core.php +++ b/src/Core.php @@ -4,6 +4,6 @@ namespace Scrapegraphai\Core; -use Scrapegraphai\Core\Implementation\Omittable; +use Scrapegraphai\Core\Implementation\Omit; -const OMIT = Omittable::OMIT; +const OMIT = Omit::omit; diff --git a/src/Core/BaseClient.php b/src/Core/BaseClient.php index 2cd40bd..d33f197 100644 --- a/src/Core/BaseClient.php +++ b/src/Core/BaseClient.php @@ -32,6 +32,8 @@ class BaseClient protected UriInterface $baseUrl; /** + * @internal + * * @param array|null> $headers */ public function __construct( @@ -108,6 +110,8 @@ protected function authHeaders(): array } /** + * @internal + * * @param string|list $path * @param array $query * @param array|null> $headers @@ -135,26 +139,31 @@ protected function buildRequest( mixed $body, RequestOptions|array|null $opts, ): array { - $opts = array_merge($this->options->toArray(), RequestOptions::parse($opts)->toArray()); - $options = new RequestOptions(...$opts); + $options = RequestOptions::parse($this->options, $opts); $parsedPath = Util::parsePath($path); /** @var array $mergedQuery */ - $mergedQuery = array_merge_recursive($query, $options->extraQueryParams); + $mergedQuery = array_merge_recursive( + $query, + $options->extraQueryParams ?? [], + ); $uri = Util::joinUri($this->baseUrl, path: $parsedPath, query: $mergedQuery)->__toString(); /** @var array|null> $mergedHeaders */ $mergedHeaders = [...$this->headers, ...$this->authHeaders(), ...$headers, - ...$options->extraHeaders, ]; + ...($options->extraHeaders ?? []), ]; $req = ['method' => strtoupper($method), 'path' => $uri, 'query' => $mergedQuery, 'headers' => $mergedHeaders, 'body' => $body]; return [$req, $options]; } + /** + * @internal + */ protected function followRedirect( ResponseInterface $rsp, RequestInterface $req @@ -170,6 +179,8 @@ protected function followRedirect( } /** + * @internal + * * @param bool|int|float|string|resource|\Traversable|array|null $data */ diff --git a/src/Core/Concerns/SdkParams.php b/src/Core/Concerns/SdkParams.php index b75e3a7..b6ba7e0 100644 --- a/src/Core/Concerns/SdkParams.php +++ b/src/Core/Concerns/SdkParams.php @@ -18,15 +18,7 @@ trait SdkParams * @param array|self|null $params * @param array|RequestOptions|null $options * - * @return array{array, array{ - * timeout: float, - * maxRetries: int, - * initialRetryDelay: float, - * maxRetryDelay: float, - * extraHeaders: list, - * extraQueryParams: list, - * extraBodyParams: list, - * }} + * @return array{array, RequestOptions} */ public static function parseRequest(array|self|null $params, array|RequestOptions|null $options): array { @@ -40,17 +32,6 @@ public static function parseRequest(array|self|null $params, array|RequestOption $opts->maxRetries = 0; } - $opt = $opts->__serialize(); - if (empty($opt['extraHeaders'])) { - unset($opt['extraHeaders']); - } - if (empty($opt['extraQueryParams'])) { - unset($opt['extraQueryParams']); - } - if (empty($opt['extraBodyParams'])) { - unset($opt['extraBodyParams']); - } - - return [$dumped, $opt]; // @phpstan-ignore-line + return [$dumped, $opts]; // @phpstan-ignore-line } } diff --git a/src/Core/Implementation/Omittable.php b/src/Core/Implementation/Omit.php similarity index 77% rename from src/Core/Implementation/Omittable.php rename to src/Core/Implementation/Omit.php index 7ec1eb2..dd7f95e 100644 --- a/src/Core/Implementation/Omittable.php +++ b/src/Core/Implementation/Omit.php @@ -7,7 +7,7 @@ /** * @internal */ -enum Omittable +enum Omit { - case OMIT; + case omit; } diff --git a/src/RequestOptions.php b/src/RequestOptions.php index a14f915..41cca88 100644 --- a/src/RequestOptions.php +++ b/src/RequestOptions.php @@ -11,7 +11,7 @@ use Scrapegraphai\Core\Attributes\Api as Property; use Scrapegraphai\Core\Concerns\SdkModel; use Scrapegraphai\Core\Contracts\BaseModel; -use Scrapegraphai\Core\Implementation\Omittable; +use Scrapegraphai\Core\Implementation\Omit; use const Scrapegraphai\Core\OMIT as omit; @@ -48,15 +48,15 @@ final class RequestOptions implements BaseModel #[Property] public float $maxRetryDelay = 8.0; - /** @var array|null> $extraHeaders */ - #[Property] - public array $extraHeaders = []; + /** @var array|null>|null $extraHeaders */ + #[Property(optional: true)] + public ?array $extraHeaders; - /** @var array $extraQueryParams */ - #[Property] - public array $extraQueryParams = []; + /** @var array|null $extraQueryParams */ + #[Property(optional: true)] + public ?array $extraQueryParams; - #[Property] + #[Property(optional: true)] public mixed $extraBodyParams; #[Property(optional: true)] @@ -71,12 +71,27 @@ final class RequestOptions implements BaseModel #[Property(optional: true)] public ?RequestFactoryInterface $requestFactory; + public function __construct() + { + $this->initialize(); + } + + /** + * @param request_opts|null $options + */ + public static function parse(RequestOptions|array|null ...$options): self + { + $parsed = array_map(static fn ($o) => $o instanceof self ? $o->toArray() : $o ?? [], array: $options); + + return self::with(...array_merge(...$parsed)); // @phpstan-ignore-line + } + /** * @param array|null>|null $extraHeaders * @param array|null $extraQueryParams - * @param mixed|Omittable $extraBodyParams + * @param mixed|Omit $extraBodyParams */ - public function __construct( + public static function with( ?float $timeout = null, ?int $maxRetries = null, ?float $initialRetryDelay = null, @@ -88,40 +103,117 @@ public function __construct( ?UriFactoryInterface $uriFactory = null, ?StreamFactoryInterface $streamFactory = null, ?RequestFactoryInterface $requestFactory = null, - ) { - $this->initialize(); + ): self { + $obj = new self; + + null !== $timeout && $obj->timeout = $timeout; + null !== $maxRetries && $obj->maxRetries = $maxRetries; + null !== $initialRetryDelay && $obj->initialRetryDelay = $initialRetryDelay; + null !== $maxRetryDelay && $obj->maxRetryDelay = $maxRetryDelay; + null !== $extraHeaders && $obj->extraHeaders = $extraHeaders; + null !== $extraQueryParams && $obj->extraQueryParams = $extraQueryParams; + omit !== $extraBodyParams && $obj->extraBodyParams = $extraBodyParams; + null !== $transporter && $obj->transporter = $transporter; + null !== $uriFactory && $obj->uriFactory = $uriFactory; + null !== $streamFactory && $obj->streamFactory = $streamFactory; + null !== $requestFactory && $obj->requestFactory = $requestFactory; + + return $obj; + } + + public function withTimeout(float $timeout): self + { + $obj = clone $this; + $obj->timeout = $timeout; + + return $obj; + } + + public function withMaxRetries(int $maxRetries): self + { + $obj = clone $this; + $obj->maxRetries = $maxRetries; + + return $obj; + } + + public function withInitialRetryDelay(float $initialRetryDelay): self + { + $obj = clone $this; + $obj->initialRetryDelay = $initialRetryDelay; - null !== $timeout && $this->timeout = $timeout; - null !== $maxRetries && $this->maxRetries = $maxRetries; - null !== $initialRetryDelay && $this - ->initialRetryDelay = $initialRetryDelay - ; - null !== $maxRetryDelay && $this->maxRetryDelay = $maxRetryDelay; - null !== $extraHeaders && $this->extraHeaders = $extraHeaders; - null !== $extraQueryParams && $this->extraQueryParams = $extraQueryParams; - omit !== $extraBodyParams && $this->extraBodyParams = $extraBodyParams; - null !== $transporter && $this->transporter = $transporter; - null !== $uriFactory && $this->uriFactory = $uriFactory; - null !== $streamFactory && $this->streamFactory = $streamFactory; - null !== $requestFactory && $this->requestFactory = $requestFactory; + return $obj; + } + + public function withMaxRetryDelay(float $maxRetryDelay): self + { + $obj = clone $this; + $obj->maxRetryDelay = $maxRetryDelay; + + return $obj; } /** - * @param request_opts|null $options + * @param array|null> $extraHeaders + */ + public function withExtraHeaders(array $extraHeaders): self + { + $obj = clone $this; + $obj->extraHeaders = $extraHeaders; + + return $obj; + } + + /** + * @param array $extraQueryParams */ - public static function parse(RequestOptions|array|null $options): self + public function withExtraQueryParams(array $extraQueryParams): self + { + $obj = clone $this; + $obj->extraQueryParams = $extraQueryParams; + + return $obj; + } + + public function withExtraBodyParams(mixed $extraBodyParams): self + { + $obj = clone $this; + $obj->extraBodyParams = $extraBodyParams; + + return $obj; + } + + public function withTransporter(ClientInterface $transporter): self + { + $obj = clone $this; + $obj->transporter = $transporter; + + return $obj; + } + + public function withUriFactory(UriFactoryInterface $uriFactory): self { - if (is_null($options)) { - return new self; - } + $obj = clone $this; + $obj->uriFactory = $uriFactory; - if ($options instanceof self) { - return $options; - } + return $obj; + } + + public function withStreamFactory( + StreamFactoryInterface $streamFactory + ): self { + $obj = clone $this; + $obj->streamFactory = $streamFactory; + + return $obj; + } - $opts = new self; - $opts->__unserialize($options); + public function withRequestFactory( + RequestFactoryInterface $requestFactory + ): self { + $obj = clone $this; + $obj->requestFactory = $requestFactory; - return $opts; + return $obj; } } From ec1924477e3428e9bc066a7ef7473084f06a768c Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Wed, 3 Sep 2025 02:40:11 +0000 Subject: [PATCH 31/44] chore(internal): refactor base client internals --- src/Core/BaseClient.php | 88 +++++++++++++++++++++++++++++----- src/Core/Concerns/SdkModel.php | 2 +- 2 files changed, 78 insertions(+), 12 deletions(-) diff --git a/src/Core/BaseClient.php b/src/Core/BaseClient.php index d33f197..5ca0390 100644 --- a/src/Core/BaseClient.php +++ b/src/Core/BaseClient.php @@ -4,6 +4,7 @@ namespace Scrapegraphai\Core; +use Psr\Http\Client\ClientExceptionInterface; use Psr\Http\Client\ClientInterface; use Psr\Http\Message\RequestFactoryInterface; use Psr\Http\Message\RequestInterface; @@ -15,6 +16,7 @@ use Scrapegraphai\Core\Contracts\BaseStream; use Scrapegraphai\Core\Conversion\Contracts\Converter; use Scrapegraphai\Core\Conversion\Contracts\ConverterSource; +use Scrapegraphai\Core\Exceptions\APIConnectionException; use Scrapegraphai\Core\Exceptions\APIStatusException; use Scrapegraphai\RequestOptions; @@ -41,7 +43,7 @@ public function __construct( string $baseUrl, protected RequestOptions $options = new RequestOptions, ) { - assert(null !== $this->options->uriFactory); + assert(!is_null($this->options->uriFactory)); $this->baseUrl = $this->options->uriFactory->createUri($baseUrl); } @@ -67,7 +69,7 @@ public function request( // @phpstan-ignore-next-line [$req, $opts] = $this->buildRequest(method: $method, path: $path, query: $query, headers: $headers, body: $body, opts: $options); ['method' => $method, 'path' => $uri, 'headers' => $headers] = $req; - assert(null !== $opts->requestFactory); + assert(!is_null($opts->requestFactory)); $request = $opts->requestFactory->createRequest($method, uri: $uri); $request = Util::withSetHeaders($request, headers: $headers); @@ -170,7 +172,7 @@ protected function followRedirect( ): RequestInterface { $location = $rsp->getHeaderLine('Location'); if (!$location) { - throw new \RuntimeException('Redirection without Location header'); + throw new APIConnectionException($req, message: 'Redirection without Location header'); } $uri = Util::joinUri($req->getUri(), path: $location); @@ -178,6 +180,55 @@ protected function followRedirect( return $req->withUri($uri); } + /** + * @internal + */ + protected function shouldRetry( + RequestOptions $opts, + int $retryCount, + ?ResponseInterface $rsp + ): bool { + if ($retryCount >= $opts->maxRetries) { + return false; + } + + $code = $rsp?->getStatusCode(); + if (408 == $code || 409 == $code || 429 == $code || $code >= 500) { + return true; + } + + return false; + } + + /** + * @internal + */ + protected function retryDelay( + RequestOptions $opts, + int $retryCount, + ?ResponseInterface $rsp + ): float { + if (!empty($header = $rsp?->getHeaderLine('retry-after'))) { + if (is_numeric($header)) { + return floatval($header); + } + + try { + $date = new \DateTimeImmutable($header); + $span = time() - $date->getTimestamp(); + + return max(0.0, $span); + } catch (\DateMalformedStringException) { + } + } + + $scale = $retryCount ** 2; + $jitter = 1 - (0.25 * mt_rand() / mt_getrandmax()); + $naive = $opts->initialRetryDelay * $scale * $jitter; + + return max(0.0, min($naive, $opts->maxRetryDelay)); + } + /** * @internal * @@ -194,12 +245,23 @@ protected function sendRequest( assert(null !== $opts->streamFactory && null !== $opts->transporter); $req = Util::withSetBody($opts->streamFactory, req: $req, body: $data); - $rsp = $opts->transporter->sendRequest($req); - $code = $rsp->getStatusCode(); + + $rsp = null; + $err = null; + + try { + $rsp = $opts->transporter->sendRequest($req); + } catch (ClientExceptionInterface $e) { + $err = $e; + } + + $code = $rsp?->getStatusCode(); if ($code >= 300 && $code < 400) { + assert(!is_null($rsp)); + if ($redirectCount >= 20) { - throw new \RuntimeException('Maximum redirects exceeded'); + throw new APIConnectionException($req, message: 'Maximum redirects exceeded'); } $req = $this->followRedirect($rsp, req: $req); @@ -207,12 +269,16 @@ protected function sendRequest( return $this->sendRequest($opts, req: $req, data: $data, retryCount: $retryCount, redirectCount: ++$redirectCount); } - if ($code >= 400 && $code < 500) { - throw APIStatusException::from(request: $req, response: $rsp); - } + if ($code >= 400 || is_null($rsp)) { + if ($this->shouldRetry($opts, retryCount: $retryCount, rsp: $rsp)) { + $exn = is_null($rsp) ? new APIConnectionException($req, previous: $err) : APIStatusException::from(request: $req, response: $rsp); + + throw $exn; + } - if ($code >= 500 && $retryCount < $opts->maxRetries) { - usleep((int) $opts->initialRetryDelay); + $seconds = $this->retryDelay($opts, retryCount: $redirectCount, rsp: $rsp); + $floor = floor($seconds); + time_nanosleep((int) $floor, nanoseconds: (int) ($seconds - $floor) * 10 ** 9); return $this->sendRequest($opts, req: $req, data: $data, retryCount: ++$retryCount, redirectCount: $redirectCount); } diff --git a/src/Core/Concerns/SdkModel.php b/src/Core/Concerns/SdkModel.php index fdc45eb..b08cb78 100644 --- a/src/Core/Concerns/SdkModel.php +++ b/src/Core/Concerns/SdkModel.php @@ -125,7 +125,7 @@ public function offsetExists(mixed $offset): bool return true; } - $property = self::$converter->properties[$offset]->property ?? new \ReflectionProperty($this, property: $offset); + $property = self::$converter->properties[$offset]->property; return $property->isInitialized($this); } From 707cff08ced7c6f1da574965a81e4b30a531b391 Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Thu, 4 Sep 2025 02:36:54 +0000 Subject: [PATCH 32/44] chore: document parameter object usage --- src/Crawl/CrawlStartParams.php | 11 +++++++++++ src/Feedback/FeedbackSubmitParams.php | 11 +++++++++++ src/GenerateSchema/GenerateSchemaCreateParams.php | 11 +++++++++++ src/Markdownify/MarkdownifyConvertParams.php | 11 +++++++++++ src/Searchscraper/SearchscraperCreateParams.php | 11 +++++++++++ src/Smartscraper/SmartscraperCreateParams.php | 11 +++++++++++ 6 files changed, 66 insertions(+) diff --git a/src/Crawl/CrawlStartParams.php b/src/Crawl/CrawlStartParams.php index 03aeca4..f52ba5c 100644 --- a/src/Crawl/CrawlStartParams.php +++ b/src/Crawl/CrawlStartParams.php @@ -11,10 +11,21 @@ use Scrapegraphai\Crawl\CrawlStartParams\Rules; /** + * An object containing the method's parameters. + * Example usage: + * ``` + * $params = (new CrawlStartParams); // set properties as needed + * $client->crawl->start(...$params->toArray()); + * ``` * Initiate comprehensive website crawling with sitemap support. * Supports both AI extraction mode and markdown conversion mode. * Returns a task ID for async processing. * + * @method toArray() + * Returns the parameters as an associative array suitable for passing to the client method. + * + * `$client->crawl->start(...$params->toArray());` + * * @see Scrapegraphai\Crawl->start * * @phpstan-type crawl_start_params = array{ diff --git a/src/Feedback/FeedbackSubmitParams.php b/src/Feedback/FeedbackSubmitParams.php index adacb16..b3d2cac 100644 --- a/src/Feedback/FeedbackSubmitParams.php +++ b/src/Feedback/FeedbackSubmitParams.php @@ -10,8 +10,19 @@ use Scrapegraphai\Core\Contracts\BaseModel; /** + * An object containing the method's parameters. + * Example usage: + * ``` + * $params = (new FeedbackSubmitParams); // set properties as needed + * $client->feedback->submit(...$params->toArray()); + * ``` * Submit feedback for a specific request. * + * @method toArray() + * Returns the parameters as an associative array suitable for passing to the client method. + * + * `$client->feedback->submit(...$params->toArray());` + * * @see Scrapegraphai\Feedback->submit * * @phpstan-type feedback_submit_params = array{ diff --git a/src/GenerateSchema/GenerateSchemaCreateParams.php b/src/GenerateSchema/GenerateSchemaCreateParams.php index bb2c871..f7cfde5 100644 --- a/src/GenerateSchema/GenerateSchemaCreateParams.php +++ b/src/GenerateSchema/GenerateSchemaCreateParams.php @@ -10,9 +10,20 @@ use Scrapegraphai\Core\Contracts\BaseModel; /** + * An object containing the method's parameters. + * Example usage: + * ``` + * $params = (new GenerateSchemaCreateParams); // set properties as needed + * $client->generateSchema->create(...$params->toArray()); + * ``` * Generate or modify JSON schemas based on natural language descriptions. * Can create new schemas or extend existing ones. * + * @method toArray() + * Returns the parameters as an associative array suitable for passing to the client method. + * + * `$client->generateSchema->create(...$params->toArray());` + * * @see Scrapegraphai\GenerateSchema->create * * @phpstan-type generate_schema_create_params = array{ diff --git a/src/Markdownify/MarkdownifyConvertParams.php b/src/Markdownify/MarkdownifyConvertParams.php index 3204aeb..7e7eefd 100644 --- a/src/Markdownify/MarkdownifyConvertParams.php +++ b/src/Markdownify/MarkdownifyConvertParams.php @@ -10,8 +10,19 @@ use Scrapegraphai\Core\Contracts\BaseModel; /** + * An object containing the method's parameters. + * Example usage: + * ``` + * $params = (new MarkdownifyConvertParams); // set properties as needed + * $client->markdownify->convert(...$params->toArray()); + * ``` * Convert web page content to clean Markdown format. * + * @method toArray() + * Returns the parameters as an associative array suitable for passing to the client method. + * + * `$client->markdownify->convert(...$params->toArray());` + * * @see Scrapegraphai\Markdownify->convert * * @phpstan-type markdownify_convert_params = array{ diff --git a/src/Searchscraper/SearchscraperCreateParams.php b/src/Searchscraper/SearchscraperCreateParams.php index 0797d82..3c3ef60 100644 --- a/src/Searchscraper/SearchscraperCreateParams.php +++ b/src/Searchscraper/SearchscraperCreateParams.php @@ -10,9 +10,20 @@ use Scrapegraphai\Core\Contracts\BaseModel; /** + * An object containing the method's parameters. + * Example usage: + * ``` + * $params = (new SearchscraperCreateParams); // set properties as needed + * $client->searchscraper->create(...$params->toArray()); + * ``` * Performs web search, selects relevant URLs, and extracts structured data from multiple websites. * Uses LLM to refine search queries and merge results from different sources. * + * @method toArray() + * Returns the parameters as an associative array suitable for passing to the client method. + * + * `$client->searchscraper->create(...$params->toArray());` + * * @see Scrapegraphai\Searchscraper->create * * @phpstan-type searchscraper_create_params = array{ diff --git a/src/Smartscraper/SmartscraperCreateParams.php b/src/Smartscraper/SmartscraperCreateParams.php index 3998338..ad75b1c 100644 --- a/src/Smartscraper/SmartscraperCreateParams.php +++ b/src/Smartscraper/SmartscraperCreateParams.php @@ -10,9 +10,20 @@ use Scrapegraphai\Core\Contracts\BaseModel; /** + * An object containing the method's parameters. + * Example usage: + * ``` + * $params = (new SmartscraperCreateParams); // set properties as needed + * $client->smartscraper->create(...$params->toArray()); + * ``` * Main scraping endpoint with LLM-powered content analysis. Supports various fetching providers, * infinite scrolling, pagination, and custom output schemas. * + * @method toArray() + * Returns the parameters as an associative array suitable for passing to the client method. + * + * `$client->smartscraper->create(...$params->toArray());` + * * @see Scrapegraphai\Smartscraper->create * * @phpstan-type smartscraper_create_params = array{ From 372bf283cdd910023fd38ecbfb16570870450afd Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Fri, 5 Sep 2025 02:51:33 +0000 Subject: [PATCH 33/44] chore: cleanup streaming --- src/Core/Util.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Core/Util.php b/src/Core/Util.php index d1197bd..d2f9ce9 100644 --- a/src/Core/Util.php +++ b/src/Core/Util.php @@ -10,6 +10,11 @@ use Psr\Http\Message\StreamInterface; use Psr\Http\Message\UriInterface; +/** + * @phpstan-type SSEvent = array{ + * event?: string|null, data?: string|null, id?: string|null, retry?: int|null + * } + */ final class Util { public const BUF_SIZE = 8192; @@ -239,11 +244,7 @@ public static function decodeLines(\Iterator $stream): \Iterator /** * @param \Iterator $lines * - * @return \Generator< - * array{ - * event?: string|null, data?: string|null, id?: string|null, retry?: int|null - * }, - * > + * @return \Generator */ public static function decodeSSE(\Iterator $lines): \Generator { From c33963e7a8f9f8fbd461ca989e027974da1e8476 Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Fri, 5 Sep 2025 02:52:35 +0000 Subject: [PATCH 34/44] chore: make more targeted phpstan ignores --- tests/Services/CrawlTest.php | 6 +++--- tests/Services/CreditsTest.php | 2 +- tests/Services/FeedbackTest.php | 4 ++-- tests/Services/GenerateSchemaTest.php | 6 +++--- tests/Services/HealthzTest.php | 2 +- tests/Services/MarkdownifyTest.php | 6 +++--- tests/Services/SearchscraperTest.php | 6 +++--- tests/Services/SmartscraperTest.php | 8 ++++---- tests/Services/ValidateTest.php | 2 +- 9 files changed, 21 insertions(+), 21 deletions(-) diff --git a/tests/Services/CrawlTest.php b/tests/Services/CrawlTest.php index b5e68c4..6480cfb 100644 --- a/tests/Services/CrawlTest.php +++ b/tests/Services/CrawlTest.php @@ -35,7 +35,7 @@ public function testRetrieveResults(): void $result = $this->client->crawl->retrieveResults('task_id'); - $this->assertTrue(true); // @phpstan-ignore-line + $this->assertTrue(true); // @phpstan-ignore method.alreadyNarrowedType } #[Test] @@ -47,7 +47,7 @@ public function testStart(): void $result = $this->client->crawl->start(url: 'https://example.com'); - $this->assertTrue(true); // @phpstan-ignore-line + $this->assertTrue(true); // @phpstan-ignore method.alreadyNarrowedType } #[Test] @@ -59,6 +59,6 @@ public function testStartWithOptionalParams(): void $result = $this->client->crawl->start(url: 'https://example.com'); - $this->assertTrue(true); // @phpstan-ignore-line + $this->assertTrue(true); // @phpstan-ignore method.alreadyNarrowedType } } diff --git a/tests/Services/CreditsTest.php b/tests/Services/CreditsTest.php index de38f31..e23f6db 100644 --- a/tests/Services/CreditsTest.php +++ b/tests/Services/CreditsTest.php @@ -35,6 +35,6 @@ public function testRetrieve(): void $result = $this->client->credits->retrieve(); - $this->assertTrue(true); // @phpstan-ignore-line + $this->assertTrue(true); // @phpstan-ignore method.alreadyNarrowedType } } diff --git a/tests/Services/FeedbackTest.php b/tests/Services/FeedbackTest.php index 0b4e091..185d4f7 100644 --- a/tests/Services/FeedbackTest.php +++ b/tests/Services/FeedbackTest.php @@ -38,7 +38,7 @@ public function testSubmit(): void requestID: '182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e' ); - $this->assertTrue(true); // @phpstan-ignore-line + $this->assertTrue(true); // @phpstan-ignore method.alreadyNarrowedType } #[Test] @@ -53,6 +53,6 @@ public function testSubmitWithOptionalParams(): void requestID: '182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e' ); - $this->assertTrue(true); // @phpstan-ignore-line + $this->assertTrue(true); // @phpstan-ignore method.alreadyNarrowedType } } diff --git a/tests/Services/GenerateSchemaTest.php b/tests/Services/GenerateSchemaTest.php index dd02c2e..a7ee938 100644 --- a/tests/Services/GenerateSchemaTest.php +++ b/tests/Services/GenerateSchemaTest.php @@ -37,7 +37,7 @@ public function testCreate(): void userPrompt: 'Create a schema for product information including name, price, and reviews', ); - $this->assertTrue(true); // @phpstan-ignore-line + $this->assertTrue(true); // @phpstan-ignore method.alreadyNarrowedType } #[Test] @@ -51,7 +51,7 @@ public function testCreateWithOptionalParams(): void userPrompt: 'Create a schema for product information including name, price, and reviews', ); - $this->assertTrue(true); // @phpstan-ignore-line + $this->assertTrue(true); // @phpstan-ignore method.alreadyNarrowedType } #[Test] @@ -65,6 +65,6 @@ public function testRetrieve(): void '182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e' ); - $this->assertTrue(true); // @phpstan-ignore-line + $this->assertTrue(true); // @phpstan-ignore method.alreadyNarrowedType } } diff --git a/tests/Services/HealthzTest.php b/tests/Services/HealthzTest.php index a800457..9fddcab 100644 --- a/tests/Services/HealthzTest.php +++ b/tests/Services/HealthzTest.php @@ -35,6 +35,6 @@ public function testCheck(): void $result = $this->client->healthz->check(); - $this->assertTrue(true); // @phpstan-ignore-line + $this->assertTrue(true); // @phpstan-ignore method.alreadyNarrowedType } } diff --git a/tests/Services/MarkdownifyTest.php b/tests/Services/MarkdownifyTest.php index 4b48c12..cf98528 100644 --- a/tests/Services/MarkdownifyTest.php +++ b/tests/Services/MarkdownifyTest.php @@ -37,7 +37,7 @@ public function testConvert(): void websiteURL: 'https://example.com' ); - $this->assertTrue(true); // @phpstan-ignore-line + $this->assertTrue(true); // @phpstan-ignore method.alreadyNarrowedType } #[Test] @@ -51,7 +51,7 @@ public function testConvertWithOptionalParams(): void websiteURL: 'https://example.com' ); - $this->assertTrue(true); // @phpstan-ignore-line + $this->assertTrue(true); // @phpstan-ignore method.alreadyNarrowedType } #[Test] @@ -65,6 +65,6 @@ public function testRetrieveStatus(): void '182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e' ); - $this->assertTrue(true); // @phpstan-ignore-line + $this->assertTrue(true); // @phpstan-ignore method.alreadyNarrowedType } } diff --git a/tests/Services/SearchscraperTest.php b/tests/Services/SearchscraperTest.php index c4d4d35..2831df2 100644 --- a/tests/Services/SearchscraperTest.php +++ b/tests/Services/SearchscraperTest.php @@ -37,7 +37,7 @@ public function testCreate(): void userPrompt: 'Find the latest AI news and extract headlines and summaries' ); - $this->assertTrue(true); // @phpstan-ignore-line + $this->assertTrue(true); // @phpstan-ignore method.alreadyNarrowedType } #[Test] @@ -51,7 +51,7 @@ public function testCreateWithOptionalParams(): void userPrompt: 'Find the latest AI news and extract headlines and summaries' ); - $this->assertTrue(true); // @phpstan-ignore-line + $this->assertTrue(true); // @phpstan-ignore method.alreadyNarrowedType } #[Test] @@ -65,6 +65,6 @@ public function testRetrieveStatus(): void '182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e' ); - $this->assertTrue(true); // @phpstan-ignore-line + $this->assertTrue(true); // @phpstan-ignore method.alreadyNarrowedType } } diff --git a/tests/Services/SmartscraperTest.php b/tests/Services/SmartscraperTest.php index 7779c32..8c2e846 100644 --- a/tests/Services/SmartscraperTest.php +++ b/tests/Services/SmartscraperTest.php @@ -37,7 +37,7 @@ public function testCreate(): void userPrompt: 'Extract the product name, price, and description' ); - $this->assertTrue(true); // @phpstan-ignore-line + $this->assertTrue(true); // @phpstan-ignore method.alreadyNarrowedType } #[Test] @@ -51,7 +51,7 @@ public function testCreateWithOptionalParams(): void userPrompt: 'Extract the product name, price, and description' ); - $this->assertTrue(true); // @phpstan-ignore-line + $this->assertTrue(true); // @phpstan-ignore method.alreadyNarrowedType } #[Test] @@ -65,7 +65,7 @@ public function testRetrieve(): void '182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e' ); - $this->assertTrue(true); // @phpstan-ignore-line + $this->assertTrue(true); // @phpstan-ignore method.alreadyNarrowedType } #[Test] @@ -77,6 +77,6 @@ public function testList(): void $result = $this->client->smartscraper->list(); - $this->assertTrue(true); // @phpstan-ignore-line + $this->assertTrue(true); // @phpstan-ignore method.alreadyNarrowedType } } diff --git a/tests/Services/ValidateTest.php b/tests/Services/ValidateTest.php index 27ccaaa..7d45ecc 100644 --- a/tests/Services/ValidateTest.php +++ b/tests/Services/ValidateTest.php @@ -35,6 +35,6 @@ public function testAPIKey(): void $result = $this->client->validate->apiKey(); - $this->assertTrue(true); // @phpstan-ignore-line + $this->assertTrue(true); // @phpstan-ignore method.alreadyNarrowedType } } From fcb58d5574acc36d7c2e0e41c2b973c7b6ff1613 Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Sat, 6 Sep 2025 02:53:50 +0000 Subject: [PATCH 35/44] feat!: expose services and service contracts --- src/Client.php | 18 +++++++++--------- .../ServiceContracts/CrawlContract.php | 2 +- .../ServiceContracts/CreditsContract.php | 2 +- .../ServiceContracts/FeedbackContract.php | 2 +- .../GenerateSchemaContract.php | 2 +- .../ServiceContracts/HealthzContract.php | 2 +- .../ServiceContracts/MarkdownifyContract.php | 2 +- .../ServiceContracts/SearchscraperContract.php | 2 +- .../ServiceContracts/SmartscraperContract.php | 2 +- .../ServiceContracts/ValidateContract.php | 2 +- src/{Core => }/Services/CrawlService.php | 4 ++-- src/{Core => }/Services/CreditsService.php | 4 ++-- src/{Core => }/Services/FeedbackService.php | 4 ++-- .../Services/GenerateSchemaService.php | 4 ++-- src/{Core => }/Services/HealthzService.php | 4 ++-- src/{Core => }/Services/MarkdownifyService.php | 4 ++-- .../Services/SearchscraperService.php | 4 ++-- .../Services/SmartscraperService.php | 4 ++-- src/{Core => }/Services/ValidateService.php | 4 ++-- 19 files changed, 36 insertions(+), 36 deletions(-) rename src/{Core => }/ServiceContracts/CrawlContract.php (96%) rename src/{Core => }/ServiceContracts/CreditsContract.php (85%) rename src/{Core => }/ServiceContracts/FeedbackContract.php (92%) rename src/{Core => }/ServiceContracts/GenerateSchemaContract.php (95%) rename src/{Core => }/ServiceContracts/HealthzContract.php (85%) rename src/{Core => }/ServiceContracts/MarkdownifyContract.php (94%) rename src/{Core => }/ServiceContracts/SearchscraperContract.php (95%) rename src/{Core => }/ServiceContracts/SmartscraperContract.php (97%) rename src/{Core => }/ServiceContracts/ValidateContract.php (85%) rename src/{Core => }/Services/CrawlService.php (96%) rename src/{Core => }/Services/CreditsService.php (88%) rename src/{Core => }/Services/FeedbackService.php (93%) rename src/{Core => }/Services/GenerateSchemaService.php (95%) rename src/{Core => }/Services/HealthzService.php (88%) rename src/{Core => }/Services/MarkdownifyService.php (95%) rename src/{Core => }/Services/SearchscraperService.php (95%) rename src/{Core => }/Services/SmartscraperService.php (97%) rename src/{Core => }/Services/ValidateService.php (88%) diff --git a/src/Client.php b/src/Client.php index b2ef76e..b249101 100644 --- a/src/Client.php +++ b/src/Client.php @@ -7,15 +7,15 @@ use Http\Discovery\Psr17FactoryDiscovery; use Http\Discovery\Psr18ClientDiscovery; use Scrapegraphai\Core\BaseClient; -use Scrapegraphai\Core\Services\CrawlService; -use Scrapegraphai\Core\Services\CreditsService; -use Scrapegraphai\Core\Services\FeedbackService; -use Scrapegraphai\Core\Services\GenerateSchemaService; -use Scrapegraphai\Core\Services\HealthzService; -use Scrapegraphai\Core\Services\MarkdownifyService; -use Scrapegraphai\Core\Services\SearchscraperService; -use Scrapegraphai\Core\Services\SmartscraperService; -use Scrapegraphai\Core\Services\ValidateService; +use Scrapegraphai\Services\CrawlService; +use Scrapegraphai\Services\CreditsService; +use Scrapegraphai\Services\FeedbackService; +use Scrapegraphai\Services\GenerateSchemaService; +use Scrapegraphai\Services\HealthzService; +use Scrapegraphai\Services\MarkdownifyService; +use Scrapegraphai\Services\SearchscraperService; +use Scrapegraphai\Services\SmartscraperService; +use Scrapegraphai\Services\ValidateService; class Client extends BaseClient { diff --git a/src/Core/ServiceContracts/CrawlContract.php b/src/ServiceContracts/CrawlContract.php similarity index 96% rename from src/Core/ServiceContracts/CrawlContract.php rename to src/ServiceContracts/CrawlContract.php index a005c6b..8f96ff7 100644 --- a/src/Core/ServiceContracts/CrawlContract.php +++ b/src/ServiceContracts/CrawlContract.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Scrapegraphai\Core\ServiceContracts; +namespace Scrapegraphai\ServiceContracts; use Scrapegraphai\Crawl\CrawlGetResultsResponse; use Scrapegraphai\Crawl\CrawlStartParams\Rules; diff --git a/src/Core/ServiceContracts/CreditsContract.php b/src/ServiceContracts/CreditsContract.php similarity index 85% rename from src/Core/ServiceContracts/CreditsContract.php rename to src/ServiceContracts/CreditsContract.php index a5381d0..66a5438 100644 --- a/src/Core/ServiceContracts/CreditsContract.php +++ b/src/ServiceContracts/CreditsContract.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Scrapegraphai\Core\ServiceContracts; +namespace Scrapegraphai\ServiceContracts; use Scrapegraphai\Credits\CreditGetResponse; use Scrapegraphai\RequestOptions; diff --git a/src/Core/ServiceContracts/FeedbackContract.php b/src/ServiceContracts/FeedbackContract.php similarity index 92% rename from src/Core/ServiceContracts/FeedbackContract.php rename to src/ServiceContracts/FeedbackContract.php index b1aa9b6..e2a0c43 100644 --- a/src/Core/ServiceContracts/FeedbackContract.php +++ b/src/ServiceContracts/FeedbackContract.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Scrapegraphai\Core\ServiceContracts; +namespace Scrapegraphai\ServiceContracts; use Scrapegraphai\Feedback\FeedbackSubmitResponse; use Scrapegraphai\RequestOptions; diff --git a/src/Core/ServiceContracts/GenerateSchemaContract.php b/src/ServiceContracts/GenerateSchemaContract.php similarity index 95% rename from src/Core/ServiceContracts/GenerateSchemaContract.php rename to src/ServiceContracts/GenerateSchemaContract.php index 41a3110..41ac390 100644 --- a/src/Core/ServiceContracts/GenerateSchemaContract.php +++ b/src/ServiceContracts/GenerateSchemaContract.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Scrapegraphai\Core\ServiceContracts; +namespace Scrapegraphai\ServiceContracts; use Scrapegraphai\GenerateSchema\GenerateSchemaGetResponse\CompletedSchemaGenerationResponse; use Scrapegraphai\GenerateSchema\GenerateSchemaGetResponse\FailedSchemaGenerationResponse; diff --git a/src/Core/ServiceContracts/HealthzContract.php b/src/ServiceContracts/HealthzContract.php similarity index 85% rename from src/Core/ServiceContracts/HealthzContract.php rename to src/ServiceContracts/HealthzContract.php index ec42978..2a8bdd5 100644 --- a/src/Core/ServiceContracts/HealthzContract.php +++ b/src/ServiceContracts/HealthzContract.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Scrapegraphai\Core\ServiceContracts; +namespace Scrapegraphai\ServiceContracts; use Scrapegraphai\Healthz\HealthzCheckResponse; use Scrapegraphai\RequestOptions; diff --git a/src/Core/ServiceContracts/MarkdownifyContract.php b/src/ServiceContracts/MarkdownifyContract.php similarity index 94% rename from src/Core/ServiceContracts/MarkdownifyContract.php rename to src/ServiceContracts/MarkdownifyContract.php index a2b30ca..2496cab 100644 --- a/src/Core/ServiceContracts/MarkdownifyContract.php +++ b/src/ServiceContracts/MarkdownifyContract.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Scrapegraphai\Core\ServiceContracts; +namespace Scrapegraphai\ServiceContracts; use Scrapegraphai\Markdownify\CompletedMarkdownify; use Scrapegraphai\Markdownify\MarkdownifyGetStatusResponse\FailedMarkdownifyResponse; diff --git a/src/Core/ServiceContracts/SearchscraperContract.php b/src/ServiceContracts/SearchscraperContract.php similarity index 95% rename from src/Core/ServiceContracts/SearchscraperContract.php rename to src/ServiceContracts/SearchscraperContract.php index 180c248..3bcf944 100644 --- a/src/Core/ServiceContracts/SearchscraperContract.php +++ b/src/ServiceContracts/SearchscraperContract.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Scrapegraphai\Core\ServiceContracts; +namespace Scrapegraphai\ServiceContracts; use Scrapegraphai\RequestOptions; use Scrapegraphai\Searchscraper\CompletedSearchScraper; diff --git a/src/Core/ServiceContracts/SmartscraperContract.php b/src/ServiceContracts/SmartscraperContract.php similarity index 97% rename from src/Core/ServiceContracts/SmartscraperContract.php rename to src/ServiceContracts/SmartscraperContract.php index 8778595..8d87a4f 100644 --- a/src/Core/ServiceContracts/SmartscraperContract.php +++ b/src/ServiceContracts/SmartscraperContract.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Scrapegraphai\Core\ServiceContracts; +namespace Scrapegraphai\ServiceContracts; use Scrapegraphai\RequestOptions; use Scrapegraphai\Smartscraper\CompletedSmartscraper; diff --git a/src/Core/ServiceContracts/ValidateContract.php b/src/ServiceContracts/ValidateContract.php similarity index 85% rename from src/Core/ServiceContracts/ValidateContract.php rename to src/ServiceContracts/ValidateContract.php index 5c41a7c..c241d83 100644 --- a/src/Core/ServiceContracts/ValidateContract.php +++ b/src/ServiceContracts/ValidateContract.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Scrapegraphai\Core\ServiceContracts; +namespace Scrapegraphai\ServiceContracts; use Scrapegraphai\RequestOptions; use Scrapegraphai\Validate\ValidateAPIKeyResponse; diff --git a/src/Core/Services/CrawlService.php b/src/Services/CrawlService.php similarity index 96% rename from src/Core/Services/CrawlService.php rename to src/Services/CrawlService.php index e3a48bb..cdece3e 100644 --- a/src/Core/Services/CrawlService.php +++ b/src/Services/CrawlService.php @@ -2,15 +2,15 @@ declare(strict_types=1); -namespace Scrapegraphai\Core\Services; +namespace Scrapegraphai\Services; use Scrapegraphai\Client; -use Scrapegraphai\Core\ServiceContracts\CrawlContract; use Scrapegraphai\Crawl\CrawlGetResultsResponse; use Scrapegraphai\Crawl\CrawlStartParams; use Scrapegraphai\Crawl\CrawlStartParams\Rules; use Scrapegraphai\Crawl\CrawlStartResponse; use Scrapegraphai\RequestOptions; +use Scrapegraphai\ServiceContracts\CrawlContract; use const Scrapegraphai\Core\OMIT as omit; diff --git a/src/Core/Services/CreditsService.php b/src/Services/CreditsService.php similarity index 88% rename from src/Core/Services/CreditsService.php rename to src/Services/CreditsService.php index e69f63c..e601334 100644 --- a/src/Core/Services/CreditsService.php +++ b/src/Services/CreditsService.php @@ -2,12 +2,12 @@ declare(strict_types=1); -namespace Scrapegraphai\Core\Services; +namespace Scrapegraphai\Services; use Scrapegraphai\Client; -use Scrapegraphai\Core\ServiceContracts\CreditsContract; use Scrapegraphai\Credits\CreditGetResponse; use Scrapegraphai\RequestOptions; +use Scrapegraphai\ServiceContracts\CreditsContract; final class CreditsService implements CreditsContract { diff --git a/src/Core/Services/FeedbackService.php b/src/Services/FeedbackService.php similarity index 93% rename from src/Core/Services/FeedbackService.php rename to src/Services/FeedbackService.php index 553f933..60f0a69 100644 --- a/src/Core/Services/FeedbackService.php +++ b/src/Services/FeedbackService.php @@ -2,13 +2,13 @@ declare(strict_types=1); -namespace Scrapegraphai\Core\Services; +namespace Scrapegraphai\Services; use Scrapegraphai\Client; -use Scrapegraphai\Core\ServiceContracts\FeedbackContract; use Scrapegraphai\Feedback\FeedbackSubmitParams; use Scrapegraphai\Feedback\FeedbackSubmitResponse; use Scrapegraphai\RequestOptions; +use Scrapegraphai\ServiceContracts\FeedbackContract; use const Scrapegraphai\Core\OMIT as omit; diff --git a/src/Core/Services/GenerateSchemaService.php b/src/Services/GenerateSchemaService.php similarity index 95% rename from src/Core/Services/GenerateSchemaService.php rename to src/Services/GenerateSchemaService.php index 02566f2..191698b 100644 --- a/src/Core/Services/GenerateSchemaService.php +++ b/src/Services/GenerateSchemaService.php @@ -2,16 +2,16 @@ declare(strict_types=1); -namespace Scrapegraphai\Core\Services; +namespace Scrapegraphai\Services; use Scrapegraphai\Client; -use Scrapegraphai\Core\ServiceContracts\GenerateSchemaContract; use Scrapegraphai\GenerateSchema\GenerateSchemaCreateParams; use Scrapegraphai\GenerateSchema\GenerateSchemaGetResponse; use Scrapegraphai\GenerateSchema\GenerateSchemaGetResponse\CompletedSchemaGenerationResponse; use Scrapegraphai\GenerateSchema\GenerateSchemaGetResponse\FailedSchemaGenerationResponse; use Scrapegraphai\GenerateSchema\GenerateSchemaNewResponse; use Scrapegraphai\RequestOptions; +use Scrapegraphai\ServiceContracts\GenerateSchemaContract; use const Scrapegraphai\Core\OMIT as omit; diff --git a/src/Core/Services/HealthzService.php b/src/Services/HealthzService.php similarity index 88% rename from src/Core/Services/HealthzService.php rename to src/Services/HealthzService.php index d11d8cc..818ab60 100644 --- a/src/Core/Services/HealthzService.php +++ b/src/Services/HealthzService.php @@ -2,12 +2,12 @@ declare(strict_types=1); -namespace Scrapegraphai\Core\Services; +namespace Scrapegraphai\Services; use Scrapegraphai\Client; -use Scrapegraphai\Core\ServiceContracts\HealthzContract; use Scrapegraphai\Healthz\HealthzCheckResponse; use Scrapegraphai\RequestOptions; +use Scrapegraphai\ServiceContracts\HealthzContract; final class HealthzService implements HealthzContract { diff --git a/src/Core/Services/MarkdownifyService.php b/src/Services/MarkdownifyService.php similarity index 95% rename from src/Core/Services/MarkdownifyService.php rename to src/Services/MarkdownifyService.php index f527084..633309e 100644 --- a/src/Core/Services/MarkdownifyService.php +++ b/src/Services/MarkdownifyService.php @@ -2,15 +2,15 @@ declare(strict_types=1); -namespace Scrapegraphai\Core\Services; +namespace Scrapegraphai\Services; use Scrapegraphai\Client; -use Scrapegraphai\Core\ServiceContracts\MarkdownifyContract; use Scrapegraphai\Markdownify\CompletedMarkdownify; use Scrapegraphai\Markdownify\MarkdownifyConvertParams; use Scrapegraphai\Markdownify\MarkdownifyGetStatusResponse; use Scrapegraphai\Markdownify\MarkdownifyGetStatusResponse\FailedMarkdownifyResponse; use Scrapegraphai\RequestOptions; +use Scrapegraphai\ServiceContracts\MarkdownifyContract; use const Scrapegraphai\Core\OMIT as omit; diff --git a/src/Core/Services/SearchscraperService.php b/src/Services/SearchscraperService.php similarity index 95% rename from src/Core/Services/SearchscraperService.php rename to src/Services/SearchscraperService.php index c474c36..c0009f9 100644 --- a/src/Core/Services/SearchscraperService.php +++ b/src/Services/SearchscraperService.php @@ -2,15 +2,15 @@ declare(strict_types=1); -namespace Scrapegraphai\Core\Services; +namespace Scrapegraphai\Services; use Scrapegraphai\Client; -use Scrapegraphai\Core\ServiceContracts\SearchscraperContract; use Scrapegraphai\RequestOptions; use Scrapegraphai\Searchscraper\CompletedSearchScraper; use Scrapegraphai\Searchscraper\SearchscraperCreateParams; use Scrapegraphai\Searchscraper\SearchscraperGetStatusResponse; use Scrapegraphai\Searchscraper\SearchscraperGetStatusResponse\FailedSearchScraperResponse; +use Scrapegraphai\ServiceContracts\SearchscraperContract; use const Scrapegraphai\Core\OMIT as omit; diff --git a/src/Core/Services/SmartscraperService.php b/src/Services/SmartscraperService.php similarity index 97% rename from src/Core/Services/SmartscraperService.php rename to src/Services/SmartscraperService.php index d24ce64..f03793e 100644 --- a/src/Core/Services/SmartscraperService.php +++ b/src/Services/SmartscraperService.php @@ -2,11 +2,11 @@ declare(strict_types=1); -namespace Scrapegraphai\Core\Services; +namespace Scrapegraphai\Services; use Scrapegraphai\Client; -use Scrapegraphai\Core\ServiceContracts\SmartscraperContract; use Scrapegraphai\RequestOptions; +use Scrapegraphai\ServiceContracts\SmartscraperContract; use Scrapegraphai\Smartscraper\CompletedSmartscraper; use Scrapegraphai\Smartscraper\FailedSmartscraper; use Scrapegraphai\Smartscraper\SmartscraperCreateParams; diff --git a/src/Core/Services/ValidateService.php b/src/Services/ValidateService.php similarity index 88% rename from src/Core/Services/ValidateService.php rename to src/Services/ValidateService.php index 7bb0a7f..68a834c 100644 --- a/src/Core/Services/ValidateService.php +++ b/src/Services/ValidateService.php @@ -2,11 +2,11 @@ declare(strict_types=1); -namespace Scrapegraphai\Core\Services; +namespace Scrapegraphai\Services; use Scrapegraphai\Client; -use Scrapegraphai\Core\ServiceContracts\ValidateContract; use Scrapegraphai\RequestOptions; +use Scrapegraphai\ServiceContracts\ValidateContract; use Scrapegraphai\Validate\ValidateAPIKeyResponse; final class ValidateService implements ValidateContract From 196fd2985d799749f11af3ef4df7e29f30a4455f Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Tue, 9 Sep 2025 02:28:16 +0000 Subject: [PATCH 36/44] feat(client): use real enums --- src/Core/Attributes/Api.php | 34 ++++++++++++++++--- src/Crawl/CrawlGetResultsResponse.php | 16 ++++----- src/Crawl/CrawlGetResultsResponse/Status.php | 19 ++++------- .../CompletedSchemaGenerationResponse.php | 16 ++++----- .../Status.php | 9 ++--- .../FailedSchemaGenerationResponse.php | 16 ++++----- .../FailedSchemaGenerationResponse/Status.php | 9 ++--- .../GenerateSchemaNewResponse.php | 16 ++++----- .../GenerateSchemaNewResponse/Status.php | 9 ++--- src/Markdownify/CompletedMarkdownify.php | 16 ++++----- .../CompletedMarkdownify/Status.php | 13 +++---- .../FailedMarkdownifyResponse.php | 16 ++++----- .../FailedMarkdownifyResponse/Status.php | 9 ++--- src/Searchscraper/CompletedSearchScraper.php | 16 ++++----- .../CompletedSearchScraper/Status.php | 13 +++---- .../FailedSearchScraperResponse.php | 16 ++++----- .../FailedSearchScraperResponse/Status.php | 9 ++--- src/Smartscraper/CompletedSmartscraper.php | 16 ++++----- .../CompletedSmartscraper/Status.php | 13 +++---- src/Smartscraper/FailedSmartscraper.php | 16 ++++----- .../FailedSmartscraper/Status.php | 9 ++--- 21 files changed, 140 insertions(+), 166 deletions(-) diff --git a/src/Core/Attributes/Api.php b/src/Core/Attributes/Api.php index a24e309..3ee64ba 100644 --- a/src/Core/Attributes/Api.php +++ b/src/Core/Attributes/Api.php @@ -6,6 +6,7 @@ use Scrapegraphai\Core\Conversion\Contracts\Converter; use Scrapegraphai\Core\Conversion\Contracts\ConverterSource; +use Scrapegraphai\Core\Conversion\EnumOf; use Scrapegraphai\Core\Conversion\ListOf; use Scrapegraphai\Core\Conversion\MapOf; @@ -15,14 +16,15 @@ #[\Attribute(\Attribute::TARGET_PROPERTY)] final class Api { - /** - * @var class-string|Converter|string|null - */ + /** @var class-string|Converter|string|null */ public readonly Converter|string|null $type; + /** @var array */ + private static array $enumConverters = []; + /** * @param class-string|Converter|string|null $type - * @param class-string|Converter|null $enum + * @param class-string<\BackedEnum>|Converter|null $enum * @param class-string|Converter|null $union * @param class-string|Converter|string|null $list * @param class-string|Converter|string|null $map @@ -37,6 +39,28 @@ public function __construct( public readonly bool $nullable = false, public readonly bool $optional = false, ) { - $this->type = $type ?? $enum ?? $union ?? ($list ? new ListOf($list) : ($map ? new MapOf($map) : null)); + $type ??= $union; + if (null !== $list) { + $type ??= new ListOf($list); + } + if (null !== $map) { + $type ??= new MapOf($map); + } + if (null !== $enum) { + $type ??= $enum instanceof Converter ? $enum : $this->getEnumConverter($enum); + } + + $this->type = $type; + } + + /** @property class-string<\BackedEnum> $enum */ + private function getEnumConverter(string $enum): Converter + { + if (!isset(self::$enumConverters[$enum])) { + $converter = new EnumOf(array_column($enum::cases(), 'value')); // @phpstan-ignore-line + self::$enumConverters[$enum] = $converter; + } + + return self::$enumConverters[$enum]; } } diff --git a/src/Crawl/CrawlGetResultsResponse.php b/src/Crawl/CrawlGetResultsResponse.php index 54fe5ed..7ef9b98 100644 --- a/src/Crawl/CrawlGetResultsResponse.php +++ b/src/Crawl/CrawlGetResultsResponse.php @@ -13,7 +13,7 @@ /** * @phpstan-type crawl_get_results_response = array{ * result?: mixed|string|null, - * status?: Status::*|null, + * status?: value-of|null, * taskID?: string|null, * traceback?: string|null, * } @@ -31,7 +31,7 @@ final class CrawlGetResultsResponse implements BaseModel #[Api(union: Result::class, optional: true)] public mixed $result; - /** @var Status::*|null $status */ + /** @var value-of|null $status */ #[Api(enum: Status::class, optional: true)] public ?string $status; @@ -55,18 +55,18 @@ public function __construct() * You must use named parameters to construct any parameters with a default value. * * @param mixed|string $result - * @param Status::* $status + * @param Status|value-of $status */ public static function with( mixed $result = null, - ?string $status = null, + Status|string|null $status = null, ?string $taskID = null, ?string $traceback = null, ): self { $obj = new self; null !== $result && $obj->result = $result; - null !== $status && $obj->status = $status; + null !== $status && $obj->status = $status instanceof Status ? $status->value : $status; null !== $taskID && $obj->taskID = $taskID; null !== $traceback && $obj->traceback = $traceback; @@ -87,12 +87,12 @@ public function withResult(mixed $result): self } /** - * @param Status::* $status + * @param Status|value-of $status */ - public function withStatus(string $status): self + public function withStatus(Status|string $status): self { $obj = clone $this; - $obj->status = $status; + $obj->status = $status instanceof Status ? $status->value : $status; return $obj; } diff --git a/src/Crawl/CrawlGetResultsResponse/Status.php b/src/Crawl/CrawlGetResultsResponse/Status.php index 47c9438..01f20ee 100644 --- a/src/Crawl/CrawlGetResultsResponse/Status.php +++ b/src/Crawl/CrawlGetResultsResponse/Status.php @@ -4,22 +4,17 @@ namespace Scrapegraphai\Crawl\CrawlGetResultsResponse; -use Scrapegraphai\Core\Concerns\SdkEnum; -use Scrapegraphai\Core\Conversion\Contracts\ConverterSource; - -final class Status implements ConverterSource +enum Status: string { - use SdkEnum; - - public const PENDING = 'PENDING'; + case PENDING = 'PENDING'; - public const STARTED = 'STARTED'; + case STARTED = 'STARTED'; - public const SUCCESS = 'SUCCESS'; + case SUCCESS = 'SUCCESS'; - public const FAILURE = 'FAILURE'; + case FAILURE = 'FAILURE'; - public const RETRY = 'RETRY'; + case RETRY = 'RETRY'; - public const REVOKED = 'REVOKED'; + case REVOKED = 'REVOKED'; } diff --git a/src/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse.php b/src/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse.php index b4f3fbf..6cfa20a 100644 --- a/src/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse.php +++ b/src/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse.php @@ -15,7 +15,7 @@ * generatedSchema?: mixed, * refinedPrompt?: string|null, * requestID?: string|null, - * status?: Status::*|null, + * status?: value-of|null, * userPrompt?: string|null, * } */ @@ -36,7 +36,7 @@ final class CompletedSchemaGenerationResponse implements BaseModel #[Api('request_id', optional: true)] public ?string $requestID; - /** @var Status::*|null $status */ + /** @var value-of|null $status */ #[Api(enum: Status::class, optional: true)] public ?string $status; @@ -53,14 +53,14 @@ public function __construct() * * You must use named parameters to construct any parameters with a default value. * - * @param Status::* $status + * @param Status|value-of $status */ public static function with( ?string $error = null, mixed $generatedSchema = null, ?string $refinedPrompt = null, ?string $requestID = null, - ?string $status = null, + Status|string|null $status = null, ?string $userPrompt = null, ): self { $obj = new self; @@ -69,7 +69,7 @@ public static function with( null !== $generatedSchema && $obj->generatedSchema = $generatedSchema; null !== $refinedPrompt && $obj->refinedPrompt = $refinedPrompt; null !== $requestID && $obj->requestID = $requestID; - null !== $status && $obj->status = $status; + null !== $status && $obj->status = $status instanceof Status ? $status->value : $status; null !== $userPrompt && $obj->userPrompt = $userPrompt; return $obj; @@ -108,12 +108,12 @@ public function withRequestID(string $requestID): self } /** - * @param Status::* $status + * @param Status|value-of $status */ - public function withStatus(string $status): self + public function withStatus(Status|string $status): self { $obj = clone $this; - $obj->status = $status; + $obj->status = $status instanceof Status ? $status->value : $status; return $obj; } diff --git a/src/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse/Status.php b/src/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse/Status.php index f862250..76dee4c 100644 --- a/src/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse/Status.php +++ b/src/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse/Status.php @@ -4,12 +4,7 @@ namespace Scrapegraphai\GenerateSchema\GenerateSchemaGetResponse\CompletedSchemaGenerationResponse; -use Scrapegraphai\Core\Concerns\SdkEnum; -use Scrapegraphai\Core\Conversion\Contracts\ConverterSource; - -final class Status implements ConverterSource +enum Status: string { - use SdkEnum; - - public const COMPLETED = 'completed'; + case COMPLETED = 'completed'; } diff --git a/src/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse.php b/src/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse.php index 1dfdc0c..83a661b 100644 --- a/src/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse.php +++ b/src/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse.php @@ -15,7 +15,7 @@ * generatedSchema?: mixed, * refinedPrompt?: string|null, * requestID?: string|null, - * status?: Status::*|null, + * status?: value-of|null, * userPrompt?: string|null, * } */ @@ -36,7 +36,7 @@ final class FailedSchemaGenerationResponse implements BaseModel #[Api('request_id', optional: true)] public ?string $requestID; - /** @var Status::*|null $status */ + /** @var value-of|null $status */ #[Api(enum: Status::class, optional: true)] public ?string $status; @@ -53,14 +53,14 @@ public function __construct() * * You must use named parameters to construct any parameters with a default value. * - * @param Status::* $status + * @param Status|value-of $status */ public static function with( ?string $error = null, mixed $generatedSchema = null, ?string $refinedPrompt = null, ?string $requestID = null, - ?string $status = null, + Status|string|null $status = null, ?string $userPrompt = null, ): self { $obj = new self; @@ -69,7 +69,7 @@ public static function with( null !== $generatedSchema && $obj->generatedSchema = $generatedSchema; null !== $refinedPrompt && $obj->refinedPrompt = $refinedPrompt; null !== $requestID && $obj->requestID = $requestID; - null !== $status && $obj->status = $status; + null !== $status && $obj->status = $status instanceof Status ? $status->value : $status; null !== $userPrompt && $obj->userPrompt = $userPrompt; return $obj; @@ -108,12 +108,12 @@ public function withRequestID(string $requestID): self } /** - * @param Status::* $status + * @param Status|value-of $status */ - public function withStatus(string $status): self + public function withStatus(Status|string $status): self { $obj = clone $this; - $obj->status = $status; + $obj->status = $status instanceof Status ? $status->value : $status; return $obj; } diff --git a/src/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse/Status.php b/src/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse/Status.php index 07d8023..b894cca 100644 --- a/src/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse/Status.php +++ b/src/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse/Status.php @@ -4,12 +4,7 @@ namespace Scrapegraphai\GenerateSchema\GenerateSchemaGetResponse\FailedSchemaGenerationResponse; -use Scrapegraphai\Core\Concerns\SdkEnum; -use Scrapegraphai\Core\Conversion\Contracts\ConverterSource; - -final class Status implements ConverterSource +enum Status: string { - use SdkEnum; - - public const FAILED = 'failed'; + case FAILED = 'failed'; } diff --git a/src/GenerateSchema/GenerateSchemaNewResponse.php b/src/GenerateSchema/GenerateSchemaNewResponse.php index 17888c7..58e94d8 100644 --- a/src/GenerateSchema/GenerateSchemaNewResponse.php +++ b/src/GenerateSchema/GenerateSchemaNewResponse.php @@ -15,7 +15,7 @@ * generatedSchema?: mixed, * refinedPrompt?: string|null, * requestID?: string|null, - * status?: Status::*|null, + * status?: value-of|null, * userPrompt?: string|null, * } */ @@ -42,7 +42,7 @@ final class GenerateSchemaNewResponse implements BaseModel #[Api('request_id', optional: true)] public ?string $requestID; - /** @var Status::*|null $status */ + /** @var value-of|null $status */ #[Api(enum: Status::class, optional: true)] public ?string $status; @@ -59,14 +59,14 @@ public function __construct() * * You must use named parameters to construct any parameters with a default value. * - * @param Status::* $status + * @param Status|value-of $status */ public static function with( ?string $error = null, mixed $generatedSchema = null, ?string $refinedPrompt = null, ?string $requestID = null, - ?string $status = null, + Status|string|null $status = null, ?string $userPrompt = null, ): self { $obj = new self; @@ -75,7 +75,7 @@ public static function with( null !== $generatedSchema && $obj->generatedSchema = $generatedSchema; null !== $refinedPrompt && $obj->refinedPrompt = $refinedPrompt; null !== $requestID && $obj->requestID = $requestID; - null !== $status && $obj->status = $status; + null !== $status && $obj->status = $status instanceof Status ? $status->value : $status; null !== $userPrompt && $obj->userPrompt = $userPrompt; return $obj; @@ -120,12 +120,12 @@ public function withRequestID(string $requestID): self } /** - * @param Status::* $status + * @param Status|value-of $status */ - public function withStatus(string $status): self + public function withStatus(Status|string $status): self { $obj = clone $this; - $obj->status = $status; + $obj->status = $status instanceof Status ? $status->value : $status; return $obj; } diff --git a/src/GenerateSchema/GenerateSchemaNewResponse/Status.php b/src/GenerateSchema/GenerateSchemaNewResponse/Status.php index 46f448d..373b084 100644 --- a/src/GenerateSchema/GenerateSchemaNewResponse/Status.php +++ b/src/GenerateSchema/GenerateSchemaNewResponse/Status.php @@ -4,12 +4,7 @@ namespace Scrapegraphai\GenerateSchema\GenerateSchemaNewResponse; -use Scrapegraphai\Core\Concerns\SdkEnum; -use Scrapegraphai\Core\Conversion\Contracts\ConverterSource; - -final class Status implements ConverterSource +enum Status: string { - use SdkEnum; - - public const COMPLETED = 'completed'; + case COMPLETED = 'completed'; } diff --git a/src/Markdownify/CompletedMarkdownify.php b/src/Markdownify/CompletedMarkdownify.php index 752b512..e68eff4 100644 --- a/src/Markdownify/CompletedMarkdownify.php +++ b/src/Markdownify/CompletedMarkdownify.php @@ -14,7 +14,7 @@ * error?: string|null, * requestID?: string|null, * result?: string|null, - * status?: Status::*|null, + * status?: value-of|null, * websiteURL?: string|null, * } */ @@ -35,7 +35,7 @@ final class CompletedMarkdownify implements BaseModel #[Api(nullable: true, optional: true)] public ?string $result; - /** @var Status::*|null $status */ + /** @var value-of|null $status */ #[Api(enum: Status::class, optional: true)] public ?string $status; @@ -52,13 +52,13 @@ public function __construct() * * You must use named parameters to construct any parameters with a default value. * - * @param Status::* $status + * @param Status|value-of $status */ public static function with( ?string $error = null, ?string $requestID = null, ?string $result = null, - ?string $status = null, + Status|string|null $status = null, ?string $websiteURL = null, ): self { $obj = new self; @@ -66,7 +66,7 @@ public static function with( null !== $error && $obj->error = $error; null !== $requestID && $obj->requestID = $requestID; null !== $result && $obj->result = $result; - null !== $status && $obj->status = $status; + null !== $status && $obj->status = $status instanceof Status ? $status->value : $status; null !== $websiteURL && $obj->websiteURL = $websiteURL; return $obj; @@ -100,12 +100,12 @@ public function withResult(?string $result): self } /** - * @param Status::* $status + * @param Status|value-of $status */ - public function withStatus(string $status): self + public function withStatus(Status|string $status): self { $obj = clone $this; - $obj->status = $status; + $obj->status = $status instanceof Status ? $status->value : $status; return $obj; } diff --git a/src/Markdownify/CompletedMarkdownify/Status.php b/src/Markdownify/CompletedMarkdownify/Status.php index 8087933..1a370bd 100644 --- a/src/Markdownify/CompletedMarkdownify/Status.php +++ b/src/Markdownify/CompletedMarkdownify/Status.php @@ -4,16 +4,11 @@ namespace Scrapegraphai\Markdownify\CompletedMarkdownify; -use Scrapegraphai\Core\Concerns\SdkEnum; -use Scrapegraphai\Core\Conversion\Contracts\ConverterSource; - -final class Status implements ConverterSource +enum Status: string { - use SdkEnum; - - public const QUEUED = 'queued'; + case QUEUED = 'queued'; - public const PROCESSING = 'processing'; + case PROCESSING = 'processing'; - public const COMPLETED = 'completed'; + case COMPLETED = 'completed'; } diff --git a/src/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse.php b/src/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse.php index f15dc7b..3dadcb0 100644 --- a/src/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse.php +++ b/src/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse.php @@ -14,7 +14,7 @@ * error?: string|null, * requestID?: string|null, * result?: string|null, - * status?: Status::*|null, + * status?: value-of|null, * websiteURL?: string|null, * } */ @@ -32,7 +32,7 @@ final class FailedMarkdownifyResponse implements BaseModel #[Api(nullable: true, optional: true)] public ?string $result; - /** @var Status::*|null $status */ + /** @var value-of|null $status */ #[Api(enum: Status::class, optional: true)] public ?string $status; @@ -49,13 +49,13 @@ public function __construct() * * You must use named parameters to construct any parameters with a default value. * - * @param Status::* $status + * @param Status|value-of $status */ public static function with( ?string $error = null, ?string $requestID = null, ?string $result = null, - ?string $status = null, + Status|string|null $status = null, ?string $websiteURL = null, ): self { $obj = new self; @@ -63,7 +63,7 @@ public static function with( null !== $error && $obj->error = $error; null !== $requestID && $obj->requestID = $requestID; null !== $result && $obj->result = $result; - null !== $status && $obj->status = $status; + null !== $status && $obj->status = $status instanceof Status ? $status->value : $status; null !== $websiteURL && $obj->websiteURL = $websiteURL; return $obj; @@ -94,12 +94,12 @@ public function withResult(?string $result): self } /** - * @param Status::* $status + * @param Status|value-of $status */ - public function withStatus(string $status): self + public function withStatus(Status|string $status): self { $obj = clone $this; - $obj->status = $status; + $obj->status = $status instanceof Status ? $status->value : $status; return $obj; } diff --git a/src/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse/Status.php b/src/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse/Status.php index a8981ab..89511c8 100644 --- a/src/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse/Status.php +++ b/src/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse/Status.php @@ -4,12 +4,7 @@ namespace Scrapegraphai\Markdownify\MarkdownifyGetStatusResponse\FailedMarkdownifyResponse; -use Scrapegraphai\Core\Concerns\SdkEnum; -use Scrapegraphai\Core\Conversion\Contracts\ConverterSource; - -final class Status implements ConverterSource +enum Status: string { - use SdkEnum; - - public const FAILED = 'failed'; + case FAILED = 'failed'; } diff --git a/src/Searchscraper/CompletedSearchScraper.php b/src/Searchscraper/CompletedSearchScraper.php index 8466de0..1ccc93b 100644 --- a/src/Searchscraper/CompletedSearchScraper.php +++ b/src/Searchscraper/CompletedSearchScraper.php @@ -16,7 +16,7 @@ * referenceURLs?: list|null, * requestID?: string|null, * result?: mixed, - * status?: Status::*|null, + * status?: value-of|null, * userPrompt?: string|null, * } */ @@ -48,7 +48,7 @@ final class CompletedSearchScraper implements BaseModel #[Api(optional: true)] public mixed $result; - /** @var Status::*|null $status */ + /** @var value-of|null $status */ #[Api(enum: Status::class, optional: true)] public ?string $status; @@ -66,7 +66,7 @@ public function __construct() * You must use named parameters to construct any parameters with a default value. * * @param list $referenceURLs - * @param Status::* $status + * @param Status|value-of $status */ public static function with( ?string $error = null, @@ -74,7 +74,7 @@ public static function with( ?array $referenceURLs = null, ?string $requestID = null, mixed $result = null, - ?string $status = null, + Status|string|null $status = null, ?string $userPrompt = null, ): self { $obj = new self; @@ -84,7 +84,7 @@ public static function with( null !== $referenceURLs && $obj->referenceURLs = $referenceURLs; null !== $requestID && $obj->requestID = $requestID; null !== $result && $obj->result = $result; - null !== $status && $obj->status = $status; + null !== $status && $obj->status = $status instanceof Status ? $status->value : $status; null !== $userPrompt && $obj->userPrompt = $userPrompt; return $obj; @@ -139,12 +139,12 @@ public function withResult(mixed $result): self } /** - * @param Status::* $status + * @param Status|value-of $status */ - public function withStatus(string $status): self + public function withStatus(Status|string $status): self { $obj = clone $this; - $obj->status = $status; + $obj->status = $status instanceof Status ? $status->value : $status; return $obj; } diff --git a/src/Searchscraper/CompletedSearchScraper/Status.php b/src/Searchscraper/CompletedSearchScraper/Status.php index 68e49ff..8b63933 100644 --- a/src/Searchscraper/CompletedSearchScraper/Status.php +++ b/src/Searchscraper/CompletedSearchScraper/Status.php @@ -4,16 +4,11 @@ namespace Scrapegraphai\Searchscraper\CompletedSearchScraper; -use Scrapegraphai\Core\Concerns\SdkEnum; -use Scrapegraphai\Core\Conversion\Contracts\ConverterSource; - -final class Status implements ConverterSource +enum Status: string { - use SdkEnum; - - public const QUEUED = 'queued'; + case QUEUED = 'queued'; - public const PROCESSING = 'processing'; + case PROCESSING = 'processing'; - public const COMPLETED = 'completed'; + case COMPLETED = 'completed'; } diff --git a/src/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse.php b/src/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse.php index 545f3a3..98e011a 100644 --- a/src/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse.php +++ b/src/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse.php @@ -16,7 +16,7 @@ * referenceURLs?: list|null, * requestID?: string|null, * result?: mixed, - * status?: Status::*|null, + * status?: value-of|null, * userPrompt?: string|null, * } */ @@ -41,7 +41,7 @@ final class FailedSearchScraperResponse implements BaseModel #[Api(nullable: true, optional: true)] public mixed $result; - /** @var Status::*|null $status */ + /** @var value-of|null $status */ #[Api(enum: Status::class, optional: true)] public ?string $status; @@ -59,7 +59,7 @@ public function __construct() * You must use named parameters to construct any parameters with a default value. * * @param list $referenceURLs - * @param Status::* $status + * @param Status|value-of $status */ public static function with( ?string $error = null, @@ -67,7 +67,7 @@ public static function with( ?array $referenceURLs = null, ?string $requestID = null, mixed $result = null, - ?string $status = null, + Status|string|null $status = null, ?string $userPrompt = null, ): self { $obj = new self; @@ -77,7 +77,7 @@ public static function with( null !== $referenceURLs && $obj->referenceURLs = $referenceURLs; null !== $requestID && $obj->requestID = $requestID; null !== $result && $obj->result = $result; - null !== $status && $obj->status = $status; + null !== $status && $obj->status = $status instanceof Status ? $status->value : $status; null !== $userPrompt && $obj->userPrompt = $userPrompt; return $obj; @@ -127,12 +127,12 @@ public function withResult(mixed $result): self } /** - * @param Status::* $status + * @param Status|value-of $status */ - public function withStatus(string $status): self + public function withStatus(Status|string $status): self { $obj = clone $this; - $obj->status = $status; + $obj->status = $status instanceof Status ? $status->value : $status; return $obj; } diff --git a/src/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse/Status.php b/src/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse/Status.php index c1970d9..62743e8 100644 --- a/src/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse/Status.php +++ b/src/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse/Status.php @@ -4,12 +4,7 @@ namespace Scrapegraphai\Searchscraper\SearchscraperGetStatusResponse\FailedSearchScraperResponse; -use Scrapegraphai\Core\Concerns\SdkEnum; -use Scrapegraphai\Core\Conversion\Contracts\ConverterSource; - -final class Status implements ConverterSource +enum Status: string { - use SdkEnum; - - public const FAILED = 'failed'; + case FAILED = 'failed'; } diff --git a/src/Smartscraper/CompletedSmartscraper.php b/src/Smartscraper/CompletedSmartscraper.php index b016b74..48a8623 100644 --- a/src/Smartscraper/CompletedSmartscraper.php +++ b/src/Smartscraper/CompletedSmartscraper.php @@ -14,7 +14,7 @@ * error?: string|null, * requestID?: string|null, * result?: mixed, - * status?: Status::*|null, + * status?: value-of|null, * userPrompt?: string|null, * websiteURL?: string|null, * } @@ -45,7 +45,7 @@ final class CompletedSmartscraper implements BaseModel /** * Processing status. * - * @var Status::*|null $status + * @var value-of|null $status */ #[Api(enum: Status::class, optional: true)] public ?string $status; @@ -66,13 +66,13 @@ public function __construct() * * You must use named parameters to construct any parameters with a default value. * - * @param Status::* $status + * @param Status|value-of $status */ public static function with( ?string $error = null, ?string $requestID = null, mixed $result = null, - ?string $status = null, + Status|string|null $status = null, ?string $userPrompt = null, ?string $websiteURL = null, ): self { @@ -81,7 +81,7 @@ public static function with( null !== $error && $obj->error = $error; null !== $requestID && $obj->requestID = $requestID; null !== $result && $obj->result = $result; - null !== $status && $obj->status = $status; + null !== $status && $obj->status = $status instanceof Status ? $status->value : $status; null !== $userPrompt && $obj->userPrompt = $userPrompt; null !== $websiteURL && $obj->websiteURL = $websiteURL; @@ -124,12 +124,12 @@ public function withResult(mixed $result): self /** * Processing status. * - * @param Status::* $status + * @param Status|value-of $status */ - public function withStatus(string $status): self + public function withStatus(Status|string $status): self { $obj = clone $this; - $obj->status = $status; + $obj->status = $status instanceof Status ? $status->value : $status; return $obj; } diff --git a/src/Smartscraper/CompletedSmartscraper/Status.php b/src/Smartscraper/CompletedSmartscraper/Status.php index 1b66e4b..f9331d4 100644 --- a/src/Smartscraper/CompletedSmartscraper/Status.php +++ b/src/Smartscraper/CompletedSmartscraper/Status.php @@ -4,19 +4,14 @@ namespace Scrapegraphai\Smartscraper\CompletedSmartscraper; -use Scrapegraphai\Core\Concerns\SdkEnum; -use Scrapegraphai\Core\Conversion\Contracts\ConverterSource; - /** * Processing status. */ -final class Status implements ConverterSource +enum Status: string { - use SdkEnum; - - public const QUEUED = 'queued'; + case QUEUED = 'queued'; - public const PROCESSING = 'processing'; + case PROCESSING = 'processing'; - public const COMPLETED = 'completed'; + case COMPLETED = 'completed'; } diff --git a/src/Smartscraper/FailedSmartscraper.php b/src/Smartscraper/FailedSmartscraper.php index 9a7d219..3eb457e 100644 --- a/src/Smartscraper/FailedSmartscraper.php +++ b/src/Smartscraper/FailedSmartscraper.php @@ -14,7 +14,7 @@ * error?: string|null, * requestID?: string|null, * result?: mixed, - * status?: Status::*|null, + * status?: value-of|null, * userPrompt?: string|null, * websiteURL?: string|null, * } @@ -36,7 +36,7 @@ final class FailedSmartscraper implements BaseModel #[Api(nullable: true, optional: true)] public mixed $result; - /** @var Status::*|null $status */ + /** @var value-of|null $status */ #[Api(enum: Status::class, optional: true)] public ?string $status; @@ -56,13 +56,13 @@ public function __construct() * * You must use named parameters to construct any parameters with a default value. * - * @param Status::* $status + * @param Status|value-of $status */ public static function with( ?string $error = null, ?string $requestID = null, mixed $result = null, - ?string $status = null, + Status|string|null $status = null, ?string $userPrompt = null, ?string $websiteURL = null, ): self { @@ -71,7 +71,7 @@ public static function with( null !== $error && $obj->error = $error; null !== $requestID && $obj->requestID = $requestID; null !== $result && $obj->result = $result; - null !== $status && $obj->status = $status; + null !== $status && $obj->status = $status instanceof Status ? $status->value : $status; null !== $userPrompt && $obj->userPrompt = $userPrompt; null !== $websiteURL && $obj->websiteURL = $websiteURL; @@ -106,12 +106,12 @@ public function withResult(mixed $result): self } /** - * @param Status::* $status + * @param Status|value-of $status */ - public function withStatus(string $status): self + public function withStatus(Status|string $status): self { $obj = clone $this; - $obj->status = $status; + $obj->status = $status instanceof Status ? $status->value : $status; return $obj; } diff --git a/src/Smartscraper/FailedSmartscraper/Status.php b/src/Smartscraper/FailedSmartscraper/Status.php index ef18e98..e8c349e 100644 --- a/src/Smartscraper/FailedSmartscraper/Status.php +++ b/src/Smartscraper/FailedSmartscraper/Status.php @@ -4,12 +4,7 @@ namespace Scrapegraphai\Smartscraper\FailedSmartscraper; -use Scrapegraphai\Core\Concerns\SdkEnum; -use Scrapegraphai\Core\Conversion\Contracts\ConverterSource; - -final class Status implements ConverterSource +enum Status: string { - use SdkEnum; - - public const FAILED = 'failed'; + case FAILED = 'failed'; } From 5e8ce6f7137f9ce2a2bf3fce709cd363f4b8c196 Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Thu, 11 Sep 2025 02:31:05 +0000 Subject: [PATCH 37/44] chore: fix lints in UnionOf --- src/Core/Conversion/UnionOf.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Core/Conversion/UnionOf.php b/src/Core/Conversion/UnionOf.php index e350d4e..d5dfb9b 100644 --- a/src/Core/Conversion/UnionOf.php +++ b/src/Core/Conversion/UnionOf.php @@ -65,7 +65,7 @@ public function coerce(mixed $value, CoerceState $state): mixed public function dump(mixed $value, DumpState $state): mixed { - if (!is_null($target = $this->resolveVariant(value: $value))) { + if (null !== ($target = $this->resolveVariant(value: $value))) { return Conversion::dump($target, value: $value, state: $state); } @@ -85,8 +85,15 @@ private function resolveVariant( return $value::class; } - if (!is_null($this->discriminator) && is_array($value) && array_key_exists($this->discriminator, array: $value)) { + if ( + null !== $this->discriminator + && is_array($value) + && array_key_exists($this->discriminator, array: $value) + ) { $discriminator = $value[$this->discriminator]; + if (!is_string($discriminator)) { + return null; + } return $this->variants[$discriminator] ?? null; } From 7ea49045827081bda452412b5ef967924b61cd76 Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Sat, 13 Sep 2025 02:27:55 +0000 Subject: [PATCH 38/44] feat(client): support raw responses --- src/Core/Implementation/HasRawResponse.php | 10 ++++++++++ src/Crawl/CrawlGetResultsResponse.php | 10 +++++++--- src/Crawl/CrawlStartParams/Rules.php | 4 +--- src/Crawl/CrawlStartResponse.php | 6 +++++- src/Credits/CreditGetResponse.php | 6 +++++- src/Feedback/FeedbackSubmitResponse.php | 12 ++++++++---- .../CompletedSchemaGenerationResponse.php | 8 ++++---- .../FailedSchemaGenerationResponse.php | 8 ++++---- src/GenerateSchema/GenerateSchemaNewResponse.php | 12 ++++++++---- src/Healthz/HealthzCheckResponse.php | 6 +++++- src/Markdownify/CompletedMarkdownify.php | 12 ++++++++---- .../FailedMarkdownifyResponse.php | 8 ++++---- src/Searchscraper/CompletedSearchScraper.php | 14 +++++++++----- .../FailedSearchScraperResponse.php | 12 ++++++------ src/ServiceContracts/CrawlContract.php | 5 +++++ src/ServiceContracts/CreditsContract.php | 3 +++ src/ServiceContracts/FeedbackContract.php | 3 +++ src/ServiceContracts/GenerateSchemaContract.php | 3 +++ src/ServiceContracts/HealthzContract.php | 3 +++ src/ServiceContracts/MarkdownifyContract.php | 3 +++ src/ServiceContracts/SearchscraperContract.php | 3 +++ src/ServiceContracts/SmartscraperContract.php | 3 +++ src/ServiceContracts/ValidateContract.php | 3 +++ src/Services/CrawlService.php | 5 +++++ src/Services/CreditsService.php | 3 +++ src/Services/FeedbackService.php | 3 +++ src/Services/GenerateSchemaService.php | 3 +++ src/Services/HealthzService.php | 3 +++ src/Services/MarkdownifyService.php | 3 +++ src/Services/SearchscraperService.php | 3 +++ src/Services/SmartscraperService.php | 3 +++ src/Services/ValidateService.php | 3 +++ src/Smartscraper/CompletedSmartscraper.php | 12 ++++++++---- src/Smartscraper/FailedSmartscraper.php | 8 ++++---- src/Validate/ValidateAPIKeyResponse.php | 6 +++++- 35 files changed, 159 insertions(+), 53 deletions(-) create mode 100644 src/Core/Implementation/HasRawResponse.php diff --git a/src/Core/Implementation/HasRawResponse.php b/src/Core/Implementation/HasRawResponse.php new file mode 100644 index 0000000..6e9469c --- /dev/null +++ b/src/Core/Implementation/HasRawResponse.php @@ -0,0 +1,10 @@ +|null, - * taskID?: string|null, + * result?: mixed|string, + * status?: value-of, + * taskID?: string, * traceback?: string|null, * } + * When used in a response, this type parameter can be used to define a $rawResponse property. + * @template TRawResponse of object = object{} + * + * @mixin TRawResponse */ final class CrawlGetResultsResponse implements BaseModel { diff --git a/src/Crawl/CrawlStartParams/Rules.php b/src/Crawl/CrawlStartParams/Rules.php index 17c3a18..5d745c6 100644 --- a/src/Crawl/CrawlStartParams/Rules.php +++ b/src/Crawl/CrawlStartParams/Rules.php @@ -9,9 +9,7 @@ use Scrapegraphai\Core\Contracts\BaseModel; /** - * @phpstan-type rules_alias = array{ - * exclude?: list|null, sameDomain?: bool|null - * } + * @phpstan-type rules_alias = array{exclude?: list, sameDomain?: bool} */ final class Rules implements BaseModel { diff --git a/src/Crawl/CrawlStartResponse.php b/src/Crawl/CrawlStartResponse.php index 3104d31..4aa93b6 100644 --- a/src/Crawl/CrawlStartResponse.php +++ b/src/Crawl/CrawlStartResponse.php @@ -9,7 +9,11 @@ use Scrapegraphai\Core\Contracts\BaseModel; /** - * @phpstan-type crawl_start_response = array{taskID?: string|null} + * @phpstan-type crawl_start_response = array{taskID?: string} + * When used in a response, this type parameter can be used to define a $rawResponse property. + * @template TRawResponse of object = object{} + * + * @mixin TRawResponse */ final class CrawlStartResponse implements BaseModel { diff --git a/src/Credits/CreditGetResponse.php b/src/Credits/CreditGetResponse.php index c6a9c55..7d3cc84 100644 --- a/src/Credits/CreditGetResponse.php +++ b/src/Credits/CreditGetResponse.php @@ -10,8 +10,12 @@ /** * @phpstan-type credit_get_response = array{ - * remainingCredits?: int|null, totalCreditsUsed?: int|null + * remainingCredits?: int, totalCreditsUsed?: int * } + * When used in a response, this type parameter can be used to define a $rawResponse property. + * @template TRawResponse of object = object{} + * + * @mixin TRawResponse */ final class CreditGetResponse implements BaseModel { diff --git a/src/Feedback/FeedbackSubmitResponse.php b/src/Feedback/FeedbackSubmitResponse.php index 761da69..e06b283 100644 --- a/src/Feedback/FeedbackSubmitResponse.php +++ b/src/Feedback/FeedbackSubmitResponse.php @@ -10,11 +10,15 @@ /** * @phpstan-type feedback_submit_response = array{ - * feedbackID?: string|null, - * feedbackTimestamp?: \DateTimeInterface|null, - * message?: string|null, - * requestID?: string|null, + * feedbackID?: string, + * feedbackTimestamp?: \DateTimeInterface, + * message?: string, + * requestID?: string, * } + * When used in a response, this type parameter can be used to define a $rawResponse property. + * @template TRawResponse of object = object{} + * + * @mixin TRawResponse */ final class FeedbackSubmitResponse implements BaseModel { diff --git a/src/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse.php b/src/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse.php index 6cfa20a..31971a5 100644 --- a/src/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse.php +++ b/src/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse.php @@ -13,10 +13,10 @@ * @phpstan-type completed_schema_generation_response = array{ * error?: string|null, * generatedSchema?: mixed, - * refinedPrompt?: string|null, - * requestID?: string|null, - * status?: value-of|null, - * userPrompt?: string|null, + * refinedPrompt?: string, + * requestID?: string, + * status?: value-of, + * userPrompt?: string, * } */ final class CompletedSchemaGenerationResponse implements BaseModel diff --git a/src/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse.php b/src/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse.php index 83a661b..8d374b3 100644 --- a/src/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse.php +++ b/src/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse.php @@ -11,12 +11,12 @@ /** * @phpstan-type failed_schema_generation_response = array{ - * error?: string|null, + * error?: string, * generatedSchema?: mixed, * refinedPrompt?: string|null, - * requestID?: string|null, - * status?: value-of|null, - * userPrompt?: string|null, + * requestID?: string, + * status?: value-of, + * userPrompt?: string, * } */ final class FailedSchemaGenerationResponse implements BaseModel diff --git a/src/GenerateSchema/GenerateSchemaNewResponse.php b/src/GenerateSchema/GenerateSchemaNewResponse.php index 58e94d8..c2b5fca 100644 --- a/src/GenerateSchema/GenerateSchemaNewResponse.php +++ b/src/GenerateSchema/GenerateSchemaNewResponse.php @@ -13,11 +13,15 @@ * @phpstan-type generate_schema_new_response = array{ * error?: string|null, * generatedSchema?: mixed, - * refinedPrompt?: string|null, - * requestID?: string|null, - * status?: value-of|null, - * userPrompt?: string|null, + * refinedPrompt?: string, + * requestID?: string, + * status?: value-of, + * userPrompt?: string, * } + * When used in a response, this type parameter can be used to define a $rawResponse property. + * @template TRawResponse of object = object{} + * + * @mixin TRawResponse */ final class GenerateSchemaNewResponse implements BaseModel { diff --git a/src/Healthz/HealthzCheckResponse.php b/src/Healthz/HealthzCheckResponse.php index 2160dcf..4beb823 100644 --- a/src/Healthz/HealthzCheckResponse.php +++ b/src/Healthz/HealthzCheckResponse.php @@ -10,8 +10,12 @@ /** * @phpstan-type healthz_check_response = array{ - * services?: array|null, status?: string|null + * services?: array, status?: string * } + * When used in a response, this type parameter can be used to define a $rawResponse property. + * @template TRawResponse of object = object{} + * + * @mixin TRawResponse */ final class HealthzCheckResponse implements BaseModel { diff --git a/src/Markdownify/CompletedMarkdownify.php b/src/Markdownify/CompletedMarkdownify.php index e68eff4..c631af5 100644 --- a/src/Markdownify/CompletedMarkdownify.php +++ b/src/Markdownify/CompletedMarkdownify.php @@ -11,12 +11,16 @@ /** * @phpstan-type completed_markdownify = array{ - * error?: string|null, - * requestID?: string|null, + * error?: string, + * requestID?: string, * result?: string|null, - * status?: value-of|null, - * websiteURL?: string|null, + * status?: value-of, + * websiteURL?: string, * } + * When used in a response, this type parameter can be used to define a $rawResponse property. + * @template TRawResponse of object = object{} + * + * @mixin TRawResponse */ final class CompletedMarkdownify implements BaseModel { diff --git a/src/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse.php b/src/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse.php index 3dadcb0..f27c635 100644 --- a/src/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse.php +++ b/src/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse.php @@ -11,11 +11,11 @@ /** * @phpstan-type failed_markdownify_response = array{ - * error?: string|null, - * requestID?: string|null, + * error?: string, + * requestID?: string, * result?: string|null, - * status?: value-of|null, - * websiteURL?: string|null, + * status?: value-of, + * websiteURL?: string, * } */ final class FailedMarkdownifyResponse implements BaseModel diff --git a/src/Searchscraper/CompletedSearchScraper.php b/src/Searchscraper/CompletedSearchScraper.php index 1ccc93b..b9f1307 100644 --- a/src/Searchscraper/CompletedSearchScraper.php +++ b/src/Searchscraper/CompletedSearchScraper.php @@ -12,13 +12,17 @@ /** * @phpstan-type completed_search_scraper = array{ * error?: string|null, - * numResults?: int|null, - * referenceURLs?: list|null, - * requestID?: string|null, + * numResults?: int, + * referenceURLs?: list, + * requestID?: string, * result?: mixed, - * status?: value-of|null, - * userPrompt?: string|null, + * status?: value-of, + * userPrompt?: string, * } + * When used in a response, this type parameter can be used to define a $rawResponse property. + * @template TRawResponse of object = object{} + * + * @mixin TRawResponse */ final class CompletedSearchScraper implements BaseModel { diff --git a/src/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse.php b/src/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse.php index 98e011a..1b16175 100644 --- a/src/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse.php +++ b/src/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse.php @@ -11,13 +11,13 @@ /** * @phpstan-type failed_search_scraper_response = array{ - * error?: string|null, - * numResults?: int|null, - * referenceURLs?: list|null, - * requestID?: string|null, + * error?: string, + * numResults?: int, + * referenceURLs?: list, + * requestID?: string, * result?: mixed, - * status?: value-of|null, - * userPrompt?: string|null, + * status?: value-of, + * userPrompt?: string, * } */ final class FailedSearchScraperResponse implements BaseModel diff --git a/src/ServiceContracts/CrawlContract.php b/src/ServiceContracts/CrawlContract.php index 8f96ff7..dbb769c 100644 --- a/src/ServiceContracts/CrawlContract.php +++ b/src/ServiceContracts/CrawlContract.php @@ -4,6 +4,7 @@ namespace Scrapegraphai\ServiceContracts; +use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\Crawl\CrawlGetResultsResponse; use Scrapegraphai\Crawl\CrawlStartParams\Rules; use Scrapegraphai\Crawl\CrawlStartResponse; @@ -15,6 +16,8 @@ interface CrawlContract { /** * @api + * + * @return CrawlGetResultsResponse */ public function retrieveResults( string $taskID, @@ -33,6 +36,8 @@ public function retrieveResults( * @param Rules $rules * @param mixed $schema Output schema for extraction * @param bool $sitemap Use sitemap for crawling + * + * @return CrawlStartResponse */ public function start( $url, diff --git a/src/ServiceContracts/CreditsContract.php b/src/ServiceContracts/CreditsContract.php index 66a5438..9114be8 100644 --- a/src/ServiceContracts/CreditsContract.php +++ b/src/ServiceContracts/CreditsContract.php @@ -4,6 +4,7 @@ namespace Scrapegraphai\ServiceContracts; +use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\Credits\CreditGetResponse; use Scrapegraphai\RequestOptions; @@ -11,6 +12,8 @@ interface CreditsContract { /** * @api + * + * @return CreditGetResponse */ public function retrieve( ?RequestOptions $requestOptions = null diff --git a/src/ServiceContracts/FeedbackContract.php b/src/ServiceContracts/FeedbackContract.php index e2a0c43..4e6ed0d 100644 --- a/src/ServiceContracts/FeedbackContract.php +++ b/src/ServiceContracts/FeedbackContract.php @@ -4,6 +4,7 @@ namespace Scrapegraphai\ServiceContracts; +use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\Feedback\FeedbackSubmitResponse; use Scrapegraphai\RequestOptions; @@ -17,6 +18,8 @@ interface FeedbackContract * @param int $rating Rating score * @param string $requestID Request to provide feedback for * @param string|null $feedbackText Optional feedback comments + * + * @return FeedbackSubmitResponse */ public function submit( $rating, diff --git a/src/ServiceContracts/GenerateSchemaContract.php b/src/ServiceContracts/GenerateSchemaContract.php index 41ac390..360f827 100644 --- a/src/ServiceContracts/GenerateSchemaContract.php +++ b/src/ServiceContracts/GenerateSchemaContract.php @@ -4,6 +4,7 @@ namespace Scrapegraphai\ServiceContracts; +use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\GenerateSchema\GenerateSchemaGetResponse\CompletedSchemaGenerationResponse; use Scrapegraphai\GenerateSchema\GenerateSchemaGetResponse\FailedSchemaGenerationResponse; use Scrapegraphai\GenerateSchema\GenerateSchemaNewResponse; @@ -18,6 +19,8 @@ interface GenerateSchemaContract * * @param string $userPrompt Natural language description of desired schema * @param mixed $existingSchema Existing schema to modify or extend + * + * @return GenerateSchemaNewResponse */ public function create( $userPrompt, diff --git a/src/ServiceContracts/HealthzContract.php b/src/ServiceContracts/HealthzContract.php index 2a8bdd5..5e93387 100644 --- a/src/ServiceContracts/HealthzContract.php +++ b/src/ServiceContracts/HealthzContract.php @@ -4,6 +4,7 @@ namespace Scrapegraphai\ServiceContracts; +use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\Healthz\HealthzCheckResponse; use Scrapegraphai\RequestOptions; @@ -11,6 +12,8 @@ interface HealthzContract { /** * @api + * + * @return HealthzCheckResponse */ public function check( ?RequestOptions $requestOptions = null diff --git a/src/ServiceContracts/MarkdownifyContract.php b/src/ServiceContracts/MarkdownifyContract.php index 2496cab..8906d01 100644 --- a/src/ServiceContracts/MarkdownifyContract.php +++ b/src/ServiceContracts/MarkdownifyContract.php @@ -4,6 +4,7 @@ namespace Scrapegraphai\ServiceContracts; +use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\Markdownify\CompletedMarkdownify; use Scrapegraphai\Markdownify\MarkdownifyGetStatusResponse\FailedMarkdownifyResponse; use Scrapegraphai\RequestOptions; @@ -18,6 +19,8 @@ interface MarkdownifyContract * @param string $websiteURL URL to convert to markdown * @param array $headers * @param list $steps Interaction steps before conversion + * + * @return CompletedMarkdownify */ public function convert( $websiteURL, diff --git a/src/ServiceContracts/SearchscraperContract.php b/src/ServiceContracts/SearchscraperContract.php index 3bcf944..abf0d23 100644 --- a/src/ServiceContracts/SearchscraperContract.php +++ b/src/ServiceContracts/SearchscraperContract.php @@ -4,6 +4,7 @@ namespace Scrapegraphai\ServiceContracts; +use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\RequestOptions; use Scrapegraphai\Searchscraper\CompletedSearchScraper; use Scrapegraphai\Searchscraper\SearchscraperGetStatusResponse\FailedSearchScraperResponse; @@ -19,6 +20,8 @@ interface SearchscraperContract * @param array $headers * @param int $numResults Number of websites to scrape from search results * @param mixed $outputSchema JSON schema for structured output + * + * @return CompletedSearchScraper */ public function create( $userPrompt, diff --git a/src/ServiceContracts/SmartscraperContract.php b/src/ServiceContracts/SmartscraperContract.php index 8d87a4f..a8afaed 100644 --- a/src/ServiceContracts/SmartscraperContract.php +++ b/src/ServiceContracts/SmartscraperContract.php @@ -4,6 +4,7 @@ namespace Scrapegraphai\ServiceContracts; +use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\RequestOptions; use Scrapegraphai\Smartscraper\CompletedSmartscraper; use Scrapegraphai\Smartscraper\FailedSmartscraper; @@ -25,6 +26,8 @@ interface SmartscraperContract * @param int $totalPages Number of pages to process for pagination * @param string $websiteHTML HTML content to process (max 2MB, mutually exclusive with website_url) * @param string $websiteURL URL to scrape (mutually exclusive with website_html) + * + * @return CompletedSmartscraper */ public function create( $userPrompt, diff --git a/src/ServiceContracts/ValidateContract.php b/src/ServiceContracts/ValidateContract.php index c241d83..72f4b42 100644 --- a/src/ServiceContracts/ValidateContract.php +++ b/src/ServiceContracts/ValidateContract.php @@ -4,6 +4,7 @@ namespace Scrapegraphai\ServiceContracts; +use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\RequestOptions; use Scrapegraphai\Validate\ValidateAPIKeyResponse; @@ -11,6 +12,8 @@ interface ValidateContract { /** * @api + * + * @return ValidateAPIKeyResponse */ public function apiKey( ?RequestOptions $requestOptions = null diff --git a/src/Services/CrawlService.php b/src/Services/CrawlService.php index cdece3e..030dc35 100644 --- a/src/Services/CrawlService.php +++ b/src/Services/CrawlService.php @@ -5,6 +5,7 @@ namespace Scrapegraphai\Services; use Scrapegraphai\Client; +use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\Crawl\CrawlGetResultsResponse; use Scrapegraphai\Crawl\CrawlStartParams; use Scrapegraphai\Crawl\CrawlStartParams\Rules; @@ -25,6 +26,8 @@ public function __construct(private Client $client) {} * @api * * Retrieve the status and results of a crawling job + * + * @return CrawlGetResultsResponse */ public function retrieveResults( string $taskID, @@ -55,6 +58,8 @@ public function retrieveResults( * @param Rules $rules * @param mixed $schema Output schema for extraction * @param bool $sitemap Use sitemap for crawling + * + * @return CrawlStartResponse */ public function start( $url, diff --git a/src/Services/CreditsService.php b/src/Services/CreditsService.php index e601334..4afd763 100644 --- a/src/Services/CreditsService.php +++ b/src/Services/CreditsService.php @@ -5,6 +5,7 @@ namespace Scrapegraphai\Services; use Scrapegraphai\Client; +use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\Credits\CreditGetResponse; use Scrapegraphai\RequestOptions; use Scrapegraphai\ServiceContracts\CreditsContract; @@ -20,6 +21,8 @@ public function __construct(private Client $client) {} * @api * * Retrieve the current credit balance and usage for the authenticated user + * + * @return CreditGetResponse */ public function retrieve( ?RequestOptions $requestOptions = null diff --git a/src/Services/FeedbackService.php b/src/Services/FeedbackService.php index 60f0a69..8c75e06 100644 --- a/src/Services/FeedbackService.php +++ b/src/Services/FeedbackService.php @@ -5,6 +5,7 @@ namespace Scrapegraphai\Services; use Scrapegraphai\Client; +use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\Feedback\FeedbackSubmitParams; use Scrapegraphai\Feedback\FeedbackSubmitResponse; use Scrapegraphai\RequestOptions; @@ -27,6 +28,8 @@ public function __construct(private Client $client) {} * @param int $rating Rating score * @param string $requestID Request to provide feedback for * @param string|null $feedbackText Optional feedback comments + * + * @return FeedbackSubmitResponse */ public function submit( $rating, diff --git a/src/Services/GenerateSchemaService.php b/src/Services/GenerateSchemaService.php index 191698b..2a280f1 100644 --- a/src/Services/GenerateSchemaService.php +++ b/src/Services/GenerateSchemaService.php @@ -5,6 +5,7 @@ namespace Scrapegraphai\Services; use Scrapegraphai\Client; +use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\GenerateSchema\GenerateSchemaCreateParams; use Scrapegraphai\GenerateSchema\GenerateSchemaGetResponse; use Scrapegraphai\GenerateSchema\GenerateSchemaGetResponse\CompletedSchemaGenerationResponse; @@ -30,6 +31,8 @@ public function __construct(private Client $client) {} * * @param string $userPrompt Natural language description of desired schema * @param mixed $existingSchema Existing schema to modify or extend + * + * @return GenerateSchemaNewResponse */ public function create( $userPrompt, diff --git a/src/Services/HealthzService.php b/src/Services/HealthzService.php index 818ab60..0195305 100644 --- a/src/Services/HealthzService.php +++ b/src/Services/HealthzService.php @@ -5,6 +5,7 @@ namespace Scrapegraphai\Services; use Scrapegraphai\Client; +use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\Healthz\HealthzCheckResponse; use Scrapegraphai\RequestOptions; use Scrapegraphai\ServiceContracts\HealthzContract; @@ -20,6 +21,8 @@ public function __construct(private Client $client) {} * @api * * Check the health status of the service + * + * @return HealthzCheckResponse */ public function check( ?RequestOptions $requestOptions = null diff --git a/src/Services/MarkdownifyService.php b/src/Services/MarkdownifyService.php index 633309e..2746911 100644 --- a/src/Services/MarkdownifyService.php +++ b/src/Services/MarkdownifyService.php @@ -5,6 +5,7 @@ namespace Scrapegraphai\Services; use Scrapegraphai\Client; +use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\Markdownify\CompletedMarkdownify; use Scrapegraphai\Markdownify\MarkdownifyConvertParams; use Scrapegraphai\Markdownify\MarkdownifyGetStatusResponse; @@ -29,6 +30,8 @@ public function __construct(private Client $client) {} * @param string $websiteURL URL to convert to markdown * @param array $headers * @param list $steps Interaction steps before conversion + * + * @return CompletedMarkdownify */ public function convert( $websiteURL, diff --git a/src/Services/SearchscraperService.php b/src/Services/SearchscraperService.php index c0009f9..7a55572 100644 --- a/src/Services/SearchscraperService.php +++ b/src/Services/SearchscraperService.php @@ -5,6 +5,7 @@ namespace Scrapegraphai\Services; use Scrapegraphai\Client; +use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\RequestOptions; use Scrapegraphai\Searchscraper\CompletedSearchScraper; use Scrapegraphai\Searchscraper\SearchscraperCreateParams; @@ -31,6 +32,8 @@ public function __construct(private Client $client) {} * @param array $headers * @param int $numResults Number of websites to scrape from search results * @param mixed $outputSchema JSON schema for structured output + * + * @return CompletedSearchScraper */ public function create( $userPrompt, diff --git a/src/Services/SmartscraperService.php b/src/Services/SmartscraperService.php index f03793e..9ee09e7 100644 --- a/src/Services/SmartscraperService.php +++ b/src/Services/SmartscraperService.php @@ -5,6 +5,7 @@ namespace Scrapegraphai\Services; use Scrapegraphai\Client; +use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\RequestOptions; use Scrapegraphai\ServiceContracts\SmartscraperContract; use Scrapegraphai\Smartscraper\CompletedSmartscraper; @@ -38,6 +39,8 @@ public function __construct(private Client $client) {} * @param int $totalPages Number of pages to process for pagination * @param string $websiteHTML HTML content to process (max 2MB, mutually exclusive with website_url) * @param string $websiteURL URL to scrape (mutually exclusive with website_html) + * + * @return CompletedSmartscraper */ public function create( $userPrompt, diff --git a/src/Services/ValidateService.php b/src/Services/ValidateService.php index 68a834c..e876e8a 100644 --- a/src/Services/ValidateService.php +++ b/src/Services/ValidateService.php @@ -5,6 +5,7 @@ namespace Scrapegraphai\Services; use Scrapegraphai\Client; +use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\RequestOptions; use Scrapegraphai\ServiceContracts\ValidateContract; use Scrapegraphai\Validate\ValidateAPIKeyResponse; @@ -20,6 +21,8 @@ public function __construct(private Client $client) {} * @api * * Validate the API key and retrieve associated user email + * + * @return ValidateAPIKeyResponse */ public function apiKey( ?RequestOptions $requestOptions = null diff --git a/src/Smartscraper/CompletedSmartscraper.php b/src/Smartscraper/CompletedSmartscraper.php index 48a8623..15ce69d 100644 --- a/src/Smartscraper/CompletedSmartscraper.php +++ b/src/Smartscraper/CompletedSmartscraper.php @@ -11,13 +11,17 @@ /** * @phpstan-type completed_smartscraper = array{ - * error?: string|null, - * requestID?: string|null, + * error?: string, + * requestID?: string, * result?: mixed, - * status?: value-of|null, - * userPrompt?: string|null, + * status?: value-of, + * userPrompt?: string, * websiteURL?: string|null, * } + * When used in a response, this type parameter can be used to define a $rawResponse property. + * @template TRawResponse of object = object{} + * + * @mixin TRawResponse */ final class CompletedSmartscraper implements BaseModel { diff --git a/src/Smartscraper/FailedSmartscraper.php b/src/Smartscraper/FailedSmartscraper.php index 3eb457e..0198cd7 100644 --- a/src/Smartscraper/FailedSmartscraper.php +++ b/src/Smartscraper/FailedSmartscraper.php @@ -11,11 +11,11 @@ /** * @phpstan-type failed_smartscraper = array{ - * error?: string|null, - * requestID?: string|null, + * error?: string, + * requestID?: string, * result?: mixed, - * status?: value-of|null, - * userPrompt?: string|null, + * status?: value-of, + * userPrompt?: string, * websiteURL?: string|null, * } */ diff --git a/src/Validate/ValidateAPIKeyResponse.php b/src/Validate/ValidateAPIKeyResponse.php index 6c12fdb..eabf303 100644 --- a/src/Validate/ValidateAPIKeyResponse.php +++ b/src/Validate/ValidateAPIKeyResponse.php @@ -9,7 +9,11 @@ use Scrapegraphai\Core\Contracts\BaseModel; /** - * @phpstan-type validate_api_key_response = array{email?: string|null} + * @phpstan-type validate_api_key_response = array{email?: string} + * When used in a response, this type parameter can be used to define a $rawResponse property. + * @template TRawResponse of object = object{} + * + * @mixin TRawResponse */ final class ValidateAPIKeyResponse implements BaseModel { From 2c62976824aa8bc2b4119990a4253bd5918852a3 Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Sat, 13 Sep 2025 02:28:56 +0000 Subject: [PATCH 39/44] feat(client): add raw methods --- src/Crawl/CrawlGetResultsResponse.php | 2 +- src/Crawl/CrawlStartResponse.php | 2 +- src/Credits/CreditGetResponse.php | 2 +- src/Feedback/FeedbackSubmitResponse.php | 2 +- .../GenerateSchemaNewResponse.php | 2 +- src/Healthz/HealthzCheckResponse.php | 2 +- src/Markdownify/CompletedMarkdownify.php | 2 +- src/Searchscraper/CompletedSearchScraper.php | 2 +- src/ServiceContracts/CrawlContract.php | 32 ++++++++ src/ServiceContracts/CreditsContract.php | 15 ++++ src/ServiceContracts/FeedbackContract.php | 17 ++++ .../GenerateSchemaContract.php | 30 +++++++ src/ServiceContracts/HealthzContract.php | 15 ++++ src/ServiceContracts/MarkdownifyContract.php | 30 +++++++ .../SearchscraperContract.php | 30 +++++++ src/ServiceContracts/SmartscraperContract.php | 42 ++++++++++ src/ServiceContracts/ValidateContract.php | 15 ++++ src/Services/CrawlService.php | 64 ++++++++++++--- src/Services/CreditsService.php | 19 +++++ src/Services/FeedbackService.php | 33 ++++++-- src/Services/GenerateSchemaService.php | 44 +++++++++- src/Services/HealthzService.php | 19 +++++ src/Services/MarkdownifyService.php | 44 +++++++++- src/Services/SearchscraperService.php | 52 ++++++++++-- src/Services/SmartscraperService.php | 80 ++++++++++++++++--- src/Services/ValidateService.php | 19 +++++ src/Smartscraper/CompletedSmartscraper.php | 2 +- src/Validate/ValidateAPIKeyResponse.php | 2 +- 28 files changed, 568 insertions(+), 52 deletions(-) diff --git a/src/Crawl/CrawlGetResultsResponse.php b/src/Crawl/CrawlGetResultsResponse.php index 757a85d..9a7a890 100644 --- a/src/Crawl/CrawlGetResultsResponse.php +++ b/src/Crawl/CrawlGetResultsResponse.php @@ -17,7 +17,7 @@ * taskID?: string, * traceback?: string|null, * } - * When used in a response, this type parameter can be used to define a $rawResponse property. + * When used in a response, this type parameter can define a $rawResponse property. * @template TRawResponse of object = object{} * * @mixin TRawResponse diff --git a/src/Crawl/CrawlStartResponse.php b/src/Crawl/CrawlStartResponse.php index 4aa93b6..2a1af64 100644 --- a/src/Crawl/CrawlStartResponse.php +++ b/src/Crawl/CrawlStartResponse.php @@ -10,7 +10,7 @@ /** * @phpstan-type crawl_start_response = array{taskID?: string} - * When used in a response, this type parameter can be used to define a $rawResponse property. + * When used in a response, this type parameter can define a $rawResponse property. * @template TRawResponse of object = object{} * * @mixin TRawResponse diff --git a/src/Credits/CreditGetResponse.php b/src/Credits/CreditGetResponse.php index 7d3cc84..96d8e71 100644 --- a/src/Credits/CreditGetResponse.php +++ b/src/Credits/CreditGetResponse.php @@ -12,7 +12,7 @@ * @phpstan-type credit_get_response = array{ * remainingCredits?: int, totalCreditsUsed?: int * } - * When used in a response, this type parameter can be used to define a $rawResponse property. + * When used in a response, this type parameter can define a $rawResponse property. * @template TRawResponse of object = object{} * * @mixin TRawResponse diff --git a/src/Feedback/FeedbackSubmitResponse.php b/src/Feedback/FeedbackSubmitResponse.php index e06b283..b9f4e9a 100644 --- a/src/Feedback/FeedbackSubmitResponse.php +++ b/src/Feedback/FeedbackSubmitResponse.php @@ -15,7 +15,7 @@ * message?: string, * requestID?: string, * } - * When used in a response, this type parameter can be used to define a $rawResponse property. + * When used in a response, this type parameter can define a $rawResponse property. * @template TRawResponse of object = object{} * * @mixin TRawResponse diff --git a/src/GenerateSchema/GenerateSchemaNewResponse.php b/src/GenerateSchema/GenerateSchemaNewResponse.php index c2b5fca..e0ecb4d 100644 --- a/src/GenerateSchema/GenerateSchemaNewResponse.php +++ b/src/GenerateSchema/GenerateSchemaNewResponse.php @@ -18,7 +18,7 @@ * status?: value-of, * userPrompt?: string, * } - * When used in a response, this type parameter can be used to define a $rawResponse property. + * When used in a response, this type parameter can define a $rawResponse property. * @template TRawResponse of object = object{} * * @mixin TRawResponse diff --git a/src/Healthz/HealthzCheckResponse.php b/src/Healthz/HealthzCheckResponse.php index 4beb823..07d1bfc 100644 --- a/src/Healthz/HealthzCheckResponse.php +++ b/src/Healthz/HealthzCheckResponse.php @@ -12,7 +12,7 @@ * @phpstan-type healthz_check_response = array{ * services?: array, status?: string * } - * When used in a response, this type parameter can be used to define a $rawResponse property. + * When used in a response, this type parameter can define a $rawResponse property. * @template TRawResponse of object = object{} * * @mixin TRawResponse diff --git a/src/Markdownify/CompletedMarkdownify.php b/src/Markdownify/CompletedMarkdownify.php index c631af5..cb91d68 100644 --- a/src/Markdownify/CompletedMarkdownify.php +++ b/src/Markdownify/CompletedMarkdownify.php @@ -17,7 +17,7 @@ * status?: value-of, * websiteURL?: string, * } - * When used in a response, this type parameter can be used to define a $rawResponse property. + * When used in a response, this type parameter can define a $rawResponse property. * @template TRawResponse of object = object{} * * @mixin TRawResponse diff --git a/src/Searchscraper/CompletedSearchScraper.php b/src/Searchscraper/CompletedSearchScraper.php index b9f1307..9cb9b5f 100644 --- a/src/Searchscraper/CompletedSearchScraper.php +++ b/src/Searchscraper/CompletedSearchScraper.php @@ -19,7 +19,7 @@ * status?: value-of, * userPrompt?: string, * } - * When used in a response, this type parameter can be used to define a $rawResponse property. + * When used in a response, this type parameter can define a $rawResponse property. * @template TRawResponse of object = object{} * * @mixin TRawResponse diff --git a/src/ServiceContracts/CrawlContract.php b/src/ServiceContracts/CrawlContract.php index dbb769c..8c10e04 100644 --- a/src/ServiceContracts/CrawlContract.php +++ b/src/ServiceContracts/CrawlContract.php @@ -4,6 +4,7 @@ namespace Scrapegraphai\ServiceContracts; +use Scrapegraphai\Core\Exceptions\APIException; use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\Crawl\CrawlGetResultsResponse; use Scrapegraphai\Crawl\CrawlStartParams\Rules; @@ -18,12 +19,27 @@ interface CrawlContract * @api * * @return CrawlGetResultsResponse + * + * @throws APIException */ public function retrieveResults( string $taskID, ?RequestOptions $requestOptions = null ): CrawlGetResultsResponse; + /** + * @api + * + * @return CrawlGetResultsResponse + * + * @throws APIException + */ + public function retrieveResultsRaw( + string $taskID, + mixed $params, + ?RequestOptions $requestOptions = null + ): CrawlGetResultsResponse; + /** * @api * @@ -38,6 +54,8 @@ public function retrieveResults( * @param bool $sitemap Use sitemap for crawling * * @return CrawlStartResponse + * + * @throws APIException */ public function start( $url, @@ -51,4 +69,18 @@ public function start( $sitemap = omit, ?RequestOptions $requestOptions = null, ): CrawlStartResponse; + + /** + * @api + * + * @param array $params + * + * @return CrawlStartResponse + * + * @throws APIException + */ + public function startRaw( + array $params, + ?RequestOptions $requestOptions = null + ): CrawlStartResponse; } diff --git a/src/ServiceContracts/CreditsContract.php b/src/ServiceContracts/CreditsContract.php index 9114be8..b770739 100644 --- a/src/ServiceContracts/CreditsContract.php +++ b/src/ServiceContracts/CreditsContract.php @@ -4,6 +4,7 @@ namespace Scrapegraphai\ServiceContracts; +use Scrapegraphai\Core\Exceptions\APIException; use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\Credits\CreditGetResponse; use Scrapegraphai\RequestOptions; @@ -14,8 +15,22 @@ interface CreditsContract * @api * * @return CreditGetResponse + * + * @throws APIException */ public function retrieve( ?RequestOptions $requestOptions = null ): CreditGetResponse; + + /** + * @api + * + * @return CreditGetResponse + * + * @throws APIException + */ + public function retrieveRaw( + mixed $params, + ?RequestOptions $requestOptions = null + ): CreditGetResponse; } diff --git a/src/ServiceContracts/FeedbackContract.php b/src/ServiceContracts/FeedbackContract.php index 4e6ed0d..aee9907 100644 --- a/src/ServiceContracts/FeedbackContract.php +++ b/src/ServiceContracts/FeedbackContract.php @@ -4,6 +4,7 @@ namespace Scrapegraphai\ServiceContracts; +use Scrapegraphai\Core\Exceptions\APIException; use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\Feedback\FeedbackSubmitResponse; use Scrapegraphai\RequestOptions; @@ -20,6 +21,8 @@ interface FeedbackContract * @param string|null $feedbackText Optional feedback comments * * @return FeedbackSubmitResponse + * + * @throws APIException */ public function submit( $rating, @@ -27,4 +30,18 @@ public function submit( $feedbackText = omit, ?RequestOptions $requestOptions = null, ): FeedbackSubmitResponse; + + /** + * @api + * + * @param array $params + * + * @return FeedbackSubmitResponse + * + * @throws APIException + */ + public function submitRaw( + array $params, + ?RequestOptions $requestOptions = null + ): FeedbackSubmitResponse; } diff --git a/src/ServiceContracts/GenerateSchemaContract.php b/src/ServiceContracts/GenerateSchemaContract.php index 360f827..ce40edd 100644 --- a/src/ServiceContracts/GenerateSchemaContract.php +++ b/src/ServiceContracts/GenerateSchemaContract.php @@ -4,6 +4,7 @@ namespace Scrapegraphai\ServiceContracts; +use Scrapegraphai\Core\Exceptions\APIException; use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\GenerateSchema\GenerateSchemaGetResponse\CompletedSchemaGenerationResponse; use Scrapegraphai\GenerateSchema\GenerateSchemaGetResponse\FailedSchemaGenerationResponse; @@ -21,6 +22,8 @@ interface GenerateSchemaContract * @param mixed $existingSchema Existing schema to modify or extend * * @return GenerateSchemaNewResponse + * + * @throws APIException */ public function create( $userPrompt, @@ -30,9 +33,36 @@ public function create( /** * @api + * + * @param array $params + * + * @return GenerateSchemaNewResponse + * + * @throws APIException + */ + public function createRaw( + array $params, + ?RequestOptions $requestOptions = null + ): GenerateSchemaNewResponse; + + /** + * @api + * + * @throws APIException */ public function retrieve( string $requestID, ?RequestOptions $requestOptions = null ): CompletedSchemaGenerationResponse|FailedSchemaGenerationResponse; + + /** + * @api + * + * @throws APIException + */ + public function retrieveRaw( + string $requestID, + mixed $params, + ?RequestOptions $requestOptions = null + ): CompletedSchemaGenerationResponse|FailedSchemaGenerationResponse; } diff --git a/src/ServiceContracts/HealthzContract.php b/src/ServiceContracts/HealthzContract.php index 5e93387..5727b31 100644 --- a/src/ServiceContracts/HealthzContract.php +++ b/src/ServiceContracts/HealthzContract.php @@ -4,6 +4,7 @@ namespace Scrapegraphai\ServiceContracts; +use Scrapegraphai\Core\Exceptions\APIException; use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\Healthz\HealthzCheckResponse; use Scrapegraphai\RequestOptions; @@ -14,8 +15,22 @@ interface HealthzContract * @api * * @return HealthzCheckResponse + * + * @throws APIException */ public function check( ?RequestOptions $requestOptions = null ): HealthzCheckResponse; + + /** + * @api + * + * @return HealthzCheckResponse + * + * @throws APIException + */ + public function checkRaw( + mixed $params, + ?RequestOptions $requestOptions = null + ): HealthzCheckResponse; } diff --git a/src/ServiceContracts/MarkdownifyContract.php b/src/ServiceContracts/MarkdownifyContract.php index 8906d01..2876d68 100644 --- a/src/ServiceContracts/MarkdownifyContract.php +++ b/src/ServiceContracts/MarkdownifyContract.php @@ -4,6 +4,7 @@ namespace Scrapegraphai\ServiceContracts; +use Scrapegraphai\Core\Exceptions\APIException; use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\Markdownify\CompletedMarkdownify; use Scrapegraphai\Markdownify\MarkdownifyGetStatusResponse\FailedMarkdownifyResponse; @@ -21,6 +22,8 @@ interface MarkdownifyContract * @param list $steps Interaction steps before conversion * * @return CompletedMarkdownify + * + * @throws APIException */ public function convert( $websiteURL, @@ -31,9 +34,36 @@ public function convert( /** * @api + * + * @param array $params + * + * @return CompletedMarkdownify + * + * @throws APIException + */ + public function convertRaw( + array $params, + ?RequestOptions $requestOptions = null + ): CompletedMarkdownify; + + /** + * @api + * + * @throws APIException */ public function retrieveStatus( string $requestID, ?RequestOptions $requestOptions = null ): CompletedMarkdownify|FailedMarkdownifyResponse; + + /** + * @api + * + * @throws APIException + */ + public function retrieveStatusRaw( + string $requestID, + mixed $params, + ?RequestOptions $requestOptions = null + ): CompletedMarkdownify|FailedMarkdownifyResponse; } diff --git a/src/ServiceContracts/SearchscraperContract.php b/src/ServiceContracts/SearchscraperContract.php index abf0d23..0036b3a 100644 --- a/src/ServiceContracts/SearchscraperContract.php +++ b/src/ServiceContracts/SearchscraperContract.php @@ -4,6 +4,7 @@ namespace Scrapegraphai\ServiceContracts; +use Scrapegraphai\Core\Exceptions\APIException; use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\RequestOptions; use Scrapegraphai\Searchscraper\CompletedSearchScraper; @@ -22,6 +23,8 @@ interface SearchscraperContract * @param mixed $outputSchema JSON schema for structured output * * @return CompletedSearchScraper + * + * @throws APIException */ public function create( $userPrompt, @@ -33,9 +36,36 @@ public function create( /** * @api + * + * @param array $params + * + * @return CompletedSearchScraper + * + * @throws APIException + */ + public function createRaw( + array $params, + ?RequestOptions $requestOptions = null + ): CompletedSearchScraper; + + /** + * @api + * + * @throws APIException */ public function retrieveStatus( string $requestID, ?RequestOptions $requestOptions = null ): CompletedSearchScraper|FailedSearchScraperResponse; + + /** + * @api + * + * @throws APIException + */ + public function retrieveStatusRaw( + string $requestID, + mixed $params, + ?RequestOptions $requestOptions = null + ): CompletedSearchScraper|FailedSearchScraperResponse; } diff --git a/src/ServiceContracts/SmartscraperContract.php b/src/ServiceContracts/SmartscraperContract.php index a8afaed..6e5696e 100644 --- a/src/ServiceContracts/SmartscraperContract.php +++ b/src/ServiceContracts/SmartscraperContract.php @@ -4,6 +4,7 @@ namespace Scrapegraphai\ServiceContracts; +use Scrapegraphai\Core\Exceptions\APIException; use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\RequestOptions; use Scrapegraphai\Smartscraper\CompletedSmartscraper; @@ -28,6 +29,8 @@ interface SmartscraperContract * @param string $websiteURL URL to scrape (mutually exclusive with website_html) * * @return CompletedSmartscraper + * + * @throws APIException */ public function create( $userPrompt, @@ -45,6 +48,22 @@ public function create( /** * @api + * + * @param array $params + * + * @return CompletedSmartscraper + * + * @throws APIException + */ + public function createRaw( + array $params, + ?RequestOptions $requestOptions = null + ): CompletedSmartscraper; + + /** + * @api + * + * @throws APIException */ public function retrieve( string $requestID, @@ -53,8 +72,31 @@ public function retrieve( /** * @api + * + * @throws APIException + */ + public function retrieveRaw( + string $requestID, + mixed $params, + ?RequestOptions $requestOptions = null + ): CompletedSmartscraper|FailedSmartscraper; + + /** + * @api + * + * @throws APIException */ public function list( ?RequestOptions $requestOptions = null ): CompletedSmartscraper|FailedSmartscraper; + + /** + * @api + * + * @throws APIException + */ + public function listRaw( + mixed $params, + ?RequestOptions $requestOptions = null + ): CompletedSmartscraper|FailedSmartscraper; } diff --git a/src/ServiceContracts/ValidateContract.php b/src/ServiceContracts/ValidateContract.php index 72f4b42..ac7fc33 100644 --- a/src/ServiceContracts/ValidateContract.php +++ b/src/ServiceContracts/ValidateContract.php @@ -4,6 +4,7 @@ namespace Scrapegraphai\ServiceContracts; +use Scrapegraphai\Core\Exceptions\APIException; use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\RequestOptions; use Scrapegraphai\Validate\ValidateAPIKeyResponse; @@ -14,8 +15,22 @@ interface ValidateContract * @api * * @return ValidateAPIKeyResponse + * + * @throws APIException */ public function apiKey( ?RequestOptions $requestOptions = null ): ValidateAPIKeyResponse; + + /** + * @api + * + * @return ValidateAPIKeyResponse + * + * @throws APIException + */ + public function apiKeyRaw( + mixed $params, + ?RequestOptions $requestOptions = null + ): ValidateAPIKeyResponse; } diff --git a/src/Services/CrawlService.php b/src/Services/CrawlService.php index 030dc35..c1805ed 100644 --- a/src/Services/CrawlService.php +++ b/src/Services/CrawlService.php @@ -5,6 +5,7 @@ namespace Scrapegraphai\Services; use Scrapegraphai\Client; +use Scrapegraphai\Core\Exceptions\APIException; use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\Crawl\CrawlGetResultsResponse; use Scrapegraphai\Crawl\CrawlStartParams; @@ -28,10 +29,29 @@ public function __construct(private Client $client) {} * Retrieve the status and results of a crawling job * * @return CrawlGetResultsResponse + * + * @throws APIException */ public function retrieveResults( string $taskID, ?RequestOptions $requestOptions = null + ): CrawlGetResultsResponse { + $params = []; + + return $this->retrieveResultsRaw($taskID, $params, $requestOptions); + } + + /** + * @api + * + * @return CrawlGetResultsResponse + * + * @throws APIException + */ + public function retrieveResultsRaw( + string $taskID, + mixed $params, + ?RequestOptions $requestOptions = null ): CrawlGetResultsResponse { // @phpstan-ignore-next-line; return $this->client->request( @@ -60,6 +80,8 @@ public function retrieveResults( * @param bool $sitemap Use sitemap for crawling * * @return CrawlStartResponse + * + * @throws APIException */ public function start( $url, @@ -72,20 +94,38 @@ public function start( $schema = omit, $sitemap = omit, ?RequestOptions $requestOptions = null, + ): CrawlStartResponse { + $params = [ + 'url' => $url, + 'depth' => $depth, + 'extractionMode' => $extractionMode, + 'maxPages' => $maxPages, + 'prompt' => $prompt, + 'renderHeavyJs' => $renderHeavyJs, + 'rules' => $rules, + 'schema' => $schema, + 'sitemap' => $sitemap, + ]; + + return $this->startRaw($params, $requestOptions); + } + + /** + * @api + * + * @param array $params + * + * @return CrawlStartResponse + * + * @throws APIException + */ + public function startRaw( + array $params, + ?RequestOptions $requestOptions = null ): CrawlStartResponse { [$parsed, $options] = CrawlStartParams::parseRequest( - [ - 'url' => $url, - 'depth' => $depth, - 'extractionMode' => $extractionMode, - 'maxPages' => $maxPages, - 'prompt' => $prompt, - 'renderHeavyJs' => $renderHeavyJs, - 'rules' => $rules, - 'schema' => $schema, - 'sitemap' => $sitemap, - ], - $requestOptions, + $params, + $requestOptions ); // @phpstan-ignore-next-line; diff --git a/src/Services/CreditsService.php b/src/Services/CreditsService.php index 4afd763..223d123 100644 --- a/src/Services/CreditsService.php +++ b/src/Services/CreditsService.php @@ -5,6 +5,7 @@ namespace Scrapegraphai\Services; use Scrapegraphai\Client; +use Scrapegraphai\Core\Exceptions\APIException; use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\Credits\CreditGetResponse; use Scrapegraphai\RequestOptions; @@ -23,9 +24,27 @@ public function __construct(private Client $client) {} * Retrieve the current credit balance and usage for the authenticated user * * @return CreditGetResponse + * + * @throws APIException */ public function retrieve( ?RequestOptions $requestOptions = null + ): CreditGetResponse { + $params = []; + + return $this->retrieveRaw($params, $requestOptions); + } + + /** + * @api + * + * @return CreditGetResponse + * + * @throws APIException + */ + public function retrieveRaw( + mixed $params, + ?RequestOptions $requestOptions = null ): CreditGetResponse { // @phpstan-ignore-next-line; return $this->client->request( diff --git a/src/Services/FeedbackService.php b/src/Services/FeedbackService.php index 8c75e06..4f36e2a 100644 --- a/src/Services/FeedbackService.php +++ b/src/Services/FeedbackService.php @@ -5,6 +5,7 @@ namespace Scrapegraphai\Services; use Scrapegraphai\Client; +use Scrapegraphai\Core\Exceptions\APIException; use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\Feedback\FeedbackSubmitParams; use Scrapegraphai\Feedback\FeedbackSubmitResponse; @@ -30,20 +31,40 @@ public function __construct(private Client $client) {} * @param string|null $feedbackText Optional feedback comments * * @return FeedbackSubmitResponse + * + * @throws APIException */ public function submit( $rating, $requestID, $feedbackText = omit, ?RequestOptions $requestOptions = null, + ): FeedbackSubmitResponse { + $params = [ + 'rating' => $rating, + 'requestID' => $requestID, + 'feedbackText' => $feedbackText, + ]; + + return $this->submitRaw($params, $requestOptions); + } + + /** + * @api + * + * @param array $params + * + * @return FeedbackSubmitResponse + * + * @throws APIException + */ + public function submitRaw( + array $params, + ?RequestOptions $requestOptions = null ): FeedbackSubmitResponse { [$parsed, $options] = FeedbackSubmitParams::parseRequest( - [ - 'rating' => $rating, - 'requestID' => $requestID, - 'feedbackText' => $feedbackText, - ], - $requestOptions, + $params, + $requestOptions ); // @phpstan-ignore-next-line; diff --git a/src/Services/GenerateSchemaService.php b/src/Services/GenerateSchemaService.php index 2a280f1..ba93968 100644 --- a/src/Services/GenerateSchemaService.php +++ b/src/Services/GenerateSchemaService.php @@ -5,6 +5,7 @@ namespace Scrapegraphai\Services; use Scrapegraphai\Client; +use Scrapegraphai\Core\Exceptions\APIException; use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\GenerateSchema\GenerateSchemaCreateParams; use Scrapegraphai\GenerateSchema\GenerateSchemaGetResponse; @@ -33,15 +34,37 @@ public function __construct(private Client $client) {} * @param mixed $existingSchema Existing schema to modify or extend * * @return GenerateSchemaNewResponse + * + * @throws APIException */ public function create( $userPrompt, $existingSchema = omit, ?RequestOptions $requestOptions = null + ): GenerateSchemaNewResponse { + $params = [ + 'userPrompt' => $userPrompt, 'existingSchema' => $existingSchema, + ]; + + return $this->createRaw($params, $requestOptions); + } + + /** + * @api + * + * @param array $params + * + * @return GenerateSchemaNewResponse + * + * @throws APIException + */ + public function createRaw( + array $params, + ?RequestOptions $requestOptions = null ): GenerateSchemaNewResponse { [$parsed, $options] = GenerateSchemaCreateParams::parseRequest( - ['userPrompt' => $userPrompt, 'existingSchema' => $existingSchema], - $requestOptions, + $params, + $requestOptions ); // @phpstan-ignore-next-line; @@ -58,10 +81,27 @@ public function create( * @api * * Retrieve the status and results of a schema generation request + * + * @throws APIException */ public function retrieve( string $requestID, ?RequestOptions $requestOptions = null + ): CompletedSchemaGenerationResponse|FailedSchemaGenerationResponse { + $params = []; + + return $this->retrieveRaw($requestID, $params, $requestOptions); + } + + /** + * @api + * + * @throws APIException + */ + public function retrieveRaw( + string $requestID, + mixed $params, + ?RequestOptions $requestOptions = null ): CompletedSchemaGenerationResponse|FailedSchemaGenerationResponse { // @phpstan-ignore-next-line; return $this->client->request( diff --git a/src/Services/HealthzService.php b/src/Services/HealthzService.php index 0195305..494e3ba 100644 --- a/src/Services/HealthzService.php +++ b/src/Services/HealthzService.php @@ -5,6 +5,7 @@ namespace Scrapegraphai\Services; use Scrapegraphai\Client; +use Scrapegraphai\Core\Exceptions\APIException; use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\Healthz\HealthzCheckResponse; use Scrapegraphai\RequestOptions; @@ -23,9 +24,27 @@ public function __construct(private Client $client) {} * Check the health status of the service * * @return HealthzCheckResponse + * + * @throws APIException */ public function check( ?RequestOptions $requestOptions = null + ): HealthzCheckResponse { + $params = []; + + return $this->checkRaw($params, $requestOptions); + } + + /** + * @api + * + * @return HealthzCheckResponse + * + * @throws APIException + */ + public function checkRaw( + mixed $params, + ?RequestOptions $requestOptions = null ): HealthzCheckResponse { // @phpstan-ignore-next-line; return $this->client->request( diff --git a/src/Services/MarkdownifyService.php b/src/Services/MarkdownifyService.php index 2746911..e584bb4 100644 --- a/src/Services/MarkdownifyService.php +++ b/src/Services/MarkdownifyService.php @@ -5,6 +5,7 @@ namespace Scrapegraphai\Services; use Scrapegraphai\Client; +use Scrapegraphai\Core\Exceptions\APIException; use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\Markdownify\CompletedMarkdownify; use Scrapegraphai\Markdownify\MarkdownifyConvertParams; @@ -32,16 +33,38 @@ public function __construct(private Client $client) {} * @param list $steps Interaction steps before conversion * * @return CompletedMarkdownify + * + * @throws APIException */ public function convert( $websiteURL, $headers = omit, $steps = omit, ?RequestOptions $requestOptions = null, + ): CompletedMarkdownify { + $params = [ + 'websiteURL' => $websiteURL, 'headers' => $headers, 'steps' => $steps, + ]; + + return $this->convertRaw($params, $requestOptions); + } + + /** + * @api + * + * @param array $params + * + * @return CompletedMarkdownify + * + * @throws APIException + */ + public function convertRaw( + array $params, + ?RequestOptions $requestOptions = null ): CompletedMarkdownify { [$parsed, $options] = MarkdownifyConvertParams::parseRequest( - ['websiteURL' => $websiteURL, 'headers' => $headers, 'steps' => $steps], - $requestOptions, + $params, + $requestOptions ); // @phpstan-ignore-next-line; @@ -58,10 +81,27 @@ public function convert( * @api * * Retrieve the status and results of a markdown conversion + * + * @throws APIException */ public function retrieveStatus( string $requestID, ?RequestOptions $requestOptions = null + ): CompletedMarkdownify|FailedMarkdownifyResponse { + $params = []; + + return $this->retrieveStatusRaw($requestID, $params, $requestOptions); + } + + /** + * @api + * + * @throws APIException + */ + public function retrieveStatusRaw( + string $requestID, + mixed $params, + ?RequestOptions $requestOptions = null ): CompletedMarkdownify|FailedMarkdownifyResponse { // @phpstan-ignore-next-line; return $this->client->request( diff --git a/src/Services/SearchscraperService.php b/src/Services/SearchscraperService.php index 7a55572..9555281 100644 --- a/src/Services/SearchscraperService.php +++ b/src/Services/SearchscraperService.php @@ -5,6 +5,7 @@ namespace Scrapegraphai\Services; use Scrapegraphai\Client; +use Scrapegraphai\Core\Exceptions\APIException; use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\RequestOptions; use Scrapegraphai\Searchscraper\CompletedSearchScraper; @@ -34,6 +35,8 @@ public function __construct(private Client $client) {} * @param mixed $outputSchema JSON schema for structured output * * @return CompletedSearchScraper + * + * @throws APIException */ public function create( $userPrompt, @@ -41,15 +44,33 @@ public function create( $numResults = omit, $outputSchema = omit, ?RequestOptions $requestOptions = null, + ): CompletedSearchScraper { + $params = [ + 'userPrompt' => $userPrompt, + 'headers' => $headers, + 'numResults' => $numResults, + 'outputSchema' => $outputSchema, + ]; + + return $this->createRaw($params, $requestOptions); + } + + /** + * @api + * + * @param array $params + * + * @return CompletedSearchScraper + * + * @throws APIException + */ + public function createRaw( + array $params, + ?RequestOptions $requestOptions = null ): CompletedSearchScraper { [$parsed, $options] = SearchscraperCreateParams::parseRequest( - [ - 'userPrompt' => $userPrompt, - 'headers' => $headers, - 'numResults' => $numResults, - 'outputSchema' => $outputSchema, - ], - $requestOptions, + $params, + $requestOptions ); // @phpstan-ignore-next-line; @@ -66,10 +87,27 @@ public function create( * @api * * Retrieve the status and results of a search scraping operation + * + * @throws APIException */ public function retrieveStatus( string $requestID, ?RequestOptions $requestOptions = null + ): CompletedSearchScraper|FailedSearchScraperResponse { + $params = []; + + return $this->retrieveStatusRaw($requestID, $params, $requestOptions); + } + + /** + * @api + * + * @throws APIException + */ + public function retrieveStatusRaw( + string $requestID, + mixed $params, + ?RequestOptions $requestOptions = null ): CompletedSearchScraper|FailedSearchScraperResponse { // @phpstan-ignore-next-line; return $this->client->request( diff --git a/src/Services/SmartscraperService.php b/src/Services/SmartscraperService.php index 9ee09e7..b8ae2bd 100644 --- a/src/Services/SmartscraperService.php +++ b/src/Services/SmartscraperService.php @@ -5,6 +5,7 @@ namespace Scrapegraphai\Services; use Scrapegraphai\Client; +use Scrapegraphai\Core\Exceptions\APIException; use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\RequestOptions; use Scrapegraphai\ServiceContracts\SmartscraperContract; @@ -41,6 +42,8 @@ public function __construct(private Client $client) {} * @param string $websiteURL URL to scrape (mutually exclusive with website_html) * * @return CompletedSmartscraper + * + * @throws APIException */ public function create( $userPrompt, @@ -54,21 +57,39 @@ public function create( $websiteHTML = omit, $websiteURL = omit, ?RequestOptions $requestOptions = null, + ): CompletedSmartscraper { + $params = [ + 'userPrompt' => $userPrompt, + 'cookies' => $cookies, + 'headers' => $headers, + 'numberOfScrolls' => $numberOfScrolls, + 'outputSchema' => $outputSchema, + 'renderHeavyJs' => $renderHeavyJs, + 'steps' => $steps, + 'totalPages' => $totalPages, + 'websiteHTML' => $websiteHTML, + 'websiteURL' => $websiteURL, + ]; + + return $this->createRaw($params, $requestOptions); + } + + /** + * @api + * + * @param array $params + * + * @return CompletedSmartscraper + * + * @throws APIException + */ + public function createRaw( + array $params, + ?RequestOptions $requestOptions = null ): CompletedSmartscraper { [$parsed, $options] = SmartscraperCreateParams::parseRequest( - [ - 'userPrompt' => $userPrompt, - 'cookies' => $cookies, - 'headers' => $headers, - 'numberOfScrolls' => $numberOfScrolls, - 'outputSchema' => $outputSchema, - 'renderHeavyJs' => $renderHeavyJs, - 'steps' => $steps, - 'totalPages' => $totalPages, - 'websiteHTML' => $websiteHTML, - 'websiteURL' => $websiteURL, - ], - $requestOptions, + $params, + $requestOptions ); // @phpstan-ignore-next-line; @@ -85,10 +106,27 @@ public function create( * @api * * Retrieve the status and results of a scraping operation + * + * @throws APIException */ public function retrieve( string $requestID, ?RequestOptions $requestOptions = null + ): CompletedSmartscraper|FailedSmartscraper { + $params = []; + + return $this->retrieveRaw($requestID, $params, $requestOptions); + } + + /** + * @api + * + * @throws APIException + */ + public function retrieveRaw( + string $requestID, + mixed $params, + ?RequestOptions $requestOptions = null ): CompletedSmartscraper|FailedSmartscraper { // @phpstan-ignore-next-line; return $this->client->request( @@ -103,9 +141,25 @@ public function retrieve( * @api * * Retrieve the status and results of a scraping operation + * + * @throws APIException */ public function list( ?RequestOptions $requestOptions = null + ): CompletedSmartscraper|FailedSmartscraper { + $params = []; + + return $this->listRaw($params, $requestOptions); + } + + /** + * @api + * + * @throws APIException + */ + public function listRaw( + mixed $params, + ?RequestOptions $requestOptions = null ): CompletedSmartscraper|FailedSmartscraper { // @phpstan-ignore-next-line; return $this->client->request( diff --git a/src/Services/ValidateService.php b/src/Services/ValidateService.php index e876e8a..ad5f862 100644 --- a/src/Services/ValidateService.php +++ b/src/Services/ValidateService.php @@ -5,6 +5,7 @@ namespace Scrapegraphai\Services; use Scrapegraphai\Client; +use Scrapegraphai\Core\Exceptions\APIException; use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\RequestOptions; use Scrapegraphai\ServiceContracts\ValidateContract; @@ -23,9 +24,27 @@ public function __construct(private Client $client) {} * Validate the API key and retrieve associated user email * * @return ValidateAPIKeyResponse + * + * @throws APIException */ public function apiKey( ?RequestOptions $requestOptions = null + ): ValidateAPIKeyResponse { + $params = []; + + return $this->apiKeyRaw($params, $requestOptions); + } + + /** + * @api + * + * @return ValidateAPIKeyResponse + * + * @throws APIException + */ + public function apiKeyRaw( + mixed $params, + ?RequestOptions $requestOptions = null ): ValidateAPIKeyResponse { // @phpstan-ignore-next-line; return $this->client->request( diff --git a/src/Smartscraper/CompletedSmartscraper.php b/src/Smartscraper/CompletedSmartscraper.php index 15ce69d..fe6a4c4 100644 --- a/src/Smartscraper/CompletedSmartscraper.php +++ b/src/Smartscraper/CompletedSmartscraper.php @@ -18,7 +18,7 @@ * userPrompt?: string, * websiteURL?: string|null, * } - * When used in a response, this type parameter can be used to define a $rawResponse property. + * When used in a response, this type parameter can define a $rawResponse property. * @template TRawResponse of object = object{} * * @mixin TRawResponse diff --git a/src/Validate/ValidateAPIKeyResponse.php b/src/Validate/ValidateAPIKeyResponse.php index eabf303..6462292 100644 --- a/src/Validate/ValidateAPIKeyResponse.php +++ b/src/Validate/ValidateAPIKeyResponse.php @@ -10,7 +10,7 @@ /** * @phpstan-type validate_api_key_response = array{email?: string} - * When used in a response, this type parameter can be used to define a $rawResponse property. + * When used in a response, this type parameter can define a $rawResponse property. * @template TRawResponse of object = object{} * * @mixin TRawResponse From ecf153b12a488ad416d25e3b494e523ffd08f2c8 Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Tue, 23 Sep 2025 02:15:25 +0000 Subject: [PATCH 40/44] chore(docs): update readme formatting --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index d062da8..126bda9 100644 --- a/README.md +++ b/README.md @@ -124,7 +124,6 @@ use Scrapegraphai\RequestOptions; $client = new Client(maxRetries: 0); // Or, configure per-request: - $result = $client->smartscraper->create( userPrompt: "Extract the product name, price, and description", requestOptions: RequestOptions::with(maxRetries: 5), From ddd88d881ca5924d6d2f25e6ae4e4656e7ff5722 Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Sat, 4 Oct 2025 02:09:16 +0000 Subject: [PATCH 41/44] chore: refactor methods --- scripts/lint | 2 +- src/Client.php | 4 +-- src/Core/BaseClient.php | 18 ++++------ src/Core/Concerns/SdkResponse.php | 29 ++++++++++++++++ src/Core/Contracts/BasePage.php | 3 +- src/Core/Contracts/BaseStream.php | 4 --- src/Core/Conversion.php | 14 ++++++++ .../Contracts/ResponseConverter.php | 18 ++++++++++ src/Core/Implementation/HasRawResponse.php | 10 ------ src/Crawl/CrawlGetResultsResponse.php | 14 ++++---- src/Crawl/CrawlStartResponse.php | 10 +++--- src/Credits/CreditGetResponse.php | 10 +++--- src/Feedback/FeedbackSubmitResponse.php | 10 +++--- .../CompletedSchemaGenerationResponse.php | 4 +-- .../FailedSchemaGenerationResponse.php | 4 +-- .../GenerateSchemaNewResponse.php | 14 ++++---- src/Healthz/HealthzCheckResponse.php | 10 +++--- src/Markdownify/CompletedMarkdownify.php | 14 ++++---- .../FailedMarkdownifyResponse.php | 4 +-- src/Searchscraper/CompletedSearchScraper.php | 14 ++++---- .../FailedSearchScraperResponse.php | 4 +-- src/ServiceContracts/CrawlContract.php | 20 ----------- src/ServiceContracts/CreditsContract.php | 15 -------- src/ServiceContracts/FeedbackContract.php | 5 --- .../GenerateSchemaContract.php | 16 --------- src/ServiceContracts/HealthzContract.php | 15 -------- src/ServiceContracts/MarkdownifyContract.php | 16 --------- .../SearchscraperContract.php | 16 --------- src/ServiceContracts/SmartscraperContract.php | 26 -------------- src/ServiceContracts/ValidateContract.php | 15 -------- src/Services/CrawlService.php | 24 ------------- src/Services/CreditsService.php | 19 ----------- src/Services/FeedbackService.php | 5 --- src/Services/GenerateSchemaService.php | 20 ----------- src/Services/HealthzService.php | 19 ----------- src/Services/MarkdownifyService.php | 20 ----------- src/Services/SearchscraperService.php | 20 ----------- src/Services/SmartscraperService.php | 34 ------------------- src/Services/ValidateService.php | 19 ----------- src/Smartscraper/CompletedSmartscraper.php | 14 ++++---- src/Smartscraper/FailedSmartscraper.php | 4 +-- src/Validate/ValidateAPIKeyResponse.php | 10 +++--- 42 files changed, 142 insertions(+), 424 deletions(-) create mode 100644 src/Core/Concerns/SdkResponse.php create mode 100644 src/Core/Conversion/Contracts/ResponseConverter.php delete mode 100644 src/Core/Implementation/HasRawResponse.php diff --git a/scripts/lint b/scripts/lint index 6d629c2..13f2f01 100755 --- a/scripts/lint +++ b/scripts/lint @@ -5,4 +5,4 @@ set -e cd -- "$(dirname -- "$0")/.." echo "==> Running PHPStan" -exec -- ./vendor/bin/phpstan analyse --memory-limit=1G +exec -- ./vendor/bin/phpstan analyse --memory-limit=2G diff --git a/src/Client.php b/src/Client.php index b249101..9cf7405 100644 --- a/src/Client.php +++ b/src/Client.php @@ -70,7 +70,7 @@ public function __construct(?string $apiKey = null, ?string $baseUrl = null) { $this->apiKey = (string) ($apiKey ?? getenv('SCRAPEGRAPHAI_API_KEY')); - $base = $baseUrl ?? getenv( + $baseUrl ??= getenv( 'SCRAPEGRAPHAI_BASE_URL' ) ?: 'https://api.scrapegraphai.com/v1'; @@ -85,7 +85,7 @@ public function __construct(?string $apiKey = null, ?string $baseUrl = null) headers: [ 'Content-Type' => 'application/json', 'Accept' => 'application/json', ], - baseUrl: $base, + baseUrl: $baseUrl, options: $options, ); diff --git a/src/Core/BaseClient.php b/src/Core/BaseClient.php index 5ca0390..9230b1f 100644 --- a/src/Core/BaseClient.php +++ b/src/Core/BaseClient.php @@ -29,7 +29,7 @@ * body: mixed, * } */ -class BaseClient +abstract class BaseClient { protected UriInterface $baseUrl; @@ -77,14 +77,11 @@ public function request( // @phpstan-ignore-next-line $rsp = $this->sendRequest($opts, req: $request, data: $body, redirectCount: 0, retryCount: 0); - $decoded = Util::decodeContent($rsp); - if (!is_null($stream)) { return new $stream( convert: $convert, request: $request, - response: $rsp, - stream: $decoded + response: $rsp ); } @@ -93,23 +90,20 @@ public function request( convert: $convert, client: $this, request: $req, + response: $rsp, options: $opts, - data: $decoded, ); } if (!is_null($convert)) { - return Conversion::coerce($convert, value: $decoded); + return Conversion::coerceResponse($convert, response: $rsp); } - return $decoded; + return Util::decodeContent($rsp); } /** @return array */ - protected function authHeaders(): array - { - return []; - } + abstract protected function authHeaders(): array; /** * @internal diff --git a/src/Core/Concerns/SdkResponse.php b/src/Core/Concerns/SdkResponse.php new file mode 100644 index 0000000..bcda042 --- /dev/null +++ b/src/Core/Concerns/SdkResponse.php @@ -0,0 +1,29 @@ +_rawResponse = $response; + $instance->__unserialize(Util::decodeContent($response)); // @phpstan-ignore-line + + return $instance; + } + + public function getRawResponse(): ?ResponseInterface + { + return $this->_rawResponse; + } +} diff --git a/src/Core/Contracts/BasePage.php b/src/Core/Contracts/BasePage.php index 0829df8..88996d6 100644 --- a/src/Core/Contracts/BasePage.php +++ b/src/Core/Contracts/BasePage.php @@ -4,6 +4,7 @@ namespace Scrapegraphai\Core\Contracts; +use Psr\Http\Message\ResponseInterface; use Scrapegraphai\Client; use Scrapegraphai\Core\Conversion\Contracts\Converter; use Scrapegraphai\Core\Conversion\Contracts\ConverterSource; @@ -30,7 +31,7 @@ public function __construct( Client $client, array $request, RequestOptions $options, - mixed $data, + ResponseInterface $response, ); public function hasNextPage(): bool; diff --git a/src/Core/Contracts/BaseStream.php b/src/Core/Contracts/BaseStream.php index 2f95a2b..1e5e69a 100644 --- a/src/Core/Contracts/BaseStream.php +++ b/src/Core/Contracts/BaseStream.php @@ -18,14 +18,10 @@ */ interface BaseStream extends \IteratorAggregate { - /** - * @param \Generator $stream - */ public function __construct( Converter|ConverterSource|string $convert, RequestInterface $request, ResponseInterface $response, - \Generator $stream, ); /** diff --git a/src/Core/Conversion.php b/src/Core/Conversion.php index b1a12ab..4fc8c96 100644 --- a/src/Core/Conversion.php +++ b/src/Core/Conversion.php @@ -4,11 +4,16 @@ namespace Scrapegraphai\Core; +use Psr\Http\Message\ResponseInterface; use Scrapegraphai\Core\Conversion\CoerceState; use Scrapegraphai\Core\Conversion\Contracts\Converter; use Scrapegraphai\Core\Conversion\Contracts\ConverterSource; +use Scrapegraphai\Core\Conversion\Contracts\ResponseConverter; use Scrapegraphai\Core\Conversion\DumpState; +/** + * @internal + */ final class Conversion { public static function dump_unknown(mixed $value, DumpState $state): mixed @@ -38,6 +43,15 @@ public static function dump_unknown(mixed $value, DumpState $state): mixed return $value; } + public static function coerceResponse(Converter|ConverterSource|string $target, ResponseInterface $response): mixed + { + if (is_a($target, ResponseConverter::class, allow_string: true)) { + return $target::fromResponse($response); + } + + return self::coerce($target, Util::decodeContent($response)); + } + public static function coerce(Converter|ConverterSource|string $target, mixed $value, CoerceState $state = new CoerceState): mixed { if ($value instanceof $target) { diff --git a/src/Core/Conversion/Contracts/ResponseConverter.php b/src/Core/Conversion/Contracts/ResponseConverter.php new file mode 100644 index 0000000..ed64242 --- /dev/null +++ b/src/Core/Conversion/Contracts/ResponseConverter.php @@ -0,0 +1,18 @@ + */ use SdkModel; + use SdkResponse; + /** * Successful crawl results. * @@ -70,7 +70,7 @@ public static function with( $obj = new self; null !== $result && $obj->result = $result; - null !== $status && $obj->status = $status instanceof Status ? $status->value : $status; + null !== $status && $obj['status'] = $status; null !== $taskID && $obj->taskID = $taskID; null !== $traceback && $obj->traceback = $traceback; @@ -96,7 +96,7 @@ public function withResult(mixed $result): self public function withStatus(Status|string $status): self { $obj = clone $this; - $obj->status = $status instanceof Status ? $status->value : $status; + $obj['status'] = $status; return $obj; } diff --git a/src/Crawl/CrawlStartResponse.php b/src/Crawl/CrawlStartResponse.php index 2a1af64..5ea9580 100644 --- a/src/Crawl/CrawlStartResponse.php +++ b/src/Crawl/CrawlStartResponse.php @@ -6,20 +6,20 @@ use Scrapegraphai\Core\Attributes\Api; use Scrapegraphai\Core\Concerns\SdkModel; +use Scrapegraphai\Core\Concerns\SdkResponse; use Scrapegraphai\Core\Contracts\BaseModel; +use Scrapegraphai\Core\Conversion\Contracts\ResponseConverter; /** * @phpstan-type crawl_start_response = array{taskID?: string} - * When used in a response, this type parameter can define a $rawResponse property. - * @template TRawResponse of object = object{} - * - * @mixin TRawResponse */ -final class CrawlStartResponse implements BaseModel +final class CrawlStartResponse implements BaseModel, ResponseConverter { /** @use SdkModel */ use SdkModel; + use SdkResponse; + /** * Celery task identifier. */ diff --git a/src/Credits/CreditGetResponse.php b/src/Credits/CreditGetResponse.php index 96d8e71..5b9d31a 100644 --- a/src/Credits/CreditGetResponse.php +++ b/src/Credits/CreditGetResponse.php @@ -6,22 +6,22 @@ use Scrapegraphai\Core\Attributes\Api; use Scrapegraphai\Core\Concerns\SdkModel; +use Scrapegraphai\Core\Concerns\SdkResponse; use Scrapegraphai\Core\Contracts\BaseModel; +use Scrapegraphai\Core\Conversion\Contracts\ResponseConverter; /** * @phpstan-type credit_get_response = array{ * remainingCredits?: int, totalCreditsUsed?: int * } - * When used in a response, this type parameter can define a $rawResponse property. - * @template TRawResponse of object = object{} - * - * @mixin TRawResponse */ -final class CreditGetResponse implements BaseModel +final class CreditGetResponse implements BaseModel, ResponseConverter { /** @use SdkModel */ use SdkModel; + use SdkResponse; + /** * Number of credits remaining. */ diff --git a/src/Feedback/FeedbackSubmitResponse.php b/src/Feedback/FeedbackSubmitResponse.php index b9f4e9a..739ca1f 100644 --- a/src/Feedback/FeedbackSubmitResponse.php +++ b/src/Feedback/FeedbackSubmitResponse.php @@ -6,7 +6,9 @@ use Scrapegraphai\Core\Attributes\Api; use Scrapegraphai\Core\Concerns\SdkModel; +use Scrapegraphai\Core\Concerns\SdkResponse; use Scrapegraphai\Core\Contracts\BaseModel; +use Scrapegraphai\Core\Conversion\Contracts\ResponseConverter; /** * @phpstan-type feedback_submit_response = array{ @@ -15,16 +17,14 @@ * message?: string, * requestID?: string, * } - * When used in a response, this type parameter can define a $rawResponse property. - * @template TRawResponse of object = object{} - * - * @mixin TRawResponse */ -final class FeedbackSubmitResponse implements BaseModel +final class FeedbackSubmitResponse implements BaseModel, ResponseConverter { /** @use SdkModel */ use SdkModel; + use SdkResponse; + #[Api('feedback_id', optional: true)] public ?string $feedbackID; diff --git a/src/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse.php b/src/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse.php index 31971a5..5970da7 100644 --- a/src/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse.php +++ b/src/GenerateSchema/GenerateSchemaGetResponse/CompletedSchemaGenerationResponse.php @@ -69,7 +69,7 @@ public static function with( null !== $generatedSchema && $obj->generatedSchema = $generatedSchema; null !== $refinedPrompt && $obj->refinedPrompt = $refinedPrompt; null !== $requestID && $obj->requestID = $requestID; - null !== $status && $obj->status = $status instanceof Status ? $status->value : $status; + null !== $status && $obj['status'] = $status; null !== $userPrompt && $obj->userPrompt = $userPrompt; return $obj; @@ -113,7 +113,7 @@ public function withRequestID(string $requestID): self public function withStatus(Status|string $status): self { $obj = clone $this; - $obj->status = $status instanceof Status ? $status->value : $status; + $obj['status'] = $status; return $obj; } diff --git a/src/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse.php b/src/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse.php index 8d374b3..19c6216 100644 --- a/src/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse.php +++ b/src/GenerateSchema/GenerateSchemaGetResponse/FailedSchemaGenerationResponse.php @@ -69,7 +69,7 @@ public static function with( null !== $generatedSchema && $obj->generatedSchema = $generatedSchema; null !== $refinedPrompt && $obj->refinedPrompt = $refinedPrompt; null !== $requestID && $obj->requestID = $requestID; - null !== $status && $obj->status = $status instanceof Status ? $status->value : $status; + null !== $status && $obj['status'] = $status; null !== $userPrompt && $obj->userPrompt = $userPrompt; return $obj; @@ -113,7 +113,7 @@ public function withRequestID(string $requestID): self public function withStatus(Status|string $status): self { $obj = clone $this; - $obj->status = $status instanceof Status ? $status->value : $status; + $obj['status'] = $status; return $obj; } diff --git a/src/GenerateSchema/GenerateSchemaNewResponse.php b/src/GenerateSchema/GenerateSchemaNewResponse.php index e0ecb4d..85397dd 100644 --- a/src/GenerateSchema/GenerateSchemaNewResponse.php +++ b/src/GenerateSchema/GenerateSchemaNewResponse.php @@ -6,7 +6,9 @@ use Scrapegraphai\Core\Attributes\Api; use Scrapegraphai\Core\Concerns\SdkModel; +use Scrapegraphai\Core\Concerns\SdkResponse; use Scrapegraphai\Core\Contracts\BaseModel; +use Scrapegraphai\Core\Conversion\Contracts\ResponseConverter; use Scrapegraphai\GenerateSchema\GenerateSchemaNewResponse\Status; /** @@ -18,16 +20,14 @@ * status?: value-of, * userPrompt?: string, * } - * When used in a response, this type parameter can define a $rawResponse property. - * @template TRawResponse of object = object{} - * - * @mixin TRawResponse */ -final class GenerateSchemaNewResponse implements BaseModel +final class GenerateSchemaNewResponse implements BaseModel, ResponseConverter { /** @use SdkModel */ use SdkModel; + use SdkResponse; + #[Api(nullable: true, optional: true)] public ?string $error; @@ -79,7 +79,7 @@ public static function with( null !== $generatedSchema && $obj->generatedSchema = $generatedSchema; null !== $refinedPrompt && $obj->refinedPrompt = $refinedPrompt; null !== $requestID && $obj->requestID = $requestID; - null !== $status && $obj->status = $status instanceof Status ? $status->value : $status; + null !== $status && $obj['status'] = $status; null !== $userPrompt && $obj->userPrompt = $userPrompt; return $obj; @@ -129,7 +129,7 @@ public function withRequestID(string $requestID): self public function withStatus(Status|string $status): self { $obj = clone $this; - $obj->status = $status instanceof Status ? $status->value : $status; + $obj['status'] = $status; return $obj; } diff --git a/src/Healthz/HealthzCheckResponse.php b/src/Healthz/HealthzCheckResponse.php index 07d1bfc..7d79a33 100644 --- a/src/Healthz/HealthzCheckResponse.php +++ b/src/Healthz/HealthzCheckResponse.php @@ -6,22 +6,22 @@ use Scrapegraphai\Core\Attributes\Api; use Scrapegraphai\Core\Concerns\SdkModel; +use Scrapegraphai\Core\Concerns\SdkResponse; use Scrapegraphai\Core\Contracts\BaseModel; +use Scrapegraphai\Core\Conversion\Contracts\ResponseConverter; /** * @phpstan-type healthz_check_response = array{ * services?: array, status?: string * } - * When used in a response, this type parameter can define a $rawResponse property. - * @template TRawResponse of object = object{} - * - * @mixin TRawResponse */ -final class HealthzCheckResponse implements BaseModel +final class HealthzCheckResponse implements BaseModel, ResponseConverter { /** @use SdkModel */ use SdkModel; + use SdkResponse; + /** @var array|null $services */ #[Api(map: 'string', optional: true)] public ?array $services; diff --git a/src/Markdownify/CompletedMarkdownify.php b/src/Markdownify/CompletedMarkdownify.php index cb91d68..3c7a923 100644 --- a/src/Markdownify/CompletedMarkdownify.php +++ b/src/Markdownify/CompletedMarkdownify.php @@ -6,7 +6,9 @@ use Scrapegraphai\Core\Attributes\Api; use Scrapegraphai\Core\Concerns\SdkModel; +use Scrapegraphai\Core\Concerns\SdkResponse; use Scrapegraphai\Core\Contracts\BaseModel; +use Scrapegraphai\Core\Conversion\Contracts\ResponseConverter; use Scrapegraphai\Markdownify\CompletedMarkdownify\Status; /** @@ -17,16 +19,14 @@ * status?: value-of, * websiteURL?: string, * } - * When used in a response, this type parameter can define a $rawResponse property. - * @template TRawResponse of object = object{} - * - * @mixin TRawResponse */ -final class CompletedMarkdownify implements BaseModel +final class CompletedMarkdownify implements BaseModel, ResponseConverter { /** @use SdkModel */ use SdkModel; + use SdkResponse; + #[Api(optional: true)] public ?string $error; @@ -70,7 +70,7 @@ public static function with( null !== $error && $obj->error = $error; null !== $requestID && $obj->requestID = $requestID; null !== $result && $obj->result = $result; - null !== $status && $obj->status = $status instanceof Status ? $status->value : $status; + null !== $status && $obj['status'] = $status; null !== $websiteURL && $obj->websiteURL = $websiteURL; return $obj; @@ -109,7 +109,7 @@ public function withResult(?string $result): self public function withStatus(Status|string $status): self { $obj = clone $this; - $obj->status = $status instanceof Status ? $status->value : $status; + $obj['status'] = $status; return $obj; } diff --git a/src/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse.php b/src/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse.php index f27c635..1cc820a 100644 --- a/src/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse.php +++ b/src/Markdownify/MarkdownifyGetStatusResponse/FailedMarkdownifyResponse.php @@ -63,7 +63,7 @@ public static function with( null !== $error && $obj->error = $error; null !== $requestID && $obj->requestID = $requestID; null !== $result && $obj->result = $result; - null !== $status && $obj->status = $status instanceof Status ? $status->value : $status; + null !== $status && $obj['status'] = $status; null !== $websiteURL && $obj->websiteURL = $websiteURL; return $obj; @@ -99,7 +99,7 @@ public function withResult(?string $result): self public function withStatus(Status|string $status): self { $obj = clone $this; - $obj->status = $status instanceof Status ? $status->value : $status; + $obj['status'] = $status; return $obj; } diff --git a/src/Searchscraper/CompletedSearchScraper.php b/src/Searchscraper/CompletedSearchScraper.php index 9cb9b5f..5c6f734 100644 --- a/src/Searchscraper/CompletedSearchScraper.php +++ b/src/Searchscraper/CompletedSearchScraper.php @@ -6,7 +6,9 @@ use Scrapegraphai\Core\Attributes\Api; use Scrapegraphai\Core\Concerns\SdkModel; +use Scrapegraphai\Core\Concerns\SdkResponse; use Scrapegraphai\Core\Contracts\BaseModel; +use Scrapegraphai\Core\Conversion\Contracts\ResponseConverter; use Scrapegraphai\Searchscraper\CompletedSearchScraper\Status; /** @@ -19,16 +21,14 @@ * status?: value-of, * userPrompt?: string, * } - * When used in a response, this type parameter can define a $rawResponse property. - * @template TRawResponse of object = object{} - * - * @mixin TRawResponse */ -final class CompletedSearchScraper implements BaseModel +final class CompletedSearchScraper implements BaseModel, ResponseConverter { /** @use SdkModel */ use SdkModel; + use SdkResponse; + #[Api(nullable: true, optional: true)] public ?string $error; @@ -88,7 +88,7 @@ public static function with( null !== $referenceURLs && $obj->referenceURLs = $referenceURLs; null !== $requestID && $obj->requestID = $requestID; null !== $result && $obj->result = $result; - null !== $status && $obj->status = $status instanceof Status ? $status->value : $status; + null !== $status && $obj['status'] = $status; null !== $userPrompt && $obj->userPrompt = $userPrompt; return $obj; @@ -148,7 +148,7 @@ public function withResult(mixed $result): self public function withStatus(Status|string $status): self { $obj = clone $this; - $obj->status = $status instanceof Status ? $status->value : $status; + $obj['status'] = $status; return $obj; } diff --git a/src/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse.php b/src/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse.php index 1b16175..9f1ffcd 100644 --- a/src/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse.php +++ b/src/Searchscraper/SearchscraperGetStatusResponse/FailedSearchScraperResponse.php @@ -77,7 +77,7 @@ public static function with( null !== $referenceURLs && $obj->referenceURLs = $referenceURLs; null !== $requestID && $obj->requestID = $requestID; null !== $result && $obj->result = $result; - null !== $status && $obj->status = $status instanceof Status ? $status->value : $status; + null !== $status && $obj['status'] = $status; null !== $userPrompt && $obj->userPrompt = $userPrompt; return $obj; @@ -132,7 +132,7 @@ public function withResult(mixed $result): self public function withStatus(Status|string $status): self { $obj = clone $this; - $obj->status = $status instanceof Status ? $status->value : $status; + $obj['status'] = $status; return $obj; } diff --git a/src/ServiceContracts/CrawlContract.php b/src/ServiceContracts/CrawlContract.php index 8c10e04..a758a89 100644 --- a/src/ServiceContracts/CrawlContract.php +++ b/src/ServiceContracts/CrawlContract.php @@ -5,7 +5,6 @@ namespace Scrapegraphai\ServiceContracts; use Scrapegraphai\Core\Exceptions\APIException; -use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\Crawl\CrawlGetResultsResponse; use Scrapegraphai\Crawl\CrawlStartParams\Rules; use Scrapegraphai\Crawl\CrawlStartResponse; @@ -18,8 +17,6 @@ interface CrawlContract /** * @api * - * @return CrawlGetResultsResponse - * * @throws APIException */ public function retrieveResults( @@ -27,19 +24,6 @@ public function retrieveResults( ?RequestOptions $requestOptions = null ): CrawlGetResultsResponse; - /** - * @api - * - * @return CrawlGetResultsResponse - * - * @throws APIException - */ - public function retrieveResultsRaw( - string $taskID, - mixed $params, - ?RequestOptions $requestOptions = null - ): CrawlGetResultsResponse; - /** * @api * @@ -53,8 +37,6 @@ public function retrieveResultsRaw( * @param mixed $schema Output schema for extraction * @param bool $sitemap Use sitemap for crawling * - * @return CrawlStartResponse - * * @throws APIException */ public function start( @@ -75,8 +57,6 @@ public function start( * * @param array $params * - * @return CrawlStartResponse - * * @throws APIException */ public function startRaw( diff --git a/src/ServiceContracts/CreditsContract.php b/src/ServiceContracts/CreditsContract.php index b770739..ce8fe81 100644 --- a/src/ServiceContracts/CreditsContract.php +++ b/src/ServiceContracts/CreditsContract.php @@ -5,7 +5,6 @@ namespace Scrapegraphai\ServiceContracts; use Scrapegraphai\Core\Exceptions\APIException; -use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\Credits\CreditGetResponse; use Scrapegraphai\RequestOptions; @@ -14,23 +13,9 @@ interface CreditsContract /** * @api * - * @return CreditGetResponse - * * @throws APIException */ public function retrieve( ?RequestOptions $requestOptions = null ): CreditGetResponse; - - /** - * @api - * - * @return CreditGetResponse - * - * @throws APIException - */ - public function retrieveRaw( - mixed $params, - ?RequestOptions $requestOptions = null - ): CreditGetResponse; } diff --git a/src/ServiceContracts/FeedbackContract.php b/src/ServiceContracts/FeedbackContract.php index aee9907..9309192 100644 --- a/src/ServiceContracts/FeedbackContract.php +++ b/src/ServiceContracts/FeedbackContract.php @@ -5,7 +5,6 @@ namespace Scrapegraphai\ServiceContracts; use Scrapegraphai\Core\Exceptions\APIException; -use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\Feedback\FeedbackSubmitResponse; use Scrapegraphai\RequestOptions; @@ -20,8 +19,6 @@ interface FeedbackContract * @param string $requestID Request to provide feedback for * @param string|null $feedbackText Optional feedback comments * - * @return FeedbackSubmitResponse - * * @throws APIException */ public function submit( @@ -36,8 +33,6 @@ public function submit( * * @param array $params * - * @return FeedbackSubmitResponse - * * @throws APIException */ public function submitRaw( diff --git a/src/ServiceContracts/GenerateSchemaContract.php b/src/ServiceContracts/GenerateSchemaContract.php index ce40edd..9ff584e 100644 --- a/src/ServiceContracts/GenerateSchemaContract.php +++ b/src/ServiceContracts/GenerateSchemaContract.php @@ -5,7 +5,6 @@ namespace Scrapegraphai\ServiceContracts; use Scrapegraphai\Core\Exceptions\APIException; -use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\GenerateSchema\GenerateSchemaGetResponse\CompletedSchemaGenerationResponse; use Scrapegraphai\GenerateSchema\GenerateSchemaGetResponse\FailedSchemaGenerationResponse; use Scrapegraphai\GenerateSchema\GenerateSchemaNewResponse; @@ -21,8 +20,6 @@ interface GenerateSchemaContract * @param string $userPrompt Natural language description of desired schema * @param mixed $existingSchema Existing schema to modify or extend * - * @return GenerateSchemaNewResponse - * * @throws APIException */ public function create( @@ -36,8 +33,6 @@ public function create( * * @param array $params * - * @return GenerateSchemaNewResponse - * * @throws APIException */ public function createRaw( @@ -54,15 +49,4 @@ public function retrieve( string $requestID, ?RequestOptions $requestOptions = null ): CompletedSchemaGenerationResponse|FailedSchemaGenerationResponse; - - /** - * @api - * - * @throws APIException - */ - public function retrieveRaw( - string $requestID, - mixed $params, - ?RequestOptions $requestOptions = null - ): CompletedSchemaGenerationResponse|FailedSchemaGenerationResponse; } diff --git a/src/ServiceContracts/HealthzContract.php b/src/ServiceContracts/HealthzContract.php index 5727b31..a20931b 100644 --- a/src/ServiceContracts/HealthzContract.php +++ b/src/ServiceContracts/HealthzContract.php @@ -5,7 +5,6 @@ namespace Scrapegraphai\ServiceContracts; use Scrapegraphai\Core\Exceptions\APIException; -use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\Healthz\HealthzCheckResponse; use Scrapegraphai\RequestOptions; @@ -14,23 +13,9 @@ interface HealthzContract /** * @api * - * @return HealthzCheckResponse - * * @throws APIException */ public function check( ?RequestOptions $requestOptions = null ): HealthzCheckResponse; - - /** - * @api - * - * @return HealthzCheckResponse - * - * @throws APIException - */ - public function checkRaw( - mixed $params, - ?RequestOptions $requestOptions = null - ): HealthzCheckResponse; } diff --git a/src/ServiceContracts/MarkdownifyContract.php b/src/ServiceContracts/MarkdownifyContract.php index 2876d68..42b747f 100644 --- a/src/ServiceContracts/MarkdownifyContract.php +++ b/src/ServiceContracts/MarkdownifyContract.php @@ -5,7 +5,6 @@ namespace Scrapegraphai\ServiceContracts; use Scrapegraphai\Core\Exceptions\APIException; -use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\Markdownify\CompletedMarkdownify; use Scrapegraphai\Markdownify\MarkdownifyGetStatusResponse\FailedMarkdownifyResponse; use Scrapegraphai\RequestOptions; @@ -21,8 +20,6 @@ interface MarkdownifyContract * @param array $headers * @param list $steps Interaction steps before conversion * - * @return CompletedMarkdownify - * * @throws APIException */ public function convert( @@ -37,8 +34,6 @@ public function convert( * * @param array $params * - * @return CompletedMarkdownify - * * @throws APIException */ public function convertRaw( @@ -55,15 +50,4 @@ public function retrieveStatus( string $requestID, ?RequestOptions $requestOptions = null ): CompletedMarkdownify|FailedMarkdownifyResponse; - - /** - * @api - * - * @throws APIException - */ - public function retrieveStatusRaw( - string $requestID, - mixed $params, - ?RequestOptions $requestOptions = null - ): CompletedMarkdownify|FailedMarkdownifyResponse; } diff --git a/src/ServiceContracts/SearchscraperContract.php b/src/ServiceContracts/SearchscraperContract.php index 0036b3a..9d4779f 100644 --- a/src/ServiceContracts/SearchscraperContract.php +++ b/src/ServiceContracts/SearchscraperContract.php @@ -5,7 +5,6 @@ namespace Scrapegraphai\ServiceContracts; use Scrapegraphai\Core\Exceptions\APIException; -use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\RequestOptions; use Scrapegraphai\Searchscraper\CompletedSearchScraper; use Scrapegraphai\Searchscraper\SearchscraperGetStatusResponse\FailedSearchScraperResponse; @@ -22,8 +21,6 @@ interface SearchscraperContract * @param int $numResults Number of websites to scrape from search results * @param mixed $outputSchema JSON schema for structured output * - * @return CompletedSearchScraper - * * @throws APIException */ public function create( @@ -39,8 +36,6 @@ public function create( * * @param array $params * - * @return CompletedSearchScraper - * * @throws APIException */ public function createRaw( @@ -57,15 +52,4 @@ public function retrieveStatus( string $requestID, ?RequestOptions $requestOptions = null ): CompletedSearchScraper|FailedSearchScraperResponse; - - /** - * @api - * - * @throws APIException - */ - public function retrieveStatusRaw( - string $requestID, - mixed $params, - ?RequestOptions $requestOptions = null - ): CompletedSearchScraper|FailedSearchScraperResponse; } diff --git a/src/ServiceContracts/SmartscraperContract.php b/src/ServiceContracts/SmartscraperContract.php index 6e5696e..d1188f4 100644 --- a/src/ServiceContracts/SmartscraperContract.php +++ b/src/ServiceContracts/SmartscraperContract.php @@ -5,7 +5,6 @@ namespace Scrapegraphai\ServiceContracts; use Scrapegraphai\Core\Exceptions\APIException; -use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\RequestOptions; use Scrapegraphai\Smartscraper\CompletedSmartscraper; use Scrapegraphai\Smartscraper\FailedSmartscraper; @@ -28,8 +27,6 @@ interface SmartscraperContract * @param string $websiteHTML HTML content to process (max 2MB, mutually exclusive with website_url) * @param string $websiteURL URL to scrape (mutually exclusive with website_html) * - * @return CompletedSmartscraper - * * @throws APIException */ public function create( @@ -51,8 +48,6 @@ public function create( * * @param array $params * - * @return CompletedSmartscraper - * * @throws APIException */ public function createRaw( @@ -70,17 +65,6 @@ public function retrieve( ?RequestOptions $requestOptions = null ): CompletedSmartscraper|FailedSmartscraper; - /** - * @api - * - * @throws APIException - */ - public function retrieveRaw( - string $requestID, - mixed $params, - ?RequestOptions $requestOptions = null - ): CompletedSmartscraper|FailedSmartscraper; - /** * @api * @@ -89,14 +73,4 @@ public function retrieveRaw( public function list( ?RequestOptions $requestOptions = null ): CompletedSmartscraper|FailedSmartscraper; - - /** - * @api - * - * @throws APIException - */ - public function listRaw( - mixed $params, - ?RequestOptions $requestOptions = null - ): CompletedSmartscraper|FailedSmartscraper; } diff --git a/src/ServiceContracts/ValidateContract.php b/src/ServiceContracts/ValidateContract.php index ac7fc33..798ebec 100644 --- a/src/ServiceContracts/ValidateContract.php +++ b/src/ServiceContracts/ValidateContract.php @@ -5,7 +5,6 @@ namespace Scrapegraphai\ServiceContracts; use Scrapegraphai\Core\Exceptions\APIException; -use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\RequestOptions; use Scrapegraphai\Validate\ValidateAPIKeyResponse; @@ -14,23 +13,9 @@ interface ValidateContract /** * @api * - * @return ValidateAPIKeyResponse - * * @throws APIException */ public function apiKey( ?RequestOptions $requestOptions = null ): ValidateAPIKeyResponse; - - /** - * @api - * - * @return ValidateAPIKeyResponse - * - * @throws APIException - */ - public function apiKeyRaw( - mixed $params, - ?RequestOptions $requestOptions = null - ): ValidateAPIKeyResponse; } diff --git a/src/Services/CrawlService.php b/src/Services/CrawlService.php index c1805ed..635c8d7 100644 --- a/src/Services/CrawlService.php +++ b/src/Services/CrawlService.php @@ -6,7 +6,6 @@ use Scrapegraphai\Client; use Scrapegraphai\Core\Exceptions\APIException; -use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\Crawl\CrawlGetResultsResponse; use Scrapegraphai\Crawl\CrawlStartParams; use Scrapegraphai\Crawl\CrawlStartParams\Rules; @@ -28,30 +27,11 @@ public function __construct(private Client $client) {} * * Retrieve the status and results of a crawling job * - * @return CrawlGetResultsResponse - * * @throws APIException */ public function retrieveResults( string $taskID, ?RequestOptions $requestOptions = null - ): CrawlGetResultsResponse { - $params = []; - - return $this->retrieveResultsRaw($taskID, $params, $requestOptions); - } - - /** - * @api - * - * @return CrawlGetResultsResponse - * - * @throws APIException - */ - public function retrieveResultsRaw( - string $taskID, - mixed $params, - ?RequestOptions $requestOptions = null ): CrawlGetResultsResponse { // @phpstan-ignore-next-line; return $this->client->request( @@ -79,8 +59,6 @@ public function retrieveResultsRaw( * @param mixed $schema Output schema for extraction * @param bool $sitemap Use sitemap for crawling * - * @return CrawlStartResponse - * * @throws APIException */ public function start( @@ -115,8 +93,6 @@ public function start( * * @param array $params * - * @return CrawlStartResponse - * * @throws APIException */ public function startRaw( diff --git a/src/Services/CreditsService.php b/src/Services/CreditsService.php index 223d123..2584fc7 100644 --- a/src/Services/CreditsService.php +++ b/src/Services/CreditsService.php @@ -6,7 +6,6 @@ use Scrapegraphai\Client; use Scrapegraphai\Core\Exceptions\APIException; -use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\Credits\CreditGetResponse; use Scrapegraphai\RequestOptions; use Scrapegraphai\ServiceContracts\CreditsContract; @@ -23,28 +22,10 @@ public function __construct(private Client $client) {} * * Retrieve the current credit balance and usage for the authenticated user * - * @return CreditGetResponse - * * @throws APIException */ public function retrieve( ?RequestOptions $requestOptions = null - ): CreditGetResponse { - $params = []; - - return $this->retrieveRaw($params, $requestOptions); - } - - /** - * @api - * - * @return CreditGetResponse - * - * @throws APIException - */ - public function retrieveRaw( - mixed $params, - ?RequestOptions $requestOptions = null ): CreditGetResponse { // @phpstan-ignore-next-line; return $this->client->request( diff --git a/src/Services/FeedbackService.php b/src/Services/FeedbackService.php index 4f36e2a..58442c8 100644 --- a/src/Services/FeedbackService.php +++ b/src/Services/FeedbackService.php @@ -6,7 +6,6 @@ use Scrapegraphai\Client; use Scrapegraphai\Core\Exceptions\APIException; -use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\Feedback\FeedbackSubmitParams; use Scrapegraphai\Feedback\FeedbackSubmitResponse; use Scrapegraphai\RequestOptions; @@ -30,8 +29,6 @@ public function __construct(private Client $client) {} * @param string $requestID Request to provide feedback for * @param string|null $feedbackText Optional feedback comments * - * @return FeedbackSubmitResponse - * * @throws APIException */ public function submit( @@ -54,8 +51,6 @@ public function submit( * * @param array $params * - * @return FeedbackSubmitResponse - * * @throws APIException */ public function submitRaw( diff --git a/src/Services/GenerateSchemaService.php b/src/Services/GenerateSchemaService.php index ba93968..db55f39 100644 --- a/src/Services/GenerateSchemaService.php +++ b/src/Services/GenerateSchemaService.php @@ -6,7 +6,6 @@ use Scrapegraphai\Client; use Scrapegraphai\Core\Exceptions\APIException; -use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\GenerateSchema\GenerateSchemaCreateParams; use Scrapegraphai\GenerateSchema\GenerateSchemaGetResponse; use Scrapegraphai\GenerateSchema\GenerateSchemaGetResponse\CompletedSchemaGenerationResponse; @@ -33,8 +32,6 @@ public function __construct(private Client $client) {} * @param string $userPrompt Natural language description of desired schema * @param mixed $existingSchema Existing schema to modify or extend * - * @return GenerateSchemaNewResponse - * * @throws APIException */ public function create( @@ -54,8 +51,6 @@ public function create( * * @param array $params * - * @return GenerateSchemaNewResponse - * * @throws APIException */ public function createRaw( @@ -87,21 +82,6 @@ public function createRaw( public function retrieve( string $requestID, ?RequestOptions $requestOptions = null - ): CompletedSchemaGenerationResponse|FailedSchemaGenerationResponse { - $params = []; - - return $this->retrieveRaw($requestID, $params, $requestOptions); - } - - /** - * @api - * - * @throws APIException - */ - public function retrieveRaw( - string $requestID, - mixed $params, - ?RequestOptions $requestOptions = null ): CompletedSchemaGenerationResponse|FailedSchemaGenerationResponse { // @phpstan-ignore-next-line; return $this->client->request( diff --git a/src/Services/HealthzService.php b/src/Services/HealthzService.php index 494e3ba..4e7c81c 100644 --- a/src/Services/HealthzService.php +++ b/src/Services/HealthzService.php @@ -6,7 +6,6 @@ use Scrapegraphai\Client; use Scrapegraphai\Core\Exceptions\APIException; -use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\Healthz\HealthzCheckResponse; use Scrapegraphai\RequestOptions; use Scrapegraphai\ServiceContracts\HealthzContract; @@ -23,28 +22,10 @@ public function __construct(private Client $client) {} * * Check the health status of the service * - * @return HealthzCheckResponse - * * @throws APIException */ public function check( ?RequestOptions $requestOptions = null - ): HealthzCheckResponse { - $params = []; - - return $this->checkRaw($params, $requestOptions); - } - - /** - * @api - * - * @return HealthzCheckResponse - * - * @throws APIException - */ - public function checkRaw( - mixed $params, - ?RequestOptions $requestOptions = null ): HealthzCheckResponse { // @phpstan-ignore-next-line; return $this->client->request( diff --git a/src/Services/MarkdownifyService.php b/src/Services/MarkdownifyService.php index e584bb4..2cae2a8 100644 --- a/src/Services/MarkdownifyService.php +++ b/src/Services/MarkdownifyService.php @@ -6,7 +6,6 @@ use Scrapegraphai\Client; use Scrapegraphai\Core\Exceptions\APIException; -use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\Markdownify\CompletedMarkdownify; use Scrapegraphai\Markdownify\MarkdownifyConvertParams; use Scrapegraphai\Markdownify\MarkdownifyGetStatusResponse; @@ -32,8 +31,6 @@ public function __construct(private Client $client) {} * @param array $headers * @param list $steps Interaction steps before conversion * - * @return CompletedMarkdownify - * * @throws APIException */ public function convert( @@ -54,8 +51,6 @@ public function convert( * * @param array $params * - * @return CompletedMarkdownify - * * @throws APIException */ public function convertRaw( @@ -87,21 +82,6 @@ public function convertRaw( public function retrieveStatus( string $requestID, ?RequestOptions $requestOptions = null - ): CompletedMarkdownify|FailedMarkdownifyResponse { - $params = []; - - return $this->retrieveStatusRaw($requestID, $params, $requestOptions); - } - - /** - * @api - * - * @throws APIException - */ - public function retrieveStatusRaw( - string $requestID, - mixed $params, - ?RequestOptions $requestOptions = null ): CompletedMarkdownify|FailedMarkdownifyResponse { // @phpstan-ignore-next-line; return $this->client->request( diff --git a/src/Services/SearchscraperService.php b/src/Services/SearchscraperService.php index 9555281..69dc02a 100644 --- a/src/Services/SearchscraperService.php +++ b/src/Services/SearchscraperService.php @@ -6,7 +6,6 @@ use Scrapegraphai\Client; use Scrapegraphai\Core\Exceptions\APIException; -use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\RequestOptions; use Scrapegraphai\Searchscraper\CompletedSearchScraper; use Scrapegraphai\Searchscraper\SearchscraperCreateParams; @@ -34,8 +33,6 @@ public function __construct(private Client $client) {} * @param int $numResults Number of websites to scrape from search results * @param mixed $outputSchema JSON schema for structured output * - * @return CompletedSearchScraper - * * @throws APIException */ public function create( @@ -60,8 +57,6 @@ public function create( * * @param array $params * - * @return CompletedSearchScraper - * * @throws APIException */ public function createRaw( @@ -93,21 +88,6 @@ public function createRaw( public function retrieveStatus( string $requestID, ?RequestOptions $requestOptions = null - ): CompletedSearchScraper|FailedSearchScraperResponse { - $params = []; - - return $this->retrieveStatusRaw($requestID, $params, $requestOptions); - } - - /** - * @api - * - * @throws APIException - */ - public function retrieveStatusRaw( - string $requestID, - mixed $params, - ?RequestOptions $requestOptions = null ): CompletedSearchScraper|FailedSearchScraperResponse { // @phpstan-ignore-next-line; return $this->client->request( diff --git a/src/Services/SmartscraperService.php b/src/Services/SmartscraperService.php index b8ae2bd..2b5e079 100644 --- a/src/Services/SmartscraperService.php +++ b/src/Services/SmartscraperService.php @@ -6,7 +6,6 @@ use Scrapegraphai\Client; use Scrapegraphai\Core\Exceptions\APIException; -use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\RequestOptions; use Scrapegraphai\ServiceContracts\SmartscraperContract; use Scrapegraphai\Smartscraper\CompletedSmartscraper; @@ -41,8 +40,6 @@ public function __construct(private Client $client) {} * @param string $websiteHTML HTML content to process (max 2MB, mutually exclusive with website_url) * @param string $websiteURL URL to scrape (mutually exclusive with website_html) * - * @return CompletedSmartscraper - * * @throws APIException */ public function create( @@ -79,8 +76,6 @@ public function create( * * @param array $params * - * @return CompletedSmartscraper - * * @throws APIException */ public function createRaw( @@ -112,21 +107,6 @@ public function createRaw( public function retrieve( string $requestID, ?RequestOptions $requestOptions = null - ): CompletedSmartscraper|FailedSmartscraper { - $params = []; - - return $this->retrieveRaw($requestID, $params, $requestOptions); - } - - /** - * @api - * - * @throws APIException - */ - public function retrieveRaw( - string $requestID, - mixed $params, - ?RequestOptions $requestOptions = null ): CompletedSmartscraper|FailedSmartscraper { // @phpstan-ignore-next-line; return $this->client->request( @@ -146,20 +126,6 @@ public function retrieveRaw( */ public function list( ?RequestOptions $requestOptions = null - ): CompletedSmartscraper|FailedSmartscraper { - $params = []; - - return $this->listRaw($params, $requestOptions); - } - - /** - * @api - * - * @throws APIException - */ - public function listRaw( - mixed $params, - ?RequestOptions $requestOptions = null ): CompletedSmartscraper|FailedSmartscraper { // @phpstan-ignore-next-line; return $this->client->request( diff --git a/src/Services/ValidateService.php b/src/Services/ValidateService.php index ad5f862..c57a1e7 100644 --- a/src/Services/ValidateService.php +++ b/src/Services/ValidateService.php @@ -6,7 +6,6 @@ use Scrapegraphai\Client; use Scrapegraphai\Core\Exceptions\APIException; -use Scrapegraphai\Core\Implementation\HasRawResponse; use Scrapegraphai\RequestOptions; use Scrapegraphai\ServiceContracts\ValidateContract; use Scrapegraphai\Validate\ValidateAPIKeyResponse; @@ -23,28 +22,10 @@ public function __construct(private Client $client) {} * * Validate the API key and retrieve associated user email * - * @return ValidateAPIKeyResponse - * * @throws APIException */ public function apiKey( ?RequestOptions $requestOptions = null - ): ValidateAPIKeyResponse { - $params = []; - - return $this->apiKeyRaw($params, $requestOptions); - } - - /** - * @api - * - * @return ValidateAPIKeyResponse - * - * @throws APIException - */ - public function apiKeyRaw( - mixed $params, - ?RequestOptions $requestOptions = null ): ValidateAPIKeyResponse { // @phpstan-ignore-next-line; return $this->client->request( diff --git a/src/Smartscraper/CompletedSmartscraper.php b/src/Smartscraper/CompletedSmartscraper.php index fe6a4c4..4b8744f 100644 --- a/src/Smartscraper/CompletedSmartscraper.php +++ b/src/Smartscraper/CompletedSmartscraper.php @@ -6,7 +6,9 @@ use Scrapegraphai\Core\Attributes\Api; use Scrapegraphai\Core\Concerns\SdkModel; +use Scrapegraphai\Core\Concerns\SdkResponse; use Scrapegraphai\Core\Contracts\BaseModel; +use Scrapegraphai\Core\Conversion\Contracts\ResponseConverter; use Scrapegraphai\Smartscraper\CompletedSmartscraper\Status; /** @@ -18,16 +20,14 @@ * userPrompt?: string, * websiteURL?: string|null, * } - * When used in a response, this type parameter can define a $rawResponse property. - * @template TRawResponse of object = object{} - * - * @mixin TRawResponse */ -final class CompletedSmartscraper implements BaseModel +final class CompletedSmartscraper implements BaseModel, ResponseConverter { /** @use SdkModel */ use SdkModel; + use SdkResponse; + /** * Error message (empty on success). */ @@ -85,7 +85,7 @@ public static function with( null !== $error && $obj->error = $error; null !== $requestID && $obj->requestID = $requestID; null !== $result && $obj->result = $result; - null !== $status && $obj->status = $status instanceof Status ? $status->value : $status; + null !== $status && $obj['status'] = $status; null !== $userPrompt && $obj->userPrompt = $userPrompt; null !== $websiteURL && $obj->websiteURL = $websiteURL; @@ -133,7 +133,7 @@ public function withResult(mixed $result): self public function withStatus(Status|string $status): self { $obj = clone $this; - $obj->status = $status instanceof Status ? $status->value : $status; + $obj['status'] = $status; return $obj; } diff --git a/src/Smartscraper/FailedSmartscraper.php b/src/Smartscraper/FailedSmartscraper.php index 0198cd7..2f4ee5e 100644 --- a/src/Smartscraper/FailedSmartscraper.php +++ b/src/Smartscraper/FailedSmartscraper.php @@ -71,7 +71,7 @@ public static function with( null !== $error && $obj->error = $error; null !== $requestID && $obj->requestID = $requestID; null !== $result && $obj->result = $result; - null !== $status && $obj->status = $status instanceof Status ? $status->value : $status; + null !== $status && $obj['status'] = $status; null !== $userPrompt && $obj->userPrompt = $userPrompt; null !== $websiteURL && $obj->websiteURL = $websiteURL; @@ -111,7 +111,7 @@ public function withResult(mixed $result): self public function withStatus(Status|string $status): self { $obj = clone $this; - $obj->status = $status instanceof Status ? $status->value : $status; + $obj['status'] = $status; return $obj; } diff --git a/src/Validate/ValidateAPIKeyResponse.php b/src/Validate/ValidateAPIKeyResponse.php index 6462292..9d2e350 100644 --- a/src/Validate/ValidateAPIKeyResponse.php +++ b/src/Validate/ValidateAPIKeyResponse.php @@ -6,20 +6,20 @@ use Scrapegraphai\Core\Attributes\Api; use Scrapegraphai\Core\Concerns\SdkModel; +use Scrapegraphai\Core\Concerns\SdkResponse; use Scrapegraphai\Core\Contracts\BaseModel; +use Scrapegraphai\Core\Conversion\Contracts\ResponseConverter; /** * @phpstan-type validate_api_key_response = array{email?: string} - * When used in a response, this type parameter can define a $rawResponse property. - * @template TRawResponse of object = object{} - * - * @mixin TRawResponse */ -final class ValidateAPIKeyResponse implements BaseModel +final class ValidateAPIKeyResponse implements BaseModel, ResponseConverter { /** @use SdkModel */ use SdkModel; + use SdkResponse; + #[Api(optional: true)] public ?string $email; From 1eb0061420ae6088f5d3323fd46a80a9c12d2783 Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Thu, 9 Oct 2025 02:25:10 +0000 Subject: [PATCH 42/44] fix(ci): release doctor workflow --- .github/workflows/release-doctor.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/release-doctor.yml b/.github/workflows/release-doctor.yml index 22b3397..aeb54bd 100644 --- a/.github/workflows/release-doctor.yml +++ b/.github/workflows/release-doctor.yml @@ -17,3 +17,6 @@ jobs: - name: Check release environment run: | bash ./bin/check-release-environment + env: + PACKAGIST_USERNAME: ${{ secrets.SCRAPEGRAPHAI_PACKAGIST_USERNAME || secrets.PACKAGIST_USERNAME }} + PACKAGIST_SAFE_KEY: ${{ secrets.SCRAPEGRAPHAI_PACKAGIST_SAFE_KEY || secrets.PACKAGIST_SAFE_KEY }} From d3fcfede9458ad6d62b76363dec9d87e8a4d0469 Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Sat, 11 Oct 2025 02:09:38 +0000 Subject: [PATCH 43/44] chore: add license --- composer.json | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index daca4bc..8a62463 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,11 @@ { "$schema": "https://getcomposer.org/schema.json", + "license": "Apache-2.0", "autoload": { - "files": ["src/Core.php", "src/Client.php"], + "files": [ + "src/Core.php", + "src/Client.php" + ], "psr-4": { "Scrapegraphai\\": "src/" } From 3ac17e299b270b4383ffe8522b9c22d0daddf3e8 Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Sat, 11 Oct 2025 02:10:01 +0000 Subject: [PATCH 44/44] release: 0.1.0 --- .release-please-manifest.json | 2 +- CHANGELOG.md | 70 +++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 CHANGELOG.md diff --git a/.release-please-manifest.json b/.release-please-manifest.json index 1332969..3d2ac0b 100644 --- a/.release-please-manifest.json +++ b/.release-please-manifest.json @@ -1,3 +1,3 @@ { - ".": "0.0.1" + ".": "0.1.0" } \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..cbc5a86 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,70 @@ +# Changelog + +## 0.1.0 (2025-10-11) + +Full Changelog: [v0.0.1...v0.1.0](https://github.com/ScrapeGraphAI/scrapegraphai-php/compare/v0.0.1...v0.1.0) + +### ⚠ BREAKING CHANGES + +* expose services and service contracts +* use builders for RequestOptions +* rename errors to exceptions +* pagination field rename, and basic streaming docs +* **refactor:** namespacing cleanup +* **refactor:** clean up pagination, errors, as well as request methods + +### Features + +* add files ([adb926e](https://github.com/ScrapeGraphAI/scrapegraphai-php/commit/adb926eb9cfb18594355fa39f642a41a76af19a8)) +* add files ([f728b05](https://github.com/ScrapeGraphAI/scrapegraphai-php/commit/f728b05e4bcd0b29355c77cf529d2f18b5cbce11)) +* **client:** add raw methods ([2c62976](https://github.com/ScrapeGraphAI/scrapegraphai-php/commit/2c62976824aa8bc2b4119990a4253bd5918852a3)) +* **client:** add streaming ([d191c29](https://github.com/ScrapeGraphAI/scrapegraphai-php/commit/d191c29f3e1889640e16911918041bd3850fd4e8)) +* **client:** improve error handling ([b3eab16](https://github.com/ScrapeGraphAI/scrapegraphai-php/commit/b3eab16fb85eafac7eba187d23d9fc9a5678fd15)) +* **client:** support raw responses ([7ea4904](https://github.com/ScrapeGraphAI/scrapegraphai-php/commit/7ea49045827081bda452412b5ef967924b61cd76)) +* **client:** use named parameters in methods ([2a938ad](https://github.com/ScrapeGraphAI/scrapegraphai-php/commit/2a938ad3e8e2658d53813570165fca69ebca554d)) +* **client:** use real enums ([196fd29](https://github.com/ScrapeGraphAI/scrapegraphai-php/commit/196fd2985d799749f11af3ef4df7e29f30a4455f)) +* **client:** use with for constructors ([d00f77a](https://github.com/ScrapeGraphAI/scrapegraphai-php/commit/d00f77a024ea0faa3f08b2fcdcac387b5c248c1a)) +* ensure `->toArray()` benefits from structural typing ([205ab97](https://github.com/ScrapeGraphAI/scrapegraphai-php/commit/205ab97f9f2bc8ca38e9847850163701827777dc)) +* expose services and service contracts ([fcb58d5](https://github.com/ScrapeGraphAI/scrapegraphai-php/commit/fcb58d5574acc36d7c2e0e41c2b973c7b6ff1613)) +* pagination field rename, and basic streaming docs ([d79ffc9](https://github.com/ScrapeGraphAI/scrapegraphai-php/commit/d79ffc9ea439c191f0ffed61f2752e7bfe47b00c)) +* **php:** differentiate null and omit ([f19a57a](https://github.com/ScrapeGraphAI/scrapegraphai-php/commit/f19a57af690dba72f269a940930da3efd35d4b2c)) +* **php:** rename internal types ([c55bbd7](https://github.com/ScrapeGraphAI/scrapegraphai-php/commit/c55bbd74aa39be1cf5ab1997fa140d4e92f96de3)) +* **refactor:** clean up pagination, errors, as well as request methods ([7135820](https://github.com/ScrapeGraphAI/scrapegraphai-php/commit/713582082c5123e8152bbdb5896926ae58496095)) +* **refactor:** namespacing cleanup ([8c75ed0](https://github.com/ScrapeGraphAI/scrapegraphai-php/commit/8c75ed0c279a15e9051ac891e69a5fa2c9e7be30)) +* rename errors to exceptions ([b568600](https://github.com/ScrapeGraphAI/scrapegraphai-php/commit/b568600fb25d39cbd315e98dc8654360383523fe)) +* use builders for RequestOptions ([c2300dc](https://github.com/ScrapeGraphAI/scrapegraphai-php/commit/c2300dcdc4d9370eb060b04ee25601572821f59c)) + + +### Bug Fixes + +* add create release workflow ([7361bd6](https://github.com/ScrapeGraphAI/scrapegraphai-php/commit/7361bd6438c71f72ca9e11290c008beeb04e390a)) +* basic pagination should work ([ecebf37](https://github.com/ScrapeGraphAI/scrapegraphai-php/commit/ecebf37bdf9a4b6ede506820789805c8d4ae4f9e)) +* **ci:** release doctor workflow ([1eb0061](https://github.com/ScrapeGraphAI/scrapegraphai-php/commit/1eb0061420ae6088f5d3323fd46a80a9c12d2783)) +* **client:** elide null named parameters ([7297553](https://github.com/ScrapeGraphAI/scrapegraphai-php/commit/7297553957c8444178ca2dce6c0c89fd743a0a1e)) +* minor bugs ([d905655](https://github.com/ScrapeGraphAI/scrapegraphai-php/commit/d90565586eb26ccf73f67961856133e9c0bc90fd)) +* remove inaccurate `license` field in composer.json ([1cfba5f](https://github.com/ScrapeGraphAI/scrapegraphai-php/commit/1cfba5fa543a792ecf6651259309247fa3ce726b)) +* streaming internals ([2516566](https://github.com/ScrapeGraphAI/scrapegraphai-php/commit/25165664e3d3c7fd4f11f72589f89baa1305eb56)) + + +### Chores + +* add additional php doc tags ([3a18c70](https://github.com/ScrapeGraphAI/scrapegraphai-php/commit/3a18c70daa9fa2d26264429cddd2b092bfcd5473)) +* add license ([d3fcfed](https://github.com/ScrapeGraphAI/scrapegraphai-php/commit/d3fcfede9458ad6d62b76363dec9d87e8a4d0469)) +* cleanup streaming ([372bf28](https://github.com/ScrapeGraphAI/scrapegraphai-php/commit/372bf283cdd910023fd38ecbfb16570870450afd)) +* configure new SDK language ([57c78ba](https://github.com/ScrapeGraphAI/scrapegraphai-php/commit/57c78ba863893bb956cfc44b9a34037a52d7d4cf)) +* **docs:** update readme formatting ([ecf153b](https://github.com/ScrapeGraphAI/scrapegraphai-php/commit/ecf153b12a488ad416d25e3b494e523ffd08f2c8)) +* document parameter object usage ([707cff0](https://github.com/ScrapeGraphAI/scrapegraphai-php/commit/707cff08ced7c6f1da574965a81e4b30a531b391)) +* fix lints in UnionOf ([5e8ce6f](https://github.com/ScrapeGraphAI/scrapegraphai-php/commit/5e8ce6f7137f9ce2a2bf3fce709cd363f4b8c196)) +* improve model annotations ([585d282](https://github.com/ScrapeGraphAI/scrapegraphai-php/commit/585d282dfdc9c34081e6cb44c0c83256037be12f)) +* **internal:** refactor base client internals ([ec19244](https://github.com/ScrapeGraphAI/scrapegraphai-php/commit/ec1924477e3428e9bc066a7ef7473084f06a768c)) +* **internal:** refactored internal codepaths ([d3577db](https://github.com/ScrapeGraphAI/scrapegraphai-php/commit/d3577db6255af45ab790915545f4b8044e90c921)) +* intuitively order union types ([0cd048d](https://github.com/ScrapeGraphAI/scrapegraphai-php/commit/0cd048d4ce590110cdf01edc1649d4ef2e707a6d)) +* make more targeted phpstan ignores ([c33963e](https://github.com/ScrapeGraphAI/scrapegraphai-php/commit/c33963e7a8f9f8fbd461ca989e027974da1e8476)) +* readme improvements ([c38da93](https://github.com/ScrapeGraphAI/scrapegraphai-php/commit/c38da939ecdc0ee7a092a58faf41e63a92b61b11)) +* refactor methods ([ddd88d8](https://github.com/ScrapeGraphAI/scrapegraphai-php/commit/ddd88d881ca5924d6d2f25e6ae4e4656e7ff5722)) +* refactor request options ([3898066](https://github.com/ScrapeGraphAI/scrapegraphai-php/commit/389806624293626a1246486efa870095a959eca5)) +* **refactor:** simplify base page interface ([77b2f76](https://github.com/ScrapeGraphAI/scrapegraphai-php/commit/77b2f7692824f3d6d7ff151704174cfe550b1339)) +* remove `php-http/multipart-stream-builder` as a required dependency ([03a02b4](https://github.com/ScrapeGraphAI/scrapegraphai-php/commit/03a02b472282af3977cab5be1296edee8a8750ce)) +* remove type aliases ([586c954](https://github.com/ScrapeGraphAI/scrapegraphai-php/commit/586c9548945e0522a71f888803bc3c9e7804ccba)) +* simplify model initialization ([4a166fc](https://github.com/ScrapeGraphAI/scrapegraphai-php/commit/4a166fcc6584323b68d900e6809453a8d0eb1bcd)) +* update SDK settings ([f566b27](https://github.com/ScrapeGraphAI/scrapegraphai-php/commit/f566b27a274db61218c98bc775642fec51799888))