refactor: return proper response object (#4169)

This commit is contained in:
Dag 2024-07-31 17:30:06 +02:00 committed by GitHub
parent aa3989873c
commit 891c8979a3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
28 changed files with 69 additions and 84 deletions

View file

@ -54,8 +54,8 @@ class ConnectivityAction implements ActionInterface
];
try {
$response = getContents($bridge::URI, [], $curl_opts, true);
$result['http_code'] = $response['code'];
if (in_array($response['code'], [200])) {
$result['http_code'] = $response->getCode();
if (in_array($result['http_code'], [200])) {
$result['successful'] = true;
}
} catch (\Exception $e) {

View file

@ -284,8 +284,7 @@ class BadDragonBridge extends BridgeAbstract
case 'Clearance':
$toyData = json_decode(getContents($this->inputToURL(true)));
$productList = json_decode(getContents(self::URI
. 'api/inventory-toy/product-list'));
$productList = json_decode(getContents(self::URI . 'api/inventory-toy/product-list'));
foreach ($toyData->toys as $toy) {
$item = [];

View file

@ -111,12 +111,12 @@ class BandcampBridge extends BridgeAbstract
$url = self::URI . 'api/hub/1/dig_deeper';
$data = $this->buildRequestJson();
$header = [
'Content-Type: application/json',
'Content-Length: ' . strlen($data)
'Content-Type: application/json',
'Content-Length: ' . strlen($data),
];
$opts = [
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $data
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $data,
];
$content = getContents($url, $header, $opts);
@ -314,7 +314,8 @@ class BandcampBridge extends BridgeAbstract
{
$url = self::URI . 'api/' . $endpoint . '?' . http_build_query($query_data);
// todo: 429 Too Many Requests happens a lot
$data = json_decode(getContents($url));
$response = getContents($url);
$data = json_decode($response);
return $data;
}

View file

@ -47,8 +47,10 @@ class CubariBridge extends BridgeAbstract
*/
public function collectData()
{
// TODO: fix trivial SSRF
$json = getContents($this->getInput('gist'));
$jsonFile = json_decode($json, true);
$jsonFile = Json::decode($json);
$this->mangaTitle = $jsonFile['title'];

View file

@ -24,7 +24,8 @@ class DemosBerlinBridge extends BridgeAbstract
public function collectData()
{
$json = getContents('https://www.berlin.de/polizei/service/versammlungsbehoerde/versammlungen-aufzuege/index.php/index/all.json');
$url = 'https://www.berlin.de/polizei/service/versammlungsbehoerde/versammlungen-aufzuege/index.php/index/all.json';
$json = getContents($url);
$jsonFile = json_decode($json, true);
$daysInterval = DateInterval::createFromDateString($this->getInput('days') . ' day');

View file

@ -78,13 +78,9 @@ class DerpibooruBridge extends BridgeAbstract
public function collectData()
{
$queryJson = json_decode(getContents(
self::URI
. 'api/v1/json/search/images?filter_id='
. urlencode($this->getInput('f'))
. '&q='
. urlencode($this->getInput('q'))
));
$url = self::URI . 'api/v1/json/search/images?filter_id=' . urlencode($this->getInput('f')) . '&q=' . urlencode($this->getInput('q'));
$queryJson = json_decode(getContents($url));
foreach ($queryJson->images as $post) {
$item = [];

View file

@ -50,7 +50,9 @@ class EZTVBridge extends BridgeAbstract
$eztv_uri = $this->getEztvUri();
$ids = explode(',', trim($this->getInput('ids')));
foreach ($ids as $id) {
$data = json_decode(getContents(sprintf('%s/api/get-torrents?imdb_id=%s', $eztv_uri, $id)));
$url = sprintf('%s/api/get-torrents?imdb_id=%s', $eztv_uri, $id);
$json = getContents($url);
$data = json_decode($json);
if (!isset($data->torrents)) {
// No results
continue;

View file

@ -34,11 +34,9 @@ class ElloBridge extends BridgeAbstract
];
if (!empty($this->getInput('u'))) {
$postData = getContents(self::URI . 'api/v2/users/~' . urlencode($this->getInput('u')) . '/posts', $header) or
returnServerError('Unable to query Ello API.');
$postData = getContents(self::URI . 'api/v2/users/~' . urlencode($this->getInput('u')) . '/posts', $header);
} else {
$postData = getContents(self::URI . 'api/v2/posts?terms=' . urlencode($this->getInput('s')), $header) or
returnServerError('Unable to query Ello API.');
$postData = getContents(self::URI . 'api/v2/posts?terms=' . urlencode($this->getInput('s')), $header);
}
$postData = json_decode($postData);
@ -117,7 +115,7 @@ class ElloBridge extends BridgeAbstract
$apiKey = $this->cache->get($cacheKey);
if (!$apiKey) {
$keyInfo = getContents(self::URI . 'api/webapp-token') or returnServerError('Unable to get token.');
$keyInfo = getContents(self::URI . 'api/webapp-token');
$apiKey = json_decode($keyInfo)->token->access_token;
$ttl = 60 * 60 * 20;
$this->cache->set($cacheKey, $apiKey, $ttl);

View file

@ -31,7 +31,7 @@ class FDroidBridge extends BridgeAbstract
CURLOPT_NOBODY => true,
];
$reponse = getContents($url, [], $curlOptions, true);
$lastModified = $reponse['headers']['last-modified'][0] ?? null;
$lastModified = $reponse->getHeader('last-modified');
$timestamp = strtotime($lastModified ?? 'today');
return $timestamp;
}

View file

@ -32,7 +32,7 @@ class FunkBridge extends BridgeAbstract
$url .= '?size=' . $this->getInput('max');
}
$jsonString = getContents($url) or returnServerError('No contents received!');
$jsonString = getContents($url);
$json = json_decode($jsonString, true);
foreach ($json['list'] as $element) {

View file

@ -41,8 +41,7 @@ class GlowficBridge extends BridgeAbstract
$first_page = 1;
}
for ($page_offset = $first_page; $page_offset <= $metadata['Last-Page']; $page_offset++) {
$jsonContents = getContents($url . '/replies?page=' . $page_offset) or
returnClientError('Could not retrieve replies for page ' . $page_offset . '.');
$jsonContents = getContents($url . '/replies?page=' . $page_offset);
$replies = json_decode($jsonContents);
foreach ($replies as $reply) {
$item = [];
@ -75,8 +74,9 @@ class GlowficBridge extends BridgeAbstract
private function getPost()
{
$url = $this->getAPIURI();
$jsonPost = getContents($url) or returnClientError('Could not retrieve post metadata.');
$jsonPost = getContents($url);
$post = json_decode($jsonPost);
return $post;
}

View file

@ -30,7 +30,7 @@ class InternationalInstituteForStrategicStudiesBridge extends BridgeAbstract
];
$headers = [
'Accept: application/json, text/plain, */*',
'Content-Type: application/json;charset=UTF-8'
'Content-Type: application/json;charset=UTF-8',
];
$json = getContents($url, $headers, $opts);
$data = json_decode($json);

View file

@ -669,11 +669,11 @@ class ItakuBridge extends BridgeAbstract
if ($cache) {
$data = $this->loadCacheValue($url);
if (is_null($data)) {
$data = getContents($url, $httpHeaders, $curlOptions) or returnServerError("Could not load $url");
$data = getContents($url, $httpHeaders, $curlOptions);
$this->saveCacheValue($url, $data);
}
} else {
$data = getContents($url, $httpHeaders, $curlOptions) or returnServerError("Could not load $url");
$data = getContents($url, $httpHeaders, $curlOptions);
}
return json_decode($data, true);
} else { //get simpleHTMLDOM object

View file

@ -12,8 +12,7 @@ class KilledbyGoogleBridge extends BridgeAbstract
public function collectData()
{
$json = getContents(self::URI . '/graveyard.json')
or returnServerError('Could not request: ' . self::URI . '/graveyard.json');
$json = getContents(self::URI . '/graveyard.json');
$this->handleJson($json);
$this->orderItems();

View file

@ -52,8 +52,7 @@ Once a project reaches 10,000 supporters, it gets reviewed by the lego experts.'
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $this->getHttpPostData()
];
$responseData = getContents($this->getHttpPostURI(), $header, $opts) or
returnServerError('Unable to query Lego Ideas API.');
$responseData = getContents($this->getHttpPostURI(), $header, $opts);
foreach (json_decode($responseData)->results as $project) {
preg_match('/datetime=\"(\S+)\"/', $project->entity->published_at, $date_matches);

View file

@ -147,10 +147,9 @@ class OpenCVEBridge extends BridgeAbstract
for ($i = 1; $i <= $this->getInput('pages'); $i++) {
$queryPaginated = array_merge($query, ['page' => $i]);
$url = $instance . '/api/cve?' . http_build_query($queryPaginated);
$response = getContents(
$url,
[$authHeader]
);
$response = getContents($url, [$authHeader]);
$titlePrefix = '';
if (count($queries) > 1) {
$titlePrefix = '[' . $queryName . '] ';
@ -205,10 +204,8 @@ class OpenCVEBridge extends BridgeAbstract
private function fetchContents($cveItem, $titlePrefix, $instance, $authHeader)
{
$url = $instance . '/api/cve/' . $cveItem->id;
$response = getContents(
$url,
[$authHeader]
);
$response = getContents($url, [$authHeader]);
$datum = json_decode($response);
$title = $this->getTitleFromDatum($datum, $titlePrefix);

View file

@ -191,15 +191,12 @@ HEREDOC;
}
}
/**
* Extract the cookies obtained from the URL
* @return array the array containing the cookies set by the URL
*/
private function getCookiesHeaderValue($url)
{
$response = getContents($url, [], [], true);
$setCookieHeaders = $response['headers']['set-cookie'] ?? [];
$setCookieHeaders = $response->getHeader('set-cookie', true);
$cookies = array_map(fn($c): string => explode(';', $c)[0], $setCookieHeaders);
return implode('; ', $cookies);
}

View file

@ -332,21 +332,20 @@ class PixivBridge extends BridgeAbstract
}
if ($cache) {
$data = $this->loadCacheValue($url);
if (!$data) {
$data = getContents($url, $httpHeaders, $curlOptions, true);
$this->saveCacheValue($url, $data);
$response = $this->loadCacheValue($url);
if (!$response || is_array($response)) {
$response = getContents($url, $httpHeaders, $curlOptions, true);
$this->saveCacheValue($url, $response);
}
} else {
$data = getContents($url, $httpHeaders, $curlOptions, true);
$response = getContents($url, $httpHeaders, $curlOptions, true);
}
$this->checkCookie($data['headers']);
$this->checkCookie($response->getHeaders());
if ($getJSON) {
return json_decode($data['content'], true);
} else {
return $data['content'];
return json_decode($response->getBody(), true);
}
return $response->getBody();
}
}

View file

@ -22,7 +22,7 @@ class RainbowSixSiegeBridge extends BridgeAbstract
$dlUrl = $dlUrl . '&limit=6&mediaFilter=all&skip=0&startIndex=0&tags=BR-rainbow-six%20GA-siege';
$dlUrl = $dlUrl . '&locale=en-us&fallbackLocale=en-us&environment=master';
$jsonString = getContents($dlUrl, [
'Authorization: ' . self::NIMBUS_API_KEY
'Authorization: ' . self::NIMBUS_API_KEY,
]);
$json = json_decode($jsonString, true);

View file

@ -149,7 +149,7 @@ class RedditBridge extends BridgeAbstract
$response = getContents($url, ['User-Agent: ' . $useragent], [], true);
$json = $response['content'];
$json = $response->getBody();
$parsedJson = Json::decode($json, false);

View file

@ -417,9 +417,11 @@ class ReutersBridge extends BridgeAbstract
$get_embed_url = 'https://publish.twitter.com/oembed?url='
. urlencode($tweet_url) .
'&partner=&hide_thread=false';
$oembed_json = json_decode(getContents($get_embed_url), true);
$embed .= $oembed_json['html'];
} catch (Exception $e) { // In case not found any tweet.
} catch (\Exception $e) {
// In case not found any tweet.
$embed .= '';
}
break;

View file

@ -68,9 +68,4 @@ class RoadAndTrackBridge extends BridgeAbstract
$item['content'] = $content;
return $item;
}
private function getArticleContent($article)
{
return getContents($article->contentUrl);
}
}

View file

@ -286,9 +286,9 @@ class SpotifyBridge extends BridgeAbstract
} else {
$basicAuth = base64_encode(sprintf('%s:%s', $this->getInput('clientid'), $this->getInput('clientsecret')));
$json = getContents('https://accounts.spotify.com/api/token', [
"Authorization: Basic $basicAuth"
"Authorization: Basic $basicAuth",
], [
CURLOPT_POSTFIELDS => 'grant_type=client_credentials'
CURLOPT_POSTFIELDS => 'grant_type=client_credentials',
]);
$data = Json::decode($json);
$this->token = $data['access_token'];

View file

@ -20,8 +20,12 @@ class SummitsOnTheAirBridge extends BridgeAbstract
public function collectData()
{
$header = ['Content-type:application/json'];
$opts = [CURLOPT_HTTPGET => 1];
$header = [
'Content-type:application/json',
];
$opts = [
CURLOPT_HTTPGET => 1,
];
$json = getContents($this->getURI() . $this->getInput('c'), $header, $opts);
$spots = json_decode($json, true);

View file

@ -598,7 +598,7 @@ EXTERNAL;
private function makeApiCall($api, $authHeaders, $params)
{
$uri = self::API_URI . $api . '?' . http_build_query($params);
$result = getContents($uri, $authHeaders, [], false);
$result = getContents($uri, $authHeaders);
$data = json_decode($result);
return $data;
}

View file

@ -92,7 +92,7 @@ class UnogsBridge extends BridgeAbstract
{
$header = [
'Referer: https://unogs.com/',
'referrer: http://unogs.com'
'referrer: http://unogs.com',
];
$raw = getContents($url, $header);

View file

@ -511,11 +511,11 @@ class VkBridge extends BridgeAbstract
while ($redirects < 2) {
$response = getContents($uri, $httpHeaders, [CURLOPT_FOLLOWLOCATION => false], true);
if (in_array($response['code'], [200, 304])) {
return $response['content'];
if (in_array($response->getCode(), [200, 304])) {
return $response->getBody();
}
$headers = $response['headers'];
$headers = $response->getHeaders();
$uri = urljoin(self::URI, $headers['location'][0]);
if (str_contains($uri, '/429.html')) {

View file

@ -5,8 +5,8 @@
*
* @param array $httpHeaders E.g. ['Content-type: text/plain']
* @param array $curlOptions Associative array e.g. [CURLOPT_MAXREDIRS => 3]
* @param bool $returnFull Whether to return an array: ['code' => int, 'headers' => array, 'content' => string]
* @return string|array
* @param bool $returnFull Whether to return Response object
* @return string|Response
*/
function getContents(
string $url,
@ -113,13 +113,7 @@ function getContents(
throw $e;
}
if ($returnFull === true) {
// todo: return the actual response object
return [
'code' => $response->getCode(),
'headers' => $response->getHeaders(),
// For legacy reasons, use 'content' instead of 'body'
'content' => $response->getBody(),
];
return $response;
}
return $response->getBody();
}