Skip to content

Commit

Permalink
feat: add retry logic to the http client (#2692)
Browse files Browse the repository at this point in the history
* refactor: extract http client

* feat: add retry logic to http client
  • Loading branch information
dvikan committed May 8, 2022
1 parent 0c7a7f3 commit 5d77d14
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 312 deletions.
8 changes: 4 additions & 4 deletions bridges/TwitterBridge.php
Expand Up @@ -610,8 +610,8 @@ private function makeApiCall($api, $params) {

try {
$result = getContents($uri, $this->authHeaders, array(), true);
} catch (UnexpectedResponseException $e) {
switch ($e->getResponseCode()) {
} catch (HttpException $e) {
switch ($e->getCode()) {
case 401:
case 403:
if ($retries) {
Expand All @@ -621,8 +621,8 @@ private function makeApiCall($api, $params) {
continue 2;
}
default:
$code = $e->getResponseCode();
$data = $e->getResponseBody();
$code = $e->getCode();
$data = $e->getMessage();
returnServerError(<<<EOD
Failed to make api call: $api
HTTP Status: $code
Expand Down
65 changes: 10 additions & 55 deletions contrib/prepare_release/fetch_contributors.php
@@ -1,36 +1,26 @@
<?php
/* Generate the "Contributors" list for README.md automatically utilizing the GitHub API */

require __DIR__ . '/../../lib/rssbridge.php';

$url = 'https://api.github.com/repos/rss-bridge/rss-bridge/contributors';
$contributors = array();
$next = true;

while($next) { /* Collect all contributors */

$c = curl_init();

curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Content-Type: application/json',
'User-Agent: RSS-Bridge'
));
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_HEADER, true);

$data = curl_exec($c);
$headers = [
'Accept: application/json',
'Content-Type: application/json',
'User-Agent: RSS-Bridge'
];
$result = _http_request($url, ['headers' => $headers]);

$headerSize = curl_getinfo($c, CURLINFO_HEADER_SIZE);
$header = substr($data, 0, $headerSize);
$headers = parseResponseHeader($header);

curl_close($c);

foreach(json_decode(substr($data, $headerSize)) as $contributor)
foreach(json_decode($result['body']) as $contributor)
$contributors[] = $contributor;

// Extract links to "next", "last", etc...
$links = explode(',', $headers[0]['link']);
$links = explode(',', $result['headers']['link'][0]);
$next = false;

// Check if there is a link with 'rel="next"'
Expand All @@ -57,38 +47,3 @@
foreach($contributors as $contributor) {
echo " * [{$contributor->login}]({$contributor->html_url})\n";
}

/**
* Parses the provided response header into an associative array
*
* Based on https://stackoverflow.com/a/18682872
*/
function parseResponseHeader($header) {

$headers = array();
$requests = explode("\r\n\r\n", trim($header));

foreach ($requests as $request) {

$header = array();

foreach (explode("\r\n", $request) as $i => $line) {

if($i === 0) {
$header['http_code'] = $line;
} else {

list ($key, $value) = explode(': ', $line);
$header[$key] = $value;

}

}

$headers[] = $header;

}

return $headers;

}

0 comments on commit 5d77d14

Please sign in to comment.