|
2 | 2 |
|
3 | 3 | namespace App\SupportedApps\Gogs;
|
4 | 4 |
|
5 |
| -class Gogs extends \App\SupportedApps |
| 5 | +class Gogs extends \App\SupportedApps implements \App\EnhancedApps |
6 | 6 | {
|
| 7 | + public $config; |
| 8 | + |
| 9 | + //protected $login_first = true; // Uncomment if api requests need to be authed first |
| 10 | + //protected $method = 'POST'; // Uncomment if requests to the API should be set by POST |
| 11 | + |
| 12 | + public function __construct() |
| 13 | + { |
| 14 | + //$this->jar = new \GuzzleHttp\Cookie\CookieJar; // Uncomment if cookies need to be set |
| 15 | + } |
| 16 | + |
| 17 | + private function fetchApi($path) { |
| 18 | + $token = $this->config->apikey; |
| 19 | + $attrs = [ |
| 20 | + "headers" => [ |
| 21 | + "Accept" => "application/json", |
| 22 | + "Authorization" => "token " . $token |
| 23 | + ], |
| 24 | + ]; |
| 25 | + $res = parent::execute($this->url($path), $attrs); |
| 26 | + switch ($res->getStatusCode()) { |
| 27 | + case 200: |
| 28 | + return json_decode($res->getBody()); |
| 29 | + case 401: |
| 30 | + case 403: |
| 31 | + throw new \Exception("Invalid token"); |
| 32 | + default: |
| 33 | + throw new \Exception("Could not connect to Gogs"); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + public function test() |
| 38 | + { |
| 39 | + try { |
| 40 | + $this->fetchApi("api/v1/user/repos"); |
| 41 | + echo "Successfully communicated with the API"; |
| 42 | + } catch (Exception $err) { |
| 43 | + echo $err->getMessage(); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + public function livestats() |
| 48 | + { |
| 49 | + $status = "inactive"; |
| 50 | + |
| 51 | + $reposDetails = $this->fetchApi("api/v1/user/repos"); |
| 52 | + $orgsDetails = $this->fetchApi("api/v1/user/orgs"); |
| 53 | + |
| 54 | + $data = [ |
| 55 | + "repositories" => count($reposDetails), |
| 56 | + "organizations" => count($orgsDetails), |
| 57 | + ]; |
| 58 | + return parent::getLiveStats($status, $data); |
| 59 | + } |
| 60 | + |
| 61 | + public function url($endpoint) |
| 62 | + { |
| 63 | + $api_url = parent::normaliseurl($this->config->url) . $endpoint; |
| 64 | + |
| 65 | + return $api_url; |
| 66 | + } |
7 | 67 | }
|
0 commit comments