44
55use Buzz \Browser ;
66use Buzz \Client \Curl ;
7- use Buzz \Message \Response ;
7+ use Buzz \Message \MessageInterface ;
8+
9+ use Github \HttpClient \Listener \AuthListener ;
810
911/**
1012 * Performs requests on GitHub API. API documentation should be self-explanatory.
@@ -19,16 +21,18 @@ class HttpClient implements HttpClientInterface
1921 * @var array
2022 */
2123 protected $ options = array (
22- 'url ' => 'https://api.github.com/:path ' ,
23- 'user_agent ' => 'php-github-api (http://github.com/KnpLabs/php-github-api) ' ,
24- 'http_port ' => 443 ,
25- 'timeout ' => 10 ,
24+ 'url ' => 'https://api.github.com/:path ' ,
25+ 'user_agent ' => 'php-github-api (http://github.com/KnpLabs/php-github-api) ' ,
26+ 'http_port ' => 443 ,
27+ 'timeout ' => 10 ,
28+
29+ 'api_limit ' => 5000 ,
2630
27- 'api_limit ' => 5000 ,
31+ 'auth_method ' => null ,
2832
29- 'login ' => null ,
30- 'password ' => null ,
31- 'token ' => null ,
33+ 'login ' => null ,
34+ 'password ' => null ,
35+ 'token ' => null ,
3236 );
3337
3438 /**
@@ -42,7 +46,7 @@ class HttpClient implements HttpClientInterface
4246 protected $ headers = array ();
4347
4448 /**
45- * @var Buzz\ Browser
49+ * @var Browser
4650 */
4751 protected $ browser ;
4852
@@ -60,17 +64,23 @@ public function __construct(array $options = array(), Browser $browser = null)
6064 $ this ->browser ->getClient ()->setTimeout ($ this ->options ['timeout ' ]);
6165 $ this ->browser ->getClient ()->setVerifyPeer (false );
6266
63- if ($ this ->options ['login ' ]) {
67+ if (null !== $ this ->options ['login ' ] || null !== $ this ->options ['token ' ]) {
68+ if (null !== $ this ->options ['token ' ]) {
69+ $ options = array ($ this ->options ['token ' ]);
70+ } else {
71+ $ options = array ($ this ->options ['login ' ], $ this ->options ['password ' ]);
72+ }
73+
6474 $ this ->browser ->addListener (
65- new Listener \AuthListener (
66- $ this ->options ['auth_method ' ],
67- array ($ this ->options ['login ' ], $ this ->options ['password ' ])
68- )
75+ new AuthListener ($ this ->options ['auth_method ' ], $ options )
6976 );
7077 }
7178 }
7279
73- public function setHeaders ($ headers )
80+ /**
81+ * @param array $headers
82+ */
83+ public function setHeaders (array $ headers )
7484 {
7585 $ this ->headers = $ headers ;
7686 }
@@ -91,39 +101,39 @@ public function setOption($name, $value)
91101 }
92102
93103 /**
94- * {@inheridoc }
104+ * {@inheritDoc }
95105 */
96106 public function get ($ path , array $ parameters = array (), array $ options = array ())
97107 {
98108 return $ this ->request ($ path , $ parameters , 'GET ' , $ options );
99109 }
100110
101111 /**
102- * {@inheridoc }
112+ * {@inheritDoc }
103113 */
104114 public function post ($ path , array $ parameters = array (), array $ options = array ())
105115 {
106116 return $ this ->request ($ path , $ parameters , 'POST ' , $ options );
107117 }
108118
109119 /**
110- * {@inheridoc }
120+ * {@inheritDoc }
111121 */
112122 public function patch ($ path , array $ parameters = array (), array $ options = array ())
113123 {
114124 return $ this ->request ($ path , $ parameters , 'PATCH ' , $ options );
115125 }
116126
117127 /**
118- * {@inheridoc }
128+ * {@inheritDoc }
119129 */
120130 public function delete ($ path , array $ parameters = array (), array $ options = array ())
121131 {
122132 return $ this ->request ($ path , $ parameters , 'DELETE ' , $ options );
123133 }
124134
125135 /**
126- * {@inheridoc }
136+ * {@inheritDoc }
127137 */
128138 public function put ($ path , array $ options = array ())
129139 {
@@ -164,7 +174,7 @@ public function request($path, array $parameters = array(), $httpMethod = 'GET',
164174 * @param string $httpMethod HTTP method to use
165175 * @param array $options Request options
166176 *
167- * @return string HTTP response
177+ * @return array HTTP response
168178 */
169179 protected function doRequest ($ url , array $ parameters = array (), $ httpMethod = 'GET ' , array $ options = array ())
170180 {
@@ -203,11 +213,11 @@ protected function decodeResponse($response)
203213 /**
204214 * Report to user he reached his GitHub API limit.
205215 *
206- * @param Response $response
216+ * @param MessageInterface $response
207217 *
208218 * @throws \RuntimeException
209219 */
210- protected function checkApiLimit (Response $ response )
220+ protected function checkApiLimit (MessageInterface $ response )
211221 {
212222 $ limit = $ response ->getHeader ('X-RateLimit-Remaining ' );
213223
0 commit comments