-
Notifications
You must be signed in to change notification settings - Fork 24
/
PantheonGuzzle.php
179 lines (169 loc) · 5.43 KB
/
PantheonGuzzle.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
namespace Drupal\search_api_pantheon\Services;
use Drupal\search_api_pantheon\Traits\EndpointAwareTrait;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use Http\Factory\Guzzle\RequestFactory;
use Http\Factory\Guzzle\StreamFactory;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Solarium\Core\Client\Adapter\AdapterInterface;
use Solarium\Core\Client\Adapter\Psr18Adapter;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Session\AccountProxyInterface;
/**
* Pantheon-specific extension of the Guzzle http query class.
*
* @package \Drupal\search_api_pantheon
*/
class PantheonGuzzle extends Client implements
ClientInterface,
LoggerAwareInterface {
use LoggerAwareTrait;
use EndpointAwareTrait;
public static $messageFormats = [
'{method} {uri} HTTP/{version}',
'HEADERS: {req_headers}',
'BODY: {req_body}',
'RESPONSE: {code} - {res_body}',
];
/**
* Class Constructor.
*/
public function __construct(Endpoint $endpoint, LoggerChannelFactoryInterface $logger_factory, AccountProxyInterface $current_user) {
$stack = new HandlerStack();
$stack->setHandler(new CurlHandler());
$stack->push(
Middleware::mapRequest([$this, 'requestUriAlterForPantheonEnvironment']),
'rewriter'
);
/**
*$stack->push(
* Middleware::log(
* $loggerChannelFactory->get('PantheonGuzzle'),
* new MessageFormatter(static::$messageFormats),
* LogLevel::DEBUG
* ), 'logger'
* );
**/
$cert = ($_SERVER['HOME'] ?? '') . '/certs/binding.pem';
$config = [
'base_uri' => $endpoint->getBaseUri(),
'http_errors' => FALSE,
'debug' => FALSE,
'verify' => FALSE,
'handler' => $stack,
'allow_redirects' => FALSE,
];
// Putting `?debug=true` at the end of any Solr url will show you the low-level debugging from guzzle.
if ((php_sapi_name() === 'cli' || isset($_GET['debug'])) && $current_user->hasPermission('access search_api_pantheon debug information')) {
$config['debug'] = TRUE;
}
if (is_file($cert)) {
$config['cert'] = $cert;
}
parent::__construct($config);
if (!$endpoint instanceof Endpoint) {
throw new \InvalidArgumentException('Endpoint must be an instance of Endpoint');
}
$this->setEndpoint($endpoint);
if ($logger_factory instanceof LoggerChannelFactoryInterface) {
$this->setLogger($logger_factory->get('PantheonGuzzle'));
}
$this->setLogger($logger_factory->get('PantheonGuzzle'));
}
/**
* Send a guzzle request.
*
* @param \Psr\Http\Message\RequestInterface $request
* A PSR 7 request.
*
* @return \Psr\Http\Message\ResponseInterface
* Response from the guzzle send.
*
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function sendRequest(RequestInterface $request): ResponseInterface {
return $this->send($request);
}
/**
* Make a query and return the JSON results.
*
* @param string $path
* URL path to add to the query.
* @param array $guzzleOptions
* Options to pass to the Guzzle client.
*
* @return mixed
* Response from the query.
*
* @throws \Exception
*
* @throws \JsonException
*/
public function getQueryResult(
string $path,
array $guzzleOptions = ['query' => [], 'headers' => ['Content-Type' => 'application/json']]
) {
$response = $this->get($path, $guzzleOptions);
if (
$response instanceof ResponseInterface && !in_array($response->getStatusCode(), [200, 201, 202, 203, 204])
) {
$this->logger->error('Query Failed: ' . $response->getReasonPhrase());
}
$content_type = $response->getHeader('Content-Type')[0] ?? '';
if (strpos($content_type, 'application/json') !== FALSE) {
return json_decode(
$response->getBody(),
TRUE,
512,
JSON_THROW_ON_ERROR
);
}
return (string) $response->getBody();
}
/**
* Get a PSR adapter interface based on this class.
*
* @return \Solarium\Core\Client\Adapter\AdapterInterface
* The interface in question.
*/
public function getPsr18Adapter(): AdapterInterface {
return new Psr18Adapter(
$this,
new RequestFactory(),
new StreamFactory()
);
}
/**
* Request Middleware Callback.
*
* @param \Psr\Http\Message\RequestInterface $request
*
* @return \Psr\Http\Message\RequestInterface
*/
public function requestUriAlterForPantheonEnvironment(RequestInterface $request) {
$toAdd = '';
$uri = $request->getUri();
$path = $uri->getPath();
$path_parts = explode('/', $path);
$shouldBeInUrl = $this->endpoint->getMySitename();
$shouldBeInPath = $this->endpoint->getPath();
if (!in_array(trim($shouldBeInUrl, '/'), $path_parts)) {
array_unshift($path_parts, trim($this->endpoint->getCore(), '/'));
}
if (!in_array(trim($shouldBeInPath, '/'), $path_parts)) {
array_unshift($path_parts, trim($this->endpoint->getPath(), '/'));
}
$path_parts = array_filter($path_parts, function ($item) {
return !empty($item);
});
$uri = $uri->withPath('/' . ltrim(implode('/', $path_parts), '/'));
return $request->withUri($uri);
}
}