Browse Source

Refactoring. Classes and spl follows PSR-4

Joachim M. Giæver 6 years ago
parent
commit
ca9f057629

+ 10 - 4
index.php

@@ -9,17 +9,21 @@
 
 require "./src/gpac.php";
 
+use Gogs\Lib\Curl\Exception as ApiException;
+
 define('API_URL', 'https://git.giaever.org/api/v1');
 define('API_TOKEN', 'ccc9a0ea6fefa8df558f4994e8c8ada2efa97f12');
 //define('API_TOKEN', '142efbfd6fbdf147f03d289f8b22a438eaa1b5d1');
 
 
 try {
-    $client =  new \Client\GogsAPI(API_URL, API_TOKEN);
+    $client =  new Gogs\API\Client(API_URL, API_TOKEN);
 
-    //$users = $client->users()->search(array("name" => "bje007"));
+    $users = $client->users()->search(array("name" => "bje007"));
 
     $me = $client->user()->load();
+
+    var_dump($me, $users);
     
     #try {
     #    // Create new repo under authorized user
@@ -99,9 +103,11 @@ try {
         "this is desc."
     ));
 
-} catch (\Lib\NotAuthorizedException $e) {
+} catch (ApiException\NotAuthorizedException $e) {
     die("NOT AUTH: " . $e->getMessage());
-} catch (\Lib\HTTPUnexpectedResponse $e) {
+} catch (ApiException\HTTPUnexpectedResponse $e) {
+    die($e);
+} catch (Exception $e) {
     die($e);
 }
 ?>

+ 0 - 74
src/client/gogsapi.php

@@ -1,74 +0,0 @@
-<?php namespace Client;
-
-/** 
- * Gogs API client. This class initially provide 
- * the programmer with a starting point and a kind
- * of an interface to start from, to keep  track of
- * the context.
- * 
- * @author Joachim M. Giaever (joachim[]giaever.org)
- * @version 0.1
- */
-
-class GogsAPI {
-    use \Lib\Curl;
-
-    /** 
-     * Constructor of the class to store the api data.
-     * 
-     * @param string $api_url 
-     * @param string $api_token 
-     * @return null
-     */
-    public function __construct(string $api_url, string $api_token) {
-        $this->url = $api_url;
-        $this->token = $api_token;
-    }
-
-    /** 
-     * Returns an object to fetch users from the
-     * Gogs installation. See \Request\Users class
-     * to understand usage (e.g ->load() to fetch all, 
-     * ->search(array params) to search for one or several 
-     * users etc).
-     * 
-     * @return \Request\Users
-     */
-    public function users() {
-        return new Request\Users($this->url, $this->token);
-    }
-
-    /** 
-     * Returns an object to fetch the repositories
-     * on the Gogs installation. See \Request\Repos to
-     * understand usage. Inherits the same class as \R\Users,
-     * but the usage may differ!
-     *
-     * Note! To fetch a particular repo under a user, you
-     * should go through the user (see method below).
-     * 
-     * @return \Request\Repos
-     */
-    public function repos() {
-        return new Request\Repos($this->url, $this->token);
-    }
-
-    /** 
-     * Returns either
-     * * the authorized user ($name = "" or "me")
-     * * the specified user ($name = anything else)
-     * 
-     * @return \Request\User
-     */
-    public function user(string $name = "me") {
-        return new Request\User($this->url, $this->token, $name);
-    }
-
-    /** 
-     * Nothing to "destruct" at this time. 
-     * 
-     * @return null
-     */
-    public function __destruct() {}
-}
-?>

+ 0 - 108
src/client/request/base.php

