11<?php
22
3- require_once (dirname (__FILE__ ).'/phpGitHubApiRequestException.php ' );
4-
53/**
64 * Performs requests on GitHub API. API documentation should be self-explanatory.
75 *
86 * @author Thibault Duplessis <thibault.duplessis at gmail dot com>
97 * @license MIT License
108 */
11- class phpGitHubApiRequest
9+ abstract class Github_HttpClient_Abstract
1210{
13-
1411 /**
1512 * The request options
1613 * @var array
1714 */
1815 protected $ options = array (
19- 'protocol ' => 'http ' ,
20- 'url ' => ':protocol://github.com/api/v2/:format/:path ' ,
21- 'format ' => 'json ' ,
16+ 'protocol ' => 'http ' ,
17+ 'url ' => ':protocol://github.com/api/v2/:format/:path ' ,
18+ 'format ' => 'json ' ,
2219 'user_agent ' => 'php-github-api (http://github.com/ornicar/php-github-api) ' ,
23- 'http_port ' => 80 ,
24- 'timeout ' => 10 ,
25- 'login ' => null ,
26- 'token ' => null ,
27- 'debug ' => false
20+ 'http_port ' => 80 ,
21+ 'timeout ' => 10 ,
22+ 'login ' => null ,
23+ 'token ' => null ,
24+ 'debug ' => false
2825 );
26+
2927 protected static $ history = array ();
3028
3129 /**
@@ -39,64 +37,73 @@ public function __construct(array $options = array())
3937 }
4038
4139 /**
42- * Configure the request
40+ * Send a request to the server, receive a response
4341 *
44- * @param array $options Request options
45- * @return phpGitHubApiRequest $this Fluent interface
42+ * @param string $url Request API url
43+ * @param array $parameters Parameters
44+ * @param string $httpMethod HTTP method to use
45+ * @param array $options Request options
46+ *
47+ * @return string HTTP response
4648 */
47- public function configure (array $ options )
48- {
49- $ this ->options = array_merge ($ this ->options , $ options );
50-
51- return $ this ;
52- }
49+ abstract protected function doSend ($ url , array $ parameters = array (), $ httpMethod = 'GET ' , array $ options );
5350
5451 /**
55- * Send a request to the server, receive a response,
56- * decode the response and returns an associative array
52+ * Send a GET request
5753 *
58- * @param string $apiPath Request API path
59- * @param array $parameters Parameters
54+ * @param string $url Request URL
55+ * @param array $parameters GET Parameters
6056 * @param string $httpMethod HTTP method to use
61- * @param array $options reconfigure the request for this call only
57+ * @param array $options Request options
6258 *
6359 * @return array Data
6460 */
65- public function send ($ apiPath , array $ parameters = array (), $ httpMethod = 'GET ' , array $ options = array ())
66- {
67- if (!empty ($ options )) {
68- $ initialOptions = $ this ->options ;
69- $ this ->configure ($ options );
70- }
71-
72- $ response = $ this ->doSend ($ apiPath , $ parameters , $ httpMethod );
73- $ response = $ this ->decodeResponse ($ response );
74-
75- if (isset ($ initialOptions )) {
76- $ this ->options = $ initialOptions ;
77- }
78-
79- return $ response ;
80- }
81-
82- /**
83- * Send a GET request
84- * @see send
85- */
8661 public function get ($ apiPath , array $ parameters = array (), array $ options = array ())
8762 {
8863 return $ this ->send ($ apiPath , $ parameters , 'GET ' , $ options );
8964 }
9065
9166 /**
9267 * Send a POST request
93- * @see send
68+ *
69+ * @param string $url Request URL
70+ * @param array $parameters POST Parameters
71+ * @param string $httpMethod HTTP method to use
72+ * @param array $options reconfigure the request for this call only
73+ *
74+ * @return array Data
9475 */
9576 public function post ($ apiPath , array $ parameters = array (), array $ options = array ())
9677 {
9778 return $ this ->send ($ apiPath , $ parameters , 'POST ' , $ options );
9879 }
9980
81+ /**
82+ * Send a request to the server, receive a response,
83+ * decode the response and returns an associative array
84+ *
85+ * @param string $url Request API url
86+ * @param array $parameters Parameters
87+ * @param string $httpMethod HTTP method to use
88+ * @param array $options Request options
89+ *
90+ * @return array Data
91+ */
92+ protected function send ($ url , array $ parameters = array (), $ httpMethod = 'GET ' , array $ options = array ())
93+ {
94+ $ this ->updateHistory ();
95+
96+ $ requestOptions = array_merge ($ this ->options , $ options );
97+
98+ // get encoded response
99+ $ response = $ this ->doSend ($ apiPath , $ parameters , $ httpMethod , $ options );
100+
101+ // decode response
102+ $ response = $ this ->decodeResponse ($ response );
103+
104+ return $ response ;
105+ }
106+
100107 /**
101108 * Get a JSON response and transform it to a PHP array
102109 *
@@ -114,111 +121,16 @@ protected function decodeResponse($response)
114121 }
115122
116123 /**
117- * Send a request to the server, receive a response
118- *
119- * @param string $apiPath Request API path
120- * @param array $parameters Parameters
121- * @param string $httpMethod HTTP method to use
124+ * Configure the request
122125 *
123- * @return string HTTP response
126+ * @param array $options Request options
127+ * @return Github_HttpClientInterface $this Fluent interface
124128 */
125- public function doSend ( $ apiPath , array $ parameters = array (), $ httpMethod = ' GET ' )
129+ public function configure ( array $ options )
126130 {
127- $ this ->updateHistory ();
128-
129- $ url = strtr ($ this ->options ['url ' ], array (
130- ':protocol ' => $ this ->options ['protocol ' ],
131- ':format ' => $ this ->options ['format ' ],
132- ':path ' => trim ($ apiPath , '/ ' )
133- ));
134-
135- $ curlOptions = array ();
136-
137- if ($ this ->options ['login ' ]) {
138- switch ($ this ->options ['auth_method ' ]) {
139- case phpGitHubApi::AUTH_HTTP_PASSWORD :
140- $ curlOptions += array (
141- CURLOPT_USERPWD => $ this ->options ['login ' ].': ' .$ this ->options ['secret ' ],
142- );
143- break ;
144- case phpGitHubApi::AUTH_HTTP_TOKEN :
145- $ curlOptions += array (
146- CURLOPT_USERPWD => $ this ->options ['login ' ].'/token: ' .$ this ->options ['secret ' ],
147- );
148- break ;
149- case phpGitHubApi::AUTH_URL_TOKEN :
150- default :
151- $ parameters = array_merge (array (
152- 'login ' => $ this ->options ['login ' ],
153- 'token ' => $ this ->options ['secret ' ]
154- ), $ parameters );
155- break ;
156- }
157- }
158-
159- if (!empty ($ parameters )) {
160- $ queryString = utf8_encode (http_build_query ($ parameters , '' , '& ' ));
161-
162- if ('GET ' === $ httpMethod ) {
163- $ url .= '? ' .$ queryString ;
164- } else {
165- $ curlOptions += array (
166- CURLOPT_POST => true ,
167- CURLOPT_POSTFIELDS => $ queryString
168- );
169- }
170- }
171-
172- $ this ->debug ('send ' .$ httpMethod .' request: ' .$ url );
173-
174- $ curlOptions += array (
175- CURLOPT_URL => $ url ,
176- CURLOPT_PORT => $ this ->options ['http_port ' ],
177- CURLOPT_USERAGENT => $ this ->options ['user_agent ' ],
178- CURLOPT_FOLLOWLOCATION => true ,
179- CURLOPT_RETURNTRANSFER => true ,
180- CURLOPT_TIMEOUT => $ this ->options ['timeout ' ]
181- );
182-
183- $ curl = curl_init ();
184-
185- curl_setopt_array ($ curl , $ curlOptions );
186-
187- $ response = curl_exec ($ curl );
188- $ headers = curl_getinfo ($ curl );
189- $ errorNumber = curl_errno ($ curl );
190- $ errorMessage = curl_error ($ curl );
191-
192- curl_close ($ curl );
193-
194- if (!in_array ($ headers ['http_code ' ], array (0 , 200 , 201 ))) {
195- throw new phpGitHubApiRequestException (null , (int ) $ headers ['http_code ' ]);
196- }
197-
198- if ($ errorNumber != '' ) {
199- throw new phpGitHubApiRequestException ('error ' .$ errorNumber );
200- }
201-
202- return $ response ;
203- }
131+ $ this ->options = array_merge ($ this ->options , $ options );
204132
205- /**
206- * Records the requests times
207- * When 30 request have been sent in less than a minute,
208- * sleeps for two second to prevent reaching GitHub API limitation.
209- *
210- * @access protected
211- * @return void
212- */
213- protected function updateHistory ()
214- {
215- self ::$ history [] = time ();
216- if (30 === count (self ::$ history )) {
217- if (reset (self ::$ history ) >= (time () - 35 )) {
218- sleep (2 );
219- }
220- array_shift (self ::$ history );
221- }
133+ return $ this ;
222134 }
223135
224136 /**
@@ -227,7 +139,7 @@ protected function updateHistory()
227139 * @param string $name The option name
228140 * @param mixed $value The value
229141 *
230- * @return dmConfigurable The current object instance
142+ * @return Github_HttpClientInterface The current object instance
231143 */
232144 public function setOption ($ name , $ value )
233145 {
@@ -248,11 +160,29 @@ public function getOption($name, $default = null)
248160 return isset ($ this ->options [$ name ]) ? $ this ->options [$ name ] : $ default ;
249161 }
250162
163+ /**
164+ * Records the requests times
165+ * When 30 request have been sent in less than a minute,
166+ * sleeps for two second to prevent reaching GitHub API limitation.
167+ *
168+ * @access protected
169+ * @return void
170+ */
171+ protected function updateHistory ()
172+ {
173+ self ::$ history [] = time ();
174+ if (30 === count (self ::$ history )) {
175+ if (reset (self ::$ history ) >= (time () - 35 )) {
176+ sleep (2 );
177+ }
178+ array_shift (self ::$ history );
179+ }
180+ }
181+
251182 protected function debug ($ message )
252183 {
253184 if ($ this ->options ['debug ' ]) {
254185 print $ message ."\n" ;
255186 }
256187 }
257-
258188}
0 commit comments