Skip to content

Commit

Permalink
增加 withMiddlewar,withRequestMiddleware withResponseMiddleware withBod…
Browse files Browse the repository at this point in the history
…y connectTimeout maxRedirects sink 方法
  • Loading branch information
yuanzhihai committed Aug 31, 2023
1 parent 3f69ecd commit 78ec5ce
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 15 deletions.
11 changes: 9 additions & 2 deletions src/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* @method static \yzh52521\EasyHttp\Request withVerify(bool|string $verify)
* @method static \yzh52521\EasyHttp\Request withHost(string $host)
* @method static \yzh52521\EasyHttp\Request withHeaders(array $headers)
* @method static \yzh52521\EasyHttp\Request withBody($content,$contentType='application/json')
* @method static \yzh52521\EasyHttp\Request withBasicAuth(string $username, string $password)
* @method static \yzh52521\EasyHttp\Request withDigestAuth(string $username, string $password)
* @method static \yzh52521\EasyHttp\Request withUA(string $ua)
Expand All @@ -22,15 +23,19 @@
* @method static \yzh52521\EasyHttp\Request withProxy(string|array $proxy)
* @method static \yzh52521\EasyHttp\Request withVersion(string $version)
* @method static \yzh52521\EasyHttp\Request withOptions(array $options)
* @method static \yzh52521\EasyHttp\Request withMiddleware(callable $middleware)
* @method static \yzh52521\EasyHttp\Request withRequestMiddleware(callable $middleware)
* @method static \yzh52521\EasyHttp\Request withResponseMiddleware(callable $middleware)
*
* @method static \yzh52521\EasyHttp\Request debug($class)
* @method static \yzh52521\EasyHttp\Request retry(int $retries=1,int $sleep=0)
* @method static \yzh52521\EasyHttp\Request delay(int $seconds)
* @method static \yzh52521\EasyHttp\Request timeout(float $seconds)
* @method static \yzh52521\EasyHttp\Request connectTimeout(float $seconds)
* @method static \yzh52521\EasyHttp\Request sink(string|resource $to)
* @method static \yzh52521\EasyHttp\Request concurrency(int $times)
* @method static \yzh52521\EasyHttp\Response client(string $method, string $url, array $options = [])
* @method static \yzh52521\EasyHttp\Response clientAsync(string $method, string $url, array $options = [])
* @method static \yzh52521\EasyHttp\Request removeBodyFormat()
* @method static \yzh52521\EasyHttp\Request maxRedirects(int $max)
*
* @method static \yzh52521\EasyHttp\Response get(string $url, array $query = [])
* @method static \yzh52521\EasyHttp\Response post(string $url, array $data = [])
Expand All @@ -39,6 +44,8 @@
* @method static \yzh52521\EasyHttp\Response delete(string $url, array $data = [])
* @method static \yzh52521\EasyHttp\Response head(string $url, array $data = [])
* @method static \yzh52521\EasyHttp\Response options(string $url, array $data = [])
* @method static \yzh52521\EasyHttp\Response client(string $method, string $url, array $options = [])
* @method static \yzh52521\EasyHttp\Response clientAsync(string $method, string $url, array $options = [])
*
* @method static \GuzzleHttp\Promise\PromiseInterface getAsync(string $url, array|null $query = null, callable $success = null, callable $fail = null)
* @method static \GuzzleHttp\Promise\PromiseInterface postAsync(string $url, array|null $data = null, callable $success = null, callable $fail = null)
Expand Down
113 changes: 100 additions & 13 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\HandlerStack;

use GuzzleHttp\Middleware;
use GuzzleHttp\Pool;
use GuzzleHttp\Client;
use GuzzleHttp\Promise;
Expand Down Expand Up @@ -44,6 +45,13 @@ class Request
*/
protected $bodyFormat;

/**
* The raw body for the request.
*
* @var string
*/
protected $pendingBody;

protected $isRemoveBodyFormat = false;

