Skip to content

Commit d95302d

Browse files
committed
Add dedicated methods to control options, deprecate withOptions()
1 parent 5be8c54 commit d95302d

File tree

5 files changed

+443
-128
lines changed

5 files changed

+443
-128
lines changed

README.md

Lines changed: 163 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,13 @@ mess with most of the low-level details.
6161
* [requestStreaming()](#requeststreaming)
6262
* [~~submit()~~](#submit)
6363
* [~~send()~~](#send)
64-
* [withOptions()](#withoptions)
64+
* [withTimeout()](#withtimeout)
65+
* [withFollowRedirects()](#withfollowredirects)
66+
* [withRejectErrorResponse()](#withrejecterrorresponse)
6567
* [withBase()](#withbase)
6668
* [withoutBase()](#withoutbase)
6769
* [withProtocolVersion()](#withprotocolversion)
70+
* [~~withOptions()~~](#withoptions)
6871
* [ResponseInterface](#responseinterface)
6972
* [RequestInterface](#requestinterface)
7073
* [UriInterface](#uriinterface)
@@ -211,23 +214,21 @@ response body and following any eventual [redirects](#redirects). See also
211214
disable following redirects altogether) and also [streaming](#streaming-response)
212215
below to not take receiving large response bodies into account for this timeout.
213216

214-
You can use the [`timeout` option](#withoptions) to pass a custom timeout value
215-
in seconds like this:
217+
You can use the [`withTimeout()` method](#withtimeout) to pass a custom timeout
218+
value in seconds like this:
216219

217220
```php
218-
$browser = $browser->withOptions(array(
219-
'timeout' => 10.0
220-
));
221+
$browser = $browser->withTimeout(10.0);
221222

222223
$browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
223224
// response received within 10 seconds maximum
224225
var_dump($response->getHeaders());
225226
});
226227
```
227228

228-
Similarly, you can use a negative timeout value to not apply a timeout at all
229-
or use a `null` value to restore the default handling.
230-
See also [`withOptions()`](#withoptions) for more details.
229+
Similarly, you can use a bool `false` to not apply a timeout at all
230+
or use a bool `true` value to restore the default handling.
231+
See [`withTimeout()`](#withtimeout) for more details.
231232

232233
If you're using a [streaming response body](#streaming-response), the time it
233234
takes to receive the response body stream will not be included in the timeout.
@@ -267,8 +268,8 @@ using the `Authorization: Basic …` request header or allows you to set an expl
267268

268269
By default, this library does not include an outgoing `Authorization` request
269270
header. If the server requires authentication, if may return a `401` (Unauthorized)
270-
status code which will reject the request by default (see also
271-
[`obeySuccessCode` option](#withoptions) below).
271+
status code which will reject the request by default (see also the
272+
[`withRejectErrorResponse()` method](#withrejecterrorresponse) below).
272273

273274
In order to pass authentication details, you can simple pass the username and
274275
password as part of the request URL like this:
@@ -329,23 +330,20 @@ possible privacy/security concerns. When following a redirect where the `Locatio
329330
response header contains authentication details, these details will be sent for
330331
following requests.
331332

332-
You can use the [`maxRedirects` option](#withoptions) to control the maximum
333-
number of redirects to follow or the [`followRedirects` option](#withoptions)
334-
to return any redirect responses as-is and apply custom redirection logic
335-
like this:
333+
You can use the [`withFollowRedirects()`](#withfollowredirects) method to
334+
control the maximum number of redirects to follow or to return any redirect
335+
responses as-is and apply custom redirection logic like this:
336336

337337
```php
338-
$browser = $browser->withOptions(array(
339-
'followRedirects' => false
340-
));
338+
$browser = $browser->withFollowRedirects(false);
341339

342340
$browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
343341
// any redirects will now end up here
344342
var_dump($response->getHeaders());
345343
});
346344
```
347345

348-
See also [`withOptions()`](#withoptions) for more details.
346+
See also [`withFollowRedirects()`](#withfollowredirects) for more details.
349347

350348
### Blocking
351349

@@ -979,32 +977,129 @@ For an empty request body, if will only include a `Content-Length: 0`
979977
request header if the request method usually expects a request body (only
980978
applies to `POST`, `PUT` and `PATCH`).
981979

982-
#### withOptions()
980+
#### withTimeout()
983981

984-
The `withOptions(array $options): Browser` method can be used to
985-
change the options to use:
982+
The `withTimeout(bool|number $timeout): Browser` method can be used to
983+
change the maximum timeout used for waiting for pending requests.
986984

987-
The [`Browser`](#browser) class exposes several options for the handling of
988-
HTTP transactions. These options resemble some of PHP's
989-
[HTTP context options](https://www.php.net/manual/en/context.http.php) and
990-
can be controlled via the following API (and their defaults):
985+
You can pass in the number of seconds to use as a new timeout value:
991986

992987
```php
993-
$newBrowser = $browser->withOptions(array(
994-
'timeout' => null,
995-
'followRedirects' => true,
996-
'maxRedirects' => 10,
997-
'obeySuccessCode' => true,
998-
'streaming' => false, // deprecated, see requestStreaming() instead
999-
));
988+
$browser = $browser->withTimeout(10.0);
1000989
```
1001990

1002-
See also [timeouts](#timeouts), [redirects](#redirects) and
1003-
[streaming](#streaming-response) for more details.
991+
You can pass in a bool `false` to disable any timeouts. In this case,
992+
requests can stay pending forever:
993+
994+
```php
995+
$browser = $browser->withTimeout(false);
996+
```
997+
998+
You can pass in a bool `true` to re-enable default timeout handling. This
999+
will respects PHP's `default_socket_timeout` setting (default 60s):
1000+
1001+
```php
1002+
$browser = $browser->withTimeout(true);
1003+
```
1004+
1005+
See also [timeouts](#timeouts) for more details about timeout handling.
10041006

10051007
Notice that the [`Browser`](#browser) is an immutable object, i.e. this
10061008
method actually returns a *new* [`Browser`](#browser) instance with the
1007-
options applied.
1009+
given timeout value applied.
1010+
1011+
#### withFollowRedirects()
1012+
1013+
The `withTimeout(bool|int $$followRedirects): Browser` method can be used to
1014+
change how HTTP redirects will be followed.
1015+
1016+
You can pass in the maximum number of redirects to follow:
1017+
1018+
```php
1019+
$new = $browser->withFollowRedirects(5);
1020+
```
1021+
1022+
The request will automatically be rejected when the number of redirects
1023+
is exceeded. You can pass in a `0` to reject the request for any
1024+
redirects encountered:
1025+
1026+
```php
1027+
$browser = $browser->withFollowRedirects(0);
1028+
1029+
$browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
1030+
// only non-redirected responses will now end up here
1031+
var_dump($response->getHeaders());
1032+
});
1033+
```
1034+
1035+
You can pass in a bool `false` to disable following any redirects. In
1036+
this case, requests will resolve with the redirection response instead
1037+
of following the `Location` response header:
1038+
1039+
```php
1040+
$browser = $browser->withFollowRedirects(false);
1041+
1042+
$browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
1043+
// any redirects will now end up here
1044+
var_dump($response->getHeaderLine('Location'));
1045+
});
1046+
```
1047+
1048+
You can pass in a bool `true` to re-enable default redirect handling.
1049+
This defaults to following a maximum of 10 redirects:
1050+
1051+
```php
1052+
$browser = $browser->withFollowRedirects(true);
1053+
```
1054+
1055+
See also [redirects](#redirects) for more details about redirect handling.
1056+
1057+
Notice that the [`Browser`](#browser) is an immutable object, i.e. this
1058+
method actually returns a *new* [`Browser`](#browser) instance with the
1059+
given redirect setting applied.
1060+
1061+
#### withRejectErrorResponse()
1062+
1063+
The `withRejectErrorResponse(bool $obeySuccessCode): Browser` method can be used to
1064+
change whether non-successful HTTP response status codes (4xx and 5xx) will be rejected.
1065+
1066+
You can pass in a bool `false` to disable rejecting incoming responses
1067+
that use a 4xx or 5xx response status code. In this case, requests will
1068+
resolve with the response message indicating an error condition:
1069+
1070+
```php
1071+
$browser = $browser->withRejectErrorResponse(false);
1072+
1073+
$browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
1074+
// any HTTP response will now end up here
1075+
var_dump($response->getStatusCode(), $response->getReasonPhrase());
1076+
});
1077+
```
1078+
1079+
You can pass in a bool `true` to re-enable default status code handling.
1080+
This defaults to rejecting any response status codes in the 4xx or 5xx
1081+
range with a [`ResponseException`](#responseexception):
1082+
1083+
```php
1084+
$browser = $browser->withRejectErrorResponse(true);
1085+
1086+
$browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
1087+
// any successful HTTP response will now end up here
1088+
var_dump($response->getStatusCode(), $response->getReasonPhrase());
1089+
}, function (Exception $e) {
1090+
if ($e instanceof Clue\React\Buzz\Message\ResponseException) {
1091+
// any HTTP response error message will now end up here
1092+
$response = $e->getResponse();
1093+
var_dump($response->getStatusCode(), $response->getReasonPhrase());
1094+
} else {
1095+
var_dump($e->getMessage());
1096+
}
1097+
});
1098+
```
1099+
1100+
Notice that the [`Browser`](#browser) is an immutable object, i.e. this
1101+
method actually returns a *new* [`Browser`](#browser) instance with the
1102+
given setting applied.
10081103

10091104
#### withBase()
10101105

@@ -1068,6 +1163,37 @@ Notice that the [`Browser`](#browser) is an immutable object, i.e. this
10681163
method actually returns a *new* [`Browser`](#browser) instance with the
10691164
new protocol version applied.
10701165

1166+
#### ~~withOptions()~~
1167+
1168+
> Deprecated since v2.9.0, see [`withTimeout()](#withtimeout), [`withFollowRedirects()`](#withfollowredirects)
1169+
and [`withRejectErrorResponse()`](#withrejecterrorresponse) instead.
1170+
1171+
The deprecated `withOptions(array $options): Browser` method can be used to
1172+
change the options to use:
1173+
1174+
The [`Browser`](#browser) class exposes several options for the handling of
1175+
HTTP transactions. These options resemble some of PHP's
1176+
[HTTP context options](https://www.php.net/manual/en/context.http.php) and
1177+
can be controlled via the following API (and their defaults):
1178+
1179+
```php
1180+
// deprecated
1181+
$newBrowser = $browser->withOptions(array(
1182+
'timeout' => null, // see withTimeout() instead
1183+
'followRedirects' => true, // see withFollowRedirects() instead
1184+
'maxRedirects' => 10, // see withFollowRedirects() instead
1185+
'obeySuccessCode' => true, // see withRejectErrorResponse() instead
1186+
'streaming' => false, // deprecated, see requestStreaming() instead
1187+
));
1188+
```
1189+
1190+
See also [timeouts](#timeouts), [redirects](#redirects) and
1191+
[streaming](#streaming-response) for more details.
1192+
1193+
Notice that the [`Browser`](#browser) is an immutable object, i.e. this
1194+
method actually returns a *new* [`Browser`](#browser) instance with the
1195+
options applied.
1196+
10711197
### ResponseInterface
10721198

10731199
The `Psr\Http\Message\ResponseInterface` represents the incoming response received from the [`Browser`](#browser).
@@ -1101,7 +1227,7 @@ This is a standard interface defined in
11011227
The `ResponseException` is an `Exception` sub-class that will be used to reject
11021228
a request promise if the remote server returns a non-success status code
11031229
(anything but 2xx or 3xx).
1104-
You can control this behavior via the ["obeySuccessCode" option](#withoptions).
1230+
You can control this behavior via the [`withRejectErrorResponse()` method](#withrejecterrorresponse).
11051231

11061232
The `getCode(): int` method can be used to
11071233
return the HTTP response status code.

0 commit comments

Comments
 (0)