4
* (c) Copyright 2012-2014 Hewlett-Packard Development Company, L.P.
5
* (c) Copyright 2014 Rackspace US, Inc.
7
* Licensed under the Apache License, Version 2.0 (the "License"); you may
8
* not use this file except in compliance with the License. You may obtain
9
* a copy of the License at
11
* http://www.apache.org/licenses/LICENSE-2.0
13
* Unless required by applicable law or agreed to in writing, software
14
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16
* License for the specific language governing permissions and limitations
20
namespace OpenStack\Common\Transport\Guzzle;
22
use GuzzleHttp\Client;
23
use GuzzleHttp\ClientInterface as GuzzleClientInterface;
24
use GuzzleHttp\Exception\RequestException as GuzzleRequestException;
25
use OpenStack\Bootstrap;
26
use OpenStack\Common\Transport\AbstractClient;
27
use OpenStack\Common\Transport\Exception;
28
use OpenStack\Common\Transport\RequestInterface;
31
* An adapter class which wraps the Guzzle HTTP client. This adapter satisfies
32
* {@see OpenStack\Common\Transport\ClientInterface}, acting as an intermediary
33
* between Guzzle and our interface, reconciling their differences.
35
class GuzzleAdapter extends AbstractClient
38
* @var \GuzzleHttp\Client The client being wrapped.
43
* A factory method that allows for the easy creation of this adapter. It
44
* accepts an array of options which will be fed into the Guzzle client.
45
* This method also handles the configuration of the client being wrapped,
46
* such as overriding error handling and the default User-Agent header.
48
* @param array $options The options passed in to the Guzzle client. For a
49
* full run-through of available configuration values,
50
* view the {@link http://docs.guzzlephp.org/en/latest/clients.html#creating-a-client official docs}.
53
public static function create(array $options = [])
55
if (empty($options['defaults'])) {
56
$options['defaults'] = [];
59
// Disable Guzzle error handling and define our own error subscriber.
60
// Also override default User-Agent header with our own version.
61
$options['defaults'] += ['exceptions' => false,
62
'subscribers' => [new HttpError()],
63
'headers' => ['User-Agent' => self::getDefaultUserAgent()]
66
// Inject client and pass in options for adapter
67
return new self(new Client($options));
71
* Instantiate a new Adapter which wraps a Guzzle client.
73
* @param \GuzzleHttp\ClientInterface $guzzle The Client being wrapped
75
public function __construct(GuzzleClientInterface $guzzle)
77
$this->client = $guzzle;
80
public function createRequest($method, $uri = null, $body = null, array $options = [])
82
$headers = isset($options['headers']) ? $options['headers'] : [];
84
$request = $this->client->createRequest($method, $uri, [
85
'headers' => $headers,
89
return new RequestAdapter($request);
94
* @param \OpenStack\Common\Transport\RequestInterface $adapter
95
* @return \OpenStack\Common\Transport\ResponseInterface
96
* @throws \OpenStack\Common\Transport\Exception\RequestException
97
* @throws \GuzzleHttp\Exception\RequestException
99
public function send(RequestInterface $adapter)
102
$guzzleResponse = $this->client->send($adapter->getMessage());
103
return new ResponseAdapter($guzzleResponse);
104
} catch (GuzzleRequestException $e) {
105
// In order to satisfy {@see GuzzleHttp\ClientInterface}, Guzzle
106
// wraps all exceptions in its own RequestException class. This is
107
// not useful for our end-users, so we need to make sure our own
108
// versions are returned (Guzzle buffers them).
109
$previous = $e->getPrevious();
110
if ($previous instanceof Exception\RequestException) {
118
* Guzzle handles options using the defaults/ prefix. So if a key is passed
119
* in to be set, or got, that contains this prefix - assume that its a
120
* Guzzle option, not an adapter one.
124
public function setOption($key, $value)
126
$this->client->setDefaultOption($key, $value);
130
* Guzzle handles options using the defaults/ prefix. So if a key is passed
131
* in to be set, or got, that contains this prefix - assume that its a
132
* Guzzle option, not an adapter one.
136
public function getOption($key)
138
if ($key == 'base_url') {
139
return $this->getBaseUrl();
141
return $this->client->getDefaultOption($key);
145
public function getBaseUrl()
147
return $this->client->getBaseUrl();
151
* Prepends the SDK's version number to the standard Guzzle string.
155
public static function getDefaultUserAgent()
157
return sprintf("OpenStack/%f %s", Bootstrap::VERSION, Client::getDefaultUserAgent());
b'\\ No newline at end of file'