@@ -1,108 +0,0 @@
-<?php namespace Client\Request;
-
-abstract class Base implements RequestInterface {
-
-    protected $loaded = false;
-    protected $scope;
-
-    use \Lib\Curl {
-        get as private mget;
-        post as private mpost;
-        delete as private mdelete;
-    }
-
-    public function __construct(string $api_url, string $api_token) {
-        $this->url = $api_url;
-        $this->token = $api_token;
-    }
-
-    public function load(bool $force = false) {
-        $this->set_scope("get");
-
-        if ($this->loaded && !$force)
-            return $this;
-
-        $jenc =  $this->mget($this->scope);
-
-        $this->json_set_property($this->json_decode($jenc));
-        $this->loaded = true;
-
-        return $this;
-    }
-
-    protected function method_get(array $params = array()) {
-        return $this->mget($this->scope, $params);
-    }
-
-    protected function method_post(array $params = array()) {
-        return $this->mpost($this->scope, $params);
-    }
-
-    protected function method_delete() {
-        return $this->mdelete($this->scope);
-    }
-
-    public function get(string $s) {
-        if (!$this->set_scope("get"))
-            throw new NotImplementedException("::get:: Not implemented for class");
-    }
-
-    public function create(...$args) {
-
-        if ($this->loaded)
-            throw new Exception("::create:: Cant create on an git-initialized object. Create new object.");
-
-        if (!$this->set_scope("create"))
-            throw new NotImplementedException("::create:: Not implemented for class");
-
-        $ret = $this->method_post(...$args);
-
-        $this->json_set_property($this->json_decode($ret));
-
-        return true;
-    }
-
-    public function patch() {
-
-        if (!$this->loaded)
-            throw new InvalidMethodRequest("::patch:: Cant patch an git-uninitialized object. Load it first.");
-
-        if (!$this->set_scope("patch"))
-            throw new NotImplementedException("::patch:: Not implemented for class");
-    }
-
-    public function delete() {
-        if (!$this->set_scope("delete"))
-            throw new NotImplementedException("::delete:: Not implemented for class");
-
-        return $this->method_delete();
-    }
-
-    protected function json_decode(string $jenc) {
-        $obj = json_decode($jenc);
-
-        $this->json_error();
-
-        return $obj;
-    }
-
-    protected function json_encode(iterable $jdec) {
-        $jenc = json_encode($jdec);
-
-        $this->json_error();
-
-        return $jenc;
-    }
-
-    protected function json_error() {
-        if (($err = json_last_error()) != JSON_ERROR_NONE)
-            throw new RequestErrorException(json_last_error_msg(), $err);
-    }
-
-    abstract protected function json_set_property($obj);
-    abstract protected function set_scope(string $method);
-
-    public function __destruct() {}
-}
-
-?>

+ 0 - 99
src/client/request/collection.php

@@ -1,99 +0,0 @@
-<?php namespace Client\Request;
-
-class SearchParamException extends \Exception {};
-
-abstract class Collection extends Base implements \Lib\ArrayIterator {
-    private $objs = array();
-
-    public function add($obj, $key = null) {
-
-        if ($obj == false)
-            echo "OBJ ($key) = FALSE\n";
-
-        if (!isset($key))
-            array_push($this->objs, $obj);
-        else
-            $this->objs[$key] = $obj;
-    }
-
-    public function remove($any) {
-        if (isset($this->objs[$any])) {
-            unset($this->objs[$any]);
-            return true;
-        } else if (in_array($any, $this->objs)) {
-            $key = array_search($any, $this->objs, true);
-            return $this->remove($key);
-        }
-        return false;
-    }
-
-    public function all() {
-        return $this->objs;
-    }
-
-    public function len() {
-        return $this->objs;
-    }
-
-    public function by_key($idx) {
-        return isset($this->objs[$idx]) ? $this->objs[$idx] : false;
-    }
-
-    public function next() {
-        return next($this->objs);
-    }
-
-    public function prev() {
-        return prev($this->objs);
-    }
-
-    public function current() {
-        return current($this->objs);
-    }
-
-    public function reset() {
-        return reset($this->objs);
-    }
-
-    //abstract public function create(...$args);
-    abstract public function search(array $params = array());
-}
-
-class CollectionResult implements \Lib\ArrayIterator {
-    private $objs = array();
-
-    public function set($val, $key = null) {
-        if ($key == null && is_array($val))
-            $this->objs = $val;
-        else if ($key != null)
-            $this->objs[$key] = $val;
-        else 
-            array_push($this->objs, $val);
-    }
-
-    public function by_key($idx) {
-        return isset($this->objs[$idx]) ? $this->objs[$idx] : false;
-    }
-
-    public function all() {
-        return $this->objs;
-    }
-
-    public function len() {
-        return count($this->objs);
-    }
-
-    public function next() {
-        return next($this->objs);
-    }
-
-    public function current() {
-        return current($this->objs);
-    }
-
-    public function reset() {
-        return current($this->objs);
-    }
-
-}
-?>

+ 0 - 50
src/client/request/org.php

