Skip to content

Commit

Permalink
add multiGet and multiPost
Browse files Browse the repository at this point in the history
jae-jae committed Dec 11, 2018
1 parent df52192 commit 0c85eed
Showing 4 changed files with 72 additions and 16 deletions.
8 changes: 6 additions & 2 deletions src/Providers/HttpServiceProvider.php
Original file line number Diff line number Diff line change
@@ -29,8 +29,12 @@ public function register(Kernel $kernel)
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);
});
}
}
3 changes: 2 additions & 1 deletion src/QueryList.php
Original file line number Diff line number Diff line change
@@ -38,7 +38,8 @@
* @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)
*/
26 changes: 15 additions & 11 deletions src/Services/MultiRequestService.php
Original file line number Diff line number Diff line change
@@ -11,6 +11,9 @@

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()
{
$this->multiRequest->get();
}

public function sendPost()
public function send()
{
$this->multiRequest->post();
$this->multiRequest->{$this->method}();
}
}
51 changes: 49 additions & 2 deletions tests/Feature/HttpTest.php
Original file line number Diff line number Diff line change
@@ -16,6 +16,18 @@

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 @@ public function can_post_json_data()
/**
* @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();
}
}

0 comments on commit 0c85eed

Please sign in to comment.