Introduce "accept_header" as specific argument to the http client

This commit is contained in:
Philipp 2022-04-02 21:16:22 +02:00
parent 73c3b21665
commit e299fc67c8
No known key found for this signature in database
GPG key ID: 24A7501396EB5432
35 changed files with 150 additions and 150 deletions

View file

@ -21,6 +21,7 @@
namespace Friendica\Network\HTTPClient\Capability;
use Friendica\Network\HTTPClient\Client\HttpClientAccept;
use GuzzleHttp\Exception\TransferException;
/**
@ -35,13 +36,13 @@ interface ICanSendHttpRequests
* to preserve cookies from one request to the next.
*
* @param string $url URL to fetch
* @param int $timeout Timeout in seconds, default system config value or 60 seconds
* @param string $accept_content supply Accept: header with 'accept_content' as the value
* @param int $timeout Timeout in seconds, default system config value or 60 seconds
* @param string $cookiejar Path to cookie jar file
*
* @return string The fetched content
*/
public function fetch(string $url, int $timeout = 0, string $accept_content = '', string $cookiejar = ''): string;
public function fetch(string $url, string $accept_content = HttpClientAccept::DEFAULT, int $timeout = 0, string $cookiejar = ''): string;
/**
* Fetches the whole response of an URL.
@ -50,72 +51,75 @@ interface ICanSendHttpRequests
* all the information collected during the fetch.
*
* @param string $url URL to fetch
* @param int $timeout Timeout in seconds, default system config value or 60 seconds
* @param string $accept_content supply Accept: header with 'accept_content' as the value
* @param int $timeout Timeout in seconds, default system config value or 60 seconds
* @param string $cookiejar Path to cookie jar file
*
* @return ICanHandleHttpResponses With all relevant information, 'body' contains the actual fetched content.
*/
public function fetchFull(string $url, int $timeout = 0, string $accept_content = '', string $cookiejar = ''): ICanHandleHttpResponses;
public function fetchFull(string $url, string $accept_content = HttpClientAccept::DEFAULT, int $timeout = 0, string $cookiejar = ''): ICanHandleHttpResponses;
/**
* Send a HEAD to a URL.
*
* @param string $url URL to fetch
* @param array $opts (optional parameters) associative array with:
* '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
* @param string $url URL to fetch
* @param string $accept_content supply Accept: header with 'accept_content' as the value
* @param array $opts (optional parameters) associative array with:
* 'accept_content' => (string array) supply Accept: header with 'accept_content' as the value (overrides default parameter)
* 'timeout' => int Timeout in seconds, default system config value or 60 seconds
* 'cookiejar' => path to cookie jar file
* 'header' => header array
*
* @return ICanHandleHttpResponses
*/
public function head(string $url, array $opts = []): ICanHandleHttpResponses;
public function head(string $url, string $accept_content = HttpClientAccept::DEFAULT, array $opts = []): ICanHandleHttpResponses;
/**
* Send a GET to an URL.
* Send a GET to a URL.
*
* @param string $url URL to fetch
* @param array $opts (optional parameters) associative array with:
* '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
* @param string $url URL to get
* @param string $accept_content supply Accept: header with 'accept_content' as the value
* @param array $opts (optional parameters) associative array with:
* 'accept_content' => (string array) supply Accept: header with 'accept_content' as the value (overrides default parameter)
* 'timeout' => int Timeout in seconds, default system config value or 60 seconds
* 'cookiejar' => path to cookie jar file
* 'header' => header array
*
* @return ICanHandleHttpResponses
*/
public function get(string $url, array $opts = []): ICanHandleHttpResponses;
public function get(string $url, string $accept_content = HttpClientAccept::DEFAULT, array $opts = []): ICanHandleHttpResponses;
/**
* Sends a HTTP request to a given url
*
* @param string $method A HTTP request
* @param string $url Url to send to
* @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
* 'auth' => array authentication settings
* @param string $method A HTTP request
* @param string $url Url to send to
* @param string $accept_content supply Accept: header with 'accept_content' as the value
* @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 (overrides default parameter)
* '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
* 'auth' => array authentication settings
*
* @return ICanHandleHttpResponses
*/
public function request(string $method, string $url, array $opts = []): ICanHandleHttpResponses;
public function request(string $method, string $url, string $accept_content = HttpClientAccept::DEFAULT, array $opts = []): ICanHandleHttpResponses;
/**
* Send POST request to an URL
*
* @param string $url URL to post
* @param mixed $params array of POST variables
* @param array $headers HTTP headers
* @param int $timeout The timeout in seconds, default system config value or 60 seconds
* @param string $url URL to post
* @param mixed $params array of POST variables
* @param string $accept_content supply Accept: header with 'accept_content' as the value
* @param array $headers HTTP headers
* @param int $timeout The timeout in seconds, default system config value or 60 seconds
*
* @return ICanHandleHttpResponses The content
*/
public function post(string $url, $params, array $headers = [], int $timeout = 0): ICanHandleHttpResponses;
public function post(string $url, $params, string $accept_content = HttpClientAccept::DEFAULT, array $headers = [], int $timeout = 0): ICanHandleHttpResponses;
/**
* Returns the original URL of the provided URL

View file

@ -63,7 +63,7 @@ class HttpClient implements ICanSendHttpRequests
/**
* {@inheritDoc}
*/
public function request(string $method, string $url, array $opts = []): ICanHandleHttpResponses
public function request(string $method, string $url, string $accept_content = HttpClientAccept::DEFAULT, array $opts = []): ICanHandleHttpResponses
{
$this->profiler->startRecording('network');
$this->logger->debug('Request start.', ['url' => $url, 'method' => $method]);
@ -141,7 +141,7 @@ class HttpClient implements ICanSendHttpRequests
};
if (empty($conf[HttpClientOptions::HEADERS]['Accept'])) {
$conf[HttpClientOptions::HEADERS]['Accept'] = HttpClientAccept::DEFAULT;
$conf[HttpClientOptions::HEADERS]['Accept'] = $accept_content;
}
try {
@ -167,23 +167,23 @@ class HttpClient implements ICanSendHttpRequests
/** {@inheritDoc}
*/
public function head(string $url, array $opts = []): ICanHandleHttpResponses
public function head(string $url, string $accept_content = HttpClientAccept::DEFAULT, array $opts = []): ICanHandleHttpResponses
{
return $this->request('head', $url, $opts);
return $this->request('head', $url, $accept_content, $opts);
}
/**
* {@inheritDoc}
*/
public function get(string $url, array $opts = []): ICanHandleHttpResponses
public function get(string $url, string $accept_content = HttpClientAccept::DEFAULT, array $opts = []): ICanHandleHttpResponses
{
return $this->request('get', $url, $opts);
return $this->request('get', $url, $accept_content, $opts);
}
/**
* {@inheritDoc}
*/
public function post(string $url, $params, array $headers = [], int $timeout = 0): ICanHandleHttpResponses
public function post(string $url, $params, string $accept_content = HttpClientAccept::DEFAULT, array $headers = [], int $timeout = 0): ICanHandleHttpResponses
{
$opts = [];
@ -197,7 +197,7 @@ class HttpClient implements ICanSendHttpRequests
$opts[HttpClientOptions::TIMEOUT] = $timeout;
}
return $this->request('post', $url, $opts);
return $this->request('post', $url, $accept_content, $opts);
}
/**
@ -237,9 +237,9 @@ class HttpClient implements ICanSendHttpRequests
/**
* {@inheritDoc}
*/
public function fetch(string $url, int $timeout = 0, string $accept_content = '', string $cookiejar = ''): string
public function fetch(string $url, string $accept_content = HttpClientAccept::DEFAULT, int $timeout = 0, string $cookiejar = ''): string
{
$ret = $this->fetchFull($url, $timeout, $accept_content, $cookiejar);
$ret = $this->fetchFull($url, $accept_content, $timeout, $cookiejar);
return $ret->getBody();
}
@ -247,14 +247,14 @@ class HttpClient implements ICanSendHttpRequests
/**
* {@inheritDoc}
*/
public function fetchFull(string $url, int $timeout = 0, string $accept_content = '', string $cookiejar = ''): ICanHandleHttpResponses
public function fetchFull(string $url, string $accept_content = HttpClientAccept::DEFAULT, int $timeout = 0, string $cookiejar = ''): ICanHandleHttpResponses
{
return $this->get(
$url,
$accept_content,
[
HttpClientOptions::TIMEOUT => $timeout,
HttpClientOptions::ACCEPT_CONTENT => $accept_content,
HttpClientOptions::COOKIEJAR => $cookiejar
HttpClientOptions::TIMEOUT => $timeout,
HttpClientOptions::COOKIEJAR => $cookiejar
]
);
}

View file

@ -2,6 +2,9 @@
namespace Friendica\Network\HTTPClient\Client;
/**
* This class contains a list of possible HTTPClient ACCEPT options.
*/
class HttpClientAccept
{
/** @var string Default value for "Accept" header */
@ -20,4 +23,5 @@ class HttpClientAccept
public const TEXT = 'text/plain,text/*;q=0.9,*/*;q=0.8';
public const VIDEO = 'video/mp4,video/*;q=0.9,*/*;q=0.8';
public const XRD_XML = 'application/xrd+xml,text/xml;q=0.9,*/*;q=0.8';
public const XML = 'application/xml,text/xml;q=0.9,*/*;q=0.8';
}