2
2
3
3
namespace Github \HttpClient ;
4
4
5
- use Buzz \Client \ClientInterface ;
6
- use Buzz \Listener \ListenerInterface ;
5
+ use Github \HttpClient \Listener \AuthListener ;
6
+ use Github \HttpClient \Listener \ErrorListener ;
7
+ use Guzzle \Http \Client as GuzzleClient ;
8
+ use Guzzle \Http \ClientInterface ;
9
+ use Guzzle \Http \Message \Request ;
7
10
8
11
use Github \Exception \ErrorException ;
9
12
use Github \Exception \RuntimeException ;
10
- use Github \HttpClient \Listener \AuthListener ;
11
- use Github \HttpClient \Listener \ErrorListener ;
12
- use Github \HttpClient \Message \Request ;
13
13
use Github \HttpClient \Message \Response ;
14
- use Buzz \Client \Curl ;
15
14
16
15
/**
17
16
* Performs requests on GitHub API. API documentation should be self-explanatory.
20
19
*/
21
20
class HttpClient implements HttpClientInterface
22
21
{
23
- /**
24
- * @var array
25
- */
26
22
protected $ options = array (
27
23
'base_url ' => 'https://api.github.com/ ' ,
28
24
29
25
'user_agent ' => 'php-github-api (http://github.com/KnpLabs/php-github-api) ' ,
30
26
'timeout ' => 10 ,
31
27
32
28
'api_limit ' => 5000 ,
33
- 'api_version ' => 'beta ' ,
29
+ 'api_version ' => 'v3 ' ,
34
30
35
31
'cache_dir ' => null
36
32
);
37
- /**
38
- * @var array
39
- */
40
- protected $ listeners = array ();
41
- /**
42
- * @var array
43
- */
33
+
44
34
protected $ headers = array ();
45
35
46
36
private $ lastResponse ;
@@ -52,32 +42,14 @@ class HttpClient implements HttpClientInterface
52
42
*/
53
43
public function __construct (array $ options = array (), ClientInterface $ client = null )
54
44
{
55
- $ client = $ client ?: new Curl ();
56
- $ timeout = isset ($ options ['timeout ' ]) ? $ options ['timeout ' ] : $ this ->options ['timeout ' ];
57
- $ client ->setTimeout ($ timeout );
58
- $ client ->setVerifyPeer (false );
59
-
60
45
$ this ->options = array_merge ($ this ->options , $ options );
46
+ $ client = $ client ?: new GuzzleClient ($ options ['base_url ' ], $ this ->options );
61
47
$ this ->client = $ client ;
62
48
63
- $ this ->addListener (new ErrorListener ($ this ->options ));
64
-
49
+ $ this ->addListener ('request.error ' , array (new ErrorListener ($ this ->options ), 'onRequestError ' ));
65
50
$ this ->clearHeaders ();
66
51
}
67
52
68
- public function authenticate ($ tokenOrLogin , $ password , $ authMethod )
69
- {
70
- $ this ->addListener (
71
- new AuthListener (
72
- $ authMethod ,
73
- array (
74
- 'tokenOrLogin ' => $ tokenOrLogin ,
75
- 'password ' => $ password
76
- )
77
- )
78
- );
79
- }
80
-
81
53
/**
82
54
* {@inheritDoc}
83
55
*/
@@ -105,24 +77,17 @@ public function clearHeaders()
105
77
);
106
78
}
107
79
108
- /**
109
- * @param ListenerInterface $listener
110
- */
111
- public function addListener (ListenerInterface $ listener )
80
+ public function addListener ($ eventName , $ listener )
112
81
{
113
- $ this ->listeners [ get_class ( $ listener )] = $ listener ;
82
+ $ this ->client -> getEventDispatcher ()-> addListener ( $ eventName , $ listener) ;
114
83
}
115
84
116
85
/**
117
86
* {@inheritDoc}
118
87
*/
119
88
public function get ($ path , array $ parameters = array (), array $ headers = array ())
120
89
{
121
- if (0 < count ($ parameters )) {
122
- $ path .= (false === strpos ($ path , '? ' ) ? '? ' : '& ' ).http_build_query ($ parameters , '' , '& ' );
123
- }
124
-
125
- return $ this ->request ($ path , array (), 'GET ' , $ headers );
90
+ return $ this ->request ($ path , $ parameters , 'GET ' , $ headers );
126
91
}
127
92
128
93
/**
@@ -162,27 +127,17 @@ public function put($path, array $parameters = array(), array $headers = array()
162
127
*/
163
128
public function request ($ path , array $ parameters = array (), $ httpMethod = 'GET ' , array $ headers = array ())
164
129
{
165
- if (! empty ( $ this -> options [ ' base_url ' ]) && 0 !== strpos ( $ path , $ this -> options [ ' base_url ' ])) {
166
- $ path = trim ( $ this -> options [ ' base_url ' ]. $ path , ' / ' );
167
- }
130
+ $ requestBody = count ( $ parameters ) === 0
131
+ ? null : json_encode ( $ parameters , empty ( $ parameters ) ? JSON_FORCE_OBJECT : 0 )
132
+ ;
168
133
169
- $ request = $ this ->createRequest ($ httpMethod , $ path );
134
+ $ request = $ this ->client -> createRequest ($ httpMethod , $ path, array_merge ( $ this -> headers , $ headers ), $ requestBody );
170
135
$ request ->addHeaders ($ headers );
171
- if (count ($ parameters ) > 0 ) {
172
- $ request ->setContent (json_encode ($ parameters , empty ($ parameters ) ? JSON_FORCE_OBJECT : 0 ));
173
- }
174
-
175
- $ hasListeners = 0 < count ($ this ->listeners );
176
- if ($ hasListeners ) {
177
- foreach ($ this ->listeners as $ listener ) {
178
- $ listener ->preSend ($ request );
179
- }
180
- }
181
-
182
- $ response = $ this ->createResponse ();
183
136
184
137
try {
185
- $ this ->client ->send ($ request , $ response );
138
+ $ response = Response::fromMessage (
139
+ $ this ->client ->send ($ request )
140
+ );
186
141
} catch (\LogicException $ e ) {
187
142
throw new ErrorException ($ e ->getMessage ());
188
143
} catch (\RuntimeException $ e ) {
@@ -192,51 +147,32 @@ public function request($path, array $parameters = array(), $httpMethod = 'GET',
192
147
$ this ->lastRequest = $ request ;
193
148
$ this ->lastResponse = $ response ;
194
149
195
- if ($ hasListeners ) {
196
- foreach ($ this ->listeners as $ listener ) {
197
- $ listener ->postSend ($ request , $ response );
198
- }
199
- }
200
-
201
150
return $ response ;
202
151
}
203
152
204
153
/**
205
- * @return Request
206
- */
207
- public function getLastRequest ()
208
- {
209
- return $ this ->lastRequest ;
210
- }
211
-
212
- /**
213
- * @return Response
154
+ * {@inheritDoc}
214
155
*/
215
- public function getLastResponse ( )
156
+ public function authenticate ( $ tokenOrLogin , $ password = null , $ method )
216
157
{
217
- return $ this ->lastResponse ;
158
+ $ this ->addListener ('request.before_send ' , array (
159
+ new AuthListener ($ tokenOrLogin , $ password , $ method ), 'onRequestBeforeSend '
160
+ ));
218
161
}
219
162
220
163
/**
221
- * @param string $httpMethod
222
- * @param string $url
223
- *
224
164
* @return Request
225
165
*/
226
- protected function createRequest ( $ httpMethod , $ url )
166
+ public function getLastRequest ( )
227
167
{
228
- $ request = new Request ($ httpMethod );
229
- $ request ->setHeaders ($ this ->headers );
230
- $ request ->fromUrl ($ url );
231
-
232
- return $ request ;
168
+ return $ this ->lastRequest ;
233
169
}
234
170
235
171
/**
236
172
* @return Response
237
173
*/
238
- protected function createResponse ()
174
+ public function getLastResponse ()
239
175
{
240
- return new Response () ;
176
+ return $ this -> lastResponse ;
241
177
}
242
178
}
0 commit comments