From ca5e76497c235b24ff0c1842d27c69530d193598 Mon Sep 17 00:00:00 2001 From: JaguarJack Date: Fri, 22 May 2020 14:08:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=20http=20=E5=AE=A2=E6=88=B7?= =?UTF-8?q?=E7=AB=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extend/catcher/library/client/Http.php | 97 +++++++++++++++------- extend/catcher/library/client/Response.php | 26 ++++-- 2 files changed, 86 insertions(+), 37 deletions(-) diff --git a/extend/catcher/library/client/Http.php b/extend/catcher/library/client/Http.php index 7992e7f..4d93428 100644 --- a/extend/catcher/library/client/Http.php +++ b/extend/catcher/library/client/Http.php @@ -3,6 +3,7 @@ namespace catcher\library\client; use catcher\exceptions\FailedException; use GuzzleHttp\Client; +use GuzzleHttp\Promise\Promise; use GuzzleHttp\TransferStats; use Psr\Http\Message\ResponseInterface; use function GuzzleHttp\Psr7\stream_for; @@ -49,32 +50,27 @@ class Http protected $async = false; + /** + * @var array + */ protected $timeout = []; /** - * Http constructor. - * @param string $baseUri + * @var string */ - public function __construct(string $baseUri = '') - { - $this->getClient($baseUri); - - - } + protected $token = ''; + protected $multipart = []; /** * 获取 Guzzle 客户端 * - * @param $baseUri * @time 2020年05月21日 * @return Client */ - public function getClient(string $baseUri) + public function getClient() { if (!$this->client) { - $this->client = $baseUri ? new Client([ - 'base_uri' => $baseUri - ]) : new Client; + $this->client = new Client; } return $this->client; @@ -89,9 +85,24 @@ class Http */ public function headers(array $headers) { - $this->header = [ - 'headers' => $headers - ]; + + $this->header = isset($this->header['headers']) ? + array_merge($this->header['headers'], $headers) : + [ 'headers' => $headers ]; + + return $this; + } + + /** + * set bearer token + * + * @time 2020年05月22日 + * @param string $token + * @return $this + */ + public function token(string $token) + { + $this->header['headers']['authorization'] = 'Bearer '. $token; return $this; } @@ -151,10 +162,10 @@ class Http * @param $params * @return $this */ - public function formParams($params) + public function form(array $params) { $this->formParams = [ - 'form_params' => $params + 'form_params' => array_merge($this->multipart, $params) ]; return $this; @@ -183,11 +194,9 @@ class Http * @param $url * @return Response */ - public function get($url) + public function get(string $url) { - $response = $this->client->{$this->asyncMethod(__FUNCTION__)}($url, array_merge($this->header, $this->query, $this->timeout)); - - return new Response($response); + return new Response($this->getClient()->{$this->asyncMethod(__FUNCTION__)}($url, array_merge($this->header, $this->query, $this->timeout))); } /** @@ -197,11 +206,11 @@ class Http * @param $url * @return mixed */ - public function post($url) + public function post(string $url) { - return $this->client->{$this->asyncMethod(__FUNCTION__)}($url, array_merge( - $this->header, $this->body, $this->formParams, $this->json, $this->timeout - ) ); + return new Response($this->getClient()->{$this->asyncMethod(__FUNCTION__)}($url, array_merge( + $this->header, $this->body, $this->formParams, $this->json, $this->timeout, $this->multipart + ))); } /** @@ -211,11 +220,11 @@ class Http * @param $url * @return mixed */ - public function put($url) + public function put(string $url) { - return $this->client->{$this->asyncMethod(__FUNCTION__)}($url, array_merge( + return new Response($this->getClient()->{$this->asyncMethod(__FUNCTION__)}($url, array_merge( $this->header, $this->body, $this->formParams, $this->json, $this->timeout - )); + ))); } /** @@ -225,11 +234,11 @@ class Http * @param $url * @return mixed */ - public function delete($url) + public function delete(string $url) { - return $this->client{$this->asyncMethod(__FUNCTION__)}($url, array_merge( + return new Response($this->getClient()->{$this->asyncMethod(__FUNCTION__)}($url, array_merge( $this->header, $this->query, $this->timeout - )); + ))); } /** @@ -245,6 +254,30 @@ class Http return $this; } + /** + * 附件 + * + * @time 2020年05月22日 + * @param $name + * @param $resource + * @param $filename + * @return $this + */ + public function attach(string $name, $resource, string $filename) + { + $this->multipart = [ + 'multipart' => [ + [ + 'name' => $name, + 'contents' => $resource, + 'filename' => $filename, + ] + ] + ]; + + return $this; + } + /** * 异步方法 * diff --git a/extend/catcher/library/client/Response.php b/extend/catcher/library/client/Response.php index 8d30d05..975e8e6 100644 --- a/extend/catcher/library/client/Response.php +++ b/extend/catcher/library/client/Response.php @@ -2,6 +2,8 @@ namespace catcher\library\client; +use GuzzleHttp\Promise\Promise; + /** * http response * @@ -12,21 +14,22 @@ namespace catcher\library\client; class Response implements \ArrayAccess { /** - * @var \GuzzleHttp\Psr7\Response + * @var \GuzzleHttp\Psr7\Response|Promise */ protected $response; - public function __construct(\GuzzleHttp\Psr7\Response $response) + public function __construct($response) { $this->response = $response; } + /** * - * @time 2020年05月21日 - * @return string + * @time 2020年05月22日 + * @return bool|callable|float|\GuzzleHttp\Psr7\PumpStream|\GuzzleHttp\Psr7\Stream|int|\Iterator|\Psr\Http\Message\StreamInterface|resource|string|null */ - public function body():string + public function body() { return $this->response->getBody(); } @@ -91,6 +94,19 @@ class Response implements \ArrayAccess return $this->response->getHeaders(); } + /** + * 异步回调 + * + * @time 2020年05月22日 + * @param callable $response + * @param callable $exception + * @return \GuzzleHttp\Promise\FulfilledPromise|Promise|\GuzzleHttp\Promise\PromiseInterface|\GuzzleHttp\Promise\RejectedPromise + */ + public function then(callable $response, callable $exception) + { + return $this->response->then($response, $exception); + } + /** * * @time 2020年05月21日