From 01d5af66beed08810861e8547fd6cd0cfee6a52a Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Fri, 1 Mar 2024 14:55:54 +0100 Subject: feat(IClient): Add `request` function to do arbitrary HTTP requests Signed-off-by: Ferdinand Thiessen --- lib/private/Http/Client/Client.php | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'lib/private/Http/Client/Client.php') diff --git a/lib/private/Http/Client/Client.php b/lib/private/Http/Client/Client.php index b43583ba09a..6877018cc70 100644 --- a/lib/private/Http/Client/Client.php +++ b/lib/private/Http/Client/Client.php @@ -424,6 +424,43 @@ class Client implements IClient { throw $e; } + /** + * Sends a HTTP request + * + * @param string $uri + * @param string $method The HTTP method to use + * @param array $options Array such as + * 'query' => [ + * 'field' => 'abc', + * 'other_field' => '123', + * 'file_name' => fopen('/path/to/file', 'r'), + * ], + * 'headers' => [ + * 'foo' => 'bar', + * ], + * 'cookies' => [ + * 'foo' => 'bar', + * ], + * 'allow_redirects' => [ + * 'max' => 10, // allow at most 10 redirects. + * 'strict' => true, // use "strict" RFC compliant redirects. + * 'referer' => true, // add a Referer header + * 'protocols' => ['https'] // only allow https URLs + * ], + * 'sink' => '/path/to/file', // save to a file or a stream + * 'verify' => true, // bool or string to CA file + * 'debug' => true, + * 'timeout' => 5, + * @return IResponse + * @throws \Exception If the request could not get completed + */ + public function request(string $uri, string $method, array $options = []): IResponse { + $this->preventLocalAddress($uri, $options); + $response = $this->client->request($method, $uri, $this->buildRequestOptions($options)); + $isStream = isset($options['stream']) && $options['stream']; + return new Response($response, $isStream); + } + protected function wrapGuzzlePromise(PromiseInterface $promise): IPromise { return new GuzzlePromiseAdapter( $promise, -- cgit v1.2.3