Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Guzzle for HTTP requests #936

Merged
merged 8 commits into from
Jun 15, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
"bcosca/fatfree-core": "^3.6",
"fossar/tcpdf-parser": "^6.2",
"fossar/twitteroauth-php54": "^0.7.3-beta",
"fossar/guzzle-transcoder": "^0.0.3",
"guzzlehttp/guzzle": "^5.3",
"guzzlehttp/log-subscriber": "^1.0",
"htmlawed/htmlawed": "^1.1",
"j0k3r/graby": "^1.6",
"linkorb/jsmin-php": "^1.0",
Expand Down
220 changes: 219 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ function isNotUnimportant(dest) {
const filenameDisallowed = [
/^changelog/i,
/^contributing/i,
/^upgrading/i,
/^copying/i,
/^readme/i,
/^licen[cs]e/i,
Expand Down
3 changes: 2 additions & 1 deletion helpers/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace helpers;

use Elphin\IcoFileLoader\IcoFileService;
use GuzzleHttp;
use WideImage\WideImage;

/**
Expand Down Expand Up @@ -58,7 +59,7 @@ public function fetchFavicon($url, $isHtmlUrl = false, $width = false, $height =

$shortcutIcon = $this->parseShortcutIcon($html);
if ($shortcutIcon !== null) {
$shortcutIcon = (string) \SimplePie_IRI::absolutize($url, $shortcutIcon);
$shortcutIcon = (string) GuzzleHttp\Url::fromString($url)->combine($shortcutIcon);

$faviconAsPng = $this->loadImage($shortcutIcon, $width, $height);
if ($faviconAsPng !== false) {
Expand Down
64 changes: 44 additions & 20 deletions helpers/WebClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

namespace helpers;

use Exception;
use GuzzleHttp;
use GuzzleHttp\Subscriber\Log\LogSubscriber;
use Fossar\GuzzleTranscoder\GuzzleTranscoder;

/**
* Helper class for web request
*
Expand All @@ -10,6 +15,36 @@
* @author Alexandre Rossi <alexandre.rossi@gmail.com>
*/
class WebClient {
/** @var GuzzleHttp\Client */
private static $httpClient;

/**
* Provide a HTTP client for use by spouts
*
* @return GuzzleHttp\Client
*/
public static function getHttpClient() {
if (!isset(self::$httpClient)) {
$version = \F3::get('version');
$httpClient = new GuzzleHttp\Client([
'defaults' => [
'headers' => [
'User-Agent' => self::getUserAgent(),
]
]
]);
$httpClient->getEmitter()->attach(new GuzzleTranscoder());

if ($f3->get('logger_level') === 'DEBUG') {
$httpClient->getEmitter()->attach(new LogSubscriber(\F3::get('logger')));
}

self::$httpClient = $httpClient;
}

return self::$httpClient;
}

/**
* get the user agent to use for web based spouts
*
Expand All @@ -32,31 +67,20 @@ public static function getUserAgent($agentInfo = null) {
*
* @param string $subagent Extra user agent info to use in the request
*
* @throws GuzzleHttp\Exception\RequestException When an error is encountered
* @throws Exception Unless 200 0K response is received
*
* @return string request data
*/
public static function request($url, $agentInfo = null) {
$options = [
'user_agent' => self::getUserAgent($agentInfo),
'ignore_errors' => true,
'timeout' => 60
];
$request = \Web::instance()->request($url, $options);

// parse last (in case of redirects) HTTP status
$http_status = null;
foreach ($request['headers'] as $header) {
if (substr($header, 0, 5) == 'HTTP/') {
$tokens = explode(' ', $header);
if (isset($tokens[1])) {
$http_status = $tokens[1];
}
}
}
$http = self::getHttpClient();
$response = $http->get($url);
$data = (string) $response->getBody();

if ($http_status != '200') {
throw new \Exception(substr($request['body'], 0, 512));
if ($response->getStatusCode() !== 200) {
throw new Exception(substr($data, 0, 512));
}

return $request['body'];
return $data;
}
}
Loading