Updated library to return a MashapeResponse instead of raw response
This commit is contained in:
@@ -25,5 +25,6 @@
|
||||
*/
|
||||
|
||||
require_once(dirname(__FILE__) . "/http/HttpClient.php");
|
||||
require_once(dirname(__FILE__) . "/http/MashapeResponse.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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
?>
|
||||
|
||||
56
main/mashape/http/MashapeResponse.php
Normal file
56
main/mashape/http/MashapeResponse.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Mashape PHP Client library.
|
||||
*
|
||||
* Copyright (C) 2011 Mashape, Inc.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
||||
@@ -32,27 +32,18 @@ class UrlUtils {
|
||||
$parameters = array();
|
||||
}
|
||||
// Remove null parameters
|
||||
$keys = array_keys($parameters);
|
||||
for ($i = 0;$i<count($keys);$i++) {
|
||||
$key = $keys[$i];
|
||||
if ($parameters[$key] === null) {
|
||||
unset($parameters[$key]);
|
||||
} else {
|
||||
$parameters[$key] = (string)$parameters[$key];
|
||||
}
|
||||
}
|
||||
$parameters = array_filter($parameters, function($value) { return !is_null($value); });
|
||||
|
||||
$finalUrl = $url;
|
||||
$matches = null;
|
||||
$match = preg_match_all(PLACEHOLDER_REGEX, $url, $matches);
|
||||
|
||||
if (!empty($matches) && count($matches) > 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user