]> git.mxchange.org Git - friendica.git/commitdiff
Fixing HTTPClient::post() and introduce HTTPRequestOptions
authorPhilipp <admin@philipp.info>
Wed, 25 Aug 2021 19:09:14 +0000 (21:09 +0200)
committerPhilipp <admin@philipp.info>
Wed, 25 Aug 2021 19:31:30 +0000 (21:31 +0200)
src/Network/HTTPClient.php
src/Network/HTTPRequestOptions.php [new file with mode: 0644]
src/Network/IHTTPClient.php

index eabd5d450791fa298d0c5a623d91294406c222c4..e982d19afcd0224b946be9e3f774659fef094e1e 100644 (file)
@@ -57,7 +57,7 @@ class HTTPClient implements IHTTPClient
        }
 
        /**
-        * @throws HTTPException\InternalServerErrorException
+        * {@inheritDoc}
         */
        public function request(string $method, string $url, array $opts = []): IHTTPResult
        {
@@ -95,35 +95,39 @@ class HTTPClient implements IHTTPClient
 
                $conf = [];
 
-               if (!empty($opts['cookiejar'])) {
-                       $jar                           = new FileCookieJar($opts['cookiejar']);
+               if (!empty($opts[HTTPRequestOptions::COOKIEJAR])) {
+                       $jar                           = new FileCookieJar($opts[HTTPRequestOptions::COOKIEJAR]);
                        $conf[RequestOptions::COOKIES] = $jar;
                }
 
-               $header = [];
+               $headers = [];
 
-               if (!empty($opts['accept_content'])) {
-                       $header['Accept'] = $opts['accept_content'];
+               if (!empty($opts[HTTPRequestOptions::ACCEPT_CONTENT])) {
+                       $headers['Accept'] = $opts[HTTPRequestOptions::ACCEPT_CONTENT];
                }
 
-               if (!empty($opts['header'])) {
-                       $header = array_merge($opts['header'], $header);
+               if (!empty($opts[HTTPRequestOptions::LEGACY_HEADER])) {
+                       $this->logger->notice('Wrong option \'headers\' used.');
+                       $headers = array_merge($opts[HTTPRequestOptions::LEGACY_HEADER], $headers);
                }
 
-               if (!empty($opts['headers'])) {
-                       $this->logger->notice('Wrong option \'headers\' used.');
-                       $header = array_merge($opts['headers'], $header);
+               if (!empty($opts[HTTPRequestOptions::HEADERS])) {
+                       $headers = array_merge($opts[HTTPRequestOptions::HEADERS], $headers);
                }
 
-               $conf[RequestOptions::HEADERS] = array_merge($this->client->getConfig(RequestOptions::HEADERS), $header);
+               $conf[RequestOptions::HEADERS] = array_merge($this->client->getConfig(RequestOptions::HEADERS), $headers);
+
+               if (!empty($opts[HTTPRequestOptions::TIMEOUT])) {
+                       $conf[RequestOptions::TIMEOUT] = $opts[HTTPRequestOptions::TIMEOUT];
+               }
 
-               if (!empty($opts['timeout'])) {
-                       $conf[RequestOptions::TIMEOUT] = $opts['timeout'];
+               if (!empty($opts[HTTPRequestOptions::BODY])) {
+                       $conf[RequestOptions::BODY] = $opts[HTTPRequestOptions::BODY];
                }
 
                $conf[RequestOptions::ON_HEADERS] = function (ResponseInterface $response) use ($opts) {
-                       if (!empty($opts['content_length']) &&
-                               $response->getHeaderLine('Content-Length') > $opts['content_length']) {
+                       if (!empty($opts[HTTPRequestOptions::CONTENT_LENGTH]) &&
+                               (int)$response->getHeaderLine('Content-Length') > $opts[HTTPRequestOptions::CONTENT_LENGTH]) {
                                throw new TransferException('The file is too big!');
                        }
                };
@@ -160,8 +164,6 @@ class HTTPClient implements IHTTPClient
        }
 
        /** {@inheritDoc}
-        *
-        * @throws HTTPException\InternalServerErrorException
         */
        public function head(string $url, array $opts = []): IHTTPResult
        {
@@ -183,10 +185,10 @@ class HTTPClient implements IHTTPClient
        {
                $opts = [];
 
-               $opts[RequestOptions::JSON] = $params;
+               $opts[RequestOptions::BODY] = $params;
 
                if (!empty($headers)) {
-                       $opts['headers'] = $headers;
+                       $opts[RequestOptions::HEADERS] = $headers;
                }
 
                if (!empty($timeout)) {
diff --git a/src/Network/HTTPRequestOptions.php b/src/Network/HTTPRequestOptions.php
new file mode 100644 (file)
index 0000000..7cfee89
--- /dev/null
@@ -0,0 +1,40 @@
+<?php
+
+namespace Friendica\Network;
+
+use GuzzleHttp\RequestOptions;
+
+/**
+ * This class contains a list of possible HTTPClient request options.
+ */
+class HTTPRequestOptions
+{
+       /**
+        * accept_content: (array) supply Accept: header with 'accept_content' as the value
+        */
+       const ACCEPT_CONTENT = 'accept_content';
+       /**
+        * timeout: (int) out in seconds, default system config value or 60 seconds
+        */
+       const TIMEOUT = RequestOptions::TIMEOUT;
+       /**
+        * cookiejar: (string) path to cookie jar file
+        */
+       const COOKIEJAR = 'cookiejar';
+       /**
+        * headers: (array) header array
+        */
+       const HEADERS = RequestOptions::HEADERS;
+       /**
+        * header: (array) header array (legacy version)
+        */
+       const LEGACY_HEADER = 'header';
+       /**
+        * content_length: (int) maximum File content length
+        */
+       const CONTENT_LENGTH = 'content_length';
+       /**
+        * body: (mixed) Setting the body for sending data
+        */
+       const BODY = RequestOptions::BODY;
+}
index c7d122c8f143d302c8411f58cf5e550f0425f495..2f8bab46fca03523a7f93548c572b692f4416bb2 100644 (file)
@@ -88,9 +88,15 @@ interface IHTTPClient
        /**
         * Sends a HTTP request to a given url
         *
-        * @param string $method A HTTP request ()
+        * @param string $method A HTTP request
         * @param string $url    Url to send to
-        * @param array  $opts   parameters
+        * @param array  $opts   (optional parameters) associative array with:
+        *                               'body' => (mixed) setting the body for sending data
+        *                           'accept_content' => (string array) supply Accept: header with 'accept_content' as the value
+        *                           'timeout' => int Timeout in seconds, default system config value or 60 seconds
+        *                           'cookiejar' => path to cookie jar file
+        *                           'header' => header array
+        *                           'content_length' => int maximum File content length
         *
         * @return IHTTPResult
         */