add multiGet and multiPost

This commit is contained in:
Jaeger 2018-12-11 17:52:41 +08:00
parent df521923ac
commit 0c85eed7ef
4 changed files with 72 additions and 16 deletions

View File

@ -29,8 +29,12 @@ class HttpServiceProvider implements ServiceProviderContract
return HttpService::postJson($this,...$args);
});
$kernel->bind('multiRequest',function (...$args){
return new MultiRequestService($this,...$args);
$kernel->bind('multiGet',function (...$args){
return new MultiRequestService($this,'get',...$args);
});
$kernel->bind('multiPost',function (...$args){
return new MultiRequestService($this,'post',...$args);
});
}
}

View File

@ -38,7 +38,8 @@ use QL\Services\MultiRequestService;
* @method QueryList get($url,$args = null,$otherArgs = [])
* @method QueryList post($url,$args = null,$otherArgs = [])
* @method QueryList postJson($url,$args = null,$otherArgs = [])
* @method MultiRequestService multiRequest($urls)
* @method MultiRequestService multiGet($urls)
* @method MultiRequestService multiPost($urls)
* @method QueryList use($plugins,...$opt)
* @method QueryList pipe(Closure $callback = null)
*/

View File

@ -11,6 +11,9 @@ namespace QL\Services;
use Jaeger\GHttp;
use Closure;
use GuzzleHttp\Psr7\Response;
use QL\QueryList;
use GuzzleHttp\Exception\RequestException;
/**
* Class MultiRequestService
@ -24,39 +27,40 @@ class MultiRequestService
{
protected $ql;
protected $multiRequest;
public function __construct($ql,$urls)
protected $method;
public function __construct(QueryList $ql,$method,$urls)
{
$this->ql = $ql;
$this->method = $method;
$this->multiRequest = GHttp::multiRequest($urls);
}
public function __call($name, $arguments)
{
return $this->multiRequest->$name(...$arguments);
$this->multiRequest = $this->multiRequest->$name(...$arguments);
return $this;
}
public function success(Closure $success)
{
return $this->multiRequest->success(function($response, $index) use($success){
$this->multiRequest = $this->multiRequest->success(function(Response $response, $index) use($success){
$this->ql->setHtml((String)$response->getBody());
$success($this->ql,$response, $index);
});
return $this;
}
public function error(Closure $error)
{
return $this->multiRequest->error(function($reason, $index) use($error){
$this->multiRequest = $this->multiRequest->error(function(RequestException $reason, $index) use($error){
$error($this->ql,$reason, $index);
});
return $this;
}
public function sendGet()
public function send()
{
$this->multiRequest->get();
}
public function sendPost()
{
$this->multiRequest->post();
$this->multiRequest->{$this->method}();
}
}

View File

@ -16,6 +16,18 @@ use Tests\TestCaseBase;
class HttpTest extends TestCaseBase
{
protected $urls;
public function setUp()
{
$this->urls = [
'http://httpbin.org/get?name=php',
'http://httpbin.org/get?name=golang',
'http://httpbin.org/get?name=c++',
'http://httpbin.org/get?name=java'
];
}
/**
* @test
*/
@ -34,8 +46,43 @@ class HttpTest extends TestCaseBase
/**
* @test
*/
public function concurrent_requests()
public function concurrent_requests_base_use()
{
$urls = $this->urls;
QueryList::getInstance()
->multiGet($urls)
->success(function(QueryList $ql,Response $response, $index) use($urls){
$body = json_decode((string)$response->getBody(),true);
$this->assertEquals($urls[$index],$body['url']);
})->send();
}
/**
* @test
*/
public function concurrent_requests_advanced_use()
{
$ua = 'QueryList/4.0';
$errorUrl = 'http://web-site-not-exist.com';
$urls = array_merge($this->urls,[$errorUrl]);
QueryList::rules([])
->multiGet($urls)
->concurrency(2)
->withOptions([
'timeout' => 60
])
->withHeaders([
'User-Agent' => $ua
])
->success(function (QueryList $ql, Response $response, $index) use($ua){
$body = json_decode((string)$response->getBody(),true);
$this->assertEquals($ua,$body['headers']['User-Agent']);
})
->error(function (QueryList $ql, $reason, $index) use($urls,$errorUrl){
$this->assertEquals($urls[$index],$errorUrl);
})
->send();
}
}