Browse Source

Cleaned up rest over Request\Collections and added tests in index

Joachim M. Giæver 6 years ago
parent
commit
9d242c3d43

+ 49 - 13
index.php

@@ -13,10 +13,10 @@ use Gogs\Lib\Curl\Exception as ApiException;
 
 define('API_URL', 'https://git.giaever.org/api/v1');
 define('API_TOKEN', '142efbfd6fbdf147f03d289f8b22a438eaa1b5d1');
-//define("API_TOKEN", "e14b9eff0749b6f0c4cadf4bb72b83d44578ae28");
+
+$client =  new Gogs\API\Client(API_URL, API_TOKEN);
 
 try {
-    $client =  new Gogs\API\Client(API_URL, API_TOKEN);
 
     $me = $client->user()->load();
 
@@ -26,7 +26,6 @@ try {
 
     $repos = $me->repos()->load();
 
-    /*
     echo "\nNormal repo\n";
     foreach($repos->all() as $key => $repo)
         echo sprintf("* %s: %s\n", $key, $repo->name);
@@ -80,29 +79,66 @@ try {
         foreach($org->repos()->load()->all() as $key => $repo)
             echo sprintf("#### %s: %s\n", $key, $repo->name);
     }
-     */
-    echo "Create data under specified user";
 
+    echo "Create data under specified user";
     $repo = $repos->create(
-        "test-test-test-" . $repos->load()->len(),
-        "This is repo #" . $repos->load()->len(),
+        "test-gogs-api-repo-" . $repos->load()->len(),
+        "This is test repo #" . $repos->load()->len() . " created with Gogs PHP API Client",
         false,
         true
     );
 
-    echo sprintf("* Created repo: '%s'", $repo->name);
+    echo "\nLooking up repos of test-test-test-#\n";
+    foreach($repos->search(array("name" => "test-gogs-api-repo-"))->sort_by()->all() as $key => $repo)
+        echo sprintf("Deleting: '%s' %s\n", $repo->name, $repo->delete() ? "true" : "false");
+
+    $orgs = $me->orgs()->load();
+
+    try {
+        echo "\nCreate organization\n";
+        $org = $orgs->create(
+            "test-" . $me->username . "-organization",
+            $me->full_name . " Testing Organization"
+        );
+        echo "Organization '" . $org->username . "' created!";
+    } catch (ApiException\NotAuthorizedException $e) {
+        throw new ApiException\NotAuthorizedException("Creating organization", $e->getCode(), $e);
+    } catch (ApiException\HTTPUnexpectedResponse $e) {
+        echo $e->getResponse();
+    }
+
+    echo "\nLooking up organizations of test-" . $me->username . "\n";
+    foreach($orgs->search(array("name" => "test-" . $me->username))->all() as $key => $org)
+        echo sprintf("* '%s': %s\n", $key, $org->username);
+
+    $users = $client->users()->search(array("name" => "test-user"));
+
+    $users->create(
+        "test-user-" . $users->len(),
+        "mytestuser" . $users->len() . "@gogitservice.joke"
+    );
+
+    foreach ($users->all() as $key => $user)
+        echo sprintf("%s: delete %s\n", $key, $user->delete() ? "true" : "false");
 
-    echo "\nNow having repos\n";
-    foreach($repos->load()->sort_by()->all() as $key => $repo)
-        $repo->delete();
 
 
     echo "\n\n\nLOG:\n" . join("\n", $client->get_log());
-    die();
 
 } catch (ApiException\NotAuthorizedException $e) {
-    die("NOT AUTH: " . $e->getMessage());
+    echo "\n\n\nLOG:\n" . join("\n", $client->get_log());
+    die("NOT AUTH: " . $e->getMessage() . "\nResponse: " . $e->getResponse() . "\n" . <<<EOF
+    POSSIBLY WHAT YOU WANTED, CAUSE IT SEEMS LIKE YOUR
+    AUTHORIZED USER IS TRYIGN TO PROCESS PARTS OF THE
+    INDEX THAT ITS NOT AUTHORIZED TO DO, SUCH AS
+        * Creating organizations
+        * Creating users
+        * More? Sure!
+
+EOF
+);
 } catch (ApiException\HTTPUnexpectedResponse $e) {
+    echo "\n\n\nLOG:\n" . join("\n", $client->get_log());
     die($e);
 } catch (Exception $e) {
     die($e);

+ 4 - 2
src/API/Request/Org.php

@@ -11,7 +11,7 @@ namespace Gogs\API\Request {
      *  * POST `/admin/users/username/orgs` (**Requires** admin rights. Curl will throw NotAuthorized exception if not).
      *
      * @author Joachim M. Giaever (joachim[]giaever.org)
-     * @version 0.1
+     * @version 0.1.1
      */
     final class Org extends User {
         public $org_description;
@@ -54,6 +54,7 @@ namespace Gogs\API\Request {
                 $this->scope = "/orgs/" . $this->username;
                 return true;
             }
+            return false;
         }
 
         /** 
@@ -79,6 +80,7 @@ namespace Gogs\API\Request {
          *  This reflects the API v1 doc, but is in an order
          *  where the required fields is first.
          *
+         * @todo Create team within org with user
          * @param ...$args The parameter values.
          * @return  bool
          */
@@ -95,7 +97,7 @@ namespace Gogs\API\Request {
                 return $val != null;
             });
 
-            parent::create($params);
+            Base::create($params);
         }
     }
 

