Skip to content

Commit

Permalink
Merge branch 'feature/multi' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
SmetDenis committed Jul 31, 2016
2 parents 9b5ec0f + 0eb3b80 commit a2764c9
Show file tree
Hide file tree
Showing 7 changed files with 264 additions and 28 deletions.
24 changes: 20 additions & 4 deletions src/Driver/Auto.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,38 @@ class Auto extends Driver
* @inheritdoc
*/
public function request($url, $args, $method, Options $options)
{
return $this->_getClient()->request($url, $args, $method, $options);
}

/**
* @inheritdoc
*/
public function multiRequest(array $urls)
{
return $this->_getClient()->multiRequest($urls);
}

/**
* @return Driver
*/
protected function _getClient()
{
if (class_exists('\GuzzleHttp\Client')
&& (version_compare(Env::getVersion(), '5.3', '>') || Env::isHHVM())
) {
if (method_exists('\GuzzleHttp\Client', 'request')) {
$client = new Guzzle6($options);
$client = new Guzzle6();

} elseif (method_exists('\GuzzleHttp\Client', 'createRequest')) {
$client = new Guzzle5($options);
$client = new Guzzle5();
}
}

if (!isset($client)) { // Fallback driver
$client = new Rmccue($options);
$client = new Rmccue();
}

return $client->request($url, $args, $method, $options);
return $client;
}
}
6 changes: 6 additions & 0 deletions src/Driver/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,10 @@ abstract class Driver
* @return array
*/
abstract public function request($url, $args, $method, Options $options);

/**
* @param array $urls
* @return array
*/
abstract public function multiRequest(array $urls);
}
74 changes: 65 additions & 9 deletions src/Driver/Guzzle5.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@

namespace JBZoo\HttpClient\Driver;

use GuzzleHttp\Message\Response;
use GuzzleHttp\Client;
use GuzzleHttp\Pool;
use JBZoo\HttpClient\Options;
use JBZoo\Utils\Url;

/**
* Class Guzzle5
Expand All @@ -30,25 +33,78 @@ public function request($url, $args, $method, Options $options)
{
$client = new Client();

$httpRequest = $client->createRequest($method, $url, $this->_getClientOptions($options, $method, $args));

$httpResult = $client->send($httpRequest);

return array(
$httpResult->getStatusCode(),
$httpResult->getHeaders(),
$httpResult->getBody()->getContents()
);
}

/**
* @inheritdoc
*/
public function multiRequest(array $urls)
{
$client = new Client();

$requests = array();
foreach ($urls as $urlName => $urlData) {
$urlOptions = new Options($urlData[1]);

$method = $urlOptions->get('method', 'GET', 'up');
$args = $urlOptions->get('args');
$url = 'GET' === $method ? Url::addArg((array)$args, $urlData[0]) : $urlData[0];

$requests[$urlName] = $client->createRequest(
$method,
$url,
$this->_getClientOptions($urlOptions, $method, $args)
);
}

$httpResults = Pool::batch($client, $requests);

/** @var string $resName */
/** @var Response $httpResult */
$result = array();

$index = 0;
$keys = array_keys($urls);
foreach ($keys as $resName) {
$httpResult = $httpResults->offsetGet($index++);
$result[$resName] = array(
$httpResult->getStatusCode(),
$httpResult->getHeaders(),
$httpResult->getBody()->getContents()
);
}

return $result;
}

/**
* @param Options $options
* @param string $method
* @param string|array $args
* @return array
*/
protected function _getClientOptions(Options $options, $method, $args)
{
$headers = $options->getHeaders();
$headers['User-Agent'] = $options->getUserAgent('Guzzle5');

$httpRequest = $client->createRequest($method, $url, array(
return array(
'body' => 'GET' !== $method ? $args : null,
'headers' => $headers,
'exceptions' => $options->isExceptions(),
'timeout' => $options->getTimeout(),
'verify' => $options->isVerify(),
'auth' => $options->getAuth(),
'allow_redirects' => $this->_getAllowRedirects($options)
));

$httpResult = $client->send($httpRequest);

return array(
$httpResult->getStatusCode(),
$httpResult->getHeaders(),
$httpResult->getBody()->getContents()
);
}
}
66 changes: 59 additions & 7 deletions src/Driver/Guzzle6.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
namespace JBZoo\HttpClient\Driver;

use GuzzleHttp\Client;
use GuzzleHttp\Promise;
use GuzzleHttp\Psr7\Response;
use JBZoo\HttpClient\Options;
use JBZoo\Utils\Url;

