完善 http 客户端
This commit is contained in:
parent
b7d951bdb7
commit
ca5e76497c
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 异步方法
|
||||
*
|
||||
|
@ -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日
|
||||
|
Loading…
x
Reference in New Issue
Block a user