Skip to content
This repository was archived by the owner on Jan 16, 2018. It is now read-only.

Commit 7efe2f5

Browse files
committed
Merge pull request #2 from php-http/http-methods-client
adding utility HttpMethodsClient class
2 parents 2d0dfb8 + 3efb10f commit 7efe2f5

File tree

4 files changed

+209
-98
lines changed

4 files changed

+209
-98
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
],
1313
"require": {
1414
"php": ">=5.4",
15-
"php-http/client": "dev-utility_separation"
15+
"php-http/client": "dev-utility_separation",
16+
"php-http/message-factory": "dev-master@dev"
1617
},
1718
"require-dev": {
1819
"phpspec/phpspec": "^2.2",

spec/HttpMethodsSpec.php

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,78 +2,75 @@
22

33
namespace spec\Http\Client\Util;
44

5-
use Http\Client\Util\HttpMethods;
6-
use Http\Client\HttpMethodsClient;
5+
use Http\Client\Util\HttpMethodsClient;
76
use PhpSpec\ObjectBehavior;
87

98
class HttpMethodsSpec extends ObjectBehavior
109
{
1110
function let()
1211
{
13-
$this->beAnInstanceOf('spec\Http\Client\HttpMethodsStub');
12+
$this->beAnInstanceOf('spec\Http\Client\Util\HttpMethodsClientStub');
1413
}
1514

1615
function it_sends_a_get_request()
1716
{
18-
$data = HttpMethodsStub::$requestData;
17+
$data = HttpMethodsClientStub::$requestData;
1918

2019
$this->get($data['uri'], $data['headers'], $data['options'])->shouldReturn(true);
2120
}
2221

2322
function it_sends_a_head_request()
2423
{
25-
$data = HttpMethodsStub::$requestData;
24+
$data = HttpMethodsClientStub::$requestData;
2625

2726
$this->head($data['uri'], $data['headers'], $data['options'])->shouldReturn(true);
2827
}
2928

3029
function it_sends_a_trace_request()
3130
{
32-
$data = HttpMethodsStub::$requestData;
31+
$data = HttpMethodsClientStub::$requestData;
3332

3433
$this->trace($data['uri'], $data['headers'], $data['options'])->shouldReturn(true);
3534
}
3635

3736
function it_sends_a_post_request()
3837
{
39-
$data = HttpMethodsStub::$requestData;
38+
$data = HttpMethodsClientStub::$requestData;
4039

4140
$this->post($data['uri'], $data['headers'], $data['body'], $data['options'])->shouldReturn(true);
4241
}
4342

4443
function it_sends_a_put_request()
4544
{
46-
$data = HttpMethodsStub::$requestData;
45+
$data = HttpMethodsClientStub::$requestData;
4746

4847
$this->put($data['uri'], $data['headers'], $data['body'], $data['options'])->shouldReturn(true);
4948
}
5049

5150
function it_sends_a_patch_request()
5251
{
53-
$data = HttpMethodsStub::$requestData;
52+
$data = HttpMethodsClientStub::$requestData;
5453

5554
$this->patch($data['uri'], $data['headers'], $data['body'], $data['options'])->shouldReturn(true);
5655
}
5756

5857
function it_sends_a_delete_request()
5958
{
60-
$data = HttpMethodsStub::$requestData;
59+
$data = HttpMethodsClientStub::$requestData;
6160

6261
$this->delete($data['uri'], $data['headers'], $data['body'], $data['options'])->shouldReturn(true);
6362
}
6463

6564
function it_sends_a_options_request()
6665
{
67-
$data = HttpMethodsStub::$requestData;
66+
$data = HttpMethodsClientStub::$requestData;
6867

6968
$this->options($data['uri'], $data['headers'], $data['body'], $data['options'])->shouldReturn(true);
7069
}
7170
}
7271

73-
class HttpMethodsStub implements HttpMethodsClient
72+
class HttpMethodsClientStub extends HttpMethodsClient
7473
{
75-
use HttpMethods;
76-
7774
public static $requestData = [
7875
'uri' => '/uri',
7976
'headers' => [
@@ -88,7 +85,7 @@ class HttpMethodsStub implements HttpMethodsClient
8885
/**
8986
* {@inheritdoc}
9087
*/
91-
public function send($method, $uri, array $headers = [], $body = null, array $options = [])
88+
protected function send($method, $uri, array $headers = [], $body = null, array $options = [])
9289
{
9390
if (in_array($method, ['GET', 'HEAD', 'TRACE'])) {
9491
return $uri === self::$requestData['uri'] &&

src/HttpMethods.php

Lines changed: 0 additions & 82 deletions
This file was deleted.

src/HttpMethodsClient.php

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
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

Comments
 (0)