@@ -51,6 +51,71 @@ public function __construct(Registry $options = null, Http $client = null)
51
51
$ this ->client = $ client ?: HttpFactory::getHttp ($ options );
52
52
}
53
53
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
+
54
119
/**
55
120
* Build and return a full request URL.
56
121
*
@@ -107,39 +172,32 @@ protected function fetchUrl($path, $page = 0, $limit = 0)
107
172
}
108
173
109
174
/**
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.
123
176
*
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.
127
179
*
128
180
* @return Response
129
181
*
130
182
* @since 3.0.0
183
+ * @throws Exception\UnexpectedResponse
131
184
*/
132
- public function getDiffForPullRequest ( $ user , $ repo , $ pullId )
185
+ protected function processResponse ( Response $ response , $ expectedCode = 200 )
133
186
{
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 ' );
139
194
140
- $ prepared = $ this ->prepareRequest ($ path , 0 , 0 , $ headers );
195
+ throw new Exception \UnexpectedResponse (
196
+ $ response , $ error , $ response ->code
197
+ );
198
+ }
141
199
142
- return $ this -> processResponse ( $ this -> client -> get ( $ prepared [ ' url ' ], $ prepared [ ' headers ' ])) ;
200
+ return $ response ;
143
201
}
144
202
145
203
/**
@@ -168,7 +226,9 @@ public function getFileContents($user, $repo, $path, $ref = null)
168
226
$ prepared ['url ' ] = (string ) $ url ;
169
227
}
170
228
171
- return $ this ->processResponse ($ this ->client ->get ($ prepared ['url ' ], $ prepared ['headers ' ]));
229
+ return $ this ->processResponse (
230
+ $ this ->client ->get ($ prepared ['url ' ], $ prepared ['headers ' ])
231
+ );
172
232
}
173
233
174
234
/**
@@ -189,7 +249,9 @@ public function getFilesForPullRequest($user, $repo, $pullId)
189
249
190
250
$ prepared = $ this ->prepareRequest ($ path );
191
251
192
- return $ this ->processResponse ($ this ->client ->get ($ prepared ['url ' ], $ prepared ['headers ' ]));
252
+ return $ this ->processResponse (
253
+ $ this ->client ->get ($ prepared ['url ' ], $ prepared ['headers ' ])
254
+ );
193
255
}
194
256
195
257
/**
@@ -206,12 +268,16 @@ public function getFilesForPullRequest($user, $repo, $pullId)
206
268
*/
207
269
public function getOpenIssues ($ user , $ repo , $ page = 0 , $ limit = 0 )
208
270
{
209
- $ prepared = $ this ->prepareRequest ("/repos/ $ user/ $ repo/issues " , $ page , $ limit );
271
+ $ prepared = $ this ->prepareRequest (
272
+ "/repos/ $ user/ $ repo/issues " , $ page , $ limit
273
+ );
210
274
211
- return $ this ->processResponse ($ this ->client ->get ($ prepared ['url ' ], $ prepared ['headers ' ]));
275
+ return $ this ->processResponse (
276
+ $ this ->client ->get ($ prepared ['url ' ], $ prepared ['headers ' ])
277
+ );
212
278
}
213
279
214
- /**
280
+ /**
215
281
* Get a list of the open pull requests for a repository.
216
282
*
217
283
* @param string $user The name of the owner of the GitHub repository.
@@ -225,11 +291,15 @@ public function getOpenIssues($user, $repo, $page = 0, $limit = 0)
225
291
*/
226
292
public function getOpenPulls ($ user , $ repo , $ page = 0 , $ limit = 0 )
227
293
{
228
- $ prepared = $ this ->prepareRequest ("/repos/ $ user/ $ repo/pulls " , $ page , $ limit );
294
+ $ prepared = $ this ->prepareRequest (
295
+ "/repos/ $ user/ $ repo/pulls " , $ page , $ limit
296
+ );
229
297
230
- return $ this ->processResponse ($ this ->client ->get ($ prepared ['url ' ], $ prepared ['headers ' ]));
298
+ return $ this ->processResponse (
299
+ $ this ->client ->get ($ prepared ['url ' ], $ prepared ['headers ' ])
300
+ );
231
301
}
232
-
302
+
233
303
/**
234
304
* Get an option from the connector.
235
305
*
@@ -263,7 +333,9 @@ public function getPullRequest($user, $repo, $pullId)
263
333
264
334
$ prepared = $ this ->prepareRequest ($ path );
265
335
266
- return $ this ->processResponse ($ this ->client ->get ($ prepared ['url ' ], $ prepared ['headers ' ]));
336
+ return $ this ->processResponse (
337
+ $ this ->client ->get ($ prepared ['url ' ], $ prepared ['headers ' ])
338
+ );
267
339
}
268
340
269
341
/**
@@ -277,59 +349,9 @@ public function getRateLimit()
277
349
{
278
350
$ prepared = $ this ->prepareRequest ('/rate_limit ' );
279
351
280
- return $ this ->processResponse ($ this ->client ->get ($ prepared ['url ' ], $ prepared ['headers ' ]));
281
- }
282
-
283
- /**
284
- * Process the response and return it.
285
- *
286
- * @param Response $response The response.
287
- * @param integer $expectedCode The expected response code.
288
- *
289
- * @return Response
290
- *
291
- * @since 3.0.0
292
- * @throws Exception\UnexpectedResponse
293
- */
294
- protected function processResponse (Response $ response , $ expectedCode = 200 )
295
- {
296
- // Validate the response code.
297
- if ($ response ->code != $ expectedCode )
298
- {
299
- // Decode the error response and throw an exception.
300
- $ body = json_decode ($ response ->body );
301
- $ error = isset ($ body ->error ) ? $ body ->error : (isset ($ body ->message ) ? $ body ->message : 'Unknown Error ' );
302
-
303
- throw new Exception \UnexpectedResponse ($ response , $ error , $ response ->code );
304
- }
305
-
306
- return $ response ;
307
- }
308
-
309
- /**
310
- * Method to build and return a full request URL for the request.
311
- *
312
- * This method will add appropriate pagination details if necessary and also prepend the API url to have a complete URL for the request.
313
- *
314
- * @param string $path Path to process
315
- * @param integer $page Page to request
316
- * @param integer $limit Number of results to return per page
317
- * @param array $headers The headers to send with the request
318
- *
319
- * @return array Associative array containing the prepared URL and request headers
320
- *
321
- * @since 3.0.0
322
- */
323
- protected function prepareRequest ($ path , $ page = 0 , $ limit = 0 , array $ headers = array ())
324
- {
325
- $ url = $ this ->fetchUrl ($ path , $ page , $ limit );
326
-
327
- if ($ token = $ this ->options ->get ('gh.token ' , false ))
328
- {
329
- $ headers ['Authorization ' ] = "token $ token " ;
330
- }
331
-
332
- return array ('url ' => $ url , 'headers ' => $ headers );
352
+ return $ this ->processResponse (
353
+ $ this ->client ->get ($ prepared ['url ' ], $ prepared ['headers ' ])
354
+ );
333
355
}
334
356
335
357
/**
0 commit comments