Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/ActivityPhp/Server/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,18 @@ public static function validateAcceptHeader($accept, $strict = false)
*
* @param string $url
* @param float|int $timeout
* @param array $options
* @return array
*/
public static function fetch($url, $timeout = 10.0)
public static function fetch($url, $timeout = 10.0, array $options = [])
{
return Util::decodeJson(
(new HttpRequest($timeout))
->setMaxRetries(
Server::server()->config('http')->get('retries'),
Server::server()->config('http')->get('sleep')
)
->get($url)
->get($url, $options)
);
}
}
28 changes: 25 additions & 3 deletions src/ActivityPhp/Server/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ class Request
*/
protected $retryCounter = 0;

/**
* HTTP options
*
* @var array
*/
protected static $options = [];

/**
* Set HTTP client
*
Expand Down Expand Up @@ -116,20 +123,35 @@ protected function getMethod()
return $this->method;
}

/**
* Set HTTP options
*
* @param array $options
*/
public static function setOptions(array $options)
{
self::$options = $options;
}

/**
* Execute a GET request
*
* @param string $url
* @param array $options
* @return string
*/
public function get(string $url)
public function get(string $url, array $options = [])
{
if (CacheHelper::has($url)) {
return CacheHelper::get($url);
}

if (empty($options)) {
$options = self::$options;
}

try {
$content = $this->client->get($url)->getBody()->getContents();
$content = $this->client->get($url, $options)->getBody()->getContents();
} catch (Exception $e) {
Server::server()->logger()->error(
__METHOD__ . ':failure',
Expand All @@ -144,7 +166,7 @@ public function get(string $url)
['url' => $url]
);
sleep($this->sleepBeforeRetry);
return $this->get($url);
return $this->get($url, $options);
}

throw new Exception($e->getMessage());
Expand Down
Loading