Skip to content

Commit

Permalink
添加service request
Browse files Browse the repository at this point in the history
  • Loading branch information
titrxw committed Mar 28, 2022
1 parent 5deb32f commit 0238c82
Show file tree
Hide file tree
Showing 11 changed files with 672 additions and 106 deletions.
17 changes: 17 additions & 0 deletions Src/Exception/ResponseException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/**
* WeEngine Cloud SDK System
*
* (c) We7Team 2019 <https://www.w7.cc>
*
* This is not a free software
* Using it under the license terms
* visited https://www.w7.cc for more details
*/

namespace W7\Sdk\OpenCloud\Exception;

class ResponseException extends \Exception
{
}
89 changes: 89 additions & 0 deletions Src/Request/Middleware/Request/RetryMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

/**
* WeEngine Sdk Core System
*
* (c) We7Team 2019 <https://www.w7.cc>
*
* This is not a free software
* Using it under the license terms
* visited https://www.w7.cc for more details
*/

namespace W7\Sdk\OpenCloud\Request\Middleware\Request;

use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Request as PsrRequest;
use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\RequestInterface;
use Throwable;

class RetryMiddleware
{
protected $config = [];
/** @var callable */
protected $nextHandler;
protected $retryHandler;

public function __construct(callable $nextHandler, $config = [])
{
$this->config = $config;
$this->nextHandler = $nextHandler;
$this->retryHandler = new \GuzzleHttp\RetryMiddleware($this->retryDecider(), $this->nextHandler, $this->retryDelay());
}

public function __invoke(RequestInterface $request, array $options)
{
$retryHandler = $this->retryHandler;
return $retryHandler($request, $options);
}

protected function getRetryMaxNum()
{
return $this->config['max_retry_num'] ?? 1;
}

protected function getRetryDelay()
{
return $this->config['retry_delay'] ?? 500;
}

/**
* retryDecider
* 返回一个匿名函数, 匿名函数若返回false 表示不重试,反之则表示继续重试
* @return \Closure
*/
protected function retryDecider() : \Closure
{
return function (
$retries,
PsrRequest $request,
Response $response = null,
Throwable $exception = null
) {
// 超过最大重试次数,不再重试
if ($retries >= $this->getRetryMaxNum()) {
return false;
}

// 请求失败,继续重试
if ($exception instanceof ConnectException) {
return true;
}

return false;
};
}

/**
* 返回一个匿名函数,该匿名函数返回下次重试的时间(毫秒)
* @return \Closure
*/
protected function retryDelay() : \Closure
{
return function ($numberOfRetries) {
return $this->getRetryDelay() * $numberOfRetries;
};
}
}
43 changes: 43 additions & 0 deletions Src/Request/Middleware/Service/ServiceExtendMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/**
* WeEngine Sdk Core System
*
* (c) We7Team 2019 <https://www.w7.cc>
*
* This is not a free software
* Using it under the license terms
* visited https://www.w7.cc for more details
*/

namespace W7\Sdk\OpenCloud\Request\Middleware\Service;

use GuzzleHttp\Command\Command;
use GuzzleHttp\Command\Guzzle\GuzzleClient;
use W7\Sdk\OpenCloud\Request\ServiceRequest;

class ServiceExtendMiddleware extends ServiceMiddlewareAbstract
{
/**
* @var ServiceRequest
*/
protected $service;

public function __construct(GuzzleClient $client, ServiceRequest $service)
{
parent::__construct($client);

$this->service = $service;
}

public function __invoke(callable $handler)
{
return function (Command $command) use ($handler) {
$extend = $this->serviceClient->getDescription()->getOperation($command->getName())->toArray()['extends'] ?? '';
if ($extend && method_exists($this->service, $extend)) {
return $handler($this->service->$extend($command));
}
return $handler($command);
};
}
}
28 changes: 28 additions & 0 deletions Src/Request/Middleware/Service/ServiceMiddlewareAbstract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/**
* WeEngine Sdk Core System
*
* (c) We7Team 2019 <https://www.w7.cc>
*
* This is not a free software
* Using it under the license terms
* visited https://www.w7.cc for more details
*/

namespace W7\Sdk\OpenCloud\Request\Middleware\Service;

use GuzzleHttp\Command\Guzzle\GuzzleClient;

abstract class ServiceMiddlewareAbstract
{
/**
* @var GuzzleClient
*/
protected $serviceClient;

public function __construct(GuzzleClient $client)
{
$this->serviceClient = $client;
}
}
32 changes: 32 additions & 0 deletions Src/Request/Middleware/Service/ServiceNameMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/**
* WeEngine Sdk Core System
*
* (c) We7Team 2019 <https://www.w7.cc>
*
* This is not a free software
* Using it under the license terms
* visited https://www.w7.cc for more details
*/

namespace W7\Sdk\OpenCloud\Request\Middleware\Service;

use GuzzleHttp\Command\Command;

class ServiceNameMiddleware extends ServiceMiddlewareAbstract
{
const SERVICE_NAME_KEY = 'X-W7-OPEN-SERVICE-NAME';

public function __invoke(callable $handler)
{
return function (Command $command) use ($handler) {
$commandHttp = $command['@http'] ?? [];
$commandHttp['headers'] = $commandHttp['headers'] ?? [];
$commandHttp['headers'] = array_merge($commandHttp['headers'], [static::SERVICE_NAME_KEY => $command->getName()]);
$command['@http'] = $commandHttp;

return $handler($command);
};
}
}
Loading

0 comments on commit 0238c82

Please sign in to comment.