refactor: extract CurlHttpClient (#3532)

* refactor: extract CurlHttpClient

* refactor

* interface
This commit is contained in:
Dag 2023-07-16 22:07:34 +02:00 committed by GitHub
parent 7b46b97abd
commit a59793e8d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 145 additions and 138 deletions

View File

@ -92,7 +92,12 @@ class AO3Bridge extends BridgeAbstract
private function collectWork($id) private function collectWork($id)
{ {
$url = self::URI . "/works/$id/navigate"; $url = self::URI . "/works/$id/navigate";
$response = _http_request($url, ['useragent' => 'rss-bridge bot (https://github.com/RSS-Bridge/rss-bridge)']); $httpClient = RssBridge::getHttpClient();
$response = $httpClient->request($url, [
'useragent' => 'rss-bridge bot (https://github.com/RSS-Bridge/rss-bridge)',
]);
$html = \str_get_html($response['body']); $html = \str_get_html($response['body']);
$html = defaultLinkTo($html, self::URI); $html = defaultLinkTo($html, self::URI);

View File

@ -14,7 +14,8 @@ while ($next) { /* Collect all contributors */
'Content-Type' => 'application/json', 'Content-Type' => 'application/json',
'User-Agent' => 'RSS-Bridge', 'User-Agent' => 'RSS-Bridge',
]; ];
$result = _http_request($url, ['headers' => $headers]); $httpClient = new CurlHttpClient();
$result = $httpClient->request($url, ['headers' => $headers]);
foreach (json_decode($result['body']) as $contributor) { foreach (json_decode($result['body']) as $contributor) {
$contributors[] = $contributor; $contributors[] = $contributor;

View File

@ -2,6 +2,7 @@
final class RssBridge final class RssBridge
{ {
private static HttpClient $httpClient;
private static CacheInterface $cache; private static CacheInterface $cache;
public function main(array $argv = []) public function main(array $argv = [])
@ -71,9 +72,10 @@ final class RssBridge
// Consider: ini_set('error_reporting', E_ALL & ~E_DEPRECATED); // Consider: ini_set('error_reporting', E_ALL & ~E_DEPRECATED);
date_default_timezone_set(Configuration::getConfig('system', 'timezone')); date_default_timezone_set(Configuration::getConfig('system', 'timezone'));
// Create cache
$cacheFactory = new CacheFactory(); $cacheFactory = new CacheFactory();
self::setCache($cacheFactory->create());
self::$httpClient = new CurlHttpClient();
self::$cache = $cacheFactory->create();
if (Configuration::getConfig('authentication', 'enable')) { if (Configuration::getConfig('authentication', 'enable')) {
$authenticationMiddleware = new AuthenticationMiddleware(); $authenticationMiddleware = new AuthenticationMiddleware();
@ -105,13 +107,13 @@ final class RssBridge
} }
} }
public static function getHttpClient(): HttpClient
{
return self::$httpClient;
}
public static function getCache(): CacheInterface public static function getCache(): CacheInterface
{ {
return self::$cache; return self::$cache;
} }
public static function setCache(CacheInterface $cache): void
{
self::$cache = $cache;
}
} }

View File

@ -99,6 +99,7 @@ function getContents(
array $curlOptions = [], array $curlOptions = [],
bool $returnFull = false bool $returnFull = false
) { ) {
$httpClient = RssBridge::getHttpClient();
$cache = RssBridge::getCache(); $cache = RssBridge::getCache();
$cache->setScope('server'); $cache->setScope('server');
$cache->setKey([$url]); $cache->setKey([$url]);
@ -141,20 +142,14 @@ function getContents(
$config['if_not_modified_since'] = $cache->getTime(); $config['if_not_modified_since'] = $cache->getTime();
} }
$result = _http_request($url, $config); $response = $httpClient->request($url, $config);
$response = [
'code' => $result['code'],
'status_lines' => $result['status_lines'],
'header' => $result['headers'],
'content' => $result['body'],
];
switch ($result['code']) { switch ($response['code']) {
case 200: case 200:
case 201: case 201:
case 202: case 202:
if (isset($result['headers']['cache-control'])) { if (isset($response['headers']['cache-control'])) {
$cachecontrol = $result['headers']['cache-control']; $cachecontrol = $response['headers']['cache-control'];
$lastValue = array_pop($cachecontrol); $lastValue = array_pop($cachecontrol);
$directives = explode(',', $lastValue); $directives = explode(',', $lastValue);
$directives = array_map('trim', $directives); $directives = array_map('trim', $directives);
@ -163,7 +158,7 @@ function getContents(
break; break;
} }
} }
$cache->saveData($result['body']); $cache->saveData($response['body']);
break; break;
case 301: case 301:
case 302: case 302:
@ -172,16 +167,16 @@ function getContents(
break; break;
case 304: case 304:
// Not Modified // Not Modified
$response['content'] = $cache->loadData(); $response['body'] = $cache->loadData();
break; break;
default: default:
$exceptionMessage = sprintf( $exceptionMessage = sprintf(
'%s resulted in %s %s %s', '%s resulted in %s %s %s',
$url, $url,
$result['code'], $response['code'],
Response::STATUS_CODES[$result['code']] ?? '', Response::STATUS_CODES[$response['code']] ?? '',
// If debug, include a part of the response body in the exception message // If debug, include a part of the response body in the exception message
Debug::isEnabled() ? mb_substr($result['body'], 0, 500) : '', Debug::isEnabled() ? mb_substr($response['body'], 0, 500) : '',
); );
// The following code must be extracted if it grows too much // The following code must be extracted if it grows too much
@ -192,27 +187,30 @@ function getContents(
'<title>Security | Glassdoor', '<title>Security | Glassdoor',
]; ];
foreach ($cloudflareTitles as $cloudflareTitle) { foreach ($cloudflareTitles as $cloudflareTitle) {
if (str_contains($result['body'], $cloudflareTitle)) { if (str_contains($response['body'], $cloudflareTitle)) {
throw new CloudFlareException($exceptionMessage, $result['code']); throw new CloudFlareException($exceptionMessage, $response['code']);
} }
} }
throw new HttpException(trim($exceptionMessage), $result['code']); throw new HttpException(trim($exceptionMessage), $result['code']);
} }
if ($returnFull === true) { if ($returnFull === true) {
// For legacy reasons, use content instead of body
$response['content'] = $response['body'];
unset($response['body']);
return $response; return $response;
} }
return $response['content']; return $response['body'];
} }
/** interface HttpClient
* Fetch content from url
*
* @internal Private function used internally
* @throws HttpException
*/
function _http_request(string $url, array $config = []): array
{ {
public function request(string $url, array $config = []): array;
}
final class CurlHttpClient implements HttpClient
{
public function request(string $url, array $config = []): array
{
$defaults = [ $defaults = [
'useragent' => null, 'useragent' => null,
'timeout' => 5, 'timeout' => 5,
@ -323,6 +321,7 @@ function _http_request(string $url, array $config = []): array
'headers' => $responseHeaders, 'headers' => $responseHeaders,
'body' => $data, 'body' => $data,
]; ];
}
} }
/** /**