@@ -1,50 +0,0 @@
-<?php namespace Client\Request;
-
-class Org extends User {
-    public $udescription;
-    public $uwebsite;
-    public $ulocation;
-
-    private $owner;
-
-    public function __construct(string $api_url, string $api_token, string $oname = null, User $owner = null) {
-        $this->ousername = $oname;
-        $this->owner = $owner;
-        parent::__construct($api_url, $api_token);
-    }
-
-    protected function set_scope(string $method) {
-        switch ($method) {
-        case "create":
-            if ($this->owner == null);
-
-            $this->scope = "/admin/users/" . $this->owner->uusername . "/orgs";
-            return true;
-        case "get":
-            $this->scope = "/orgs/" . $this->uusername;
-            return true;
-        }
-    }
-
-    public function search(string $q) {
-        $searchable = sprintf("%s %s %s", $this->ufull_name, $this->uusername, $this->udescription);
-
-        return stripos($searchable, $q) !== false;
-    }
-
-    public function create(...$args) {
-        $params = array(
-            "username" => isset($args[0]) && is_string($args[0]) ? $args[0] : null,
-            "ufull_name" => isset($args[1]) && is_string($args[1]) ? $args[1] : null,
-            "description" => isset($args[2]) && is_string($args[2]) ? $args[2] : null,
-            "website" => isset($args[3]) && is_string($args[3]) ? $args[3] : null,
-            "location" => isset($args[4]) && is_string($args[4]) ? $args[4] : null
-        );
-
-        $params = array_filter($params, function($val) {
-            return $val != null;
-        });
-
-        parent::create($params);
-    }
-}

+ 0 - 66
src/client/request/orgs.php

@@ -1,66 +0,0 @@
-<?php namespace Client\Request;
-
-class Orgs extends Collection {
-    protected $owner;
-
-    public function __construct(string $api_url, string $api_token, User $owner) {
-        $this->owner = $owner;
-        parent::__construct($api_url, $api_token);
-    }
-
-    protected function set_scope(string $method) {
-        switch ($method) {
-        case "get":
-            $this->scope = ($this->owner == null || $this->owner->authenticated() ? "/user" : "/users/" . $this->owner->uusername) . "/orgs";
-            return true;
-        }
-    }
-
-    public function create(...$args) {
-
-        $org = new Org($this->url, $this->token, null, $this->owner);
-
-        if (count($args) > 0)
-            $org->create(...$args);
-
-        return $org;
-    }
-
-    public function get(string $s) {
-
-        if (($org = $this->by_key($s)))
-            return $org;
-
-        return new Org($this->url, $this->token, $s, $this->owner);
-    }
-
-    public function search(array $params = array()) {
-
-        if (!isset($params["name"]) && !isset($params["q"]))
-            throw new SearchParamException("Missing param <name>|<q>");
-
-        $q = isset($params["name"]) ? $params["name"] : $params["q"];
-        $l = isset($params["limit"]) ? $params["limit"] : 10;
-
-        $this->load();
-
-        $orgs = new CollectionResult();
-
-        foreach ($this->all() as $key => $org) {
-            if ($org->search($q))
-                $orgs->set($org);
-            if ($orgs->len() == $l)
-                break;
-        }
-
-        return $orgs;
-    }
-
-    protected function json_set_property($obj) {
-        foreach($obj as $val) {
-            $org = new Org($this->url, $this->token, null, $this->owner);
-            $org->json_set_property($val);
-            $this->add($org, $val->username);
-        }
-    }
-}

+ 0 - 105
src/client/request/repo.php

