github: honor redirect in readrepo
[pharext/pharext.org] / app / Github / API / Repos / ReadRepo.php
1 <?php
2
3 namespace app\Github\API\Repos;
4
5 use app\Github\API\Call;
6 use app\Github\Exception\RequestException;
7 use http\Client\Request;
8 use http\Client\Response;
9
10 class ReadRepo extends Call
11 {
12 function request() {
13 $url = $this->url->mod(uri_template("./repos/{+repo}", $this->args));
14 return $this->requestUrl($url);
15 }
16
17 function requestUrl($url) {
18 $request = new Request("GET", $url, [
19 "Authorization" => "token " . $this->api->getToken(),
20 "Accept" => $this->config->api->accept,
21 ]);
22 return $request;
23 }
24
25 function response(Response $response) {
26 if ($response->getResponseCode() >= 400 || null === ($json = json_decode($response->getBody()))) {
27 throw new RequestException($response);
28 }
29 if ($response->getResponseCode() >= 300 && $response->getResponseCode() < 400) {
30 $this->api->getClient()->enqueue(
31 $this->requestUrl($json->url),
32 function($response) {
33 try {
34 $this->deferred->resolve($this->response($response));
35 } catch (\Exception $e) {
36 $this->deferred->reject($e);
37 }
38 return true;
39 }
40 );
41 return $this->deferred->promise();
42 }
43 $result = [$json];
44 $this->saveToCache($result);
45 return $result;
46 }
47 }