Skip to content

Commit 7297553

Browse files
fix(client): elide null named parameters
1 parent c55bbd7 commit 7297553

18 files changed

+138
-91
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ $client = new Client(
5555
$completedSmartscraper = $client->smartscraper->create(
5656
userPrompt: "Extract the product name, price, and description"
5757
);
58-
5958
var_dump($completedSmartscraper->request_id);
6059
```
6160

src/Client.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
namespace Scrapegraphai;
66

77
use Scrapegraphai\Core\BaseClient;
8-
use Scrapegraphai\Crawl\CrawlService;
9-
use Scrapegraphai\Credits\CreditsService;
10-
use Scrapegraphai\Feedback\FeedbackService;
11-
use Scrapegraphai\GenerateSchema\GenerateSchemaService;
12-
use Scrapegraphai\Healthz\HealthzService;
13-
use Scrapegraphai\Markdownify\MarkdownifyService;
14-
use Scrapegraphai\Searchscraper\SearchscraperService;
15-
use Scrapegraphai\Smartscraper\SmartscraperService;
16-
use Scrapegraphai\Validate\ValidateService;
8+
use Scrapegraphai\Services\CrawlService;
9+
use Scrapegraphai\Services\CreditsService;
10+
use Scrapegraphai\Services\FeedbackService;
11+
use Scrapegraphai\Services\GenerateSchemaService;
12+
use Scrapegraphai\Services\HealthzService;
13+
use Scrapegraphai\Services\MarkdownifyService;
14+
use Scrapegraphai\Services\SearchscraperService;
15+
use Scrapegraphai\Services\SmartscraperService;
16+
use Scrapegraphai\Services\ValidateService;
1717

1818
class Client extends BaseClient
1919
{

src/Core/Util.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,23 @@ public static function decodeContent(MessageInterface $rsp): mixed
349349
return self::streamIterator($body);
350350
}
351351

352+
/**
353+
* @param array<string, mixed> $arr
354+
* @param list<string> $keys
355+
*
356+
* @return array<string, mixed>
357+
*/
358+
public static function array_filter_null(array $arr, array $keys): array
359+
{
360+
foreach ($keys as $key) {
361+
if (array_key_exists($key, $arr) && is_null($arr[$key])) {
362+
unset($arr[$key]);
363+
}
364+
}
365+
366+
return $arr;
367+
}
368+
352369
/**
353370
* @param list<callable> $closing
354371
*

src/Crawl/CrawlService.php renamed to src/Services/CrawlService.php

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
declare(strict_types=1);
44

5-
namespace Scrapegraphai\Crawl;
5+
namespace Scrapegraphai\Services;
66

77
use Scrapegraphai\Client;
88
use Scrapegraphai\Contracts\CrawlContract;
99
use Scrapegraphai\Core\Conversion;
10+
use Scrapegraphai\Core\Util;
11+
use Scrapegraphai\Crawl\CrawlStartParams;
1012
use Scrapegraphai\Crawl\CrawlStartParams\Rules;
1113
use Scrapegraphai\RequestOptions;
1214
use Scrapegraphai\Responses\Crawl\CrawlGetResultsResponse;
@@ -60,19 +62,33 @@ public function start(
6062
$sitemap = null,
6163
?RequestOptions $requestOptions = null,
6264
): CrawlStartResponse {
63-
[$parsed, $options] = CrawlStartParams::parseRequest(
65+
$args = [
66+
'url' => $url,
67+
'depth' => $depth,
68+
'extractionMode' => $extractionMode,
69+
'maxPages' => $maxPages,
70+
'prompt' => $prompt,
71+
'renderHeavyJs' => $renderHeavyJs,
72+
'rules' => $rules,
73+
'schema' => $schema,
74+
'sitemap' => $sitemap,
75+
];
76+
$args = Util::array_filter_null(
77+
$args,
6478
[
65-
'url' => $url,
66-
'depth' => $depth,
67-
'extractionMode' => $extractionMode,
68-
'maxPages' => $maxPages,
69-
'prompt' => $prompt,
70-
'renderHeavyJs' => $renderHeavyJs,
71-
'rules' => $rules,
72-
'schema' => $schema,
73-
'sitemap' => $sitemap,
79+
'depth',
80+
'extractionMode',
81+
'maxPages',
82+
'prompt',
83+
'renderHeavyJs',
84+
'rules',
85+
'schema',
86+
'sitemap',
7487
],
75-
$requestOptions,
88+
);
89+
[$parsed, $options] = CrawlStartParams::parseRequest(
90+
$args,
91+
$requestOptions
7692
);
7793
$resp = $this->client->request(
7894
method: 'post',

src/Credits/CreditsService.php renamed to src/Services/CreditsService.php

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

33
declare(strict_types=1);
44

5-
namespace Scrapegraphai\Credits;
5+
namespace Scrapegraphai\Services;
66

77
use Scrapegraphai\Client;
88
use Scrapegraphai\Contracts\CreditsContract;

src/Feedback/FeedbackService.php renamed to src/Services/FeedbackService.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
declare(strict_types=1);
44

5-
namespace Scrapegraphai\Feedback;
5+
namespace Scrapegraphai\Services;
66

77
use Scrapegraphai\Client;
88
use Scrapegraphai\Contracts\FeedbackContract;
99
use Scrapegraphai\Core\Conversion;
10+
use Scrapegraphai\Core\Util;
11+
use Scrapegraphai\Feedback\FeedbackSubmitParams;
1012
use Scrapegraphai\RequestOptions;
1113
use Scrapegraphai\Responses\Feedback\FeedbackSubmitResponse;
1214

@@ -27,13 +29,15 @@ public function submit(
2729
$feedbackText = null,
2830
?RequestOptions $requestOptions = null,
2931
): FeedbackSubmitResponse {
32+
$args = [
33+
'rating' => $rating,
34+
'requestID' => $requestID,
35+
'feedbackText' => $feedbackText,
36+
];
37+
$args = Util::array_filter_null($args, ['feedbackText']);
3038
[$parsed, $options] = FeedbackSubmitParams::parseRequest(
31-
[
32-
'rating' => $rating,
33-
'requestID' => $requestID,
34-
'feedbackText' => $feedbackText,
35-
],
36-
$requestOptions,
39+
$args,
40+
$requestOptions
3741
);
3842
$resp = $this->client->request(
3943
method: 'post',

src/GenerateSchema/GenerateSchemaService.php renamed to src/Services/GenerateSchemaService.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
declare(strict_types=1);
44

5-
namespace Scrapegraphai\GenerateSchema;
5+
namespace Scrapegraphai\Services;
66

77
use Scrapegraphai\Client;
88
use Scrapegraphai\Contracts\GenerateSchemaContract;
99
use Scrapegraphai\Core\Conversion;
10+
use Scrapegraphai\Core\Util;
11+
use Scrapegraphai\GenerateSchema\GenerateSchemaCreateParams;
1012
use Scrapegraphai\RequestOptions;
1113
use Scrapegraphai\Responses\GenerateSchema\GenerateSchemaGetResponse;
1214
use Scrapegraphai\Responses\GenerateSchema\GenerateSchemaGetResponse\CompletedSchemaGenerationResponse;
@@ -29,9 +31,11 @@ public function create(
2931
$existingSchema = null,
3032
?RequestOptions $requestOptions = null
3133
): GenerateSchemaNewResponse {
34+
$args = ['userPrompt' => $userPrompt, 'existingSchema' => $existingSchema];
35+
$args = Util::array_filter_null($args, ['existingSchema']);
3236
[$parsed, $options] = GenerateSchemaCreateParams::parseRequest(
33-
['userPrompt' => $userPrompt, 'existingSchema' => $existingSchema],
34-
$requestOptions,
37+
$args,
38+
$requestOptions
3539
);
3640
$resp = $this->client->request(
3741
method: 'post',

src/Healthz/HealthzService.php renamed to src/Services/HealthzService.php

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

33
declare(strict_types=1);
44

5-
namespace Scrapegraphai\Healthz;
5+
namespace Scrapegraphai\Services;
66

77
use Scrapegraphai\Client;
88
use Scrapegraphai\Contracts\HealthzContract;

src/Markdownify/MarkdownifyService.php renamed to src/Services/MarkdownifyService.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
declare(strict_types=1);
44

5-
namespace Scrapegraphai\Markdownify;
5+
namespace Scrapegraphai\Services;
66

77
use Scrapegraphai\Client;
88
use Scrapegraphai\Contracts\MarkdownifyContract;
99
use Scrapegraphai\Core\Conversion;
10+
use Scrapegraphai\Core\Util;
11+
use Scrapegraphai\Markdownify\CompletedMarkdownify;
12+
use Scrapegraphai\Markdownify\MarkdownifyConvertParams;
1013
use Scrapegraphai\RequestOptions;
1114
use Scrapegraphai\Responses\Markdownify\MarkdownifyGetStatusResponse;
1215
use Scrapegraphai\Responses\Markdownify\MarkdownifyGetStatusResponse\FailedMarkdownifyResponse;
@@ -28,9 +31,13 @@ public function convert(
2831
$steps = null,
2932
?RequestOptions $requestOptions = null,
3033
): CompletedMarkdownify {
34+
$args = [
35+
'websiteURL' => $websiteURL, 'headers' => $headers, 'steps' => $steps,
36+
];
37+
$args = Util::array_filter_null($args, ['headers', 'steps']);
3138
[$parsed, $options] = MarkdownifyConvertParams::parseRequest(
32-
['websiteURL' => $websiteURL, 'headers' => $headers, 'steps' => $steps],
33-
$requestOptions,
39+
$args,
40+
$requestOptions
3441
);
3542
$resp = $this->client->request(
3643
method: 'post',

src/Searchscraper/SearchscraperService.php renamed to src/Services/SearchscraperService.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22

33
declare(strict_types=1);
44

5-
namespace Scrapegraphai\Searchscraper;
5+
namespace Scrapegraphai\Services;
66

77
use Scrapegraphai\Client;
88
use Scrapegraphai\Contracts\SearchscraperContract;
99
use Scrapegraphai\Core\Conversion;
10+
use Scrapegraphai\Core\Util;
1011
use Scrapegraphai\RequestOptions;
1112
use Scrapegraphai\Responses\Searchscraper\SearchscraperGetStatusResponse;
1213
use Scrapegraphai\Responses\Searchscraper\SearchscraperGetStatusResponse\FailedSearchScraperResponse;
14+
use Scrapegraphai\Searchscraper\CompletedSearchScraper;
15+
use Scrapegraphai\Searchscraper\SearchscraperCreateParams;
1316

1417
final class SearchscraperService implements SearchscraperContract
1518
{
@@ -31,14 +34,19 @@ public function create(
3134
$outputSchema = null,
3235
?RequestOptions $requestOptions = null,
3336
): CompletedSearchScraper {
37+
$args = [
38+
'userPrompt' => $userPrompt,
39+
'headers' => $headers,
40+
'numResults' => $numResults,
41+
'outputSchema' => $outputSchema,
42+
];
43+
$args = Util::array_filter_null(
44+
$args,
45+
['headers', 'numResults', 'outputSchema']
46+
);
3447
[$parsed, $options] = SearchscraperCreateParams::parseRequest(
35-
[
36-
'userPrompt' => $userPrompt,
37-
'headers' => $headers,
38-
'numResults' => $numResults,
39-
'outputSchema' => $outputSchema,
40-
],
41-
$requestOptions,
48+
$args,
49+
$requestOptions
4250
);
4351
$resp = $this->client->request(
4452
method: 'post',

0 commit comments

Comments
 (0)