@@ -51,6 +51,71 @@ public function __construct(Registry $options = null, Http $client = null)
5151 $ this ->client = $ client ?: HttpFactory::getHttp ($ options );
5252 }
5353
54+ /**
55+ * Get the HTTP client for this connector.
56+ *
57+ * @return Http
58+ *
59+ * @since 3.0.0
60+ */
61+ public function getClient ()
62+ {
63+ return $ this ->client ;
64+ }
65+
66+ /**
67+ * Get the diff for a pull request.
68+ *
69+ * @param string $user The name of the owner of the GitHub repository.
70+ * @param string $repo The name of the GitHub repository.
71+ * @param integer $pullId The pull request number.
72+ *
73+ * @return Response
74+ *
75+ * @since 3.0.0
76+ */
77+ public function getDiffForPullRequest ($ user , $ repo , $ pullId )
78+ {
79+ // Build the request path.
80+ $ path = "/repos/ $ user/ $ repo/pulls/ " . (int ) $ pullId ;
81+
82+ // Build the request headers.
83+ $ headers = array ('Accept ' => 'application/vnd.github.diff ' );
84+
85+ $ prepared = $ this ->prepareRequest ($ path , 0 , 0 , $ headers );
86+
87+ return $ this ->processResponse (
88+ $ this ->client ->get ($ prepared ['url ' ], $ prepared ['headers ' ])
89+ );
90+ }
91+
92+ /**
93+ * Method to build and return a full request URL for the request.
94+ *
95+ * This method will add appropriate pagination details if necessary and also prepend the API url to have a complete URL for the request.
96+ *
97+ * @param string $path Path to process
98+ * @param integer $page Page to request
99+ * @param integer $limit Number of results to return per page
100+ * @param array $headers The headers to send with the request
101+ *
102+ * @return array Associative array containing the prepared URL and request headers
103+ *
104+ * @since 3.0.0
105+ */
106+ protected function prepareRequest ($ path , $ page = 0 , $ limit = 0 ,
107+ array $ headers = array ()
108+ ) {
109+ $ url = $ this ->fetchUrl ($ path , $ page , $ limit );
110+
111+ if ($ token = $ this ->options ->get ('gh.token ' , false ))
112+ {
113+ $ headers ['Authorization ' ] = "token $ token " ;
114+ }
115+
116+ return array ('url ' => $ url , 'headers ' => $ headers );
117+ }
118+
54119 /**
55120 * Build and return a full request URL.
56121 *
@@ -107,39 +172,32 @@ protected function fetchUrl($path, $page = 0, $limit = 0)
107172 }
108173
109174 /**
110- * Get the HTTP client for this connector.
111- *
112- * @return Http
113- *
114- * @since 3.0.0
115- */
116- public function getClient ()
117- {
118- return $ this ->client ;
119- }
120-
121- /**
122- * Get the diff for a pull request.
175+ * Process the response and return it.
123176 *
124- * @param string $user The name of the owner of the GitHub repository.
125- * @param string $repo The name of the GitHub repository.
126- * @param integer $pullId The pull request number.
177+ * @param Response $response The response.
178+ * @param integer $expectedCode The expected response code.
127179 *
128180 * @return Response
129181 *
130182 * @since 3.0.0
183+ * @throws Exception\UnexpectedResponse
131184 */
132- public function getDiffForPullRequest ( $ user , $ repo , $ pullId )
185+ protected function processResponse ( Response $ response , $ expectedCode = 200 )
133186 {
134- // Build the request path.
135- $ path = "/repos/ $ user/ $ repo/pulls/ " . (int ) $ pullId ;
136-
137- // Build the request headers.
138- $ headers = array ('Accept ' => 'application/vnd.github.diff ' );
187+ // Validate the response code.
188+ if ($ response ->code != $ expectedCode )
189+ {
190+ // Decode the error response and throw an exception.
191+ $ body = json_decode ($ response ->body );
192+ $ error = isset ($ body ->error ) ? $ body ->error
193+ : (isset ($ body ->message ) ? $ body ->message : 'Unknown Error ' );
139194
140- $ prepared = $ this ->prepareRequest ($ path , 0 , 0 , $ headers );
195+ throw new Exception \UnexpectedResponse (
196+ $ response , $ error , $ response ->code
197+ );
198+ }
141199
142- return $ this -> processResponse ( $ this -> client -> get ( $ prepared [ ' url ' ], $ prepared [ ' headers ' ])) ;
200+ return $ response ;
143201 }
144202
145203 /**
@@ -168,7 +226,9 @@ public function getFileContents($user, $repo, $path, $ref = null)
168226 $ prepared ['url ' ] = (string ) $ url ;
169227 }
170228
171- return $ this ->processResponse ($ this ->client ->get ($ prepared ['url ' ], $ prepared ['headers ' ]));
229+ return $ this ->processResponse (
230+ $ this ->client ->get ($ prepared ['url ' ], $ prepared ['headers ' ])
231+ );
172232 }
173233
174234 /**
@@ -189,7 +249,9 @@ public function getFilesForPullRequest($user, $repo, $pullId)
189249
190250 $ prepared = $ this ->prepareRequest ($ path );
191251
192- return $ this ->processResponse ($ this ->client ->get ($ prepared ['url ' ], $ prepared ['headers ' ]));
252+ return $ this ->processResponse (
253+ $ this ->client ->get ($ prepared ['url ' ], $ prepared ['headers ' ])
254+ );
193255 }
194256
195257 /**
@@ -206,9 +268,36 @@ public function getFilesForPullRequest($user, $repo, $pullId)
206268 */
207269 public function getOpenIssues ($ user , $ repo , $ page = 0 , $ limit = 0 )
208270 {
209- $ prepared = $ this ->prepareRequest ("/repos/ $ user/ $ repo/issues " , $ page , $ limit );
271+ $ prepared = $ this ->prepareRequest (
272+ "/repos/ $ user/ $ repo/issues " , $ page , $ limit
273+ );
274+
275+ return $ this ->processResponse (
276+ $ this ->client ->get ($ prepared ['url ' ], $ prepared ['headers ' ])
277+ );
278+ }
279+
280+ /**
281+ * Get a list of the open pull requests for a repository.
282+ *
283+ * @param string $user The name of the owner of the GitHub repository.
284+ * @param string $repo The name of the GitHub repository.
285+ * @param integer $page The page number from which to get items.
286+ * @param integer $limit The number of items on a page.
287+ *
288+ * @return Response
289+ *
290+ * @since 3.0.0
291+ */
292+ public function getOpenPulls ($ user , $ repo , $ page = 0 , $ limit = 0 )
293+ {
294+ $ prepared = $ this ->prepareRequest (
295+ "/repos/ $ user/ $ repo/pulls " , $ page , $ limit
296+ );
210297
211- return $ this ->processResponse ($ this ->client ->get ($ prepared ['url ' ], $ prepared ['headers ' ]));
298+ return $ this ->processResponse (
299+ $ this ->client ->get ($ prepared ['url ' ], $ prepared ['headers ' ])
300+ );
212301 }
213302
214303 /**
@@ -244,7 +333,9 @@ public function getPullRequest($user, $repo, $pullId)
244333
245334 $ prepared = $ this ->prepareRequest ($ path );
246335
247- return $ this ->processResponse ($ this ->client ->get ($ prepared ['url ' ], $ prepared ['headers ' ]));
336+ return $ this ->processResponse (
337+ $ this ->client ->get ($ prepared ['url ' ], $ prepared ['headers ' ])
338+ );
248339 }
249340
250341 /**
@@ -258,59 +349,9 @@ public function getRateLimit()
258349 {
259350 $ prepared = $ this ->prepareRequest ('/rate_limit ' );
260351
261- return $ this ->processResponse ($ this ->client ->get ($ prepared ['url ' ], $ prepared ['headers ' ]));
262- }
263-
264- /**
265- * Process the response and return it.
266- *
267- * @param Response $response The response.
268- * @param integer $expectedCode The expected response code.
269- *
270- * @return Response
271- *
272- * @since 3.0.0
273- * @throws Exception\UnexpectedResponse
274- */
275- protected function processResponse (Response $ response , $ expectedCode = 200 )
276- {
277- // Validate the response code.
278- if ($ response ->code != $ expectedCode )
279- {
280- // Decode the error response and throw an exception.
281- $ body = json_decode ($ response ->body );
282- $ error = isset ($ body ->error ) ? $ body ->error : (isset ($ body ->message ) ? $ body ->message : 'Unknown Error ' );
283-
284- throw new Exception \UnexpectedResponse ($ response , $ error , $ response ->code );
285- }
286-
287- return $ response ;
288- }
289-
290- /**
291- * Method to build and return a full request URL for the request.
292- *
293- * This method will add appropriate pagination details if necessary and also prepend the API url to have a complete URL for the request.
294- *
295- * @param string $path Path to process
296- * @param integer $page Page to request
297- * @param integer $limit Number of results to return per page
298- * @param array $headers The headers to send with the request
299- *
300- * @return array Associative array containing the prepared URL and request headers
301- *
302- * @since 3.0.0
303- */
304- protected function prepareRequest ($ path , $ page = 0 , $ limit = 0 , array $ headers = array ())
305- {
306- $ url = $ this ->fetchUrl ($ path , $ page , $ limit );
307-
308- if ($ token = $ this ->options ->get ('gh.token ' , false ))
309- {
310- $ headers ['Authorization ' ] = "token $ token " ;
311- }
312-
313- return array ('url ' => $ url , 'headers ' => $ headers );
352+ return $ this ->processResponse (
353+ $ this ->client ->get ($ prepared ['url ' ], $ prepared ['headers ' ])
354+ );
314355 }
315356
316357 /**
0 commit comments