debug option to print curl requests
authorChristian Weiske <cweiske@cweiske.de>
Wed, 7 Sep 2016 06:34:25 +0000 (08:34 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Wed, 7 Sep 2016 06:34:25 +0000 (08:34 +0200)
src/shpub/Cli.php
src/shpub/Command/Like.php
src/shpub/Command/Reply.php
src/shpub/Config.php
src/shpub/Request.php

index 34ef479..62f824e 100644 (file)
@@ -35,13 +35,13 @@ class Cli
 
             case 'like':
                 $this->requireValidHost();
-                $cmd = new Command_Like($this->cfg->host);
+                $cmd = new Command_Like($this->cfg);
                 $cmd->run($res->command->args['url']);
                 break;
 
             case 'reply':
                 $this->requireValidHost();
-                $cmd = new Command_Reply($this->cfg->host);
+                $cmd = new Command_Reply($this->cfg);
                 $cmd->run(
                     $res->command->args['url'],
                     implode(' ', $res->command->args['text'])
@@ -83,6 +83,7 @@ class Cli
             if ($opts['user'] !== null) {
                 $this->cfg->host->user = $opts['user'];
             }
+            $this->cfg->setDebug($opts['debug']);
 
             return $res;
         } catch (\Exception $exc) {
@@ -124,6 +125,16 @@ class Cli
                 'default'     => null,
             )
         );
+        $optParser->addOption(
+            'debug',
+            array(
+                'short_name'  => '-d',
+                'long_name'   => '--debug',
+                'description' => 'Verbose output',
+                'action'      => 'StoreTrue',
+                'default'     => false,
+            )
+        );
 
         $cmd = $optParser->addCommand('connect');
         $cmd->addOption(
index 7daafc2..26f615a 100644 (file)
@@ -4,13 +4,13 @@ namespace shpub;
 class Command_Like
 {
     /**
-     * @var Config_Host
+     * @var Config
      */
-    protected $host;
+    protected $cfg;
 
-    public function __construct($host)
+    public function __construct($cfg)
     {
-        $this->host = $host;
+        $this->cfg = $cfg;
     }
 
     public function run($url)
@@ -27,11 +27,17 @@ class Command_Like
             ]
         );
 
-        $req = new Request($this->host);
+        $req = new Request($this->cfg->host, $this->cfg);
         $res = $req->send($body);
         $postUrl = $res->getHeader('Location');
-        echo "Like created at server\n";
-        echo $postUrl . "\n";
+        if ($postUrl === null) {
+            Log::err('Error: Server sent no "Location" header and said:');
+            Log::err($res->getBody());
+            exit(20);
+        } else {
+            echo "Like created at server\n";
+            echo $postUrl . "\n";
+        }
     }
 }
 ?>
index cb09ba7..327111d 100644 (file)
@@ -4,13 +4,13 @@ namespace shpub;
 class Command_Reply
 {
     /**
-     * @var Config_Host
+     * @var Config
      */
-    protected $host;
+    protected $cfg;
 
-    public function __construct($host)
+    public function __construct($cfg)
     {
-        $this->host = $host;
+        $this->cfg = $cfg;
     }
 
     public function run($url, $text)
@@ -28,7 +28,7 @@ class Command_Reply
             ]
         );
 
-        $req = new Request($this->host);
+        $req = new Request($this->cfg->host, $this->cfg);
         $res = $req->send($body);
         $postUrl = $res->getHeader('Location');
         echo "Reply created at server\n";
index d41e45d..c9ebb4d 100644 (file)
@@ -12,6 +12,8 @@ class Config
      */
     public $host;
 
+    public $debug = false;
+
     protected function getConfigFilePath()
     {
         if (!isset($_SERVER['HOME'])) {
@@ -114,5 +116,10 @@ class Config
         }
         return '"' . $val . '"';
     }
+
+    public function setDebug($debug)
+    {
+        $this->debug = $debug;
+    }
 }
 ?>
index 2896c81..96d7d44 100644 (file)
@@ -4,23 +4,30 @@ namespace shpub;
 class Request
 {
     public $req;
+    public $cfg;
 
-    public function __construct($host)
+    public function __construct($host, $cfg)
     {
+        $this->cfg = $cfg;
         $this->req = new \HTTP_Request2($host->endpoints->micropub, 'POST');
+        $this->req->setHeader('User-Agent: shpub');
         if (version_compare(PHP_VERSION, '5.6.0', '<')) {
             //correct ssl validation on php 5.5 is a pain, so disable
             $this->req->setConfig('ssl_verify_host', false);
             $this->req->setConfig('ssl_verify_peer', false);
         }
         $this->req->setHeader('Content-type', 'application/x-www-form-urlencoded');
-        $this->req->setHeader('Authorization', 'Bearer ' . $host->token);
+        $this->req->setHeader('authorization', 'Bearer ' . $host->token);
     }
 
     public function send($body)
     {
         $this->req->setBody($body);
+        if ($this->cfg->debug) {
+            $this->printCurl();
+        }
         $res = $this->req->send();
+
         if (intval($res->getStatus() / 100) != 2) {
             Log::err(
                 'Server returned an error status code ' . $res->getStatus()
@@ -30,4 +37,19 @@ class Request
         }
         return $res;
     }
+
+    protected function printCurl()
+    {
+        $command = 'curl';
+        if ($this->req->getMethod() != 'GET') {
+            $command .= ' -X ' . $this->req->getMethod();
+        }
+        foreach ($this->req->getHeaders() as $key => $val) {
+            $command .= ' -H ' . escapeshellarg($key . ': ' . $val);
+        }
+        $command .= ' --data ' . escapeshellarg($this->req->getBody());
+        $command .= ' ' . escapeshellarg((string) $this->req->getUrl());
+
+        echo $command . "\n";
+    }
 }
\ No newline at end of file