~canonical-sysadmins/wordpress/openstack-objectstorage-k8s

« back to all changes in this revision

Viewing changes to src/OpenStack/Common/Transport/Guzzle/GuzzleAdapter.php

  • Committer: Jacek Nykis
  • Date: 2015-02-11 15:35:31 UTC
  • Revision ID: jacek.nykis@canonical.com-20150211153531-hmy6zi0ov2qfkl0b
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
/*
 
4
 * (c) Copyright 2012-2014 Hewlett-Packard Development Company, L.P.
 
5
 * (c) Copyright 2014      Rackspace US, Inc.
 
6
 *
 
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
 
10
 *
 
11
 *      http://www.apache.org/licenses/LICENSE-2.0
 
12
 *
 
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
 
17
 * under the License.
 
18
 */
 
19
 
 
20
namespace OpenStack\Common\Transport\Guzzle;
 
21
 
 
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;
 
29
 
 
30
/**
 
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.
 
34
 */
 
35
class GuzzleAdapter extends AbstractClient
 
36
{
 
37
    /**
 
38
     * @var \GuzzleHttp\Client The client being wrapped.
 
39
     */
 
40
    protected $client;
 
41
 
 
42
    /**
 
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.
 
47
     *
 
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}.
 
51
     * @return self
 
52
     */
 
53
    public static function create(array $options = [])
 
54
    {
 
55
        if (empty($options['defaults'])) {
 
56
            $options['defaults'] = [];
 
57
        }
 
58
 
 
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()]
 
64
        ];
 
65
 
 
66
        // Inject client and pass in options for adapter
 
67
        return new self(new Client($options));
 
68
    }
 
69
 
 
70
    /**
 
71
     * Instantiate a new Adapter which wraps a Guzzle client.
 
72
     *
 
73
     * @param \GuzzleHttp\ClientInterface $guzzle  The Client being wrapped
 
74
     */
 
75
    public function __construct(GuzzleClientInterface $guzzle)
 
76
    {
 
77
        $this->client = $guzzle;
 
78
    }
 
79
 
 
80
    public function createRequest($method, $uri = null, $body = null, array $options = [])
 
81
    {
 
82
        $headers = isset($options['headers']) ? $options['headers'] : [];
 
83
 
 
84
        $request = $this->client->createRequest($method, $uri, [
 
85
            'headers' => $headers,
 
86
            'body'    => $body,
 
87
        ]);
 
88
 
 
89
        return new RequestAdapter($request);
 
90
    }
 
91
 
 
92
    /**
 
93
     * @inheritDoc
 
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
 
98
     */
 
99
    public function send(RequestInterface $adapter)
 
100
    {
 
101
        try {
 
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) {
 
111
                throw $previous;
 
112
            }
 
113
            throw $e;
 
114
        }
 
115
    }
 
116
 
 
117
    /**
 
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.
 
121
     *
 
122
     * @inheritDoc
 
123
     */
 
124
    public function setOption($key, $value)
 
125
    {
 
126
        $this->client->setDefaultOption($key, $value);
 
127
    }
 
128
 
 
129
    /**
 
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.
 
133
     *
 
134
     * @inheritDoc
 
135
     */
 
136
    public function getOption($key)
 
137
    {
 
138
        if ($key == 'base_url') {
 
139
            return $this->getBaseUrl();
 
140
        } else {
 
141
            return $this->client->getDefaultOption($key);
 
142
        }
 
143
    }
 
144
 
 
145
    public function getBaseUrl()
 
146
    {
 
147
        return $this->client->getBaseUrl();
 
148
    }
 
149
 
 
150
    /**
 
151
     * Prepends the SDK's version number to the standard Guzzle string.
 
152
     *
 
153
     * @return string
 
154
     */
 
155
    public static function getDefaultUserAgent()
 
156
    {
 
157
        return sprintf("OpenStack/%f %s", Bootstrap::VERSION, Client::getDefaultUserAgent());
 
158
    }
 
159
}
 
 
b'\\ No newline at end of file'