@@ -1,105 +0,0 @@
-<?php namespace Client\Request;
-
-class Repo extends Base {
-    
-    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 $rwatchers_count;
-    public $ropen_issues_count;
-    public $rdefault_branch;
-    public $rcreated_at;
-    public $rupdated_at;
-    public $rpermissions;
-    public $radmin;
-    public $rpush;
-    public $rpull;
-
-    public function __construct(string $api_url, string $api_token, User $owner = null, string $name = null) {
-        $this->rowner = $owner;
-        $this->rname = $name;
-        parent::__construct($api_url, $api_token);
-    }
-
-    protected function set_scope(string $method) {
-        switch ($method) {
-        case "create":
-            if ($this->rowner instanceof Org)
-                $this->scope = "/org/" . $this->rowner->uusername . "/repos";
-            elseif ($this->rowner->authenticated())
-                $this->scope = "/user/repos";
-            else
-                $this->scope = "/admin/users/" . $this->rowner->uusername . "/repos";
-            break;
-        case "delete":
-            $this->scope = "/repos/" . $this->rowner->uusername . "/" . $this->rname;
-            break;
-        case "get":
-            $this->scope = "/repos/" . ($this->rowner ? $this->rowner->uusername . "/" . $this->rname : $this->rfull_name);
-            break;
-        default:
-            return false;
-        }
-
-        return true;
-    }
-
-    protected function json_set_property($obj) {
-        foreach($obj as $key => $val) {
-            $key = 'r' . $key;
-            if (property_exists($this, $key)) {
-                switch ($key) {
-                case 'rowner':
-                    if (!$this->rowner) {
-                        $user = new User($this->url, $this->token);
-                        $user->json_set_property($val);
-                        $this->{$key} = $user;
-                    }
-                    break;
-                default:
-                    $this->{$key} = $val;
-                }
-            }
-        }
-        $this->loaded = true;
-    }
-
-    public function search(string $q) {
-        $searchable = sprintf("%s %s", $this->rname, $this->rdescription);
-
-        return stripos($searchable, $q) !== false;
-    }
-
-    public function create(...$args) {
-
-        $params = array(
-            "name" => isset($args[0]) && is_string($args[0]) ? $args[0] : null,
-            "description" => isset($args[1]) && is_string($args[1]) ? $args[1] : null,
-            "private" => isset($args[2]) && is_bool($args[2]) ? $args[2] : false,
-            "auto_init" => isset($args[3]) && is_bool($args[3]) ? $args[3] : false,
-            "gitignores" => isset($args[4]) && is_string($args[4]) ? $args[4] : null,
-            "licence" => isset($args[5]) && is_string($args[5]) ? $args[5] : null,
-            "readme" => isset($args[6]) && is_string($args[6]) ? $args[6] : "Default"
-        );
-
-        $params = array_filter($params, function($val) {
-            return $val != null;
-        });
-
-        parent::create($params);
-    }
-}

+ 0 - 97
src/client/request/repos.php

@@ -1,97 +0,0 @@
-<?php namespace Client\Request;
-
-class Repos extends Collection {
-
-    protected $owner;
-
-    public function __construct(string $api_url, string $api_token, User $owner = null) {
-        $this->owner = $owner;
-        parent::__construct($api_url, $api_token);
-    }
-
-    protected function set_scope(string $method) {
-        switch ($method) {
-        case "get":
-            if ($this->owner instanceof Org)
-                $this->scope = "/orgs/" . $this->owner->uusername . "/repos";
-            else
-                $this->scope = ($this->owner == null || $this->owner->authenticated() ? "/user" : "/users/" . $this->owner->uusername ) . "/repos";
-        default:
-            return false;
-        }
-
-        return true;
-    }
-
-    public function create(...$args) {
-
-        $repo = new Repo($this->url, $this->token, $this->owner);
-
-        if (count($args) > 0)
-            $repo->create(...$args);
-
-        return $repo;
-    }
-
-    public function search(array $params = array()) {
-        $scope = substr($this->scope, 1, strpos($this->scope, "/", 1)-1);
-
-        if (!isset($params["name"]) && !isset($params["q"]))
-            throw new SearchParamException("Missing param <name>|<q>");
-
-        if (isset($params["name"])) {
-            $params["q"] = $params["name"];
-            unset($params["name"]);
-        }
-
-        if (!isset($params["user"]))
-            $params["user"] = 0;
-
-        if (!isset($params["limit"]))
-            $params["limit"] = 10;
-
-        $repos = new CollectionResult();
-        
-        switch ($scope) {
-        case "user":
-        case "users":
-        case "org":
-            $this->load();
-
-            foreach($this->all() as $key => $repo) {
-                if ($repo->search($params["q"]))
-                    $repos->set($repo);
-                if ($repos->len() == $params["limit"])
-                    break;
-            }
-
-            return $repos;
-        default:
-            $jenc = $this->method_get("/search", $params);
-            $jdec = $this->json_decode($jenc);
-
-            foreach($this->json_set_property($jdec) as $key)
-                $repos->set($this->by_key($key), $key);
-
-            return $repos;
-        }
-    }
-
-    protected function json_set_property($obj) {
-        if (isset($obj->data))
-            return $this->json_set_property($obj->data);
-
-        $rnames = array();
-
-        if (is_array($obj)) {
-            foreach ($obj as $key => $val) {
-                $repo = new Repo($this->url, $this->token);
-                $repo->json_set_property($val);
-                $this->add($repo, $repo->rfull_name);
-                array_push($rnames, $repo->rfull_name);
-            }
-        }
-
-        return $rnames;
-    }
-}

+ 0 - 16
src/client/request/requestinterface.php

