-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
672 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
43
Src/Request/Middleware/Service/ServiceExtendMiddleware.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
Src/Request/Middleware/Service/ServiceMiddlewareAbstract.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; | ||
} | ||
} |
Oops, something went wrong.