Joachim M. Giæver 6 years ago
parent
commit
183cf0dfc9
7 changed files with 101 additions and 134 deletions
  1. 5 6
      client/gogsapi.php
  2. 0 78
      client/request/repos.php
  3. 27 8
      client/request/requestbase.php
  4. 6 2
      client/request/requestinterface.php
  5. 42 15
      client/request/user.php
  6. 10 8
      index.php
  7. 11 17
      lib/curl.php

+ 5 - 6
client/gogsapi.php

@@ -6,17 +6,16 @@ class GogsAPI {
     public function __construct($api_url, $api_token) {
         $this->url = $api_url;
         $this->token = $api_token;
-        $this->authorized("/user");
     }
 
     public function user($name = "") {
-        $scope = sprintf("%s/user", $this->url);
+        return new Request\User($this->url, $this->token, $name);
+    }
 
-        if ($name != "") {
-            $scope = sprintf("%ss/%s", $scope, $name);
-        }
+    public function repos($user = "") {
+        if ($user == "")
+            return $this->user()->get("/repos");
 
-        return new Request\User($scope, $this->token);
     }
 
     public function __destruct() {}

+ 0 - 78
client/request/repos.php

@@ -1,78 +0,0 @@
-<?php 
-
-namespace Client\Request {
-    class Repos extends RequestBase {
-        private $repo = array();
-
-        protected function json_set_property($objs) {
-            foreach ($objs as $key => $obj) {
-                $this->repo[$key] = new Repos\Repo(
-                    sprintf("/repos/%s/%s", 
-                        $obj->owner->username, 
-                        $obj->name
-                    ),
-                    $this->token
-                );
-                $this->repo[$key]->json_set_property($obj);
-            }
-        }
-
-        public function current() {
-            return current($this->repo);
-        }
-
-        public function next() {
-            return next($this->repo);
-        }
-
-        public function reset() {
-            return reset($this->repo);
-        }
-
-        public function __destruct() {}
-    }
-}
-
-namespace Client\Request\Repos {
-    class Repo extends \Client\Request\RequestBase { 
-        public $rid;
-        public $rowner;
-        public $rname;
-        public $rfull_name;
-        public $rdescription;
-        public $rprivate;
-        public $rfork;
-        public $rparent;
-        public $rempty;
-        public $rmirror;
-        public $rsize;
-        public $rhtml_url;
-        public $rssh_url;
-        public $rclone_url;
-        public $rwebsite;
-        public $rstars_count;
-        public $rforks_count;
-        public $rwathcers_count;
-        public $ropen_issues_count;
-        public $rpublic_branch;
-        public $rcreated_at;
-        public $rupdated_at;
-        public $rpermissions;
-
-        public function __construct($api_url, $token) {
-            if (!strpos($api_url, API_URL))
-                $api_url = sprintf("%s/%s", API_URL, $api_url);
-            parent::__construct($api_url, $token);
-        }
-        
-        protected function json_set_property($obj) {
-            foreach($obj as $key => $value) {
-                $key = 'r' . $key;
-                if (property_exists($this, $key))
-                    $this->{$key} = $value;
-            }
-        }
-
-        public function __destruct() {}
-    }
-}

+ 27 - 8
client/request/requestbase.php

@@ -3,6 +3,7 @@
 abstract class RequestBase implements RequestInterface {
 
     protected $loaded = false;
+    protected $scope;
 
     use \Lib\Curl {
         get as protected mget;
@@ -11,21 +12,36 @@ abstract class RequestBase implements RequestInterface {
     public function __construct($api_url, $api_token) {
         $this->url = $api_url;
         $this->token = $api_token;
+        $this->set_scope();
     }
 
-    public function load() {
-        $jenc =  $this->mget("", "");
+    public function load($force = false) {
+
+        if ($this->loaded && !$force)
+            return $this;
+
+        $jenc =  $this->mget();
+
         $this->json_set_property($this->json_decode($jenc));
+        $this->loaded = true;
+
         return $this;
     }
 
     public function get($scope) {
-        $s = substr($scope, strrpos($scope, "/") + 1);
-        echo sprintf("Scope: %s\b", $s);
-        switch ($s) {
-        case "repos":
-            return new Repos($this->url . $scope, $this->token);
-        }
+        throw new NotImplementedException("Method not implemented for " . __CLASS__ . ", cant lookup " . $scope);
+    }
+
+    public function create() {
+        throw new NotImplementedException("Method not implemented for " . __CLASS__ . ", cant create");
+    }
+
+    public function patch() {
+        throw new NotImplementedException("Method not implemented for " . __CLASS__ . ", cant create");
+    }
+
+    public function delete() {
+        throw new NotImplementedException("Method not implemented for " . __CLASS__ . ", cant create");
     }
 
     private function json_decode($jenc) {
@@ -38,6 +54,9 @@ abstract class RequestBase implements RequestInterface {
     }
 
     abstract protected function json_set_property($obj);
+    abstract protected function set_scope();
+
+    public function __destruct() {}
 }
 
 ?>

+ 6 - 2
client/request/requestinterface.php

@@ -1,11 +1,15 @@
 <?php namespace Client\Request;
 
 class RequestErrorException extends \Exception {};
+class NotImplementedException extends \BadMethodCallException{}
 
 interface RequestInterface {
-    public function __construct($api_url, $api_token);
+
+    public function load($force = true);
     public function get($scope);
-    public function __destruct();
+    public function create();
+    public function patch();
+    public function delete();
 }
 
 ?>

+ 42 - 15
client/request/user.php

@@ -1,24 +1,51 @@
-<?php namespace Client\Request;
+<?php 
 
-class User extends RequestBase {
+namespace Client\Request {
+    class User extends RequestBase {
 
-    public $uid;
-    public $ulogin;
-    public $ufull_name;
-    public $uemail;
-    public $uavatar_url;
-    public $uusername;
+        public $uid;
+        public $ulogin;
+        public $ufull_name;
+        public $uemail;
+        public $uavatar_url;
+        public $uusername;
 
-    public function __destruct() {}
+        protected $_scope = "/user";
 
-    protected function json_set_property($obj) {
-        foreach ($obj as $key => $value) {
-            echo sprintf("KEY: %s\n", $key);
-            $key = 'u' . $key;
-            if (property_exists($this, $key))
-                $this->{$key} = $value;
+        public function __construct($api_url, $api_token, $user) {
+            if (strlen($user) != 0)
+                $this->_scope .= "s/" . $user;
+            parent::__construct($api_url, $api_token);
+        }
+
+        protected function set_scope() {
+            $this->url .= $this->_scope;
+        }
+
+        public function load($force = true) {
+            $ret = parent::load($force);
+            //$this->url = API_URL . "/users/" . $this->ulogin;
+            return $ret;
+        } 
+
+        public function get($scope) {
+            switch ($scope) {
+            case "/repos":
+                return new User\Repos($this->url, $this->token);
+            case "/tokens":
+                return new User\Tokens($this->url, $this->token);
+            }
+        }
+
+        protected function json_set_property($obj) {
+            foreach ($obj as $key => $value) {
+                $key = 'u' . $key;
+                if (property_exists($this, $key))
+                    $this->{$key} = $value;
+            }
         }
     }
+
 }
 
 ?>

+ 10 - 8
index.php

@@ -8,7 +8,8 @@
  * */
 
 define('BASE_PATH', realpath(dirname(__FILE__)));
-
+define('API_URL', 'https://git.giaever.org/api/v1');
+define('API_TOKEN', '');
 
 function my_autoloader($class)
 {
@@ -22,16 +23,17 @@ spl_autoload_register('my_autoloader');
 try {
     $client =  new \Client\GogsAPI(API_URL, API_TOKEN);
 
-    $me = $client->user();
-    $other = $client->user("bje007");
-
-    $repos = $me->get("/repos")->load();
+    $me = $client->user()->load();
+    $you = $client->user("INF3910-3-CSS-IoT")->load();
 
-    for($repo = $repos->current(); $repo != false; $repo = $repos->next())
-        var_dump($repo);
+    $mine = $me->get("/repos")->load();
+    //$yours = $you->get("/repos")->load();
 
+    //$me->get("/tokens")->load();
 
-} catch (\Lib\NotAutorizedException $e) {
+} catch (\Lib\NotAuthorizedException $e) {
+    die($e->getMessage());
+} catch (\Lib\HTTPUnexpectedResponse $e) {
     die($e->getMessage());
 }
 ?>

+ 11 - 17
lib/curl.php

@@ -82,7 +82,7 @@ class HTTPUnexpectedResponse extends \Exception {
     );
 }
 
-class NotAutorizedException extends \Exception{}
+class NotAuthorizedException extends \Exception{}
 
 trait Curl {
     protected $url;
@@ -93,13 +93,7 @@ trait Curl {
     protected $max_redirects = 4;
 
     protected function method(&$req, $scope, $params, $post, $ret) {
-        echo sprintf(
-            "URL: %s{%s} Token: %s\n", 
-            $this->url, 
-            $scope,
-            $this->token
-        );
-
+        echo sprintf("%s: %s%s\n", $post ? "POST" : "GET", $this->url, $scope);
         $c = curl_init();
 
         if (!$c) {
@@ -131,18 +125,14 @@ trait Curl {
     }
 
     protected function authorized($scope) {
-        if ($this->test($scope, 401)) {
-            throw new NotAutorizedException("Not authorized", 401);
+        $ret = "";
+        if ($this->method($ret, $scope, array(), false, false) == 401) {
+            throw new NotAuthorizedException("Not authorized", 401);
         }
         return true;
     }
 
-    protected function test($scope, $code = 200) {
-        $req = "";
-        return $this->method($ret, $scope, array(), false, false) == $code;
-    }
-
-    private function post($scope, $params) {
+    private function post($scope = "", $params = array()) {
         $req = "";
 
         $code = $this->method($req, $scope, $params, true, true);
@@ -150,12 +140,14 @@ trait Curl {
         switch ($code) {
         case 200:
             return $req;
+        case 401:
+            throw new NotAuthorizedException(HTTPUnexpectedResponse::$ecode[401], 401);
         default:
             throw new HTTPUnexpectedResponse(HTTPUnexpectedResponse::$ecode[$code], $code);
         }
     }
 
-    private function get($scope, $params) {
+    private function get($scope = "", $params = array()) {
         $req = "";
 
         $code = $this->method($req, $scope, $params, false, true);
@@ -163,6 +155,8 @@ trait Curl {
         switch ($code) {
         case 200:
             return $req;
+        case 401:
+            throw new NotAuthorizedException(HTTPUnexpectedResponse::$ecode[401], 401);
         default:
             throw new HTTPUnexpectedResponse(HTTPUnexpectedResponse::$ecode[$code], $code);