@@ -1,16 +0,0 @@
-<?php namespace Client\Request;
-
-class InvalidMethodRequest extends \Exception {};
-class RequestErrorException extends \Exception {};
-class NotImplementedException extends \BadMethodCallException{}
-
-interface RequestInterface {
-
-    public function load(bool $force = true);
-    public function get(string $s);
-    public function create(...$args);
-    public function patch();
-    public function delete();
-}
-
-?>

+ 0 - 79
src/client/request/user.php

@@ -1,79 +0,0 @@
-<?php  namespace Client\Request;
-
-class User extends Base {
-
-    private $authenticated;
-    public $uid;
-    public $ulogin;
-    public $ufull_name;
-    public $uemail;
-    public $uavatar_url;
-    public $uusername;
-
-    public function __construct(string $api_url, string $api_token, string $user = "") {
-        $this->uusername = (strlen($user) == 0 || $user == "me" ? "me" : $user);
-        $this->authenticated = $this->uusername == "me";
-        parent::__construct($api_url, $api_token);
-    }
-
-    protected function set_scope(string $method) {
-        switch($method) {
-        case "create":
-
-            if ($this->loaded)
-                throw new InvalidMethodRequest("Cannot create user of existing user");
-
-            $this->scope = "/admin/users";
-            
-            return true;
-        case "delete":
-            $this->scope = "/admin/users/" . $this->uusername;
-            return true;
-        default:
-            $this->scope = ($this->authenticated ? "/user" : "/users/" . $this->uusername);
-        }
-    }
-
-    public function authenticated() {
-        return $this->authenticated;
-    }
-
-    public function repos() {
-        return new Repos($this->url, $this->token, $this);
-    }
-
-    public function repo(string $name) {
-        return (new Repo($this->url, $this->token, $this, $name))->load();
-    }
-
-    public function organizations() {
-        return new Orgs($this->url, $this->token, $this);
-    }
-
-    public function create(...$args) {
-        $params = array(
-            "username" => isset($args[0]) && is_string($args[0]) ? $args[0] : null,
-            "email" => isset($args[1]) && is_string($args[1]) ? $args[1] : null,
-            "source_id" => isset($args[2]) && is_numeric($args[2]) ? $args[2] : null,
-            "login_name" => isset($args[3]) && is_string($args[3]) ? $args[3] : null,
-            "password" => isset($args[4]) && is_string($args[4]) ? $args[4] : null,
-            "send_notify" => isset($args[5]) && is_bool($args[5]) ? $args[5] : null
-        );
-
-        $params = array_filter($params, function($val) {
-            return $val != null;
-        });
-        parent::create($params);
-    }
-
-    protected function json_set_property($obj) {
-        foreach ($obj as $key => $value) {
-            $key = 'u' . $key;
-            if (property_exists($this, $key))
-                $this->{$key} = $value;
-        }
-        $this->loaded = true;
-    }
-}
-
-?>

+ 0 - 78
src/client/request/users.php

@@ -1,78 +0,0 @@
-<?php namespace Client\Request;
-
-/** 
- * Returns one or more users in the Gogs installation, 
- * depending on the called method. 
- * 
- * @author Joachim M. Giaever (joachim[]giaever.org)
- * @package request
- */
-class Users extends Collection {
-
-    protected function set_scope(string $method) {
-        var_dump("METHOD", $method);
-        switch ($method) {
-        default:
-            $this->scope = "/users";
-        }
-    }
-
-    /**
-     * Returns a new user object. If arguments
-     * is specified the user will be "created".
-     *
-     * The arguments can be left out to "create" the
-     * user through the user object iteself.
-     *
-     * @param ...$args User->create arguments
-     * @return \User
-     */
-    public function create(...$args) {
-
-        $user = new User($this->url, $this->token, "-");
-
-        if (count($args) != 0)
-            $user->create(...$args);
-
-        return $user;
-    }
-
-    public function get(string $s = "") {
-
-        if ($this->by_key($s))
-            return $this->by_key($s);
-
-        $user = (new User($this->url, $this->token, $s))->load();
-
-        $this->add($user, $user->ulogin);
-
-        return $user;
-    }
-
-    public function search(array $params = array()) {
-
-        if (!isset($params["name"]) && !isset($params['q']))
-            throw new SearchParamException("Missing param <name>|<q>");
-
-        if (isset($params["name"])) {
-            $params["q"] = $params["name"];
-            unset($params["name"]);
-        }
-
-        $jenc = $this->method_get("/search", $params);
-
-        $this->json_set_property($this->json_decode($jenc));
-
-        return $this;
-    }
-
-    protected function json_set_property($obj) {
-        if (isset($obj->data)) {
-            foreach($obj->data as $key => $val) {
-                $user = new User($this->url, $this->token, $val->login);
-                $user->json_set_property($val);
-                $this->add($user, $user->ulogin);
-            }
-        }
-    }
-}

