Skip to content

Commit b176ad5

Browse files
feat!: use builders for RequestOptions
1 parent c211316 commit b176ad5

File tree

11 files changed

+168
-84
lines changed

11 files changed

+168
-84
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ Certain errors will be automatically retried 2 times by default, with a short ex
157157

158158
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.
159159

160-
You can use the `max_retries` option to configure or disable this:
160+
You can use the `maxRetries` option to configure or disable this:
161161

162162
```php
163163
<?php
@@ -175,7 +175,7 @@ $result = $client->messages->create(
175175
maxTokens: 1024,
176176
messages: [MessageParam::with(role: "user", content: "Hello, Claude")],
177177
model: "claude-sonnet-4-20250514",
178-
new RequestOptions(maxRetries: 5),
178+
requestOptions: RequestOptions::with(maxRetries: 5),
179179
);
180180
```
181181

@@ -187,7 +187,7 @@ $result = $client->messages->create(
187187

188188
You can send undocumented parameters to any endpoint, and read undocumented response properties, like so:
189189

190-
Note: the `extra_` parameters of the same name overrides the documented parameters.
190+
Note: the `extra*` parameters of the same name overrides the documented parameters.
191191

192192
```php
193193
<?php
@@ -199,7 +199,7 @@ $message = $client->messages->create(
199199
maxTokens: 1024,
200200
messages: [MessageParam::with(role: "user", content: "Hello, Claude")],
201201
model: "claude-sonnet-4-20250514",
202-
new RequestOptions(
202+
requestOptions: RequestOptions::with(
203203
extraQueryParams: ["my_query_parameter" => "value"],
204204
extraBodyParams: ["my_body_parameter" => "value"],
205205
extraHeaders: ["my-header" => "value"],

src/Client.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function __construct(
4444
'ANTHROPIC_BASE_URL'
4545
) ?: 'https://api.anthropic.com';
4646

47-
$options = new RequestOptions(
47+
$options = RequestOptions::with(
4848
uriFactory: Psr17FactoryDiscovery::findUriFactory(),
4949
streamFactory: Psr17FactoryDiscovery::findStreamFactory(),
5050
requestFactory: Psr17FactoryDiscovery::findRequestFactory(),

src/Core.php

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

55
namespace Anthropic\Core;
66

7-
use Anthropic\Core\Implementation\Omittable;
7+
use Anthropic\Core\Implementation\Omit;
88

9-
const OMIT = Omittable::OMIT;
9+
const OMIT = Omit::omit;

src/Core/BaseClient.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class BaseClient
3232
protected UriInterface $baseUrl;
3333

3434
/**
35+
* @internal
36+
*
3537
* @param array<string, string|int|list<string|int>|null> $headers
3638
*/
3739
public function __construct(
@@ -108,6 +110,8 @@ protected function authHeaders(): array
108110
}
109111

110112
/**
113+
* @internal
114+
*
111115
* @param string|list<string> $path
112116
* @param array<string, mixed> $query
113117
* @param array<string, string|int|list<string|int>|null> $headers
@@ -135,26 +139,31 @@ protected function buildRequest(
135139
mixed $body,
136140
RequestOptions|array|null $opts,
137141
): array {
138-
$opts = array_merge($this->options->toArray(), RequestOptions::parse($opts)->toArray());
139-
$options = new RequestOptions(...$opts);
142+
$options = RequestOptions::parse($this->options, $opts);
140143

141144
$parsedPath = Util::parsePath($path);
142145

143146
/** @var array<string, mixed> $mergedQuery */
144-
$mergedQuery = array_merge_recursive($query, $options->extraQueryParams);
147+
$mergedQuery = array_merge_recursive(
148+
$query,
149+
$options->extraQueryParams ?? [],
150+
);
145151
$uri = Util::joinUri($this->baseUrl, path: $parsedPath, query: $mergedQuery)->__toString();
146152

147153
/** @var array<string, string|list<string>|null> $mergedHeaders */
148154
$mergedHeaders = [...$this->headers,
149155
...$this->authHeaders(),
150156
...$headers,
151-
...$options->extraHeaders, ];
157+
...($options->extraHeaders ?? []), ];
152158

153159
$req = ['method' => strtoupper($method), 'path' => $uri, 'query' => $mergedQuery, 'headers' => $mergedHeaders, 'body' => $body];
154160

155161
return [$req, $options];
156162
}
157163

164+
/**
165+
* @internal
166+
*/
158167
protected function followRedirect(
159168
ResponseInterface $rsp,
160169
RequestInterface $req
@@ -170,6 +179,8 @@ protected function followRedirect(
170179
}
171180

172181
/**
182+
* @internal
183+
*
173184
* @param bool|int|float|string|resource|\Traversable<mixed>|array<string,
174185
* mixed,>|null $data
175186
*/

src/Core/Concerns/SdkParams.php

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,7 @@ trait SdkParams
1818
* @param array<string, mixed>|self|null $params
1919
* @param array<string, mixed>|RequestOptions|null $options
2020
*
21-
* @return array{array<string, mixed>, array{
22-
* timeout: float,
23-
* maxRetries: int,
24-
* initialRetryDelay: float,
25-
* maxRetryDelay: float,
26-
* extraHeaders: list<string>,
27-
* extraQueryParams: list<string>,
28-
* extraBodyParams: list<string>,
29-
* }}
21+
* @return array{array<string, mixed>, RequestOptions}
3022
*/
3123
public static function parseRequest(array|self|null $params, array|RequestOptions|null $options): array
3224
{
@@ -40,17 +32,6 @@ public static function parseRequest(array|self|null $params, array|RequestOption
4032
$opts->maxRetries = 0;
4133
}
4234

43-
$opt = $opts->__serialize();
44-
if (empty($opt['extraHeaders'])) {
45-
unset($opt['extraHeaders']);
46-
}
47-
if (empty($opt['extraQueryParams'])) {
48-
unset($opt['extraQueryParams']);
49-
}
50-
if (empty($opt['extraBodyParams'])) {
51-
unset($opt['extraBodyParams']);
52-
}
53-
54-
return [$dumped, $opt]; // @phpstan-ignore-line
35+
return [$dumped, $opts]; // @phpstan-ignore-line
5536
}
5637
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/**
88
* @internal
99
*/
10-
enum Omittable
10+
enum Omit
1111
{
12-
case OMIT;
12+
case omit;
1313
}

src/Core/Services/Beta/FilesService.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function list(
6969
$header_params,
7070
['betas' => 'anthropic-beta']
7171
),
72-
options: array_merge(
72+
options: RequestOptions::parse(
7373
['extraHeaders' => ['anthropic-beta' => 'files-api-2025-04-14']],
7474
$options,
7575
),
@@ -103,7 +103,7 @@ public function delete(
103103
$parsed,
104104
['betas' => 'anthropic-beta']
105105
),
106-
options: array_merge(
106+
options: RequestOptions::parse(
107107
['extraHeaders' => ['anthropic-beta' => 'files-api-2025-04-14']],
108108
$options,
109109
),
@@ -136,7 +136,7 @@ public function retrieveMetadata(
136136
$parsed,
137137
['betas' => 'anthropic-beta']
138138
),
139-
options: array_merge(
139+
options: RequestOptions::parse(
140140
['extraHeaders' => ['anthropic-beta' => 'files-api-2025-04-14']],
141141
$options,
142142
),

src/Core/Services/Beta/Messages/BatchesService.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function create(
6464
$header_params
6565
),
6666
body: (object) array_diff_key($parsed, array_keys($header_params)),
67-
options: array_merge(
67+
options: RequestOptions::parse(
6868
['extraHeaders' => ['anthropic-beta' => 'message-batches-2024-09-24']],
6969
$options,
7070
),
@@ -99,7 +99,7 @@ public function retrieve(
9999
$parsed,
100100
['betas' => 'anthropic-beta']
101101
),
102-
options: array_merge(
102+
options: RequestOptions::parse(
103103
['extraHeaders' => ['anthropic-beta' => 'message-batches-2024-09-24']],
104104
$options,
105105
),
@@ -153,7 +153,7 @@ public function list(
153153
$header_params,
154154
['betas' => 'anthropic-beta']
155155
),
156-
options: array_merge(
156+
options: RequestOptions::parse(
157157
['extraHeaders' => ['anthropic-beta' => 'message-batches-2024-09-24']],
158158
$options,
159159
),
@@ -191,7 +191,7 @@ public function delete(
191191
$parsed,
192192
['betas' => 'anthropic-beta']
193193
),
194-
options: array_merge(
194+
options: RequestOptions::parse(
195195
['extraHeaders' => ['anthropic-beta' => 'message-batches-2024-09-24']],
196196
$options,
197197
),
@@ -228,7 +228,7 @@ public function cancel(
228228
$parsed,
229229
['betas' => 'anthropic-beta']
230230
),
231-
options: array_merge(
231+
options: RequestOptions::parse(
232232
['extraHeaders' => ['anthropic-beta' => 'message-batches-2024-09-24']],
233233
$options,
234234
),
@@ -265,7 +265,7 @@ public function results(
265265
['Accept' => 'application/x-jsonl', ...$parsed],
266266
['betas' => 'anthropic-beta'],
267267
),
268-
options: array_merge(
268+
options: RequestOptions::parse(
269269
['extraHeaders' => ['anthropic-beta' => 'message-batches-2024-09-24']],
270270
$options,
271271
),
@@ -296,7 +296,7 @@ public function resultsStream(
296296
['Accept' => 'application/x-jsonl', ...$parsed],
297297
['betas' => 'anthropic-beta'],
298298
),
299-
options: array_merge(
299+
options: RequestOptions::parse(
300300
['extraHeaders' => ['anthropic-beta' => 'message-batches-2024-09-24']],
301301
$options,
302302
),

src/Core/Services/Beta/MessagesService.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ public function create(
294294
$header_params
295295
),
296296
body: (object) array_diff_key($parsed, array_keys($header_params)),
297-
options: array_merge(['timeout' => 600], $options),
297+
options: $options,
298298
convert: BetaMessage::class,
299299
);
300300
}
@@ -526,7 +526,7 @@ public function createStream(
526526
$header_params
527527
),
528528
body: (object) array_diff_key($parsed, array_keys($header_params)),
529-
options: array_merge(['timeout' => 600], $options),
529+
options: $options,
530530
convert: BetaRawMessageStreamEvent::class,
531531
stream: SSEStream::class,
532532
);

src/Core/Services/MessagesService.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ public function create(
272272
method: 'post',
273273
path: 'v1/messages',
274274
body: (object) $parsed,
275-
options: array_merge(['timeout' => 600], $options),
275+
options: $options,
276276
convert: Message::class,
277277
);
278278
}
@@ -490,7 +490,7 @@ public function createStream(
490490
method: 'post',
491491
path: 'v1/messages',
492492
body: (object) $parsed,
493-
options: array_merge(['timeout' => 600], $options),
493+
options: $options,
494494
convert: RawMessageStreamEvent::class,
495495
stream: SSEStream::class,
496496
);

0 commit comments

Comments
 (0)