Skip to content

Commit e0f19f7

Browse files
committed
Abstract the HTTP Client by using HTTPlug
1 parent 4176d3e commit e0f19f7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+234
-54
lines changed

README.md

Lines changed: 17 additions & 42 deletions

composer.json

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,25 @@
1717
}
1818
],
1919
"require": {
20-
"php": ">=5.3.2",
21-
"ext-curl": "*",
22-
"guzzle/guzzle": "~3.7"
20+
"php": ">=5.4",
21+
"php-http/httplug": "1.0.0-RC1",
22+
"php-http/promise": "1.0.0-RC1",
23+
"zendframework/zend-diactoros": "^1.3"
2324
},
2425
"require-dev": {
2526
"phpunit/phpunit": "~4.0",
26-
"sllh/php-cs-fixer-styleci-bridge": "~1.3"
27+
"phpspec/phpspec": "^2.4",
28+
"php-http/guzzle6-adapter": "^0.4.1"
2729
},
2830
"suggest": {
2931
"knplabs/gaufrette": "Needed for optional Gaufrette cache"
3032
},
3133
"autoload": {
32-
"psr-4": { "Github\\": "lib/Github/" }
34+
"psr-4": { "Github\\": "src/Github/" }
3335
},
3436
"extra": {
3537
"branch-alias": {
36-
"dev-master": "1.5.x-dev"
38+
"dev-master": "2.0.x-dev"
3739
}
3840
}
3941
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace spec\Github\HttpClient;
4+
5+
use PhpSpec\ObjectBehavior;
6+
use Prophecy\Argument;
7+
use Psr\Http\Message\RequestInterface;
8+
use Psr\Http\Message\ResponseInterface;
9+
use Http\Client\HttpClient;
10+
use Github\Factory\RequestFactory;
11+
12+
class HttplugClientSpec extends ObjectBehavior
13+
{
14+
function let(HttpClient $adapter, RequestFactory $factory)
15+
{
16+
$this->beConstructedWith($adapter, $factory);
17+
}
18+
19+
function it_is_a_http_client()
20+
{
21+
$this->shouldBeAnInstanceOf('Github\HttpClient\HttpClientInterface');
22+
}
23+
24+
function it_sends_GET_request(
25+
RequestInterface $request,
26+
ResponseInterface $response,
27+
$adapter,
28+
$factory
29+
) {
30+
$headers = [
31+
'Accept' => 'application/vnd.github.v3+json',
32+
'User-Agent' => 'php-github-api (http://github.com/KnpLabs/php-github-api)',
33+
'X-Debug-Token' => '13fe23ab',
34+
];
35+
36+
$factory->createRequest('GET', 'https://api.github.com/endpoint?page=1', $headers)->willReturn($request);
37+
$adapter->sendRequest($request)->willReturn($response);
38+
39+
$this->get('/endpoint', ['page' => 1], ['X-Debug-Token' => '13fe23ab'])->shouldReturn($response);
40+
}
41+
42+
function it_sends_POST_request(
43+
RequestInterface $request,
44+
ResponseInterface $response,
45+
$adapter,
46+
$factory
47+
) {
48+
$headers = [
49+
'Accept' => 'application/vnd.github.v3+json',
50+
'User-Agent' => 'php-github-api (http://github.com/KnpLabs/php-github-api)',
51+
'X-Debug-Token' => '13fe23ab',
52+
];
53+
54+
$factory->createRequest('POST', 'https://api.github.com/endpoint', $headers, 'body')->willReturn($request);
55+
$adapter->sendRequest($request)->willReturn($response);
56+
57+
$this->post('/endpoint', 'body', ['X-Debug-Token' => '13fe23ab'])->shouldReturn($response);
58+
}
59+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/Github/Factory/RequestFactory.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Github\Factory;
4+
5+
use Zend\Diactoros\Request;
6+
7+
class RequestFactory
8+
{
9+
public function createRequest($method, $uri, array $headers, $body = 'php://temp')
10+
{
11+
return new Request($uri, $method, $body, $headers);
12+
}
13+
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
3+
namespace Github\HttpClient;
4+
5+
use Http\Adapter\HttpAdapter;
6+
use Github\Factory\RequestFactory;
7+
use Http\Client\HttpClient;
8+
9+
final class HttplugClient implements HttpClientInterface
10+
{
11+
/** @var HttpAdapter */
12+
private $adapter;
13+
14+
/** @var RequestFactory */
15+
private $factory;
16+
17+
private $options = array(
18+
'base_url' => 'https://api.github.com/',
19+
20+
'user_agent' => 'php-github-api (http://github.com/KnpLabs/php-github-api)',
21+
22+
'api_limit' => 5000,
23+
'api_version' => 'v3',
24+
25+
'cache_dir' => null
26+
);
27+
28+
/**
29+
* @param HttpAdapter $adapter
30+
*/
31+
public function __construct(
32+
HttpClient $adapter,
33+
RequestFactory $factory
34+
) {
35+
$this->adapter = $adapter;
36+
$this->factory = $factory;
37+
}
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
public function get($path, array $parameters = array(), array $headers = array())
43+
{
44+
return $this->request(
45+
sprintf(
46+
'%s/%s?%s',
47+
rtrim($this->options['base_url'], '/'),
48+
ltrim($path, '/'),
49+
http_build_query($parameters)
50+
),
51+
null,
52+
'GET',
53+
$headers
54+
);
55+
}
56+
57+
/**
58+
* {@inheritdoc}
59+
*/
60+
public function post($path, $body = null, array $headers = array())
61+
{
62+
return $this->request(
63+
sprintf('%s%s', rtrim($this->options['base_url'], '/'), $path),
64+
$body,
65+
'POST',
66+
$headers
67+
);
68+
}
69+
70+
/**
71+
* {@inheritdoc}
72+
*/
73+
public function patch($path, $body = null, array $headers = array())
74+
{
75+
}
76+
77+
/**
78+
* {@inheritdoc}
79+
*/
80+
public function put($path, $body, array $headers = array())
81+
{
82+
}
83+
84+
/**
85+
* {@inheritdoc}
86+
*/
87+
public function delete($path, $body = null, array $headers = array())
88+
{
89+
}
90+
91+
/**
92+
* {@inheritdoc}
93+
*/
94+
public function request($path, $body, $httpMethod = 'GET', array $headers = array())
95+
{
96+
$headers = array_merge([
97+
'Accept' => sprintf('application/vnd.github.%s+json', $this->options['api_version']),
98+
'User-Agent' => sprintf('%s', $this->options['user_agent']),
99+
], $headers);
100+
101+
if (null !== $body) {
102+
$request = $this->factory->createRequest($httpMethod, $path, $headers, $body);
103+
} else {
104+
$request = $this->factory->createRequest($httpMethod, $path, $headers);
105+
}
106+
107+
// TODO (2016-01-22 14:19 by Gildas): try catch
108+
return $this->adapter->sendRequest($request);
109+
}
110+
111+
/**
112+
* {@inheritdoc}
113+
*/
114+
public function setOption($name, $value)
115+
{
116+
}
117+
118+
/**
119+
* {@inheritdoc}
120+
*/
121+
public function setHeaders(array $headers)
122+
{
123+
}
124+
125+
/**
126+
* {@inheritdoc}
127+
*/
128+
public function authenticate($tokenOrLogin, $password, $authMethod)
129+
{
130+
}
131+
}

lib/Github/HttpClient/Message/ResponseMediator.php renamed to src/Github/HttpClient/Message/ResponseMediator.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
namespace Github\HttpClient\Message;
44

5-
use Guzzle\Http\Message\Response;
65
use Github\Exception\ApiLimitExceedException;
6+
use Psr\Http\Message\ResponseInterface;
77

88
class ResponseMediator
99
{
10-
public static function getContent(Response $response)
10+
public static function getContent(ResponseInterface $response)
1111
{
12-
$body = $response->getBody(true);
12+
$body = $response->getBody();
1313
$content = json_decode($body, true);
1414

1515
if (JSON_ERROR_NONE !== json_last_error()) {
@@ -19,7 +19,7 @@ public static function getContent(Response $response)
1919
return $content;
2020
}
2121

22-
public static function getPagination(Response $response)
22+
public static function getPagination(ResponseInterface $response)
2323
{
2424
$header = (string) $response->getHeader('Link');
2525

@@ -39,14 +39,14 @@ public static function getPagination(Response $response)
3939
return $pagination;
4040
}
4141

42-
public static function getApiLimit(Response $response)
42+
public static function getApiLimit(ResponseInterface $response)
4343
{
4444
$remainingCalls = (string) $response->getHeader('X-RateLimit-Remaining');
4545

4646
if (null !== $remainingCalls && 1 > $remainingCalls) {
4747
throw new ApiLimitExceedException($remainingCalls);
4848
}
49-
49+
5050
return $remainingCalls;
5151
}
5252
}

0 commit comments

Comments
 (0)