4
4
5
5
use Buzz \Browser ;
6
6
use Buzz \Client \Curl ;
7
- use Buzz \Message \Response ;
7
+ use Buzz \Message \MessageInterface ;
8
+
9
+ use Github \HttpClient \Listener \AuthListener ;
8
10
9
11
/**
10
12
* Performs requests on GitHub API. API documentation should be self-explanatory.
@@ -19,16 +21,18 @@ class HttpClient implements HttpClientInterface
19
21
* @var array
20
22
*/
21
23
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 ,
26
30
27
- 'api_limit ' => 5000 ,
31
+ 'auth_method ' => null ,
28
32
29
- 'login ' => null ,
30
- 'password ' => null ,
31
- 'token ' => null ,
33
+ 'login ' => null ,
34
+ 'password ' => null ,
35
+ 'token ' => null ,
32
36
);
33
37
34
38
/**
@@ -42,7 +46,7 @@ class HttpClient implements HttpClientInterface
42
46
protected $ headers = array ();
43
47
44
48
/**
45
- * @var Buzz\ Browser
49
+ * @var Browser
46
50
*/
47
51
protected $ browser ;
48
52
@@ -60,17 +64,23 @@ public function __construct(array $options = array(), Browser $browser = null)
60
64
$ this ->browser ->getClient ()->setTimeout ($ this ->options ['timeout ' ]);
61
65
$ this ->browser ->getClient ()->setVerifyPeer (false );
62
66
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
+
64
74
$ 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 )
69
76
);
70
77
}
71
78
}
72
79
73
- public function setHeaders ($ headers )
80
+ /**
81
+ * @param array $headers
82
+ */
83
+ public function setHeaders (array $ headers )
74
84
{
75
85
$ this ->headers = $ headers ;
76
86
}
@@ -91,39 +101,39 @@ public function setOption($name, $value)
91
101
}
92
102
93
103
/**
94
- * {@inheridoc }
104
+ * {@inheritDoc }
95
105
*/
96
106
public function get ($ path , array $ parameters = array (), array $ options = array ())
97
107
{
98
108
return $ this ->request ($ path , $ parameters , 'GET ' , $ options );
99
109
}
100
110
101
111
/**
102
- * {@inheridoc }
112
+ * {@inheritDoc }
103
113
*/
104
114
public function post ($ path , array $ parameters = array (), array $ options = array ())
105
115
{
106
116
return $ this ->request ($ path , $ parameters , 'POST ' , $ options );
107
117
}
108
118
109
119
/**
110
- * {@inheridoc }
120
+ * {@inheritDoc }
111
121
*/
112
122
public function patch ($ path , array $ parameters = array (), array $ options = array ())
113
123
{
114
124
return $ this ->request ($ path , $ parameters , 'PATCH ' , $ options );
115
125
}
116
126
117
127
/**
118
- * {@inheridoc }
128
+ * {@inheritDoc }
119
129
*/
120
130
public function delete ($ path , array $ parameters = array (), array $ options = array ())
121
131
{
122
132
return $ this ->request ($ path , $ parameters , 'DELETE ' , $ options );
123
133
}
124
134
125
135
/**
126
- * {@inheridoc }
136
+ * {@inheritDoc }
127
137
*/
128
138
public function put ($ path , array $ options = array ())
129
139
{
@@ -164,7 +174,7 @@ public function request($path, array $parameters = array(), $httpMethod = 'GET',
164
174
* @param string $httpMethod HTTP method to use
165
175
* @param array $options Request options
166
176
*
167
- * @return string HTTP response
177
+ * @return array HTTP response
168
178
*/
169
179
protected function doRequest ($ url , array $ parameters = array (), $ httpMethod = 'GET ' , array $ options = array ())
170
180
{
@@ -203,11 +213,11 @@ protected function decodeResponse($response)
203
213
/**
204
214
* Report to user he reached his GitHub API limit.
205
215
*
206
- * @param Response $response
216
+ * @param MessageInterface $response
207
217
*
208
218
* @throws \RuntimeException
209
219
*/
210
- protected function checkApiLimit (Response $ response )
220
+ protected function checkApiLimit (MessageInterface $ response )
211
221
{
212
222
$ limit = $ response ->getHeader ('X-RateLimit-Remaining ' );
213
223
0 commit comments