← All changes
|
includes/sdk/google/google/auth/src/HttpHandler/Guzzle6HttpHandler.php
+68
-7
1.2.4
→
1.4.2
View file →
| @@ -14,25 +14,35 @@ | ||
| 14 | 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | 15 | * See the License for the specific language governing permissions and |
| 16 | 16 | * limitations under the License. |
| 17 | 17 | */ |
| 18 | -namespace Dudlewebs\WPMCS\Google\Auth\HttpHandler; | |
| 18 | +namespace Dudlewebs\WPMCS\GCP\Google\Auth\HttpHandler; | |
| 19 | 19 | |
| 20 | -use Dudlewebs\WPMCS\GuzzleHttp\ClientInterface; | |
| 21 | -use Dudlewebs\WPMCS\Psr\Http\Message\RequestInterface; | |
| 22 | -use Dudlewebs\WPMCS\Psr\Http\Message\ResponseInterface; | |
| 20 | +use Dudlewebs\WPMCS\GCP\Google\Auth\Logging\LoggingTrait; | |
| 21 | +use Dudlewebs\WPMCS\GCP\Google\Auth\Logging\RpcLogEvent; | |
| 22 | +use Dudlewebs\WPMCS\GCP\GuzzleHttp\ClientInterface; | |
| 23 | +use Dudlewebs\WPMCS\GCP\Psr\Http\Message\RequestInterface; | |
| 24 | +use Dudlewebs\WPMCS\GCP\Psr\Http\Message\ResponseInterface; | |
| 25 | +use Dudlewebs\WPMCS\GCP\Psr\Log\LoggerInterface; | |
| 23 | 26 | class Guzzle6HttpHandler |
| 24 | 27 | { |
| 28 | + use LoggingTrait; | |
| 25 | 29 | /** |
| 26 | 30 | * @var ClientInterface |
| 27 | 31 | */ |
| 28 | 32 | private $client; |
| 29 | 33 | /** |
| 34 | + * @var null|LoggerInterface | |
| 35 | + */ | |
| 36 | + private $logger; | |
| 37 | + /** | |
| 30 | 38 | * @param ClientInterface $client |
| 39 | + * @param null|LoggerInterface $logger | |
| 31 | 40 | */ |
| 32 | - public function __construct(ClientInterface $client) | |
| 41 | + public function __construct(ClientInterface $client, ?LoggerInterface $logger = null) | |
| 33 | 42 | { |
| 34 | 43 | $this->client = $client; |
| 44 | + $this->logger = $logger; | |
| 35 | 45 | } |
| 36 | 46 | /** |
| 37 | 47 | * Accepts a PSR-7 request and an array of options and returns a PSR-7 response. |
| 38 | 48 | * |
| @@ -41,9 +51,17 @@ | ||
| 41 | 51 | * @return ResponseInterface |
| 42 | 52 | */ |
| 43 | 53 | public function __invoke(RequestInterface $request, array $options = []) |
| 44 | 54 | { |
| 45 | - return $this->client->send($request, $options); | |
| 55 | + $requestEvent = null; | |
| 56 | + if ($this->logger) { | |
| 57 | + $requestEvent = $this->requestLog($request, $options); | |
| 58 | + } | |
| 59 | + $response = $this->client->send($request, $options); | |
| 60 | + if ($this->logger) { | |
| 61 | + $this->responseLog($response, $requestEvent); | |
| 62 | + } | |
| 63 | + return $response; | |
| 46 | 64 | } |
| 47 | 65 | /** |
| 48 | 66 | * Accepts a PSR-7 request and an array of options and returns a PromiseInterface |
| 49 | 67 | * |
| @@ -53,7 +71,50 @@ | ||
| 53 | 71 | * @return \GuzzleHttp\Promise\PromiseInterface |
| 54 | 72 | */ |
| 55 | 73 | public function async(RequestInterface $request, array $options = []) |
| 56 | 74 | { |
| 57 | - return $this->client->sendAsync($request, $options); | |
| 75 | + $requestEvent = null; | |
| 76 | + if ($this->logger) { | |
| 77 | + $requestEvent = $this->requestLog($request, $options); | |
| 78 | + } | |
| 79 | + $promise = $this->client->sendAsync($request, $options); | |
| 80 | + if ($this->logger) { | |
| 81 | + $promise->then(function (ResponseInterface $response) use($requestEvent) { | |
| 82 | + $this->responseLog($response, $requestEvent); | |
| 83 | + return $response; | |
| 84 | + }); | |
| 85 | + } | |
| 86 | + return $promise; | |
| 87 | + } | |
| 88 | + /** | |
| 89 | + * @internal | |
| 90 | + * @param RequestInterface $request | |
| 91 | + * @param array<mixed> $options | |
| 92 | + */ | |
| 93 | + public function requestLog(RequestInterface $request, array $options) : RpcLogEvent | |
| 94 | + { | |
| 95 | + $requestEvent = new RpcLogEvent(); | |
| 96 | + $requestEvent->method = $request->getMethod(); | |
| 97 | + $requestEvent->url = (string) $request->getUri(); | |
| 98 | + $requestEvent->headers = $request->getHeaders(); | |
| 99 | + $requestEvent->payload = $request->getBody()->getContents(); | |
| 100 | + $requestEvent->retryAttempt = $options['retryAttempt'] ?? null; | |
| 101 | + $requestEvent->serviceName = $options['serviceName'] ?? null; | |
| 102 | + $requestEvent->processId = (int) \getmypid(); | |
| 103 | + $requestEvent->requestId = $options['requestId'] ?? \crc32((string) \spl_object_id($request) . \getmypid()); | |
| 104 | + $this->logRequest($requestEvent); | |
| 105 | + return $requestEvent; | |
| 106 | + } | |
| 107 | + /** | |
| 108 | + * @internal | |
| 109 | + */ | |
| 110 | + public function responseLog(ResponseInterface $response, RpcLogEvent $requestEvent) : void | |
| 111 | + { | |
| 112 | + $responseEvent = new RpcLogEvent($requestEvent->milliseconds); | |
| 113 | + $responseEvent->headers = $response->getHeaders(); | |
| 114 | + $responseEvent->payload = $response->getBody()->getContents(); | |
| 115 | + $responseEvent->status = $response->getStatusCode(); | |
| 116 | + $responseEvent->processId = $requestEvent->processId; | |
| 117 | + $responseEvent->requestId = $requestEvent->requestId; | |
| 118 | + $this->logResponse($responseEvent); | |
| 58 | 119 | } |
| 59 | 120 | } |