+ 16 - 2
src/gpac.php

@@ -1,10 +1,24 @@
-<?php
+<?php namespace Gogs;
 
 define('GPAC_BASE_PATH', realpath(dirname(__FILE__)));
 
 spl_autoload_register(function($class) {
-    $file = GPAC_BASE_PATH . DIRECTORY_SEPARATOR . str_replace("\\", DIRECTORY_SEPARATOR, strtolower($class)) . ".php";
+
+    $prefix = "Gogs\\";
+    $len = strlen($prefix);
+
+    if (strncmp($prefix, $class, $len) !== 0) {
+        return;
+    }
+
+    $file = GPAC_BASE_PATH . DIRECTORY_SEPARATOR;
+    $file .= str_replace(
+        "\\", DIRECTORY_SEPARATOR, substr($class, $len)
+    ) . ".php";
 
     if (file_exists($file))
         require_once $file;
 });
+
+return;
+?>

+ 0 - 11
src/lib/arrayiterator.php

@@ -1,11 +0,0 @@
-<?php namespace Lib;
-
-interface ArrayIterator {
-    public function current();
-    public function next();
-    public function reset();
-    public function len();
-    public function all();
-    public function by_key($idx);
-}
-

+ 0 - 350
src/lib/curl.php

@@ -1,350 +0,0 @@
-<?php namespace Lib;
-
-/** 
- * Defines an unexpected response.
- *
- * @author Joachim M. Giaever (joachim[]giaever.org)
- * @package curl
- * @version 0.1
- */
-class HTTPUnexpectedResponse extends \Exception {
-    /**
-     * Includes valid codes, as a valid code can also be unexpeted.
-     *
-     * @var array $ecode HTTP status codes
-     * @static
-     */
-    static $ecode = array(
-        0 => "Unknown error",
-        100 => 'Continue',
-        101 => 'Switching Protocols',
-        102 => 'Processing', // WebDAV; RFC 2518
-        200 => 'OK',
-        201 => 'Created',
-        202 => 'Accepted',
-        203 => 'Non-Authoritative Information', // since HTTP/1.1
-        204 => 'No Content',
-        205 => 'Reset Content',
-        206 => 'Partial Content',
-        207 => 'Multi-Status', // WebDAV; RFC 4918
-        208 => 'Already Reported', // WebDAV; RFC 5842
-        226 => 'IM Used', // RFC 3229
-        300 => 'Multiple Choices',
-        301 => 'Moved Permanently',
-        302 => 'Found',
-        303 => 'See Other', // since HTTP/1.1
-        304 => 'Not Modified',
-        305 => 'Use Proxy', // since HTTP/1.1
-        306 => 'Switch Proxy',
-        307 => 'Temporary Redirect', // since HTTP/1.1
-        308 => 'Permanent Redirect', // approved as experimental RFC
-        400 => 'Bad Request',
-        401 => 'Unauthorized',
-        402 => 'Payment Required',
-        403 => 'Forbidden',
-        404 => 'Not Found',
-        405 => 'Method Not Allowed',
-        406 => 'Not Acceptable',
-        407 => 'Proxy Authentication Required',
-        408 => 'Request Timeout',
-        409 => 'Conflict',
-        410 => 'Gone',
-        411 => 'Length Required',
-        412 => 'Precondition Failed',
-        413 => 'Request Entity Too Large',
-        414 => 'Request-URI Too Long',
-        415 => 'Unsupported Media Type',
-        416 => 'Requested Range Not Satisfiable',
-        417 => 'Expectation Failed',
-        418 => 'I\'m a teapot', // RFC 2324
-        419 => 'Authentication Timeout', // not in RFC 2616
-        420 => 'Enhance Your Calm', // Twitter
-        420 => 'Method Failure', // Spring Framework
-        422 => 'Unprocessable Entity', // WebDAV; RFC 4918
-        423 => 'Locked', // WebDAV; RFC 4918
-        424 => 'Failed Dependency', // WebDAV; RFC 4918
-        424 => 'Method Failure', // WebDAV)
-        425 => 'Unordered Collection', // Internet draft
-        426 => 'Upgrade Required', // RFC 2817
-        428 => 'Precondition Required', // RFC 6585
-        429 => 'Too Many Requests', // RFC 6585
-        431 => 'Request Header Fields Too Large', // RFC 6585
-        444 => 'No Response', // Nginx
-        449 => 'Retry With', // Microsoft
-        450 => 'Blocked by Windows Parental Controls', // Microsoft
-        451 => 'Redirect', // Microsoft
-        451 => 'Unavailable For Legal Reasons', // Internet draft
-        494 => 'Request Header Too Large', // Nginx
-        495 => 'Cert Error', // Nginx
-        496 => 'No Cert', // Nginx
-        497 => 'HTTP to HTTPS', // Nginx
-        499 => 'Client Closed Request', // Nginx
-        500 => 'Internal Server Error',
-        501 => 'Not Implemented',
-        502 => 'Bad Gateway',
-        503 => 'Service Unavailable',
-        504 => 'Gateway Timeout',
-        505 => 'HTTP Version Not Supported',
-        506 => 'Variant Also Negotiates', // RFC 2295
-        507 => 'Insufficient Storage', // WebDAV; RFC 4918
-        508 => 'Loop Detected', // WebDAV; RFC 5842
-        509 => 'Bandwidth Limit Exceeded', // Apache bw/limited extension
-        510 => 'Not Extended', // RFC 2774
-        511 => 'Network Authentication Required', // RFC 6585
-        598 => 'Network read timeout error', // Unknown
-        599 => 'Network connect timeout error', // Unknown
-    );
-
-    /**
-     * The response from server (body)
-     * @access private
-     */
-    private $response;
-
-    /**
-     * Sets the exceptions.
-     *
-     * @string $message - the response from the server.
-     * @string $code - the HTTP status code.
-     * @exception $prev - Previous exceptions
-     **/
-    public function __construct(string $message, int $code = 0, Exception $previous = null) {
-        $this->response = $message;
-        parent::__construct(HTTPUnexpectedResponse::$ecode[$code], $code, $previous);
-    }
-
-    /** 
-     * Visual representation of the exception.
-     *
-     * @return string
-     */
-    public function __toString() {
-        return __CLASS__ . ": [{$this->code} | {$this->message}]: {$this->response}\n";
-    }
-
-    /** 
-     * Get the actual response from the body or the request.
-     *
-     * @return string
-     */
-    public function getResponse() {
-        return $this->response;
-    }
-}
-
-/** 
- * When the request fails because of an unauthorized token,
- * this is thrown instead.
- *
- * @author Joachim M. Giaever (joachim[]giaever.org)
- * @package curl
- * @version 0.1
- */
-class NotAuthorizedException extends HTTPUnexpectedResponse {
-
-    /**
-     * Sets the exceptions.
-     *
-     * @string $message - the response from the server.
-     * @string $code - the HTTP status code, @default 401
-     * @exception $prev - Previous exceptions
-     **/
-    public function __construct($message, $code = 401, Exception $previous = null) {
-        parent::__construct($message, $code, $previous);
-    }
-}
-
-/** 
- * A trait used for every class referencing the api-url and token.
- *
- * @author Joachim M. Giaever (joachim[]giaever.org)
- * @package curl
- * @version 0.1
- */
-trait Curl {
-    protected $url;
-    protected $token;
-
-    // TODO: Change this to something!
-    protected $user_agent = "Gogs PHP Api Client/0.1 (compatible; LINUX)";
-    protected $timeout = 30;
-    protected $max_redirects = 4;
-
-    /** 
-     * array_2_params takes an array and converts it into a
-     * query string (e.g param=val&param2=val2).
-     *
-     * @param array $params parameters to pass
-     * @return string
-     */
-    private function array_2_params(array $params) {
-        return join("&", array_map(function($k, $v) {
-            return sprintf("%s=%s", $k, rawurlencode(is_bool($v) ? ($v ? "true" : "false") : $v ));
-        }, array_keys($params), $params));
-    }
-
-    /** 
-     * array_2_json takes an array and converts it into a
-     * json-string (e.g {'name': 'This'}) which is typically
-     * used in a request body.
-     * 
-     * @param array $params paramters to pass
-     * @return string
-     */
-    private function array_2_json(array $params) {
-        return count($params) == 0 ? null : json_encode($params);
-    }
-
-    /** 
-     * Initializes a curl request of different kinds, depending
-     * on the specified method. This can be
-     *
-     * DELETE, PATCH, POST or GET. An unidentified value will
-     * become a GET-request.
-     * 
-     * @param string $method either DELETE, PATCH, POST, GET
-     * @param string &$req variable to store request body in
-     * @param string $scope scope within the API (e.g /user/repos)
-     * @param array $params parameters to pass
-     * @param bool $ret return transfer
-     * @return int the status code
-     */
-    protected function method(string $method, string &$req, string $scope, array $params, bool $ret) {
-        $c = curl_init();
-
-        if (!$c) {
-            return false;
-        }
-
-        $headers = array(
-            sprintf("Authorization: token %s", $this->token),
-        );
-
-        $url = sprintf("%s%s", $this->url, $scope);
-
-        echo sprintf("%s: %s, params: %s\n", $method, $url, $this->array_2_json($params));
-
-        if (in_array($method, array("DELETE", "PATCH", "POST"))) {
-            $json = $this->array_2_json($params);
-            curl_setopt($c, CURLOPT_CUSTOMREQUEST, $method);
-            curl_setopt($c, CURLOPT_POSTFIELDS, $json);
-            array_unshift($headers, "Content-Type: application/json");
-            array_push($headers, "Content-Length: " . strlen($json));
-        } else {
-            $url .= "?" . $this->array_2_params($params);
-        }
-
-        curl_setopt($c, CURLOPT_USERAGENT, $this->user_agent);
-        curl_setopt($c, CURLOPT_URL, $url);
-        curl_setopt($c, CURLOPT_HTTPHEADER, $headers);
-        curl_setopt($c, CURLOPT_RETURNTRANSFER, $ret);
-        curl_setopt($c, CURLOPT_TIMEOUT, $this->timeout);
-        curl_setopt($c, CURLOPT_MAXREDIRS, $this->max_redirects);
-        curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
-
-        $req = curl_exec($c);
-
-        $status_code = curl_getinfo($c, CURLINFO_HTTP_CODE);
-
-        curl_close($c);
-
-        return $status_code;
-    }
-
-    /** 
-     * Checks if the user is authorized for the scope. Shouldn't
-     * be used frequently. One test for one scope should be enough,
-     * but if you know for sure thats you're programming with the
-     * use of an authorized user you should leave this and just
-     * handle the NotAuthorizedExeption whenever thrown.
-     *
-     * @param $scope the scope, a relative uri.
-     * @throws Not AuthorizedException if server responde with a 401
-     * @return bool
-     */
-    protected function authorized(string $scope = "") {
-        $ret = "";
-        if ($this->method("GET", $ret, $scope, array(), false) == 401) {
-            throw new NotAuthorizedException("Not authorized", 401);
-        }
-        return true;
-    }
-
-    /**
-     * Post method.
-     *
-     * @param string $scope the scope, a relative uri.
-     * @param array $params the parameters to post.
-     * @throws NotAuthorizedException on 401, 403
-     * @throws HTTPUnexpectedResponse when not 200,201,401,403
-     * @return string the request content.
-     */
-    private function post(string $scope = "", array $params = array()) {
-        $req = "";
-
-        $code = $this->method("POST", $req, $scope, $params, true);
-
-        switch ($code) {
-        case 200:
-        case 201:
-            return $req;
-        case 401:
-        case 403:
-            throw new NotAuthorizedException($req, $code);
-        default:
-            throw new HTTPUnexpectedResponse($req, $code);
-        }
-    }
-
-    /**
-     * Delete method.
-     *
-     * @param string $scope the scope, a relative uri.
-     * @throws NotAuthorizedException on 401, 403
-     * @throws HTTPUnexpectedResponse when not 200,204,401,403
-     * @return string the request content.
-     */
-    private function delete(string $scope = "") {
-        $req = "";
-
-        $code = $this->method("DELETE", $req, $scope, array(), true);
-
-        switch ($code) {
-        case 200:
-        case 204:
-            return $req;
-        case 401:
-        case 403:
-            throw new NotAuthorizedException($req, $code);
-        default:
-            throw new HTTPUnexpectedResponse($req, $code);
-        }
-    }
-
-    /**
-     * GET method.
-     *
-     * @param string $scope the scope, a relative uri.
-     * @param array $params the parameters to post.
-     * @throws NotAuthorizedException on 401, 403
-     * @throws HTTPUnexpectedResponse when not 200,401,403
-     * @return string the request content.
-     */
-    private function get($scope = "", $params = array()) {
-        $req = "";
-
-        $code = $this->method("GET", $req, $scope, $params, true);
-
-        switch ($code) {
-        case 200:
-            return $req;
-        case 401:
-        case 403:
-            throw new NotAuthorizedException($req, $code);
-        default:
-            throw new HTTPUnexpectedResponse($req, $code);
-
-        }
-    }
-
-}