Add "json" option
authorChristian Weiske <cweiske@cweiske.de>
Thu, 15 Sep 2016 20:31:16 +0000 (22:31 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Thu, 15 Sep 2016 20:31:16 +0000 (22:31 +0200)
12 files changed:
src/shpub/Command/AbstractProps.php
src/shpub/Command/Article.php
src/shpub/Command/Bookmark.php
src/shpub/Command/Delete.php
src/shpub/Command/Like.php
src/shpub/Command/Note.php
src/shpub/Command/Reply.php
src/shpub/Command/Repost.php
src/shpub/Command/Rsvp.php
src/shpub/Command/Undelete.php
src/shpub/Request.php
src/shpub/Validator.php

index 822fc77bba72636aa7fa52fd18fceff8ed815c0b..c9ea8bb1a5c323876da182ebe2c1f455afdd2ff7 100644 (file)
@@ -104,11 +104,27 @@ class Command_AbstractProps
                 'default'     => [],
             )
         );
                 'default'     => [],
             )
         );
+        static::addOptJson($cmd);
+    }
+
+    protected static function addOptJson(\Console_CommandLine_Command $cmd)
+    {
+        $cmd->addOption(
+            'json',
+            array(
+                'long_name'   => '--json',
+                'description' => 'Send request data as JSON',
+                'action'      => 'StoreTrue',
+                'default'     => false,
+            )
+        );
     }
 
     protected function handleGenericOptions(
         \Console_CommandLine_Result $cmdRes, Request $req
     ) {
     }
 
     protected function handleGenericOptions(
         \Console_CommandLine_Result $cmdRes, Request $req
     ) {
+        $this->handleOptJson($cmdRes, $req);
+
         if ($cmdRes->options['published'] !== null) {
             $req->req->addPostParameter(
                 'published', $cmdRes->options['published']
         if ($cmdRes->options['published'] !== null) {
             $req->req->addPostParameter(
                 'published', $cmdRes->options['published']
@@ -146,7 +162,7 @@ class Command_AbstractProps
             $postParams = [];
             foreach ($cmdRes->options['x'] as $xproperty) {
                 list($propkey, $propval) = explode('=', $xproperty, 2);
             $postParams = [];
             foreach ($cmdRes->options['x'] as $xproperty) {
                 list($propkey, $propval) = explode('=', $xproperty, 2);
-                if (!isset($postParams[$propkey] )) {
+                if (!isset($postParams[$propkey])) {
                     $postParams[$propkey] = [];
                 }
                 $postParams[$propkey][] = $propval;
                     $postParams[$propkey] = [];
                 }
                 $postParams[$propkey][] = $propval;
@@ -157,6 +173,12 @@ class Command_AbstractProps
         }
     }
 
         }
     }
 
+    protected function handleOptJson(
+        \Console_CommandLine_Result $cmdRes, Request $req
+    ) {
+        $req->setSendAsJson($cmdRes->options['json']);
+    }
+
     protected function handleFiles(
         \Console_CommandLine_Result $cmdRes, Request $req
     ) {
     protected function handleFiles(
         \Console_CommandLine_Result $cmdRes, Request $req
     ) {
index 9f40daf82f97943479f3905a0c314ccc2535f915..8445af49e7471eab0d803ad81c7fc03e41a37f07 100644 (file)
@@ -38,14 +38,14 @@ class Command_Article extends Command_AbstractProps
     public function run(\Console_CommandLine_Result $cmdRes)
     {
         $req = new Request($this->cfg->host, $this->cfg);
     public function run(\Console_CommandLine_Result $cmdRes)
     {
         $req = new Request($this->cfg->host, $this->cfg);
-        $req->req->addPostParameter('h', 'entry');
-        $req->req->addPostParameter('name', $cmdRes->args['title']);
+        $req->setType('entry');
+        $req->addProperty('name', $cmdRes->args['title']);
         if ($cmdRes->options['html']) {
         if ($cmdRes->options['html']) {
-            $req->req->addPostParameter(
-                'content[html]', $cmdRes->args['text']
+            $req->addProperty(
+                'content', ['html' => $cmdRes->args['text']]
             );
         } else {
             );
         } else {
-            $req->req->addPostParameter('content', $cmdRes->args['text']);
+            $req->addProperty('content', $cmdRes->args['text']);
         }
         $this->handleGenericOptions($cmdRes, $req);
 
         }
         $this->handleGenericOptions($cmdRes, $req);
 
index e25365cc03efc109406c501d40e58deaacc21683..bdbe0f3fffc5a92c2a46dafc4f60b2623c18e458 100644 (file)
@@ -32,11 +32,11 @@ class Command_Bookmark extends Command_AbstractProps
         }
 
         $req = new Request($this->cfg->host, $this->cfg);
         }
 
         $req = new Request($this->cfg->host, $this->cfg);
-        $req->req->addPostParameter('h', 'entry');
-        $req->req->addPostParameter('bookmark-of', $url);
+        $req->setType('entry');
+        $req->addProperty('bookmark-of', $url);
 
         if ($cmdRes->args['text']) {
 
         if ($cmdRes->args['text']) {
-            $req->req->addPostParameter('content', $cmdRes->args['text']);
+            $req->addProperty('content', $cmdRes->args['text']);
         }
 
 
         }
 
 
index a13775aa603881141f838384f8e9042b224af05d..7735e218814740d9787642113e112a4a4d4c0bcc 100644 (file)
@@ -6,6 +6,7 @@ class Command_Delete extends Command_AbstractProps
     public static function opts(\Console_CommandLine $optParser)
     {
         $cmd = $optParser->addCommand('delete');
     public static function opts(\Console_CommandLine $optParser)
     {
         $cmd = $optParser->addCommand('delete');
+        static::addOptJson($cmd);
         $cmd->addArgument(
             'url',
             [
         $cmd->addArgument(
             'url',
             [
@@ -23,8 +24,9 @@ class Command_Delete extends Command_AbstractProps
         }
 
         $req = new Request($this->cfg->host, $this->cfg);
         }
 
         $req = new Request($this->cfg->host, $this->cfg);
-        $req->req->addPostParameter('action', 'delete');
-        $req->req->addPostParameter('url', $url);
+        $this->handleOptJson($cmdRes, $req);
+        $req->setAction('delete');
+        $req->setUrl($url);
 
         $res = $req->send();
         Log::info('Post deleted from server');
 
         $res = $req->send();
         Log::info('Post deleted from server');
index 65020dc04cd832f57e8d6cc95929729bcccf7486..ad19b8f529838a4014f50bc70420bff4eb1932b1 100644 (file)
@@ -24,8 +24,8 @@ class Command_Like extends Command_AbstractProps
         }
 
         $req = new Request($this->cfg->host, $this->cfg);
         }
 
         $req = new Request($this->cfg->host, $this->cfg);
-        $req->req->addPostParameter('h', 'entry');
-        $req->req->addPostParameter('like-of', $url);
+        $req->setType('entry');
+        $req->addProperty('like-of', $url);
 
         $this->handleGenericOptions($cmdRes, $req);
         $res = $req->send();
 
         $this->handleGenericOptions($cmdRes, $req);
         $res = $req->send();
index 2e07e9d69765de69d984c233c21692b74208a420..c7683f3b7a289f5052e571b2e3754b0ba6e77db7 100644 (file)
@@ -20,8 +20,8 @@ class Command_Note extends Command_AbstractProps
     public function run(\Console_CommandLine_Result $cmdRes)
     {
         $req = new Request($this->cfg->host, $this->cfg);
     public function run(\Console_CommandLine_Result $cmdRes)
     {
         $req = new Request($this->cfg->host, $this->cfg);
-        $req->req->addPostParameter('h', 'entry');
-        $req->req->addPostParameter('content', $cmdRes->args['text']);
+        $req->setType('entry');
+        $req->addProperty('content', $cmdRes->args['text']);
         $this->handleGenericOptions($cmdRes, $req);
 
         $res = $req->send();
         $this->handleGenericOptions($cmdRes, $req);
 
         $res = $req->send();
index 493d58716808de438821c7d269c57f96f1d9b78b..6b3bc94cd336238cd2479af242b304b847804103 100644 (file)
@@ -32,9 +32,9 @@ class Command_Reply extends Command_AbstractProps
         }
 
         $req = new Request($this->cfg->host, $this->cfg);
         }
 
         $req = new Request($this->cfg->host, $this->cfg);
-        $req->req->addPostParameter('h', 'entry');
-        $req->req->addPostParameter('content', implode(' ', $cmdRes->args['text']));
-        $req->req->addPostParameter('in-reply-to', $url);
+        $req->setType('entry');
+        $req->addProperty('content', implode(' ', $cmdRes->args['text']));
+        $req->addProperty('in-reply-to', $url);
 
         $this->handleGenericOptions($cmdRes, $req);
         $res = $req->send();
 
         $this->handleGenericOptions($cmdRes, $req);
         $res = $req->send();
index 3c39178c84af83cda21e45a84cac6f2611fa5fb0..6d786096b7ae419f75808d4d5e9f1e24e0fc790f 100644 (file)
@@ -24,8 +24,8 @@ class Command_Repost extends Command_AbstractProps
         }
 
         $req = new Request($this->cfg->host, $this->cfg);
         }
 
         $req = new Request($this->cfg->host, $this->cfg);
-        $req->req->addPostParameter('h', 'entry');
-        $req->req->addPostParameter('repost-of', $url);
+        $req->setType('entry');
+        $req->addProperty('repost-of', $url);
 
         $this->handleGenericOptions($cmdRes, $req);
         $res = $req->send();
 
         $this->handleGenericOptions($cmdRes, $req);
         $res = $req->send();
index 5ed8ecdaf5f06b2979a6264ee62f69122496a844..69602d93ad3fd4affafbe09fdbf0140f8e5c0da0 100644 (file)
@@ -47,11 +47,11 @@ class Command_Rsvp extends Command_AbstractProps
         }
 
         $req = new Request($this->cfg->host, $this->cfg);
         }
 
         $req = new Request($this->cfg->host, $this->cfg);
-        $req->req->addPostParameter('h', 'entry');
-        $req->req->addPostParameter('in-reply-to', $url);
-        $req->req->addPostParameter('rsvp', $rsvp);
+        $req->setType('h', 'entry');
+        $req->addProperty('in-reply-to', $url);
+        $req->addProperty('rsvp', $rsvp);
         if ($cmdRes->args['text'] != '') {
         if ($cmdRes->args['text'] != '') {
-            $req->req->addPostParameter('content', $cmdRes->args['text']);
+            $req->addProperty('content', $cmdRes->args['text']);
         }
         $this->handleGenericOptions($cmdRes, $req);
 
         }
         $this->handleGenericOptions($cmdRes, $req);
 
index ad3525f90bace44582be6521ba22f4f05e910149..55dd6b99ccf51c36f9b87433d36c904da0d51493 100644 (file)
@@ -6,6 +6,7 @@ class Command_Undelete extends Command_AbstractProps
     public static function opts(\Console_CommandLine $optParser)
     {
         $cmd = $optParser->addCommand('undelete');
     public static function opts(\Console_CommandLine $optParser)
     {
         $cmd = $optParser->addCommand('undelete');
+        static::addOptJson($cmd);
         $cmd->addArgument(
             'url',
             [
         $cmd->addArgument(
             'url',
             [
@@ -23,8 +24,9 @@ class Command_Undelete extends Command_AbstractProps
         }
 
         $req = new Request($this->cfg->host, $this->cfg);
         }
 
         $req = new Request($this->cfg->host, $this->cfg);
-        $req->req->addPostParameter('action', 'undelete');
-        $req->req->addPostParameter('url', $url);
+        $this->handleOptJson($cmdRes, $req);
+        $req->setAction('undelete');
+        $req->setUrl($url);
 
         $res = $req->send();
         Log::info('Post restored at server');
 
         $res = $req->send();
         Log::info('Post restored at server');
index cbcc57e0e4a8e4fe348139ab850c78899dc53667..0cadc484c6d5391ddde586c107033ec3411e1562 100644 (file)
@@ -6,9 +6,15 @@ class Request
     public $req;
     public $cfg;
 
     public $req;
     public $cfg;
 
+    protected $sendAsJson = false;
     protected $uploadsInfo = [];
     protected $dedicatedBody = false;
 
     protected $uploadsInfo = [];
     protected $dedicatedBody = false;
 
+    protected $properties = [];
+    protected $type = null;
+    protected $action = null;
+    protected $url = null;
+
     public function __construct($host, $cfg)
     {
         $this->cfg = $cfg;
     public function __construct($host, $cfg)
     {
         $this->cfg = $cfg;
@@ -25,6 +31,49 @@ class Request
 
     public function send($body = null)
     {
 
     public function send($body = null)
     {
+        if ($this->sendAsJson) {
+            //application/json
+            if ($body !== null) {
+                throw new \Exception('body already defined');
+            }
+            $this->req->setHeader('Content-Type: application/json');
+            $data = [];
+            if ($this->action !== null) {
+                $data['action'] = $this->action;
+            }
+            if ($this->url !== null) {
+                $data['url'] = $this->url;
+            }
+            if ($this->type !== null) {
+                $data['type'] = 'h-' . $this->type;
+            }
+            if (count($this->properties)) {
+                $data['properties'] = $this->properties;
+            }
+            $body = json_encode($data);
+        } else {
+            //form-encoded
+            if ($this->type !== null) {
+                $this->req->addPostParameter('h', $this->type);
+            }
+            if ($this->action !== null) {
+                $this->req->addPostParameter('action', $this->action);
+            }
+            if ($this->url !== null) {
+                $this->req->addPostParameter('url', $this->url);
+            }
+            foreach ($this->properties as $propkey => $propval) {
+                if (isset($propval['html'])) {
+                    //workaround for content[html]
+                    $propkey = $propkey . '[html]';
+                    $propval = $propval['html'];
+                } else if (count($propval) == 1) {
+                    $propval = reset($propval);
+                }
+                $this->req->addPostParameter($propkey, $propval);
+            }
+        }
+
         if ($body !== null) {
             $this->dedicatedBody = true;
             $this->req->setBody($body);
         if ($body !== null) {
             $this->dedicatedBody = true;
             $this->req->setBody($body);
@@ -44,30 +93,44 @@ class Request
         return $res;
     }
 
         return $res;
     }
 
+    public function setAction($action)
+    {
+        $this->action = $action;
+    }
+
+    public function setType($type)
+    {
+        $this->type = $type;
+    }
+
+    public function setUrl($url)
+    {
+        $this->url = $url;
+    }
+
     /**
     /**
-     * @param string                $fieldName    name of file-upload field
-     * @param string|resource|array $filename     full name of local file,
+     * @param string                $fieldName name of file-upload field
+     * @param string|resource|array $filename  full name of local file,
      *               pointer to open file or an array of files
      */
     public function addUpload($fieldName, $filename)
     {
      *               pointer to open file or an array of files
      */
     public function addUpload($fieldName, $filename)
     {
+        if ($this->sendAsJson) {
+            throw new \Exception('File uploads do not work with JSON');
+        }
         $this->uploadsInfo[$fieldName] = $filename;
         return $this->req->addUpload($fieldName, $filename);
     }
 
     /**
         $this->uploadsInfo[$fieldName] = $filename;
         return $this->req->addUpload($fieldName, $filename);
     }
 
     /**
-     * Add one or multiple POST parameters.
-     * Automatically adds them as array or as string.
+     * Adds a micropub property to the request.
      *
      * @param string       $key    Parameter name
      * @param string|array $values One or multiple values
      */
      *
      * @param string       $key    Parameter name
      * @param string|array $values One or multiple values
      */
-    public function addPostParameter($key, $values)
+    public function addProperty($key, $values)
     {
     {
-        if (count($values) == 1) {
-            $values = reset($values);
-        }
-        $this->req->addPostParameter($key, $values);
+        $this->properties[$key] = (array) $values;
     }
 
     protected function printCurl()
     }
 
     protected function printCurl()
@@ -122,4 +185,9 @@ class Request
 
         Log::msg($command);
     }
 
         Log::msg($command);
     }
+
+    public function setSendAsJson($json)
+    {
+        $this->sendAsJson = $json;
+    }
 }
\ No newline at end of file
 }
\ No newline at end of file
index 283604d0acddfb6ed84dd637b4048c7e7869d981..c7515953c57812efdd227647889e9e1fe5967da9 100644 (file)
@@ -35,7 +35,7 @@ class Validator
         if (false === array_search($answer, $allowed)) {
             Log::err(
                 'Invalid RSVP answer given; allowed are: '
         if (false === array_search($answer, $allowed)) {
             Log::err(
                 'Invalid RSVP answer given; allowed are: '
-                . implode (', ', $allowed)
+                . implode(', ', $allowed)
             );
             return false;
         }
             );
             return false;
         }