From 2cd0780c5b46147ca530519cecb14f0e9bafea11 Mon Sep 17 00:00:00 2001 From: Evan Seguin Date: Thu, 9 Aug 2012 17:09:55 -0700 Subject: [PATCH] Updated library to return a MashapeResponse instead of raw response --- main/mashape/MashapeClient.php | 1 + main/mashape/http/HttpClient.php | 36 ++++------------- main/mashape/http/MashapeResponse.php | 56 +++++++++++++++++++++++++++ main/mashape/http/UrlUtils.php | 27 +++++-------- 4 files changed, 75 insertions(+), 45 deletions(-) create mode 100644 main/mashape/http/MashapeResponse.php diff --git a/main/mashape/MashapeClient.php b/main/mashape/MashapeClient.php index 44fed14..6c45615 100755 --- a/main/mashape/MashapeClient.php +++ b/main/mashape/MashapeClient.php @@ -25,5 +25,6 @@ */ require_once(dirname(__FILE__) . "/http/HttpClient.php"); +require_once(dirname(__FILE__) . "/http/MashapeResponse.php"); ?> diff --git a/main/mashape/http/HttpClient.php b/main/mashape/http/HttpClient.php index 56cdd0f..e180f4f 100755 --- a/main/mashape/http/HttpClient.php +++ b/main/mashape/http/HttpClient.php @@ -24,12 +24,10 @@ * */ -require_once(dirname(__FILE__) . "/../json/Json.php"); -require_once(dirname(__FILE__) . "/Chunked.php"); require_once(dirname(__FILE__) . "/../exceptions/MashapeClientException.php"); require_once(dirname(__FILE__) . "/HttpMethod.php"); require_once(dirname(__FILE__) . "/UrlUtils.php"); -require_once(dirname(__FILE__) . "/AuthUtil.php"); +require_once(dirname(__FILE__) . "/MashapeResponse.php"); require_once(dirname(__FILE__) . "/../auth/HeaderAuth.php"); require_once(dirname(__FILE__) . "/../auth/BasicAuth.php"); require_once(dirname(__FILE__) . "/../auth/CustomHeaderAuth.php"); @@ -47,20 +45,11 @@ class HttpClient { $response = self::execRequest($httpMethod, $url, $parameters, $authHandlers); - if (!$encodeJson) { - return $response; + if ($encodeJson) { + $response->parseBodyAsJson(); } - $jsonResponse = json_decode($response); - if (empty($jsonResponse)) { - // It may be a chunked response - $jsonResponse = json_decode(http_chunked_decode($response)); - if (empty($jsonResponse)) { - throw new MashapeClientException(sprintf(EXCEPTION_JSONDECODE_REQUEST, $response), EXCEPTION_SYSTEM_ERROR_CODE); - } - } - - return $jsonResponse; + return $response; } private static function execRequest($httpMethod, $url, $parameters, $authHandlers) { @@ -83,7 +72,6 @@ class HttpClient { UrlUtils::prepareRequest($url, $parameters, ($httpMethod != HttpMethod::GET) ? true : false); if ($httpMethod != HttpMethod::GET) { - //$url = self::removeQueryString($url); $data = http_build_query($parameters); } $ch = curl_init (); @@ -97,22 +85,14 @@ class HttpClient { } curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt ($ch, CURLOPT_HTTPHEADER, $headers); + curl_setopt ($ch, CURLINFO_HEADER_OUT, true); $response = curl_exec($ch); + $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + $responseHeaders = curl_getinfo($ch, CURLINFO_HEADER_OUT); curl_close($ch); - return $response; + return new MashapeResponse($response, $httpCode, $responseHeaders); } - - private static function removeQueryString($url) { - $pos = strpos($url, "?"); - if ($pos !== false) { - return substr($url, 0, $pos); - } - return $url; - } - } - - ?> diff --git a/main/mashape/http/MashapeResponse.php b/main/mashape/http/MashapeResponse.php new file mode 100644 index 0000000..a5e9fb5 --- /dev/null +++ b/main/mashape/http/MashapeResponse.php @@ -0,0 +1,56 @@ +. + * + * + * The author of this software is Mashape, Inc. + * For any question or feedback please contact us at: support@mashape.com + * + */ + +require_once(dirname(__FILE__) . "/../json/Json.php"); +require_once(dirname(__FILE__) . "/Chunked.php"); +require_once(dirname(__FILE__) . "/../exceptions/MashapeClientException.php"); + +class MashapeResponse { + public $statusCode; + public $body; + public $rawBody; + public $headers; + + function __construct($response, $statusCode, $headers) { + $this->rawBody = $response; + $this->headers = $headers; + $this->statusCode = $statusCode; + } + + function parseBodyAsJson() { + $this->body = json_decode($this->rawBody); + if (empty($this->body) && ($this->statusCode == 200)) { + // It may be a chunked response + $this->body = json_decode(http_chunked_decode($this->rawBody)); + if (empty($this->body)) { + throw new MashapeClientException( + sprintf(EXCEPTION_JSONDECODE_REQUEST, $this->rawBody), + EXCEPTION_SYSTEM_ERROR_CODE); + } + } + } +} +?> diff --git a/main/mashape/http/UrlUtils.php b/main/mashape/http/UrlUtils.php index 74ee5f6..938f121 100755 --- a/main/mashape/http/UrlUtils.php +++ b/main/mashape/http/UrlUtils.php @@ -32,27 +32,18 @@ class UrlUtils { $parameters = array(); } // Remove null parameters - $keys = array_keys($parameters); - for ($i = 0;$i 1) { - $matches = $matches[1]; - $count = count($matches); - foreach ($matches as $key) { + $bracketedMatches = $matches[0]; + $plainMatches = $matches[1]; + foreach ($plainMatches as $index => $key) { if (array_key_exists($key, $parameters)) { - $finalUrl = preg_replace("/(\?.+)\{" . $key . "\}/", '${1}' . urlencode($parameters[$key]), $finalUrl); - $finalUrl = preg_replace("/\{" . $key . "\}/", rawurlencode($parameters[$key]), $finalUrl); + $finalUrl = str_replace($bracketedMatches[$index], rawurlencode($parameters[$key]), $finalUrl); unset($parameters[$key]); } else { $finalUrl = preg_replace("/&?[\w]*=?\{" . $key . "\}/", "", $finalUrl); @@ -60,7 +51,7 @@ class UrlUtils { } } - $finalUrl = preg_replace("/\?&/", "?", $finalUrl); + $finalUrl = str_replace("?&", "?", $finalUrl); $finalUrl = preg_replace("/\?$/", "", $finalUrl); if ($addRegularQueryStringParameters) { @@ -81,11 +72,13 @@ class UrlUtils { if (count($urlParts) > 1) { $queryString = $urlParts[1]; $queryStringParameters = explode("&", $queryString); + foreach ($queryStringParameters as $queryStringParameter) { $queryStringParameterParts = explode("=", $queryStringParameter); if (count($queryStringParameterParts) > 1) { - if (!self::isPlaceHolder($queryStringParameterParts[1])) { - $parameters[$queryStringParameterParts[0]] = $queryStringParameterParts[1]; + list($paramKey, $paramValue) = $queryStringParameterParts; + if (!self::isPlaceHolder($paramValue)) { + $parameters[$paramKey] = $paramValue; } } }