/**
Expand Down Expand Up @@ -80,10 +88,7 @@ public function __construct()
$this->options = [
'http_errors' => false,
];
if (!$this->handlerStack instanceof HandlerStack) {
$this->handlerStack = HandlerStack::create( new CurlHandler() );
}
$this->options['handler'] = $this->handlerStack;
$this->handlerStack = HandlerStack::create( new CurlHandler() );
}

/**
Expand Down Expand Up @@ -141,6 +146,33 @@ public function asMultipart(string $name,string $contents,string $filename = nul
return $this;
}

public function withMiddleware(callable $middleware)
{
$this->handlerStack->push($middleware);

$this->options['handler'] = $this->handlerStack;

return $this;
}

public function withRequestMiddleware(callable $middleware)
{
$this->handlerStack->push(Middleware::mapRequest($middleware));

$this->options['handler'] = $this->handlerStack;

return $this;
}

public function withResponseMiddleware(callable $middleware)
{
$this->handlerStack->push(Middleware::mapResponse($middleware));

$this->options['handler'] = $this->handlerStack;

return $this;
}

public function withHost(string $host)
{
$this->options['base_uri'] = $host;
Expand Down Expand Up @@ -173,6 +205,17 @@ public function withHeaders(array $headers)
return $this;
}

public function withBody($content, $contentType = 'application/json')
{
$this->bodyFormat = 'body';

$this->options['headers']['Content-Type'] = $contentType;

$this->pendingBody = $content;

return $this;
}

public function withBasicAuth(string $username,string $password)
{
$this->options['auth'] = [$username,$password];
Expand Down Expand Up @@ -224,6 +267,13 @@ public function withVersion($version)
return $this;
}

public function maxRedirects(int $max)
{
$this->options['allow_redirects']['max'] = $max;

return $this;
}

public function withRedirect($redirect = false)
{
$this->options['allow_redirects'] = $redirect;
Expand Down Expand Up @@ -255,6 +305,7 @@ public function concurrency(int $times)
public function retry(int $retries = 1,int $sleep = 0)
{
$this->handlerStack->push( ( new Retry() )->handle( $retries,$sleep ) );

$this->options['handler'] = $this->handlerStack;

return $this;
Expand All @@ -274,6 +325,24 @@ public function timeout(float $seconds)
return $this;
}

public function connectTimeout(float $seconds)
{
$this->options['connect_timeout'] = $seconds;

return $this;
}

/**
* @param string|resource $to
* @return $this
*/
public function sink($to)
{
$this->options['sink'] = $to;

return $this;
}

public function removeBodyFormat()
{
$this->isRemoveBodyFormat = true;
Expand Down Expand Up @@ -489,7 +558,11 @@ public function multiAsync(array $promises,callable $success = null,callable $fa

protected function request(string $method,string $url,array $options = [])
{
isset( $this->options[$this->bodyFormat] ) && $this->options[$this->bodyFormat] = $options;
if (isset($this->options[$this->bodyFormat])) {
$this->options[$this->bodyFormat] = $options;
} else {
$this->options[$this->bodyFormat] = $this->pendingBody;
}
if ($this->isRemoveBodyFormat) {
unset( $this->options[$this->bodyFormat] );
}
Expand All @@ -512,6 +585,14 @@ protected function request(string $method,string $url,array $options = [])
*/
public function client(string $method,string $url,array $options = [])
{
if (isset($this->options[$this->bodyFormat])) {
$this->options[$this->bodyFormat] = $options;
} else {
$this->options[$this->bodyFormat] = $this->pendingBody;
}
if ($this->isRemoveBodyFormat) {
unset( $this->options[$this->bodyFormat] );
}
try {
if (empty( $options )) {
$options = $this->options;
Expand All @@ -533,6 +614,14 @@ public function client(string $method,string $url,array $options = [])
*/
public function clientAsync(string $method,string $url,array $options = [])
{
if (isset($this->options[$this->bodyFormat])) {
$this->options[$this->bodyFormat] = $options;
} else {
$this->options[$this->bodyFormat] = $this->pendingBody;
}
if ($this->isRemoveBodyFormat) {
unset( $this->options[$this->bodyFormat] );
}
try {
if (empty( $options )) {
$options = $this->options;
Expand All @@ -545,13 +634,7 @@ public function clientAsync(string $method,string $url,array $options = [])
}


protected function requestAsync(
string $method,
string $url,
$options = null,
callable $success = null,
callable $fail = null
)
protected function requestAsync(string $method, string $url, $options = null, callable $success = null, callable $fail = null)
{
if (is_callable( $options )) {
$successCallback = $options;
Expand All @@ -561,7 +644,11 @@ protected function requestAsync(
$failCallback = $fail;
}

isset( $this->options[$this->bodyFormat] ) && $this->options[$this->bodyFormat] = $options;
if (isset($this->options[$this->bodyFormat])) {
$this->options[$this->bodyFormat] = $options;
} else {
$this->options[$this->bodyFormat] = $this->pendingBody;
}

if ($this->isRemoveBodyFormat) {
unset( $this->options[$this->bodyFormat] );
Expand Down

0 comments on commit 78ec5ce

Please sign in to comment.