Skip to content

Commit f31c78e

Browse files
author
Mohamed Khaled
committed
feature/support-request-options
1 parent 5220773 commit f31c78e

File tree

11 files changed

+826
-29
lines changed

11 files changed

+826
-29
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,22 @@ $imageFile = AiClient::prompt('Generate an illustration of the PHP elephant in t
7878

7979
See the [`PromptBuilder` class](https://github.com/WordPress/php-ai-client/blob/trunk/src/Builders/PromptBuilder.php) and its public methods for all the ways you can configure the prompt.
8080

81+
### Configuring request options
82+
83+
You can configure HTTP transport options like timeout and maximum redirects using the `RequestOptions` DTO:
84+
85+
```php
86+
use WordPress\AiClient\Providers\Http\DTO\RequestOptions;
87+
88+
// Set custom timeout for long-running requests
89+
$options = new RequestOptions(120, 10);
90+
91+
// Or use defaults and modify
92+
$options = RequestOptions::defaults()->withTimeout(60);
93+
```
94+
95+
For implementation ideas in different environments (WordPress, Guzzle, cURL), check the transporter-specific examples in the SDK source and tests.
96+
8197
**More documentation is coming soon.**
8298

8399
## Further reading

src/Providers/Http/DTO/Request.php

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
* method: string,
2424
* uri: string,
2525
* headers: array<string, list<string>>,
26-
* body?: string|null
26+
* body?: string|null,
27+
* options?: array<string, mixed>
2728
* }
2829
*
2930
* @extends AbstractDataTransferObject<RequestArrayShape>
@@ -34,6 +35,7 @@ class Request extends AbstractDataTransferObject
3435
public const KEY_URI = 'uri';
3536
public const KEY_HEADERS = 'headers';
3637
public const KEY_BODY = 'body';
38+
public const KEY_OPTIONS = 'options';
3739

3840
/**
3941
* @var HttpMethodEnum The HTTP method.
@@ -60,6 +62,11 @@ class Request extends AbstractDataTransferObject
6062
*/
6163
protected ?string $body = null;
6264

65+
/**
66+
* @var RequestOptions|null The request options.
67+
*/
68+
protected ?RequestOptions $options = null;
69+
6370
/**
6471
* Constructor.
6572
*
@@ -69,18 +76,25 @@ class Request extends AbstractDataTransferObject
6976
* @param string $uri The request URI.
7077
* @param array<string, string|list<string>> $headers The request headers.
7178
* @param string|array<string, mixed>|null $data The request data.
79+
* @param RequestOptions|null $options The request options.
7280
*
7381
* @throws InvalidArgumentException If the URI is empty.
7482
*/
75-
public function __construct(HttpMethodEnum $method, string $uri, array $headers = [], $data = null)
76-
{
83+
public function __construct(
84+
HttpMethodEnum $method,
85+
string $uri,
86+
array $headers = [],
87+
$data = null,
88+
?RequestOptions $options = null
89+
) {
7790
if (empty($uri)) {
7891
throw new InvalidArgumentException('URI cannot be empty.');
7992
}
8093

8194
$this->method = $method;
8295
$this->uri = $uri;
8396
$this->headers = new HeadersCollection($headers);
97+
$this->options = $options;
8498

8599
// Separate data and body based on type
86100
if (is_string($data)) {
@@ -281,6 +295,33 @@ public function getData(): ?array
281295
return $this->data;
282296
}
283297

298+
/**
299+
* Gets the request options.
300+
*
301+
* @since n.e.x.t
302+
*
303+
* @return RequestOptions|null The request options or null if not set.
304+
*/
305+
public function getOptions(): ?RequestOptions
306+
{
307+
return $this->options;
308+
}
309+
310+
/**
311+
* Returns a new instance with the specified options.
312+
*
313+
* @since n.e.x.t
314+
*
315+
* @param RequestOptions|null $options The request options.
316+
* @return self A new instance with the options.
317+
*/
318+
public function withOptions(?RequestOptions $options): self
319+
{
320+
$new = clone $this;
321+
$new->options = $options;
322+
return $new;
323+
}
324+
284325
/**
285326
* {@inheritDoc}
286327
*
@@ -311,6 +352,10 @@ public static function getJsonSchema(): array
311352
'type' => ['string'],
312353
'description' => 'The request body.',
313354
],
355+
self::KEY_OPTIONS => [
356+
'type' => 'object',
357+
'description' => 'The request options.',
358+
],
314359
],
315360
'required' => [self::KEY_METHOD, self::KEY_URI, self::KEY_HEADERS],
316361
];
@@ -337,6 +382,11 @@ public function toArray(): array
337382
$array[self::KEY_BODY] = $body;
338383
}
339384

385+
// Include options if present
386+
if ($this->options !== null) {
387+
$array[self::KEY_OPTIONS] = $this->options->toArray();
388+
}
389+
340390
return $array;
341391
}
342392

@@ -349,11 +399,19 @@ public static function fromArray(array $array): self
349399
{
350400
static::validateFromArrayData($array, [self::KEY_METHOD, self::KEY_URI, self::KEY_HEADERS]);
351401

402+
$options = null;
403+
if (isset($array[self::KEY_OPTIONS]) && is_array($array[self::KEY_OPTIONS])) {
404+
/** @var array{timeout?: int|null, max_redirects?: int|null} $optionsArray */
405+
$optionsArray = $array[self::KEY_OPTIONS];
406+
$options = RequestOptions::fromArray($optionsArray);
407+
}
408+
352409
return new self(
353410
HttpMethodEnum::from($array[self::KEY_METHOD]),
354411
$array[self::KEY_URI],
355412
$array[self::KEY_HEADERS] ?? [],
356-
$array[self::KEY_BODY] ?? null
413+
$array[self::KEY_BODY] ?? null,
414+
$options
357415
);
358416
}
359417

0 commit comments

Comments
 (0)