22
33namespace Github \HttpClient ;
44
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 ;
710
811use Github \Exception \ErrorException ;
912use Github \Exception \RuntimeException ;
10- use Github \HttpClient \Listener \AuthListener ;
11- use Github \HttpClient \Listener \ErrorListener ;
12- use Github \HttpClient \Message \Request ;
1313use 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,17 @@ 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 ->client -> createRequest ($ httpMethod , $ path, array_merge ( $ this -> headers , $ headers ), $ requestBody );
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 = Response::fromMessage (
139+ $ this ->client ->send ($ request )
140+ );
186141 } catch (\LogicException $ e ) {
187142 throw new ErrorException ($ e ->getMessage ());
188143 } catch (\RuntimeException $ e ) {
@@ -192,51 +147,32 @@ public function request($path, array $parameters = array(), $httpMethod = 'GET',
192147 $ this ->lastRequest = $ request ;
193148 $ this ->lastResponse = $ response ;
194149
195- if ($ hasListeners ) {
196- foreach ($ this ->listeners as $ listener ) {
197- $ listener ->postSend ($ request , $ response );
198- }
199- }
200-
201150 return $ response ;
202151 }
203152
204153 /**
205- * @return Request
206- */
207- public function getLastRequest ()
208- {
209- return $ this ->lastRequest ;
210- }
211-
212- /**
213- * @return Response
154+ * {@inheritDoc}
214155 */
215- public function getLastResponse ( )
156+ public function authenticate ( $ tokenOrLogin , $ password = null , $ method )
216157 {
217- return $ this ->lastResponse ;
158+ $ this ->addListener ('request.before_send ' , array (
159+ new AuthListener ($ tokenOrLogin , $ password , $ method ), 'onRequestBeforeSend '
160+ ));
218161 }
219162
220163 /**
221- * @param string $httpMethod
222- * @param string $url
223- *
224164 * @return Request
225165 */
226- protected function createRequest ( $ httpMethod , $ url )
166+ public function getLastRequest ( )
227167 {
228- $ request = new Request ($ httpMethod );
229- $ request ->setHeaders ($ this ->headers );
230- $ request ->fromUrl ($ url );
231-
232- return $ request ;
168+ return $ this ->lastRequest ;
233169 }
234170
235171 /**
236172 * @return Response
237173 */
238- protected function createResponse ()
174+ public function getLastResponse ()
239175 {
240- return new Response () ;
176+ return $ this -> lastResponse ;
241177 }
242178}
0 commit comments