Add --dry-run option for getting curl commands
[shpub.git] / src / shpub / Request.php
index 68db98d71350f69c7d824eff53ded6deaa5f5fbb..4437164af30f2cfda0836ebd7b174d98f9a54776 100644 (file)
@@ -56,7 +56,7 @@ class Request
                 $data['url'] = $this->url;
             }
             if ($this->type !== null) {
-                $data['type'] = 'h-' . $this->type;
+                $data['type'] = array('h-' . $this->type);
             }
             if (count($this->properties)) {
                 $data['properties'] = $this->properties;
@@ -93,18 +93,50 @@ class Request
             $cp = new CurlPrinter();
             $cp->show($this->req, $this->uploadsInfo, $this->dedicatedBody);
         }
+
+        if ($this->cfg->dryRun && $this->req->getMethod() != 'GET') {
+            //do not run any modifying queries
+            //fake a successful response
+            $res = new \HTTP_Request2_Response('HTTP/1.1 200 OK', false);
+            $res->parseHeaderLine('Content-type: text/plain');
+            $res->appendBody('Fake --dry-run response');
+            return $res;
+        }
+
         $res = $this->req->send();
 
         if (intval($res->getStatus() / 100) != 2) {
-            Log::err(
-                'Server returned an error status code ' . $res->getStatus()
-            );
-            Log::err($res->getBody());
-            exit(11);
+            $this->displayErrorResponse($res);
         }
         return $res;
     }
 
+    protected function displayErrorResponse($res)
+    {
+        Log::err(
+            'Server returned an error status code ' . $res->getStatus()
+        );
+
+        $shown = false;
+        if (Util::getMimeType($res) == 'application/json') {
+            $errData = json_decode($res->getBody());
+            if (!isset($errData->error)) {
+                Log::err('Error response does not contain "error" property');
+            } else if (isset($errData->error_description)) {
+                Log::err($errData->error . ': ' . $errData->error_description);
+                $shown = true;
+            } else {
+                Log::err($errData->error);
+                $shown = true;
+            }
+        }
+
+        if (!$shown) {
+            Log::err($res->getBody());
+        }
+        exit(11);
+    }
+
     public function setAction($action)
     {
         $this->action = $action;
@@ -121,11 +153,21 @@ class Request
     }
 
     /**
+     * Add file upload
+     *
      * @param string $fieldName name of file-upload field
      * @param array  $fileNames list of local file paths
+     *
+     * @return void
      */
     public function addUpload($fieldName, $fileNames)
     {
+        if ($this->directUpload && $this->sendAsJson) {
+            throw new \Exception(
+                'Cannot do direct upload with JSON requests'
+            );
+        }
+
         if ($this->host->endpoints->media === null
             || $this->directUpload
         ) {
@@ -152,6 +194,10 @@ class Request
     }
 
     /**
+     * Execute the file upload
+     *
+     * @param string $fileName File path
+     *
      * @return string URL at media endpoint
      */
     public function uploadToMediaEndpoint($fileName)
@@ -189,7 +235,7 @@ class Request
     {
         if ($isHtml) {
             $this->addProperty(
-                'content', ['html' => $text]
+                'content', [['html' => $text]]
             );
         } else {
             $this->addProperty('content', $text);
@@ -202,6 +248,8 @@ class Request
      *
      * @param string       $key    Parameter name
      * @param string|array $values One or multiple values
+     *
+     * @return void
      */
     public function addProperty($key, $values)
     {