@@ -15,7 +15,7 @@ class Api
1515 * GitHub API location
1616 */
1717 const API_URL = 'https://api.github.com/ ' ;
18-
18+
1919 /**
2020 * Constants for available API response formats. Not all API methods
2121 * provide responses in all formats. Defaults to FORMAT_JSON
@@ -27,25 +27,26 @@ class Api
2727 const FORMAT_TEXT = 'text ' ;
2828 const FORMAT_HTML = 'html ' ;
2929 const FORMAT_FULL = 'full ' ;
30-
30+
3131 /**
3232 * Constants for HTTP status return codes
3333 */
3434 const HTTP_STATUS_OK = 200 ;
3535 const HTTP_STATUS_CREATED = 201 ;
3636 const HTTP_STATUS_NO_CONTENT = 204 ;
3737 const HTTP_STATUS_BAD_REQUEST = 400 ;
38+ const HTTP_STATUS_UNAUTHORIZED = 401 ;
3839 const HTTP_STATUS_NOT_FOUND = 404 ;
3940 const HTTP_STATUS_UNPROCESSABLE_ENTITY = 422 ;
40-
41+
4142 /**
4243 * Constant for default pagination size on paginate requests. API will allow
4344 * max of 100 requests per page
4445 *
4546 * @link http://developer.github.com/v3/#pagination
4647 */
4748 const DEFAULT_PAGE_SIZE = 30 ;
48-
49+
4950 /**
5051 * Constants for authentication methods. AUTH_TYPE_OAUTH is currently not
5152 * implemented
@@ -57,33 +58,33 @@ class Api
5758
5859 /**
5960 * Transport layer
60- *
61+ *
6162 * @var Transport
6263 */
6364 protected $ transport = null ;
64-
65+
6566 /**
6667 * Authenticatin flag. TRUE indicates subsequent API request will be made
6768 * with authentication
6869 *
6970 * @var bool
7071 */
7172 protected $ authenticated = false ;
72-
73+
7374 /**
7475 * Authenticaton username
7576 *
7677 * @var string
7778 */
7879 protected $ authUsername = null ;
79-
80+
8081 /**
8182 * Authentication password
8283 *
8384 * @var string
8485 */
8586 protected $ authPassword = null ;
86-
87+
8788 /**
8889 * Constructor
8990 *
@@ -96,7 +97,7 @@ public function __construct(Curl $transport = null)
9697 else
9798 $ this ->transport = $ transport ;
9899 }
99-
100+
100101 /**
101102 * Sets user credentials. Setting credentials does not log the user in.
102103 * A call to login() must be made aswell
@@ -109,21 +110,22 @@ public function setCredentials($username, $password)
109110 $ this ->authUsername = $ username ;
110111 $ this ->authPassword = $ password ;
111112 }
112-
113+
113114 /**
114115 * Clears credentials. Clearing credentials does not logout the user. A call
115116 * to logout() must be made first
116117 */
117118 public function clearCredentials ()
118119 {
119- if (false === $ this ->isAuthenticated ()) {
120+ if (false === $ this ->isAuthenticated ())
121+ {
120122 $ this ->authUsername = '' ;
121123 $ this ->authPassword = '' ;
122124 }
123125 else
124126 throw new ApiException ('You must logout before clearing credentials. Use logout() first ' );
125127 }
126-
128+
127129 /**
128130 * Login the user. Applies authentication to all subsequent API calls.
129131 * Credentials must be set first with setCredentials()
@@ -132,10 +134,10 @@ public function login()
132134 {
133135 if ((!strlen ($ this ->authUsername )) || !strlen ($ this ->authPassword ))
134136 throw new ApiException ('Cannot login. You must specify the credentials first. Use setCredentials() ' );
135-
137+
136138 $ this ->authenticated = true ;
137139 }
138-
140+
139141 /**
140142 * Logout the user. Cancels authentication to all subsequent API calls.
141143 *
@@ -147,7 +149,7 @@ public function logout($clearCredentials = false)
147149 if (false === $ clearCredentials )
148150 $ this ->clearCredentials ();
149151 }
150-
152+
151153 /**
152154 * Check if authentication will be applied
153155 *
@@ -157,7 +159,7 @@ public function isAuthenticated()
157159 {
158160 return $ this ->authenticated ;
159161 }
160-
162+
161163 /**
162164 * Get transport layer
163165 *
@@ -167,7 +169,7 @@ public function getTransport()
167169 {
168170 return $ this ->transport ;
169171 }
170-
172+
171173 /**
172174 * Make a HTTP GET request to API
173175 *
@@ -178,10 +180,10 @@ public function getTransport()
178180 public function requestGet ($ url , $ params = array (), $ options = array ())
179181 {
180182 $ options = $ this ->applyAuthentication ($ options );
181-
183+
182184 return $ this ->transport ->get (self ::API_URL . $ url , $ params , $ options );
183185 }
184-
186+
185187 /**
186188 * Make a HTTP POST request to API
187189 *
@@ -192,12 +194,12 @@ public function requestGet($url, $params = array(), $options = array())
192194 public function requestPost ($ url , $ params = array (), $ options = array ())
193195 {
194196 $ options = $ this ->applyAuthentication ($ options );
195-
197+
196198 $ params = (count ($ params )) ? json_encode ($ params ) : null ;
197-
199+
198200 return $ this ->transport ->post (self ::API_URL . $ url , $ params , $ options );
199201 }
200-
202+
201203 /**
202204 * Make a HTTP PUT request to API
203205 *
@@ -208,12 +210,12 @@ public function requestPost($url, $params = array(), $options = array())
208210 public function requestPut ($ url , $ params = array (), $ options = array ())
209211 {
210212 $ options = $ this ->applyAuthentication ($ options );
211-
213+
212214 $ params = (count ($ params )) ? json_encode ($ params ) : null ;
213-
215+
214216 return $ this ->transport ->put (self ::API_URL . $ url , $ params , $ options );
215217 }
216-
218+
217219 /**
218220 * Make a HTTP PATCH request to API
219221 *
@@ -224,12 +226,12 @@ public function requestPut($url, $params = array(), $options = array())
224226 public function requestPatch ($ url , $ params = array (), $ options = array ())
225227 {
226228 $ options = $ this ->applyAuthentication ($ options );
227-
229+
228230 $ params = (count ($ params )) ? json_encode ($ params ) : null ;
229-
231+
230232 return $ this ->transport ->patch (self ::API_URL . $ url , $ params , $ options );
231233 }
232-
234+
233235 /**
234236 * Make a HTTP DELETE request to API
235237 *
@@ -240,12 +242,12 @@ public function requestPatch($url, $params = array(), $options = array())
240242 public function requestDelete ($ url , $ params = array (), $ options = array ())
241243 {
242244 $ options = $ this ->applyAuthentication ($ options );
243-
245+
244246 $ params = (count ($ params )) ? json_encode ($ params ) : null ;
245-
247+
246248 return $ this ->transport ->delete (self ::API_URL . $ url , $ params , $ options );
247249 }
248-
250+
249251 /**
250252 * Check if authentication needs to be applied to requests
251253 *
@@ -263,10 +265,10 @@ public function applyAuthentication($options)
263265 'password ' => $ this ->authPassword
264266 );
265267 }
266-
268+
267269 return $ options ;
268270 }
269-
271+
270272 /**
271273 * Generate pagintaion page parameters
272274 *
@@ -280,7 +282,7 @@ protected function buildPageParams($page, $pageSize) {
280282 'per_page ' => $ pageSize
281283 );
282284 }
283-
285+
284286 /**
285287 * Generates parameters array from key/value pairs. Only values that are
286288 * not null are returned. This is useful for update functions where many of the fields
@@ -296,15 +298,16 @@ protected function buildPageParams($page, $pageSize) {
296298 protected function buildParams ($ rawParams )
297299 {
298300 $ params = array ();
299-
300- foreach ($ rawParams as $ key =>$ value ) {
301+
302+ foreach ($ rawParams as $ key =>$ value )
303+ {
301304 if (false === is_null ($ value ))
302305 $ params [$ key ] = $ value ;
303306 }
304-
307+
305308 return $ params ;
306309 }
307-
310+
308311 /**
309312 * Sets the Mime type format options
310313 *
@@ -317,27 +320,58 @@ protected function setResponseFormatOptions($format, $resourceKi, $options)
317320 {
318321 if (false === isset ($ options ['headers ' ]))
319322 $ options ['headers ' ] = array ();
320-
323+
321324 $ options ['headers ' ][] = 'Accept: application/vnd.github- ' . $ resourceKi . '. ' . $ format . '+json ' ;
322-
325+
323326 return $ options ;
324327 }
325328
329+ /**
330+ * Process the repsonse from the API
331+ *
332+ * @param array $response Raw repsonse from Transport layer
333+ * @return array|bool Processed response
334+ */
335+ protected function processResponse ($ response )
336+ {
337+ switch ($ response ['status ' ])
338+ {
339+ // Return the data
340+ case self ::HTTP_STATUS_OK :
341+ case self ::HTTP_STATUS_CREATED :
342+ return $ response ['data ' ];
343+ break ;
344+
345+ case self ::HTTP_STATUS_NO_CONTENT :
346+ return true ;
347+ break ;
348+
349+ case self ::HTTP_STATUS_NOT_FOUND :
350+ return false ;
351+ break ;
352+
353+ // Errors have happened, return entire response
354+ case self ::HTTP_STATUS_BAD_REQUEST :
355+ case self ::HTTP_STATUS_UNPROCESSABLE_ENTITY :
356+ return $ response ;
357+
358+ // Authenitication required, or credentials invalid
359+ case self ::HTTP_STATUS_UNAUTHORIZED :
360+ throw new AuthenticationException ("Unauthorized: Authentication required " );
361+ break ;
362+
363+ default :
364+ return $ response ;
365+ }
366+ }
326367}
327368
328369/**
329370 * General API Exception
330371 */
331- class ApiException extends \Exception {
332- }
372+ class ApiException extends \Exception {}
333373
334374/**
335375 * API authentication error
336376 */
337- class AuthenticationException extends \Exception
338- {
339- public function __construct ($ message , $ code = 0 , \Exception $ previous = null )
340- {
341- parent ::__construct ("Authentication required for action [ $ message] " , $ code , $ previous );
342- }
343- }
377+ class AuthenticationException extends \Exception {}
0 commit comments