|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Contracts\HttpClient; |
| 13 | + |
| 14 | +use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; |
| 15 | +use Symfony\Contracts\HttpClient\Test\HttpClientTestCase; |
| 16 | + |
| 17 | +/** |
| 18 | + * Provides flexible methods for requesting HTTP resources synchronously or asynchronously. |
| 19 | + * |
| 20 | + * @see HttpClientTestCase for a reference test suite |
| 21 | + * |
| 22 | + * @author Nicolas Grekas <p@tchwork.com> |
| 23 | + * |
| 24 | + * @experimental in 1.1 |
| 25 | + */ |
| 26 | +interface HttpClientInterface |
| 27 | +{ |
| 28 | + public const OPTIONS_DEFAULTS = [ |
| 29 | + 'auth' => null, // string - a username:password enabling HTTP Basic authentication |
| 30 | + 'query' => [], // string[] - associative array of query string values to merge with the request's URL |
| 31 | + 'headers' => [], // iterable|string[]|string[][] - headers names provided as keys or as part of values |
| 32 | + 'body' => '', // array|string|resource|\Traversable|\Closure - the callback SHOULD yield a string |
| 33 | + // smaller than the amount requested as argument; the empty string signals EOF; when |
| 34 | + // an array is passed, it is meant as a form payload of field names and values |
| 35 | + 'json' => null, // array|\JsonSerializable - when set, implementations MUST set the "body" option to |
| 36 | + // the JSON-encoded value and set the "content-type" headers to a JSON-compatible |
| 37 | + // value it is they are not defined - typically "application/json" |
| 38 | + 'user_data' => null, // mixed - any extra data to attach to the request (scalar, callable, object...) that |
| 39 | + // MUST be available via $response->getInfo('data') - not used internally |
| 40 | + 'max_redirects' => 20, // int - the maximum number of redirects to follow; a value lower or equal to 0 means |
| 41 | + // redirects should not be followed; "Authorization" and "Cookie" headers MUST |
| 42 | + // NOT follow except for the initial host name |
| 43 | + 'http_version' => null, // string - defaults to the best supported version, typically 1.1 or 2.0 |
| 44 | + 'base_uri' => null, // string - the URI to resolve relative URLs, following rules in RFC 3986, section 2 |
| 45 | + 'buffer' => true, // bool - whether the content of the response should be buffered or not |
| 46 | + 'on_progress' => null, // callable(int $dlNow, int $dlSize, array $info) - throwing any exceptions MUST abort |
| 47 | + // the request; it MUST be called on DNS resolution, on arrival of headers and on |
| 48 | + // completion; it SHOULD be called on upload/download of data and at least 1/s |
| 49 | + 'resolve' => [], // string[] - a map of host to IP address that SHOULD replace DNS resolution |
| 50 | + 'proxy' => null, // string - by default, the proxy-related env vars handled by curl SHOULD be honored |
| 51 | + 'no_proxy' => null, // string - a comma separated list of hosts that do not require a proxy to be reached |
| 52 | + 'timeout' => null, // float - the inactivity timeout - defaults to ini_get('default_socket_timeout') |
| 53 | + 'bindto' => '0', // string - the interface or the local socket to bind to |
| 54 | + 'verify_peer' => true, // see https://php.net/context.ssl for the following options |
| 55 | + 'verify_host' => true, |
| 56 | + 'cafile' => null, |
| 57 | + 'capath' => null, |
| 58 | + 'local_cert' => null, |
| 59 | + 'local_pk' => null, |
| 60 | + 'passphrase' => null, |
| 61 | + 'ciphers' => null, |
| 62 | + 'peer_fingerprint' => null, |
| 63 | + 'capture_peer_cert_chain' => false, |
| 64 | + ]; |
| 65 | + |
| 66 | + /** |
| 67 | + * Requests an HTTP resource. |
| 68 | + * |
| 69 | + * Responses MUST be lazy, but their status code MUST be |
| 70 | + * checked even if none of their public methods are called. |
| 71 | + * |
| 72 | + * Implementations are not required to support all options described above; they can also |
| 73 | + * support more custom options; but in any case, they MUST throw a TransportExceptionInterface |
| 74 | + * when an unsupported option is passed. |
| 75 | + * |
| 76 | + * @throws TransportExceptionInterface When an unsupported option is passed |
| 77 | + */ |
| 78 | + public function request(string $method, string $url, array $options = []): ResponseInterface; |
| 79 | + |
| 80 | + /** |
| 81 | + * Yields responses chunk by chunk as they complete. |
| 82 | + * |
| 83 | + * @param ResponseInterface|ResponseInterface[]|iterable $responses One or more responses created by the current HTTP client |
| 84 | + * @param float|null $timeout The inactivity timeout before exiting the iterator |
| 85 | + */ |
| 86 | + public function stream($responses, float $timeout = null): ResponseStreamInterface; |
| 87 | +} |
0 commit comments