22
33namespace Github \HttpClient ;
44
5- use Buzz \Client \ClientInterface ;
6- use Buzz \Listener \ListenerInterface ;
5+ use Guzzle \Http \Client as GuzzleClient ;
6+ use Guzzle \Http \ClientInterface ;
7+ use Guzzle \Http \Message \Request ;
8+ use Guzzle \Http \Message \Response ;
79
810use Github \Exception \ErrorException ;
911use Github \Exception \RuntimeException ;
1012use Github \HttpClient \Listener \AuthListener ;
1113use Github \HttpClient \Listener \ErrorListener ;
12- use Github \HttpClient \Message \Request ;
13- use Github \HttpClient \Message \Response ;
14- use Buzz \Client \Curl ;
1514
1615/**
1716 * Performs requests on GitHub API. API documentation should be self-explanatory.
2019 */
2120class HttpClient implements HttpClientInterface
2221{
23- /**
24- * @var array
25- */
2622 protected $ options = array (
2723 'base_url ' => 'https://api.github.com/ ' ,
2824
2925 'user_agent ' => 'php-github-api (http://github.com/KnpLabs/php-github-api) ' ,
3026 'timeout ' => 10 ,
3127
3228 'api_limit ' => 5000 ,
33- 'api_version ' => 'beta ' ,
29+ 'api_version ' => 'v3 ' ,
3430
3531 'cache_dir ' => null
3632 );
37- /**
38- * @var array
39- */
40- protected $ listeners = array ();
41- /**
42- * @var array
43- */
33+
4434 protected $ headers = array ();
4535
4636 private $ lastResponse ;
@@ -52,32 +42,14 @@ class HttpClient implements HttpClientInterface
5242 */
5343 public function __construct (array $ options = array (), ClientInterface $ client = null )
5444 {
55- $ client = $ client ?: new Curl ();
56- $ timeout = isset ($ options ['timeout ' ]) ? $ options ['timeout ' ] : $ this ->options ['timeout ' ];
57- $ client ->setTimeout ($ timeout );
58- $ client ->setVerifyPeer (false );
59-
6045 $ this ->options = array_merge ($ this ->options , $ options );
46+ $ client = $ client ?: new GuzzleClient ($ options ['base_url ' ], $ this ->options );
6147 $ this ->client = $ client ;
6248
63- $ this ->addListener (new ErrorListener ($ this ->options ));
64-
49+ $ this ->addListener ('request.error ' , array (new ErrorListener ($ this ->options ), 'onRequestError ' ));
6550 $ this ->clearHeaders ();
6651 }
6752
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-
8153 /**
8254 * {@inheritDoc}
8355 */
@@ -105,24 +77,17 @@ public function clearHeaders()
10577 );
10678 }
10779
108- /**
109- * @param ListenerInterface $listener
110- */
111- public function addListener (ListenerInterface $ listener )
80+ public function addListener ($ eventName , $ listener )
11281 {
113- $ this ->listeners [ get_class ( $ listener )] = $ listener ;
82+ $ this ->client -> getEventDispatcher ()-> addListener ( $ eventName , $ listener) ;
11483 }
11584
11685 /**
11786 * {@inheritDoc}
11887 */
11988 public function get ($ path , array $ parameters = array (), array $ headers = array ())
12089 {
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 );
12691 }
12792
12893 /**
@@ -162,27 +127,15 @@ public function put($path, array $parameters = array(), array $headers = array()
162127 */
163128 public function request ($ path , array $ parameters = array (), $ httpMethod = 'GET ' , array $ headers = array ())
164129 {
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+ ;
168133
169- $ request = $ this ->createRequest ($ httpMethod , $ path );
134+ $ request = $ this ->createRequest ($ httpMethod , $ path, $ requestBody , $ headers );
170135 $ 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 ();
183136
184137 try {
185- $ this ->client ->send ($ request, $ response );
138+ $ response = $ this ->client ->send ($ request );
186139 } catch (\LogicException $ e ) {
187140 throw new ErrorException ($ e ->getMessage ());
188141 } catch (\RuntimeException $ e ) {
@@ -192,15 +145,19 @@ public function request($path, array $parameters = array(), $httpMethod = 'GET',
192145 $ this ->lastRequest = $ request ;
193146 $ this ->lastResponse = $ response ;
194147
195- if ($ hasListeners ) {
196- foreach ($ this ->listeners as $ listener ) {
197- $ listener ->postSend ($ request , $ response );
198- }
199- }
200-
201148 return $ response ;
202149 }
203150
151+ /**
152+ * {@inheritDoc}
153+ */
154+ public function authenticate ($ tokenOrLogin , $ password = null , $ method )
155+ {
156+ $ this ->addListener ('request.before_send ' , array (
157+ new AuthListener ($ tokenOrLogin , $ password , $ method ), 'onRequestBeforeSend '
158+ ));
159+ }
160+
204161 /**
205162 * @return Request
206163 */
@@ -217,26 +174,8 @@ public function getLastResponse()
217174 return $ this ->lastResponse ;
218175 }
219176
220- /**
221- * @param string $httpMethod
222- * @param string $url
223- *
224- * @return Request
225- */
226- protected function createRequest ($ httpMethod , $ url )
227- {
228- $ request = new Request ($ httpMethod );
229- $ request ->setHeaders ($ this ->headers );
230- $ request ->fromUrl ($ url );
231-
232- return $ request ;
233- }
234-
235- /**
236- * @return Response
237- */
238- protected function createResponse ()
177+ protected function createRequest ($ httpMethod , $ path , $ requestBody , array $ headers = array ())
239178 {
240- return new Response ( );
179+ return $ this -> client -> createRequest ( $ httpMethod , $ path , array_merge ( $ this -> headers , $ headers ), $ requestBody );
241180 }
242181}
0 commit comments