/**
* Class Guzzle6
Expand All @@ -30,6 +33,61 @@ public function request($url, $args, $method, Options $options)
{
$client = new Client();

$httpResult = $client->request($method, $url, $this->_getClientOptions($options, $method, $args));

return array(
$httpResult->getStatusCode(),
$httpResult->getHeaders(),
$httpResult->getBody()->getContents()
);
}

/**
* @inheritdoc
*/
public function multiRequest(array $urls)
{
$client = new Client();

$promises = array();
foreach ($urls as $urlName => $urlData) {
$urlOptions = new Options($urlData[1]);

$method = $urlOptions->get('method', 'GET', 'up');
$args = $urlOptions->get('args');
$url = 'GET' === $method ? Url::addArg((array)$args, $urlData[0]) : $urlData[0];

$promises[$urlName] = $client->requestAsync(
$method,
$url,
$this->_getClientOptions($urlOptions, $method, $args)
);
}

$httpResults = Promise\unwrap($promises);

/** @var string $resName */
/** @var Response $httpResult */
$result = array();
foreach ($httpResults as $resName => $httpResult) {
$result[$resName] = array(
$httpResult->getStatusCode(),
$httpResult->getHeaders(),
$httpResult->getBody()->getContents()
);
}

return $result;
}

/**
* @param Options $options
* @param string $method
* @param string|array $args
* @return array
*/
protected function _getClientOptions(Options $options, $method, $args)
{
$headers = $options->getHeaders();
$headers['User-Agent'] = $options->getUserAgent('Guzzle6');

Expand All @@ -42,7 +100,7 @@ public function request($url, $args, $method, Options $options)
}
}

$httpResult = $client->request($method, $url, array(
return array(
'form_params' => $formParams,
'body' => $body,
'headers' => $headers,
Expand All @@ -52,12 +110,6 @@ public function request($url, $args, $method, Options $options)
'exceptions' => $options->isExceptions(),
'auth' => $options->getAuth(),
'allow_redirects' => $this->_getAllowRedirects($options)
));

return array(
$httpResult->getStatusCode(),
$httpResult->getHeaders(),
$httpResult->getBody()->getContents()
);
}
}
73 changes: 65 additions & 8 deletions src/Driver/Rmccue.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

use JBZoo\HttpClient\Exception;
use JBZoo\HttpClient\Options;
use JBZoo\Utils\Filter;
use JBZoo\Utils\Url;

/**
* Class Rmccue
Expand All @@ -29,16 +31,16 @@ class Rmccue extends Driver
public function request($url, $args, $method, Options $options)
{
$headers = $options->get('headers', array());
$method = Filter::up($method);
$args = 'GET' !== $method ? $args : array();

$httpResult = \Requests::request($url, $headers, $args, $method, array(
'timeout' => $options->getTimeout(),
'verify' => $options->isVerify(),
'follow_redirects' => $options->isAllowRedirects(),
'redirects' => $options->getMaxRedirects(),
'useragent' => $options->getUserAgent('Rmccue'),
'auth' => $options->getAuth(),
));
$httpResult = \Requests::request(
$url,
$headers,
$args,
$method,
$this->_getClientOptions($options)
);

if ($options->isExceptions() && $httpResult->status_code >= 400) {
throw new Exception($httpResult->body, $httpResult->status_code);
Expand All @@ -50,4 +52,59 @@ public function request($url, $args, $method, Options $options)
$httpResult->body
);
}

/**
* @inheritdoc
*/
public function multiRequest(array $urls)
{
$requests = array();
foreach ($urls as $urlName => $urlData) {
$urlOptions = new Options($urlData[1]);

$method = $urlOptions->get('method', 'GET', 'up');
$args = $urlOptions->get('args');
$url = 'GET' === $method ? Url::addArg((array)$args, $urlData[0]) : $urlData[0];
$args = 'GET' !== $method ? $args : array();

$requests[$urlName] = array(
'url' => $url,
'headers' => $urlOptions->getHeaders(),
'data' => $args,
'type' => $method,
'options' => $this->_getClientOptions($urlOptions),
);
}

$httpResults = \Requests::request_multiple($requests);

/** @var string $resName */
/** @var \Requests_Response $httpResult */
$result = array();
foreach ($httpResults as $resName => $httpResult) {
$result[$resName] = array(
$httpResult->status_code,
$httpResult->headers->getAll(),
$httpResult->body
);
}

return $result;
}

/**
* @param Options $options
* @return array
*/
protected function _getClientOptions(Options $options)
{
return array(
'timeout' => $options->getTimeout(),
'verify' => $options->isVerify(),
'follow_redirects' => $options->isAllowRedirects(),
'redirects' => $options->getMaxRedirects(),
'useragent' => $options->getUserAgent('Rmccue'),
'auth' => $options->getAuth(),
);
}
}
25 changes: 25 additions & 0 deletions src/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,31 @@ public function request($url, $args = null, $method = Options::DEFAULT_METHOD, a
return $response;
}

/**
* @param array $urls
* @param array $options
* @return array
*/
public function multiRequest(array $urls, array $options = array())
{
$options = new Options(array_merge($this->_options->getArrayCopy(), $options));
$client = $this->_getClient($options);

$httpResults = $client->multiRequest($urls, $options);

$results = array();
foreach ($httpResults as $resKey => $resultRow) {
list($code, $headers, $body) = $resultRow;

$results[$resKey] = new Response();
$results[$resKey]->setCode($code);
$results[$resKey]->setHeaders($headers);
$results[$resKey]->setBody($body);
}

return $results;
}

/**
* @param Options $options
* @return Driver
Expand Down

0 comments on commit a2764c9

Please sign in to comment.