From 0c85eed7ef83514c215cb302c44105ceece4a9df Mon Sep 17 00:00:00 2001
From: Jaeger <JaegerCode@gmail.com>
Date: Tue, 11 Dec 2018 17:52:41 +0800
Subject: [PATCH] add multiGet and multiPost

---
 src/Providers/HttpServiceProvider.php |  8 +++--
 src/QueryList.php                     |  3 +-
 src/Services/MultiRequestService.php  | 26 ++++++++------
 tests/Feature/HttpTest.php            | 51 +++++++++++++++++++++++++--
 4 files changed, 72 insertions(+), 16 deletions(-)

diff --git a/src/Providers/HttpServiceProvider.php b/src/Providers/HttpServiceProvider.php
index d12ad08..c85a658 100644
--- a/src/Providers/HttpServiceProvider.php
+++ b/src/Providers/HttpServiceProvider.php
@@ -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);
         });
     }
 }
\ No newline at end of file
diff --git a/src/QueryList.php b/src/QueryList.php
index f3c4bfa..7946840 100644
--- a/src/QueryList.php
+++ b/src/QueryList.php
@@ -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)
  */
diff --git a/src/Services/MultiRequestService.php b/src/Services/MultiRequestService.php
index 3b0ea4a..803ee80 100644
--- a/src/Services/MultiRequestService.php
+++ b/src/Services/MultiRequestService.php
@@ -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}();
     }
 }
\ No newline at end of file
diff --git a/tests/Feature/HttpTest.php b/tests/Feature/HttpTest.php
index 1f51d2c..493cfbb 100644
--- a/tests/Feature/HttpTest.php
+++ b/tests/Feature/HttpTest.php
@@ -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();
     }
 }
\ No newline at end of file