From 40e412b80c10e1c1b1116c321121e2f380b0e63a Mon Sep 17 00:00:00 2001 From: Denis Cornehl Date: Thu, 19 Jul 2012 16:10:50 +0200 Subject: [PATCH 1/8] begin of handling new error-format --- phpcclib.php | 72 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 51 insertions(+), 21 deletions(-) diff --git a/phpcclib.php b/phpcclib.php index 3250b14..3f98c14 100644 --- a/phpcclib.php +++ b/phpcclib.php @@ -1,4 +1,7 @@ $addonName); return $this->_executePost($resource, $data); } - + /** * get sso login data @@ -976,7 +979,7 @@ public function billingAccount_getVoucherList($userName, $billingName) public function addon_getList() { return $this->_executeGet('/addon/', $requiresToken=false); } - + /** * return list of all support plans * @@ -993,12 +996,12 @@ public function addon_getList() { public function support_getList() { return $this->_executeGet('/support/', $requiresToken=false); } - + /** * return a single support plan * * @param string $planName name of the support plan - * + * * @throws BadRequestError * @throws ForbiddenError * @throws GoneError @@ -1181,6 +1184,44 @@ private function _jsonDecode($content) { */ class CCException extends Exception { + public function __construct($message, $status) + { + $this->message = $message; + + + $msgs = array(); + $obj = json_decode($message); + if (json_last_error() === JSON_ERROR_NONE && !empty($obj)) { + $rc = ""; + $errors = array(); + foreach ($obj as $k => $v) { + if ($k == "rc") + $rc = $v; + else if ($k == "error") + $errors = $v; + } + + if (count($errors) > 0) + $msgs = $errors; + else if (strlen($rc) > 0) + $this->message = $rc; + else + $msgs = $obj; + } + + if (count($msgs) > 0) { + $this->message = ''; + + + if (is_array($msgs) || $msgs instanceof Traversable || $msgs instanceof stdClass) { + foreach ($msgs as $k => $v) { + $this->message .= sprintf("%s: %s\n", $k, $v); + } + }else + $this->message = $msgs; + + } + } } /* @@ -1210,24 +1251,12 @@ public function __toString() */ class BadRequestError extends CCException { - private $_msgs = array(); - - public function __construct($message) + public function __construct($message, $status) { - $this->message = 'BadRequest'; - /* - * You will get a string like this - * Bad Request {"lastname": "This field is required.", "firstname": "This field is required."} - * therefore we cut the first 12 chars from errorMessage - */ - $obj = json_decode(substr($message, 12)); - - if (json_last_error() === JSON_ERROR_NONE && !empty($obj)) { - $this->message = ''; - foreach ($obj as $k => $v) { - $this->message .= sprintf("%s: %s\n", $k, $v); - } - } + if (substr($message, 0, 11) == "Bad Request") + parent::__construct(substr($message, 12), $status); + else + parent::__construct($message, $status); } } @@ -1499,6 +1528,7 @@ private function _request($resource, $method=HTTP_Request2::METHOD_GET, $data=ar # //$headers['Content-Length'] = strlen($body); $headers['Accept-Encoding'] = 'compress, gzip'; + $headers['Accept'] = 'application/json'; # # Finally we fire the actual request. # From 7165e3f0f13a9ba99d7b0cf5696b65dae0a461fa Mon Sep 17 00:00:00 2001 From: Denis Cornehl Date: Wed, 25 Jul 2012 15:05:53 +0200 Subject: [PATCH 2/8] removed local test-path --- phpcclib.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/phpcclib.php b/phpcclib.php index 3f98c14..5cb736a 100644 --- a/phpcclib.php +++ b/phpcclib.php @@ -1,7 +1,5 @@ Date: Wed, 25 Jul 2012 17:18:10 +0200 Subject: [PATCH 3/8] error-handling for new api-version --- phpcclib.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/phpcclib.php b/phpcclib.php index 5cb736a..98df6a7 100644 --- a/phpcclib.php +++ b/phpcclib.php @@ -1204,19 +1204,18 @@ public function __construct($message, $status) else if (strlen($rc) > 0) $this->message = $rc; else - $msgs = $obj; + $this->message = $message; } if (count($msgs) > 0) { $this->message = ''; - if (is_array($msgs) || $msgs instanceof Traversable || $msgs instanceof stdClass) { foreach ($msgs as $k => $v) { $this->message .= sprintf("%s: %s\n", $k, $v); } }else - $this->message = $msgs; + $this->message = $message; } } From ad997f0d935886a358fbd8505ce9dd81aef21a75 Mon Sep 17 00:00:00 2001 From: Denis Cornehl Date: Wed, 25 Jul 2012 17:59:44 +0200 Subject: [PATCH 4/8] error is always a key-value-structure --- phpcclib.php | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/phpcclib.php b/phpcclib.php index 98df6a7..8805cfe 100644 --- a/phpcclib.php +++ b/phpcclib.php @@ -1186,8 +1186,6 @@ public function __construct($message, $status) { $this->message = $message; - - $msgs = array(); $obj = json_decode($message); if (json_last_error() === JSON_ERROR_NONE && !empty($obj)) { $rc = ""; @@ -1199,25 +1197,17 @@ public function __construct($message, $status) $errors = $v; } - if (count($errors) > 0) - $msgs = $errors; - else if (strlen($rc) > 0) - $this->message = $rc; - else - $this->message = $message; - } - - if (count($msgs) > 0) { - $this->message = ''; + if (count($errors) > 0) { + $this->message = ''; - if (is_array($msgs) || $msgs instanceof Traversable || $msgs instanceof stdClass) { foreach ($msgs as $k => $v) { $this->message .= sprintf("%s: %s\n", $k, $v); } - }else + } else if (strlen($rc) > 0) + $this->message = $rc; + else $this->message = $message; - - } + } } } From f12ad1a2d4aad8f513d9074637de2af32bc2ee26 Mon Sep 17 00:00:00 2001 From: Denis Cornehl Date: Wed, 25 Jul 2012 18:16:38 +0200 Subject: [PATCH 5/8] fallback to old error format --- phpcclib.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/phpcclib.php b/phpcclib.php index 8805cfe..f4ad42b 100644 --- a/phpcclib.php +++ b/phpcclib.php @@ -1,5 +1,6 @@ 0) { $this->message = ''; - foreach ($msgs as $k => $v) { + foreach ($errors as $k => $v) { $this->message .= sprintf("%s: %s\n", $k, $v); } } else if (strlen($rc) > 0) $this->message = $rc; else - $this->message = $message; + { + //fallback, old error-format + $this->message = ''; + foreach ($obj as $k => $v) { + $this->message .= sprintf("%s: %s\n", $k, $v); + } + } } } } From d2d0e6962d983e45c543182aa1efa3532e72050b Mon Sep 17 00:00:00 2001 From: Denis Cornehl Date: Wed, 25 Jul 2012 18:19:37 +0200 Subject: [PATCH 6/8] removed test-include-path --- phpcclib.php | 1 - 1 file changed, 1 deletion(-) diff --git a/phpcclib.php b/phpcclib.php index f4ad42b..06ce8a2 100644 --- a/phpcclib.php +++ b/phpcclib.php @@ -1,6 +1,5 @@ Date: Thu, 26 Jul 2012 14:29:08 +0200 Subject: [PATCH 7/8] new content-encoding --- phpcclib.php | 44 ++++++++++++++++++-------------------------- 1 file changed, 18 insertions(+), 26 deletions(-) diff --git a/phpcclib.php b/phpcclib.php index 06ce8a2..1a332c0 100644 --- a/phpcclib.php +++ b/phpcclib.php @@ -1232,10 +1232,9 @@ class ConnectionException extends CCException { */ class TokenRequiredError extends CCException { - public function __toString() - { - return 'No valid token. Use create_token(email, password) to get a new one'; - } + public function __construct() { + parent::__construct("No valid token. Use create_token(email, password) to get a new one", 0); + } } /* @@ -1461,17 +1460,18 @@ private function _request($resource, $method=HTTP_Request2::METHOD_GET, $data=ar $request = new HTTP_Request2($url); $request->setConfig('ssl_verify_peer', API::SSL_VERIFY_PEER); - $methods = array( - 'options' => HTTP_Request2::METHOD_OPTIONS, - 'get' => HTTP_Request2::METHOD_GET, - 'head' => HTTP_Request2::METHOD_HEAD, - 'post' => HTTP_Request2::METHOD_POST, - 'put' => HTTP_Request2::METHOD_PUT, - 'delete' => HTTP_Request2::METHOD_DELETE, - 'trace' => HTTP_Request2::METHOD_TRACE, - 'connect' => HTTP_Request2::METHOD_CONNECT - ); - $request->setMethod($methods[strtolower($method)]); + //$methods = array( + //'options' => HTTP_Request2::METHOD_OPTIONS, + //'get' => HTTP_Request2::METHOD_GET, + //'head' => HTTP_Request2::METHOD_HEAD, + //'post' => HTTP_Request2::METHOD_POST, + //'put' => HTTP_Request2::METHOD_PUT, + //'delete' => HTTP_Request2::METHOD_DELETE, + //'trace' => HTTP_Request2::METHOD_TRACE, + //'connect' => HTTP_Request2::METHOD_CONNECT + //); + //$request->setMethod($methods[strtolower($method)]); + $request->setMethod($method); # # If the current API instance has a valid token we add the Authorization @@ -1497,9 +1497,8 @@ private function _request($resource, $method=HTTP_Request2::METHOD_GET, $data=ar $url = $request->getUrl(); $url->setQueryVariables($data); } else { - // works with post and put - $request->addPostParameter($data); - $request->setBody(http_build_query($data)); + $request->setBody(json_encode($data)); + $headers['Content-Type'] = 'application/json'; } } @@ -1509,14 +1508,7 @@ private function _request($resource, $method=HTTP_Request2::METHOD_GET, $data=ar # the wild. # $headers['User-Agent'] = sprintf('phpcclib/%s', $this->_version); - # - # The API expects PUT or POST data to be x-www-form-urlencoded so we - # also set the correct Content-Type header. - # - if (strtoupper($method) == 'PUT' || strtoupper($method) == 'POST') { - $headers['Content-Type'] = 'application/x-www-form-urlencoded'; - } - # + # # We also set the Content-Length and Accept-Encoding headers. # //$headers['Content-Length'] = strlen($body); From 139abfea13fa621fa72fcad707a73902536b7909 Mon Sep 17 00:00:00 2001 From: Denis Cornehl Date: Thu, 26 Jul 2012 16:26:02 +0200 Subject: [PATCH 8/8] more flexible parsing --- phpcclib.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/phpcclib.php b/phpcclib.php index 1a332c0..4a7199d 100644 --- a/phpcclib.php +++ b/phpcclib.php @@ -1197,18 +1197,18 @@ public function __construct($message, $status) $errors = $v; } - if (count($errors) > 0) { - $this->message = ''; + $this->message = ''; + + if (strlen($rc) > 0) + $this->message .= "$rc\n"; + if (count($errors) > 0) { foreach ($errors as $k => $v) { $this->message .= sprintf("%s: %s\n", $k, $v); } - } else if (strlen($rc) > 0) - $this->message = $rc; - else - { - //fallback, old error-format - $this->message = ''; + } + + if ($this->message == '') { foreach ($obj as $k => $v) { $this->message .= sprintf("%s: %s\n", $k, $v); }