@@ -47,15 +47,6 @@ class Api
47
47
*/
48
48
const DEFAULT_PAGE_SIZE = 30 ;
49
49
50
- /**
51
- * Constants for authentication methods. AUTH_TYPE_OAUTH is currently not
52
- * implemented
53
- *
54
- * @link http://developer.github.com/v3/#authentication
55
- */
56
- const AUTH_TYPE_BASIC = 'basic ' ;
57
- const AUTH_TYPE_OAUTH = 'oauth ' ;
58
-
59
50
/**
60
51
* Transport layer
61
52
*
@@ -72,18 +63,11 @@ class Api
72
63
protected $ authenticated = false ;
73
64
74
65
/**
75
- * Authenticaton username
66
+ * Authenticator to use (Basic|OAuth)
76
67
*
77
- * @var string
68
+ * @var AuthenticationInterface
78
69
*/
79
- protected $ authUsername = null ;
80
-
81
- /**
82
- * Authentication password
83
- *
84
- * @var string
85
- */
86
- protected $ authPassword = null ;
70
+ protected $ authenticator = null ;
87
71
88
72
/**
89
73
* Constructor
@@ -97,20 +81,18 @@ public function __construct(Browser $transport = null)
97
81
else
98
82
$ this ->transport = $ transport ;
99
83
}
100
-
84
+
101
85
/**
102
- * Sets user credentials. Setting credentials does not log the user in.
103
- * A call to login() must be made aswell
86
+ * Sets user credentials via an AuthenticationInterface. Setting credentials
87
+ * does not log the user in. A call to login() must be made aswell
104
88
*
105
- * @param string $username Username
106
- * @param string $password Password
89
+ * @param AuthenticationInterface $authenticator Authenticator
107
90
*/
108
- public function setCredentials ($ username , $ password )
91
+ public function setCredentials (Authentication \ AuthenticationInterface $ authenticator )
109
92
{
110
- $ this ->authUsername = $ username ;
111
- $ this ->authPassword = $ password ;
93
+ $ this ->authenticator = $ authenticator ;
112
94
}
113
-
95
+
114
96
/**
115
97
* Clears credentials. Clearing credentials does not logout the user. A call
116
98
* to logout() must be made first
@@ -119,8 +101,7 @@ public function clearCredentials()
119
101
{
120
102
if (false === $ this ->isAuthenticated ())
121
103
{
122
- $ this ->authUsername = '' ;
123
- $ this ->authPassword = '' ;
104
+ $ this ->authenticator = null ;
124
105
}
125
106
else
126
107
throw new ApiException ('You must logout before clearing credentials. Use logout() first ' );
@@ -132,7 +113,7 @@ public function clearCredentials()
132
113
*/
133
114
public function login ()
134
115
{
135
- if ((! strlen ( $ this -> authUsername )) || ! strlen ( $ this ->authPassword ) )
116
+ if (null === $ this ->authenticator )
136
117
throw new ApiException ('Cannot login. You must specify the credentials first. Use setCredentials() ' );
137
118
138
119
$ this ->authenticated = true ;
@@ -177,11 +158,9 @@ public function getTransport()
177
158
* @param array $params Any GET params
178
159
* @return mixed API response
179
160
*/
180
- public function requestGet ($ url , $ params = array (), $ options = array ())
161
+ public function requestGet ($ url , $ params = array (), $ headers = array ())
181
162
{
182
- $ options = $ this ->applyAuthentication ($ options );
183
-
184
- return $ this ->transport ->get (self ::API_URL . $ url , $ params , $ options );
163
+ return $ this ->doRequest ('GET ' , self ::API_URL . $ url , $ params , $ headers );
185
164
}
186
165
187
166
/**
@@ -191,13 +170,11 @@ public function requestGet($url, $params = array(), $options = array())
191
170
* @param array $params Any POST params
192
171
* @return mixed API response
193
172
*/
194
- public function requestPost ($ url , $ params = array (), $ options = array ())
173
+ public function requestPost ($ url , $ params = array (), $ headers = array ())
195
174
{
196
- $ options = $ this ->applyAuthentication ($ options );
197
-
198
175
$ params = (count ($ params )) ? json_encode ($ params ) : null ;
199
176
200
- return $ this ->transport -> post ( self ::API_URL . $ url , $ params , $ options );
177
+ return $ this ->doRequest ( ' POST ' , self ::API_URL . $ url , $ params , $ headers );
201
178
}
202
179
203
180
/**
@@ -207,13 +184,11 @@ public function requestPost($url, $params = array(), $options = array())
207
184
* @param array $params Any PUT params
208
185
* @return mixed API response
209
186
*/
210
- public function requestPut ($ url , $ params = array (), $ options = array ())
187
+ public function requestPut ($ url , $ params = array (), $ headers = array ())
211
188
{
212
- $ options = $ this ->applyAuthentication ($ options );
213
-
214
189
$ params = (count ($ params )) ? json_encode ($ params ) : null ;
215
190
216
- return $ this ->transport -> put ( self ::API_URL . $ url , $ params , $ options );
191
+ return $ this ->doRequest ( ' PUT ' , self ::API_URL . $ url , $ params , $ headers );
217
192
}
218
193
219
194
/**
@@ -223,13 +198,11 @@ public function requestPut($url, $params = array(), $options = array())
223
198
* @param array $params Any PATCH params
224
199
* @return mixed API response
225
200
*/
226
- public function requestPatch ($ url , $ params = array (), $ options = array ())
201
+ public function requestPatch ($ url , $ params = array (), $ headers = array ())
227
202
{
228
- $ options = $ this ->applyAuthentication ($ options );
229
-
230
203
$ params = (count ($ params )) ? json_encode ($ params ) : null ;
231
204
232
- return $ this ->transport -> patch ( self ::API_URL . $ url , $ params , $ options );
205
+ return $ this ->doRequest ( ' PATCH ' , self ::API_URL . $ url , $ params , $ headers );
233
206
}
234
207
235
208
/**
@@ -239,34 +212,36 @@ public function requestPatch($url, $params = array(), $options = array())
239
212
* @param array $params Any DELETE params
240
213
* @return mixed API response
241
214
*/
242
- public function requestDelete ($ url , $ params = array (), $ options = array ())
215
+ public function requestDelete ($ url , $ params = array (), $ headers = array ())
243
216
{
244
- $ options = $ this ->applyAuthentication ($ options );
245
-
246
217
$ params = (count ($ params )) ? json_encode ($ params ) : null ;
247
218
248
- return $ this ->transport -> delete ( self ::API_URL . $ url , $ params , $ options );
219
+ return $ this ->doRequest ( ' DELETE ' , self ::API_URL . $ url , $ params , $ headers );
249
220
}
250
221
251
222
/**
252
- * Check if authentication needs to be applied to requests
223
+ * Perform HTTP request
253
224
*
254
- * @param array $options Array of options to add auth to
255
- * @return array Updated options
225
+ * @param string $method HTTP method
226
+ * @param string $url Full URL including protocol
227
+ * @param array $params Any params
228
+ * @return mixed API response
256
229
*/
257
- public function applyAuthentication ( $ options )
230
+ protected function doRequest ( $ method , $ url , $ params , $ headers )
258
231
{
259
- if ($ this ->isAuthenticated ())
232
+ $ request = $ this ->transport ->createRequest ();
233
+
234
+ $ request ->setMethod ($ method );
235
+ $ request ->fromUrl ($ url );
236
+ $ request ->addHeaders ($ headers );
237
+ $ request ->setContent ($ params );
238
+
239
+ if ($ this ->isAuthenticated () && null !== $ this ->authenticator )
260
240
{
261
- // Only supports basic auth for now
262
- $ options ['auth ' ] = array (
263
- 'type ' => self ::AUTH_TYPE_BASIC ,
264
- 'username ' => $ this ->authUsername ,
265
- 'password ' => $ this ->authPassword
266
- );
241
+ $ request = $ this ->authenticator ->authenticate ($ request );
267
242
}
268
-
269
- return $ options ;
243
+
244
+ return $ this -> transport -> send ( $ request );
270
245
}
271
246
272
247
/**
0 commit comments