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 34ef479ad94def40867c123e0a6524cf4fe0e497..62f824ee92546455cb309219983b63372d4f3ee1 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 7daafc22d7ee4afb51ebe1729c72ec2d941fe9e5..26f615a163e8d5e2f7643a71e866b58d70d1c441 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 cb09ba77943e33231171523df6b73ca923b0f70b..327111dbcc6a345eb14b7f875af654d58f14fbb8 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 d41e45dc2a3495179fb797eb185d471a0b8e015f..c9ebb4d783655e1d1544286be2b86f47a5b612fd 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 2896c810c3f43db8262c9c973598236cbb63aa82..96d7d44142c2dcb3991343df06c81809d3e8d5ce 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