PluginProbe
Gmail SMTP / trunk
Gmail SMTP vtrunk
1.2.3.21 1.2.3.20 trunk 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.9 1.2.0 1.2.3.14 1.2.3.15 1.2.3.16 1.2.3.18 1.2.3.5
gmail-smtp / google-api-php-client / vendor / google / auth / src / HttpHandler / Guzzle6HttpHandler.php

Guzzle6HttpHandler.php in Gmail SMTP trunk, at google-api-php-client/vendor/google/auth/src/HttpHandler/Guzzle6HttpHandler.php

141 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Copyright 2015 Google Inc. All Rights Reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 namespace Google\Auth\HttpHandler;
18
19 use Google\Auth\Logging\LoggingTrait;
20 use Google\Auth\Logging\RpcLogEvent;
21 use GuzzleHttp\ClientInterface;
22 use Psr\Http\Message\RequestInterface;
23 use Psr\Http\Message\ResponseInterface;
24 use Psr\Log\LoggerInterface;
25
26 class Guzzle6HttpHandler
27 {
28 use LoggingTrait;
29
30 /**
31 * @var ClientInterface
32 */
33 private $client;
34
35 /**
36 * @var null|LoggerInterface
37 */
38 private $logger;
39
40 /**
41 * @param ClientInterface $client
42 * @param null|LoggerInterface $logger
43 */
44 public function __construct(ClientInterface $client, ?LoggerInterface $logger = null)
45 {
46 $this->client = $client;
47 $this->logger = $logger;
48 }
49
50 /**
51 * Accepts a PSR-7 request and an array of options and returns a PSR-7 response.
52 *
53 * @param RequestInterface $request
54 * @param array<mixed> $options
55 * @return ResponseInterface
56 */
57 public function __invoke(RequestInterface $request, array $options = [])
58 {
59 $requestEvent = null;
60
61 if ($this->logger) {
62 $requestEvent = $this->requestLog($request, $options);
63 }
64
65 $response = $this->client->send($request, $options);
66
67 if ($this->logger) {
68 $this->responseLog($response, $requestEvent);
69 }
70
71 return $response;
72 }
73
74 /**
75 * Accepts a PSR-7 request and an array of options and returns a PromiseInterface
76 *
77 * @param RequestInterface $request
78 * @param array<mixed> $options
79 *
80 * @return \GuzzleHttp\Promise\PromiseInterface
81 */
82 public function async(RequestInterface $request, array $options = [])
83 {
84 $requestEvent = null;
85
86 if ($this->logger) {
87 $requestEvent = $this->requestLog($request, $options);
88 }
89
90 $promise = $this->client->sendAsync($request, $options);
91
92 if ($this->logger) {
93 $promise->then(function (ResponseInterface $response) use ($requestEvent) {
94 $this->responseLog($response, $requestEvent);
95 return $response;
96 });
97 }
98
99 return $promise;
100 }
101
102 /**
103 * @internal
104 * @param RequestInterface $request
105 * @param array<mixed> $options
106 */
107 public function requestLog(RequestInterface $request, array $options): RpcLogEvent
108 {
109 $requestEvent = new RpcLogEvent();
110
111 $requestEvent->method = $request->getMethod();
112 $requestEvent->url = (string) $request->getUri();
113 $requestEvent->headers = $request->getHeaders();
114 $requestEvent->payload = $request->getBody()->getContents();
115 $requestEvent->retryAttempt = $options['retryAttempt'] ?? null;
116 $requestEvent->serviceName = $options['serviceName'] ?? null;
117 $requestEvent->processId = (int) getmypid();
118 $requestEvent->requestId = $options['requestId'] ?? crc32((string) spl_object_id($request) . getmypid());
119
120 $this->logRequest($requestEvent);
121
122 return $requestEvent;
123 }
124
125 /**
126 * @internal
127 */
128 public function responseLog(ResponseInterface $response, RpcLogEvent $requestEvent): void
129 {
130 $responseEvent = new RpcLogEvent($requestEvent->milliseconds);
131
132 $responseEvent->headers = $response->getHeaders();
133 $responseEvent->payload = $response->getBody()->getContents();
134 $responseEvent->status = $response->getStatusCode();
135 $responseEvent->processId = $requestEvent->processId;
136 $responseEvent->requestId = $requestEvent->requestId;
137
138 $this->logResponse($responseEvent);
139 }
140 }
141