Skip to content

Commit

Permalink
refactor: return proper response object (#4169)
Browse files Browse the repository at this point in the history
  • Loading branch information
dvikan authored Jul 31, 2024
1 parent aa39898 commit 891c897
Show file tree
Hide file tree
Showing 28 changed files with 69 additions and 84 deletions.
4 changes: 2 additions & 2 deletions actions/ConnectivityAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ private function reportBridgeConnectivity($bridgeClassName)
];
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) {
Expand Down
3 changes: 1 addition & 2 deletions bridges/BadDragonBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,7 @@ public function collectData()
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 = [];
Expand Down
11 changes: 6 additions & 5 deletions bridges/BandcampBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,12 @@ public function collectData()
$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);

Expand Down Expand Up @@ -314,7 +314,8 @@ private function apiGet($endpoint, $query_data)
{
$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;
}

Expand Down
4 changes: 3 additions & 1 deletion bridges/CubariBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ public function getURI()
*/
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'];

Expand Down
3 changes: 2 additions & 1 deletion bridges/DemosBerlinBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public function getIcon()

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');
Expand Down
10 changes: 3 additions & 7 deletions bridges/DerpibooruBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,9 @@ public function getURI()

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 = [];
Expand Down
4 changes: 3 additions & 1 deletion bridges/EZTVBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ public function collectData()
$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;
Expand Down
8 changes: 3 additions & 5 deletions bridges/ElloBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,9 @@ public function collectData()
];

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);
Expand Down Expand Up @@ -117,7 +115,7 @@ private function getAPIKey()
$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);
Expand Down
2 changes: 1 addition & 1 deletion bridges/FDroidBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ private function getTimestamp($url)
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;
}
Expand Down
2 changes: 1 addition & 1 deletion bridges/FunkBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function collectData()
$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) {
Expand Down
6 changes: 3 additions & 3 deletions bridges/GlowficBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ public function collectData()
$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 = [];
Expand Down Expand Up @@ -75,8 +74,9 @@ public function getURI()
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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function collectData()
];
$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);
Expand Down
4 changes: 2 additions & 2 deletions bridges/ItakuBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -669,11 +669,11 @@ private function getData(string $url, bool $cache = false, bool $getJSON = false
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
Expand Down
3 changes: 1 addition & 2 deletions bridges/KilledbyGoogleBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 1 addition & 2 deletions bridges/LegoIdeasBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ public function collectData()
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);
Expand Down
13 changes: 5 additions & 8 deletions bridges/OpenCVEBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,9 @@ public function collectData()
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 . '] ';
Expand Down Expand Up @@ -205,10 +204,8 @@ private function getTitle($titlePrefix, $cveItem)
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);
Expand Down
7 changes: 2 additions & 5 deletions bridges/PepperBridgeAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,15 +191,12 @@ protected function collectDataTalk()
}
}

/**
* 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);
}

Expand Down
17 changes: 8 additions & 9 deletions bridges/PixivBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,21 +332,20 @@ private function getData(string $url, bool $cache = true, bool $getJSON = false,
}

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();
}
}
2 changes: 1 addition & 1 deletion bridges/RainbowSixSiegeBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function collectData()
$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);
Expand Down
2 changes: 1 addition & 1 deletion bridges/RedditBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ private function collectDataInternal(): void

$response = getContents($url, ['User-Agent: ' . $useragent], [], true);

$json = $response['content'];
$json = $response->getBody();

$parsedJson = Json::decode($json, false);

Expand Down
4 changes: 3 additions & 1 deletion bridges/ReutersBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -417,9 +417,11 @@ private function handleArticleContent($contents)
$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;
Expand Down
5 changes: 0 additions & 5 deletions bridges/RoadAndTrackBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,4 @@ private function fetchArticle($articleLink)
$item['content'] = $content;
return $item;
}

private function getArticleContent($article)
{
return getContents($article->contentUrl);
}
}
4 changes: 2 additions & 2 deletions bridges/SpotifyBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,9 @@ private function fetchAccessToken()
} 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'];
Expand Down
8 changes: 6 additions & 2 deletions bridges/SummitsOnTheAirBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion bridges/TwitterV2Bridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ private static function compareTweetDate($tweet1, $tweet2)
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;
}
Expand Down
2 changes: 1 addition & 1 deletion bridges/UnogsBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private function getJSON($url)
{
$header = [
'Referer: https://unogs.com/',
'referrer: http://unogs.com'
'referrer: http://unogs.com',
];

$raw = getContents($url, $header);
Expand Down
6 changes: 3 additions & 3 deletions bridges/VkBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -511,11 +511,11 @@ private function getContents()
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')) {
Expand Down
Loading

0 comments on commit 891c897

Please sign in to comment.