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/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__) . "/../exceptions/MashapeClientException.php");
|
||||||
require_once(dirname(__FILE__) . "/HttpMethod.php");
|
require_once(dirname(__FILE__) . "/HttpMethod.php");
|
||||||
require_once(dirname(__FILE__) . "/UrlUtils.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/HeaderAuth.php");
|
||||||
require_once(dirname(__FILE__) . "/../auth/BasicAuth.php");
|
require_once(dirname(__FILE__) . "/../auth/BasicAuth.php");
|
||||||
require_once(dirname(__FILE__) . "/../auth/CustomHeaderAuth.php");
|
require_once(dirname(__FILE__) . "/../auth/CustomHeaderAuth.php");
|
||||||
@@ -47,22 +45,13 @@ class HttpClient {
|
|||||||
|
|
||||||
$response = self::execRequest($httpMethod, $url, $parameters, $authHandlers);
|
$response = self::execRequest($httpMethod, $url, $parameters, $authHandlers);
|
||||||
|
|
||||||
if (!$encodeJson) {
|
if ($encodeJson) {
|
||||||
|
$response->parseBodyAsJson();
|
||||||
|
}
|
||||||
|
|
||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
$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;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static function execRequest($httpMethod, $url, $parameters, $authHandlers) {
|
private static function execRequest($httpMethod, $url, $parameters, $authHandlers) {
|
||||||
$data = null;
|
$data = null;
|
||||||
if ($parameters == null) {
|
if ($parameters == null) {
|
||||||
@@ -83,7 +72,6 @@ class HttpClient {
|
|||||||
UrlUtils::prepareRequest($url, $parameters, ($httpMethod != HttpMethod::GET) ? true : false);
|
UrlUtils::prepareRequest($url, $parameters, ($httpMethod != HttpMethod::GET) ? true : false);
|
||||||
|
|
||||||
if ($httpMethod != HttpMethod::GET) {
|
if ($httpMethod != HttpMethod::GET) {
|
||||||
//$url = self::removeQueryString($url);
|
|
||||||
$data = http_build_query($parameters);
|
$data = http_build_query($parameters);
|
||||||
}
|
}
|
||||||
$ch = curl_init ();
|
$ch = curl_init ();
|
||||||
@@ -97,22 +85,14 @@ class HttpClient {
|
|||||||
}
|
}
|
||||||
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
|
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
|
||||||
curl_setopt ($ch, CURLOPT_HTTPHEADER, $headers);
|
curl_setopt ($ch, CURLOPT_HTTPHEADER, $headers);
|
||||||
|
curl_setopt ($ch, CURLINFO_HEADER_OUT, true);
|
||||||
$response = curl_exec($ch);
|
$response = curl_exec($ch);
|
||||||
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||||
|
$responseHeaders = curl_getinfo($ch, CURLINFO_HEADER_OUT);
|
||||||
curl_close($ch);
|
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();
|
$parameters = array();
|
||||||
}
|
}
|
||||||
// Remove null parameters
|
// Remove null parameters
|
||||||
$keys = array_keys($parameters);
|
$parameters = array_filter($parameters, function($value) { return !is_null($value); });
|
||||||
for ($i = 0;$i<count($keys);$i++) {
|
|
||||||
$key = $keys[$i];
|
|
||||||
if ($parameters[$key] === null) {
|
|
||||||
unset($parameters[$key]);
|
|
||||||
} else {
|
|
||||||
$parameters[$key] = (string)$parameters[$key];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$finalUrl = $url;
|
$finalUrl = $url;
|
||||||
$matches = null;
|
$matches = null;
|
||||||
$match = preg_match_all(PLACEHOLDER_REGEX, $url, $matches);
|
$match = preg_match_all(PLACEHOLDER_REGEX, $url, $matches);
|
||||||
|
|
||||||
if (!empty($matches) && count($matches) > 1) {
|
if (!empty($matches) && count($matches) > 1) {
|
||||||
$matches = $matches[1];
|
$bracketedMatches = $matches[0];
|
||||||
$count = count($matches);
|
$plainMatches = $matches[1];
|
||||||
foreach ($matches as $key) {
|
foreach ($plainMatches as $index => $key) {
|
||||||
if (array_key_exists($key, $parameters)) {
|
if (array_key_exists($key, $parameters)) {
|
||||||
$finalUrl = preg_replace("/(\?.+)\{" . $key . "\}/", '${1}' . urlencode($parameters[$key]), $finalUrl);
|
$finalUrl = str_replace($bracketedMatches[$index], rawurlencode($parameters[$key]), $finalUrl);
|
||||||
$finalUrl = preg_replace("/\{" . $key . "\}/", rawurlencode($parameters[$key]), $finalUrl);
|
|
||||||
unset($parameters[$key]);
|
unset($parameters[$key]);
|
||||||
} else {
|
} else {
|
||||||
$finalUrl = preg_replace("/&?[\w]*=?\{" . $key . "\}/", "", $finalUrl);
|
$finalUrl = preg_replace("/&?[\w]*=?\{" . $key . "\}/", "", $finalUrl);
|
||||||
@@ -60,7 +51,7 @@ class UrlUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$finalUrl = preg_replace("/\?&/", "?", $finalUrl);
|
$finalUrl = str_replace("?&", "?", $finalUrl);
|
||||||
$finalUrl = preg_replace("/\?$/", "", $finalUrl);
|
$finalUrl = preg_replace("/\?$/", "", $finalUrl);
|
||||||
|
|
||||||
if ($addRegularQueryStringParameters) {
|
if ($addRegularQueryStringParameters) {
|
||||||
@@ -81,11 +72,13 @@ class UrlUtils {
|
|||||||
if (count($urlParts) > 1) {
|
if (count($urlParts) > 1) {
|
||||||
$queryString = $urlParts[1];
|
$queryString = $urlParts[1];
|
||||||
$queryStringParameters = explode("&", $queryString);
|
$queryStringParameters = explode("&", $queryString);
|
||||||
|
|
||||||
foreach ($queryStringParameters as $queryStringParameter) {
|
foreach ($queryStringParameters as $queryStringParameter) {
|
||||||
$queryStringParameterParts = explode("=", $queryStringParameter);
|
$queryStringParameterParts = explode("=", $queryStringParameter);
|
||||||
if (count($queryStringParameterParts) > 1) {
|
if (count($queryStringParameterParts) > 1) {
|
||||||
if (!self::isPlaceHolder($queryStringParameterParts[1])) {
|
list($paramKey, $paramValue) = $queryStringParameterParts;
|
||||||
$parameters[$queryStringParameterParts[0]] = $queryStringParameterParts[1];
|
if (!self::isPlaceHolder($paramValue)) {
|
||||||
|
$parameters[$paramKey] = $paramValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user