|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Http\Client\Util; |
| 4 | + |
| 5 | +use Http\Client\Exception; |
| 6 | +use Http\Client\HttpClient; |
| 7 | +use Http\Message\MessageFactory; |
| 8 | +use Psr\Http\Message\ResponseInterface; |
| 9 | +use Psr\Http\Message\StreamInterface; |
| 10 | +use Psr\Http\Message\UriInterface; |
| 11 | + |
| 12 | +/** |
| 13 | + * Convenience HTTP client that integrates the MessageFactory in order to send |
| 14 | + * requests in the form of |
| 15 | + * |
| 16 | + * $client |
| 17 | + * ->get('/foo') |
| 18 | + * ->post('/bar') |
| 19 | + * ; |
| 20 | + * |
| 21 | + * @author Márk Sági-Kazár <[email protected]> |
| 22 | + * @author David Buchmann <[email protected]> |
| 23 | + */ |
| 24 | +class HttpMethodsClient |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var HttpClient |
| 28 | + */ |
| 29 | + private $httpClient; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var MessageFactory |
| 33 | + */ |
| 34 | + private $messageFactory; |
| 35 | + |
| 36 | + /** |
| 37 | + * Instantiate the message client with a client and a message factory. |
| 38 | + * |
| 39 | + * @param HttpClient $httpClient The client to send requests with. |
| 40 | + * @param MessageFactory $messageFactory The message factory to create requests. |
| 41 | + */ |
| 42 | + public function __construct(HttpClient $httpClient, MessageFactory $messageFactory) |
| 43 | + { |
| 44 | + $this->httpClient = $httpClient; |
| 45 | + $this->messageFactory = $messageFactory; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Sends a GET request |
| 50 | + * |
| 51 | + * @param string|UriInterface $uri |
| 52 | + * @param array $headers |
| 53 | + * |
| 54 | + * @throws Exception |
| 55 | + * |
| 56 | + * @return ResponseInterface |
| 57 | + */ |
| 58 | + public function get($uri, array $headers = []) |
| 59 | + { |
| 60 | + return $this->send('GET', $uri, $headers, null); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Sends an HEAD request |
| 65 | + * |
| 66 | + * @param string|UriInterface $uri |
| 67 | + * @param array $headers |
| 68 | + * |
| 69 | + * @throws Exception |
| 70 | + * |
| 71 | + * @return ResponseInterface |
| 72 | + */ |
| 73 | + public function head($uri, array $headers = []) |
| 74 | + { |
| 75 | + return $this->send('HEAD', $uri, $headers, null); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Sends a TRACE request |
| 80 | + * |
| 81 | + * @param string|UriInterface $uri |
| 82 | + * @param array $headers |
| 83 | + * |
| 84 | + * @throws Exception |
| 85 | + * |
| 86 | + * @return ResponseInterface |
| 87 | + */ |
| 88 | + public function trace($uri, array $headers = []) |
| 89 | + { |
| 90 | + return $this->send('TRACE', $uri, $headers, null); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Sends a POST request |
| 95 | + * |
| 96 | + * @param string|UriInterface $uri |
| 97 | + * @param array $headers |
| 98 | + * @param string|StreamInterface|null $body |
| 99 | + * |
| 100 | + * @throws Exception |
| 101 | + * |
| 102 | + * @return ResponseInterface |
| 103 | + */ |
| 104 | + public function post($uri, array $headers = [], $body = null) |
| 105 | + { |
| 106 | + return $this->send('POST', $uri, $headers, $body); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Sends a PUT request |
| 111 | + * |
| 112 | + * @param string|UriInterface $uri |
| 113 | + * @param array $headers |
| 114 | + * @param string|StreamInterface|null $body |
| 115 | + * |
| 116 | + * @throws Exception |
| 117 | + * |
| 118 | + * @return ResponseInterface |
| 119 | + */ |
| 120 | + public function put($uri, array $headers = [], $body = null) |
| 121 | + { |
| 122 | + return $this->send('PUT', $uri, $headers, $body); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Sends a PATCH request |
| 127 | + * |
| 128 | + * @param string|UriInterface $uri |
| 129 | + * @param array $headers |
| 130 | + * @param string|StreamInterface|null $body |
| 131 | + * |
| 132 | + * @throws Exception |
| 133 | + * |
| 134 | + * @return ResponseInterface |
| 135 | + */ |
| 136 | + public function patch($uri, array $headers = [], $body = null) |
| 137 | + { |
| 138 | + return $this->send('PATCH', $uri, $headers, $body); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Sends a DELETE request |
| 143 | + * |
| 144 | + * @param string|UriInterface $uri |
| 145 | + * @param array $headers |
| 146 | + * @param string|StreamInterface|null $body |
| 147 | + * |
| 148 | + * @throws Exception |
| 149 | + * |
| 150 | + * @return ResponseInterface |
| 151 | + */ |
| 152 | + public function delete($uri, array $headers = [], $body = null) |
| 153 | + { |
| 154 | + return $this->send('DELETE', $uri, $headers, $body); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Sends an OPTIONS request |
| 159 | + * |
| 160 | + * @param string|UriInterface $uri |
| 161 | + * @param array $headers |
| 162 | + * @param string|StreamInterface|null $body |
| 163 | + * |
| 164 | + * @throws Exception |
| 165 | + * |
| 166 | + * @return ResponseInterface |
| 167 | + */ |
| 168 | + public function options($uri, array $headers = [], $body = null) |
| 169 | + { |
| 170 | + return $this->send('OPTIONS', $uri, $headers, $body); |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * Sends a request |
| 175 | + * |
| 176 | + * @param string $method |
| 177 | + * @param string|UriInterface $uri |
| 178 | + * @param array $headers |
| 179 | + * @param string|StreamInterface|null $body |
| 180 | + * |
| 181 | + * @throws Exception |
| 182 | + * |
| 183 | + * @return ResponseInterface |
| 184 | + */ |
| 185 | + protected function send($method, $uri, array $headers = [], $body = null) |
| 186 | + { |
| 187 | + return $this->httpClient->sendRequest($this->messageFactory->createRequest( |
| 188 | + $method, |
| 189 | + $uri, |
| 190 | + '1.1', |
| 191 | + $headers, |
| 192 | + $body |
| 193 | + )); |
| 194 | + } |
| 195 | +} |
0 commit comments