+ 4 - 2
src/API/Request/Orgs.php

@@ -6,7 +6,7 @@ namespace Gogs\API\Request {
      * Orgs is a collection of organizations.
      *
      * @author Joachim M. Giaever (joachim[]giaever.org)
-     * @version 0.1
+     * @version 0.1.1
      */
     final class Orgs extends Collection {
         protected $owner;
@@ -44,8 +44,10 @@ namespace Gogs\API\Request {
 
             $org = new Org($this->url, $this->token, $this->owner);
 
-            if (count($args) > 0)
+            if (count($args) > 0) {
                 $org->create(...$args);
+                $this->add($org, $org->username);
+            }
 
             return $org;
         }

+ 1 - 1
src/API/Request/Repo.php

@@ -170,5 +170,5 @@ namespace Gogs\API\Request {
         }
     }
 
-    }
+}
 ?>

+ 15 - 0
src/API/Request/User.php

@@ -126,6 +126,13 @@ namespace Gogs\API\Request {
             return new Orgs($this->url, $this->token, $this);
         }
 
+        /**
+         * @alias organizations
+         */
+        public function orgs() {
+            return $this->organizations();
+        }
+
         /** 
          * Return a single organization.
          *
@@ -138,6 +145,13 @@ namespace Gogs\API\Request {
             return (new Org($this->url, $this->token, $this, $name))->load();
         }
 
+        /**
+         * @alias organization
+         */
+        public function org(string $name) {
+            return $this->organization($name);
+        }
+
         /** 
          * Create a new user.
          *
@@ -169,6 +183,7 @@ namespace Gogs\API\Request {
             $params = array_filter($params, function($val) {
                 return $val != null;
             });
+
             parent::create($params);
         }
 

+ 5 - 3
src/API/Request/Users.php

@@ -37,8 +37,10 @@ namespace Gogs\API\Request {
 
             $user = new User($this->url, $this->token, "-");
 
-            if (count($args) != 0)
+            if (count($args) != 0) {
                 $user->create(...$args);
+                $this->add($user, $user->username);
+            }
 
             return $user;
         }
@@ -75,9 +77,9 @@ namespace Gogs\API\Request {
                 )
             );
 
-            $users = new \Gogs\Lib\Collection();
+            $users = new Users($this->url, $this->token);
 
-            $users->set(array_diff_key($this->all(), $old));
+            $users->add(array_diff_key($this->all(), $old));
 
             return $users;
         }

+ 8 - 11
src/Lib/Curl/Client.php

@@ -72,16 +72,13 @@ namespace Gogs\Lib\Curl {
 
             $url = sprintf("%s%s", $this->url, $scope);
 
-            array_push(
-                self::$log,
-                sprintf(
-                    "%s:[%s] %s, %s, %s", 
-                    date("y-m-d H:i:s"), 
-                    $method, 
-                    $url, 
-                    !empty($p = $this->array_2_json($params)) ? $p : "none",
-                    get_class($this)
-                )
+            self::$log[] = sprintf(
+                "%s:[%s] %s, %s, %s", 
+                date("y-m-d H:i:s"), 
+                $method, 
+                $url, 
+                !empty($p = $this->array_2_json($params)) ? $p : "none",
+                get_class($this)
             );
 
             if (in_array($method, array("DELETE", "PATCH", "POST"))) {
@@ -187,7 +184,7 @@ namespace Gogs\Lib\Curl {
             switch ($code) {
             case 200:
             case 204:
-                return $req;
+                return true;
             case 401:
             case 403:
                 throw new Exception\NotAuthorizedException($req, $code);

+ 2 - 2
src/Lib/Curl/Exception/HTTPUnexpectedResponse.php

@@ -7,7 +7,7 @@ namespace Gogs\Lib\Curl\Exception {
      *
      * @author Joachim M. Giaever (joachim[]giaever.org)
      * @package curl
-     * @version -1.1
+     * @version 0.1.1
      */
     class HTTPUnexpectedResponse extends \Exception {
         /**
@@ -110,7 +110,7 @@ namespace Gogs\Lib\Curl\Exception {
          * @string $code - the HTTP status code.
          * @exception $prev - Previous exceptions
          **/
-        public function __construct(string $message, int $code = 0, Exception $previous = null) {
+        public function __construct(string $message, int $code = 0, \Exception $previous = null) {
             $this->response = $message;
             parent::__construct(
                 isset(HTTPUnexpectedResponse::$ecode[$code]) ? HTTPUnexpectedResponse::$ecode[$code] : HTTPUnexpectedResponse::$ecode[0], 

+ 2 - 2
src/Lib/Curl/Exception/NotAuthorizedException.php

@@ -8,7 +8,7 @@ namespace Gogs\Lib\Curl\Exception {
      *
      * @author Joachim M. Giaever (joachim[]giaever.org)
      * @package curl
-     * @version 0.1
+     * @version 0.1.1
      */
     class NotAuthorizedException extends HTTPUnexpectedResponse {
 
@@ -19,7 +19,7 @@ namespace Gogs\Lib\Curl\Exception {
          * @string $code - the HTTP status code, @default 401
          * @exception $prev - Previous exceptions
          **/
-        public function __construct($message, $code = 401, Exception $previous = null) {
+        public function __construct($message, $code = 401, \Exception $previous = null) {
             parent::__construct($message, $code, $previous);
         }
     }