Refactored library. Added new validation and excpetions. Added JSON input support
This commit is contained in:
@@ -25,13 +25,30 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
define("EXCEPTION_NOTSUPPORTED_HTTPMETHOD_CODE", 1003);
|
define("EXCEPTION_NOTSUPPORTED_HTTPMETHOD_CODE", 1003);
|
||||||
define("EXCEPTION_NOTSUPPORTED_HTTPMETHOD", "HTTP method not supported. Only DELETE, GET, POST, PUT are supported");
|
define("EXCEPTION_NOTSUPPORTED_HTTPMETHOD",
|
||||||
|
"HTTP method not supported. Only DELETE, GET, POST, PUT are supported");
|
||||||
|
|
||||||
|
define("EXCEPTION_CONTENT_TYPE_JSON_ARRAY_CODE", 1004);
|
||||||
|
define("EXCEPTION_CONTENT_TYPE_JSON_ARRAY",
|
||||||
|
"Content Type JSON does not accept array parameters. Parameters should be"
|
||||||
|
." a JSON string");
|
||||||
|
|
||||||
|
define("EXCEPTION_CONTENT_TYPE_NON_ARRAY_CODE", 1005);
|
||||||
|
define("EXCEPTION_CONTENT_TYPE_NON_ARRAY",
|
||||||
|
"Parameters must be an array unless content type is set to JSON");
|
||||||
|
|
||||||
|
define("EXCEPTION_CONTENT_TYPE_JSON_QUERYAUTH_CODE", 1006);
|
||||||
|
define("EXCEPTION_CONTENT_TYPE_JSON_QUERYAUTH",
|
||||||
|
"Query Authentication cannot be used in conjunction with content type JSON");
|
||||||
|
|
||||||
define("EXCEPTION_NOTSUPPORTED_CONTENTTYPE_CODE", 415);
|
define("EXCEPTION_NOTSUPPORTED_CONTENTTYPE_CODE", 415);
|
||||||
define("EXCEPTION_NOTSUPPORTED_CONTENTTYPE", "Content Type not supported. Currently only application/x-www-form-urlencoded and multipart/form-data are supported");
|
define("EXCEPTION_NOTSUPPORTED_CONTENTTYPE",
|
||||||
|
"Content Type not supported. Currently only application/x-www-form-urlencoded, "
|
||||||
|
."application/json, and multipart/form-data are supported");
|
||||||
|
|
||||||
define("EXCEPTION_GET_INVALID_CONTENTTYPE_CODE", 415);
|
define("EXCEPTION_GET_INVALID_CONTENTTYPE_CODE", 415);
|
||||||
define("EXCEPTION_GET_INVALID_CONTENTTYPE", "A GET request must have a content type of application/x-www-form-urlencoded");
|
define("EXCEPTION_GET_INVALID_CONTENTTYPE", "A GET request must have a content"
|
||||||
|
." type of application/x-www-form-urlencoded or application/json");
|
||||||
|
|
||||||
define("EXCEPTION_SYSTEM_ERROR_CODE", 2000);
|
define("EXCEPTION_SYSTEM_ERROR_CODE", 2000);
|
||||||
define("EXCEPTION_JSONDECODE_REQUEST", "Can't deserialize the response JSON: %s");
|
define("EXCEPTION_JSONDECODE_REQUEST", "Can't deserialize the response JSON: %s");
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ require_once(dirname(__FILE__) . "/../exceptions/MashapeClientException.php");
|
|||||||
require_once(dirname(__FILE__) . "/HttpMethod.php");
|
require_once(dirname(__FILE__) . "/HttpMethod.php");
|
||||||
require_once(dirname(__FILE__) . "/ContentType.php");
|
require_once(dirname(__FILE__) . "/ContentType.php");
|
||||||
require_once(dirname(__FILE__) . "/UrlUtils.php");
|
require_once(dirname(__FILE__) . "/UrlUtils.php");
|
||||||
|
require_once(dirname(__FILE__) . "/HttpUtils.php");
|
||||||
require_once(dirname(__FILE__) . "/MashapeResponse.php");
|
require_once(dirname(__FILE__) . "/MashapeResponse.php");
|
||||||
require_once(dirname(__FILE__) . "/../authentication/HeaderAuthentication.php");
|
require_once(dirname(__FILE__) . "/../authentication/HeaderAuthentication.php");
|
||||||
require_once(dirname(__FILE__) . "/../authentication/BasicAuthentication.php");
|
require_once(dirname(__FILE__) . "/../authentication/BasicAuthentication.php");
|
||||||
@@ -37,13 +38,15 @@ require_once(dirname(__FILE__) . "/../authentication/QueryAuthentication.php");
|
|||||||
|
|
||||||
class HttpClient {
|
class HttpClient {
|
||||||
|
|
||||||
public static function doRequest($httpMethod, $url, $parameters, $authHandlers, $contentType = ContentType::FORM, $encodeJson = true) {
|
public static function doRequest($httpMethod, $url, $parameters,
|
||||||
|
$authHandlers, $contentType = ContentType::FORM, $encodeJson = true) {
|
||||||
|
HttpUtils::cleanParameters($parameters);
|
||||||
|
|
||||||
if (!($httpMethod == HttpMethod::DELETE || $httpMethod == HttpMethod::GET ||
|
if ($authHandlers == null) {
|
||||||
$httpMethod == HttpMethod::POST || $httpMethod == HttpMethod::PUT)) {
|
$authHandlers = array();
|
||||||
throw new MashapeClientException(EXCEPTION_NOTSUPPORTED_HTTPMETHOD, EXCEPTION_NOTSUPPORTED_HTTPMETHOD_CODE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self::validateRequest($httpMethod, $url, $parameters, $authHandlers, $contentType);
|
||||||
$response = self::execRequest($httpMethod, $url, $parameters, $authHandlers, $contentType);
|
$response = self::execRequest($httpMethod, $url, $parameters, $authHandlers, $contentType);
|
||||||
|
|
||||||
if ($encodeJson) {
|
if ($encodeJson) {
|
||||||
@@ -53,59 +56,65 @@ class HttpClient {
|
|||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static function execRequest($httpMethod, $url, $parameters, $authHandlers, $contentType) {
|
private static function validateRequest($httpMethod, $url, $parameters, $authHandlers, $contentType) {
|
||||||
$data = null;
|
if ( !($httpMethod == HttpMethod::DELETE
|
||||||
if ($parameters == null) {
|
|| $httpMethod == HttpMethod::GET
|
||||||
$parameters = array();
|
|| $httpMethod == HttpMethod::POST
|
||||||
|
|| $httpMethod == HttpMethod::PUT
|
||||||
|
|| $httpMethod == HttpMethod::PATCH)) {
|
||||||
|
// we only support these HTTP methods.
|
||||||
|
throw new MashapeClientException(EXCEPTION_NOTSUPPORTED_HTTPMETHOD,
|
||||||
|
EXCEPTION_NOTSUPPORTED_HTTPMETHOD_CODE);
|
||||||
}
|
}
|
||||||
if ($authHandlers == null) {
|
if ($contentType == ContentType::JSON && is_array($parameters)) {
|
||||||
$authHandlers = array();
|
// Content type JSON does not allow array parameters.
|
||||||
}
|
|
||||||
|
|
||||||
$headers = array();
|
|
||||||
$headers[] = UrlUtils::generateClientHeaders();
|
|
||||||
// Authentication
|
|
||||||
foreach($authHandlers as $handler) {
|
|
||||||
if ($handler instanceof QueryAuthentication) {
|
|
||||||
$parameters = array_merge($parameters, $handler->handleParams());
|
|
||||||
} else if ($handler instanceof HeaderAuthentication) {
|
|
||||||
$headers[] = $handler->handleHeader();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
UrlUtils::prepareRequest($url, $parameters, ($httpMethod != HttpMethod::GET) ? true : false);
|
|
||||||
|
|
||||||
if ($httpMethod != HttpMethod::GET) {
|
|
||||||
switch ($contentType) {
|
|
||||||
case ContentType::FORM:
|
|
||||||
$data = http_build_query($parameters);
|
|
||||||
break;
|
|
||||||
case ContentType::MULTIPART:
|
|
||||||
$data = $parameters;
|
|
||||||
break;
|
|
||||||
case ContentType::JSON:
|
|
||||||
// TODO support json
|
|
||||||
default:
|
|
||||||
throw new MashapeClientException(
|
throw new MashapeClientException(
|
||||||
EXCEPTION_NOTSUPPORTED_CONTENTTYPE,
|
EXCEPTION_CONTENT_TYPE_JSON_ARRAY,
|
||||||
EXCEPTION_NOTSUPPORTED_CONTENTTYPE_CODE);
|
EXCEPTION_CONTENT_TYPE_JSON_ARRAY_CODE);
|
||||||
}
|
}
|
||||||
} else if ($contentType != ContentType::FORM) {
|
if (!is_array($parameters) && $contentType != ContentType::JSON) {
|
||||||
|
// Raw parameters are only allows for ContentType::JSON
|
||||||
|
throw new MashapeClientException(
|
||||||
|
EXCEPTION_CONTENT_TYPE_NON_ARRAY,
|
||||||
|
EXCEPTION_CONTENT_TYPE_NON_ARRAY_CODE);
|
||||||
|
}
|
||||||
|
if ($httpMethod == HttpMethod::GET && $contentType != ContentType::FORM) {
|
||||||
// if we have a GET request that is anything other than urlencoded
|
// if we have a GET request that is anything other than urlencoded
|
||||||
// form data, we shouldn't allow it.
|
// form data, we shouldn't allow it.
|
||||||
throw new MashapeClientException(
|
throw new MashapeClientException(
|
||||||
EXCEPTION_GET_INVALID_CONTENTTYPE,
|
EXCEPTION_GET_INVALID_CONTENTTYPE,
|
||||||
EXCEPTION_GET_INVALID_CONTENTTYPE_CODE);
|
EXCEPTION_GET_INVALID_CONTENTTYPE_CODE);
|
||||||
}
|
}
|
||||||
$ch = curl_init ();
|
if ($contentType == ContentType::JSON) {
|
||||||
|
foreach ($authHandlers as $handler) {
|
||||||
|
if ($handler instanceof QueryAuthentication) {
|
||||||
|
// bad. No room for query auth parameters if the whole body is json
|
||||||
|
throw new MashapeClientException(
|
||||||
|
EXCEPTION_CONTENT_TYPE_JSON_QUERYAUTH,
|
||||||
|
EXCEPTION_CONTENT_TYPE_JSON_QUERYAUTH_CODE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function execRequest($httpMethod, $url, $parameters, $authHandlers, $contentType) {
|
||||||
|
// first, collect the headers and parameters we'll need from the authentication handlers
|
||||||
|
list($headers, $authParameters) = HttpUtils::handleAuthentication($authHandlers);
|
||||||
|
if (is_array($parameters)) {
|
||||||
|
$parameters = array_merge($parameters, $authParameters);
|
||||||
|
}
|
||||||
|
|
||||||
// prepare the request
|
// prepare the request
|
||||||
curl_setopt ($ch, CURLOPT_URL , $url);
|
$ch = curl_init ();
|
||||||
if ($httpMethod != HttpMethod::GET) {
|
|
||||||
|
if ($httpMethod == HttpMethod::GET) {
|
||||||
|
$url = UrlUtils::buildUrlWithQueryString($url, $parameters);
|
||||||
|
} else {
|
||||||
|
$data = HttpUtils::buildDataForContentType($contentType, $parameters, $headers);
|
||||||
curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, $httpMethod);
|
curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, $httpMethod);
|
||||||
//curl_setopt ($ch, CURLOPT_POST, 1);
|
curl_setopt ($ch, CURLOPT_POSTFIELDS, $data);
|
||||||
curl_setopt ($ch, CURLOPT_POSTFIELDS, $parameters);
|
|
||||||
}
|
}
|
||||||
|
curl_setopt ($ch, CURLOPT_URL , $url);
|
||||||
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);
|
curl_setopt ($ch, CURLINFO_HEADER_OUT, true);
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ class HttpMethod
|
|||||||
const GET = "GET";
|
const GET = "GET";
|
||||||
const POST = "POST";
|
const POST = "POST";
|
||||||
const PUT = "PUT";
|
const PUT = "PUT";
|
||||||
|
const PATCH = "PATCH";
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
87
main/mashape/http/HttpUtils.php
Normal file
87
main/mashape/http/HttpUtils.php
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
<?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
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
class HttpUtils {
|
||||||
|
|
||||||
|
public static function cleanParameters(&$parameters) {
|
||||||
|
if ($parameters == null) {
|
||||||
|
$parameters = array();
|
||||||
|
} else if (is_array($parameters)) {
|
||||||
|
// 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];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function buildDataForContentType($contentType, $parameters, &$headers) {
|
||||||
|
$data = null;
|
||||||
|
switch ($contentType) {
|
||||||
|
case ContentType::FORM:
|
||||||
|
$data = http_build_query($parameters);
|
||||||
|
break;
|
||||||
|
case ContentType::MULTIPART:
|
||||||
|
$data = $parameters;
|
||||||
|
break;
|
||||||
|
case ContentType::JSON:
|
||||||
|
$headers[] = "Content-Type: application/json";
|
||||||
|
$data = $parameters;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new MashapeClientException(
|
||||||
|
EXCEPTION_NOTSUPPORTED_CONTENTTYPE,
|
||||||
|
EXCEPTION_NOTSUPPORTED_CONTENTTYPE_CODE);
|
||||||
|
}
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function handleAuthentication($authHandlers) {
|
||||||
|
$headers = array();
|
||||||
|
$parameters = array();
|
||||||
|
$headers[] = self::generateClientHeaders();
|
||||||
|
// Authentication
|
||||||
|
foreach($authHandlers as $handler) {
|
||||||
|
if ($handler instanceof QueryAuthentication) {
|
||||||
|
$parameters = array_merge($parameters, $handler->handleParams());
|
||||||
|
} else if ($handler instanceof HeaderAuthentication) {
|
||||||
|
$headers[] = $handler->handleHeader();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return array($headers, $parameters);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function generateClientHeaders() {
|
||||||
|
$headers = "User-Agent: mashape-php/1.0: ";
|
||||||
|
return $headers;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -24,82 +24,14 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
define("PLACEHOLDER_REGEX", "/\{([\w\.]+)\}/");
|
|
||||||
class UrlUtils {
|
class UrlUtils {
|
||||||
|
|
||||||
public static function prepareRequest(&$url, &$parameters, $addRegularQueryStringParameters = false) {
|
public static function buildUrlWithQueryString($url, $parameters) {
|
||||||
if ($parameters == null) {
|
|
||||||
$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];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$finalUrl = $url;
|
|
||||||
$matches = null;
|
|
||||||
$match = preg_match_all(PLACEHOLDER_REGEX, $url, $matches);
|
|
||||||
|
|
||||||
if (!empty($matches) && count($matches) > 1) {
|
|
||||||
$bracketedMatches = $matches[0];
|
|
||||||
$plainMatches = $matches[1];
|
|
||||||
foreach ($plainMatches as $index => $key) {
|
|
||||||
if (array_key_exists($key, $parameters)) {
|
|
||||||
$finalUrl = str_replace($bracketedMatches[$index], rawurlencode($parameters[$key]), $finalUrl);
|
|
||||||
unset($parameters[$key]);
|
|
||||||
} else {
|
|
||||||
$finalUrl = preg_replace("/&?[\w]*=?\{" . $key . "\}/", "", $finalUrl);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$finalUrl = str_replace("?&", "?", $finalUrl);
|
|
||||||
$finalUrl = preg_replace("/\?$/", "", $finalUrl);
|
|
||||||
|
|
||||||
if ($addRegularQueryStringParameters) {
|
|
||||||
// Get regular query string parameters
|
|
||||||
self::addRegularQueryStringParameters($finalUrl, $parameters);
|
|
||||||
} else {
|
|
||||||
foreach ($parameters as $paramKey => $paramValue) {
|
foreach ($parameters as $paramKey => $paramValue) {
|
||||||
$delimiter = (strpos($finalUrl, "?") === false) ? "?" : "&";
|
$delimiter = (strpos($url, "?") === false) ? "?" : "&";
|
||||||
$finalUrl .= $delimiter . $paramKey . "=" . urlencode($paramValue);
|
$url .= $delimiter . $paramKey . "=" . urlencode($paramValue);
|
||||||
}
|
}
|
||||||
}
|
return $url;
|
||||||
|
|
||||||
$url = $finalUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static function addRegularQueryStringParameters($url, &$parameters) {
|
|
||||||
$urlParts = explode("?", $url);
|
|
||||||
if (count($urlParts) > 1) {
|
|
||||||
$queryString = $urlParts[1];
|
|
||||||
$queryStringParameters = explode("&", $queryString);
|
|
||||||
|
|
||||||
foreach ($queryStringParameters as $queryStringParameter) {
|
|
||||||
$queryStringParameterParts = explode("=", $queryStringParameter);
|
|
||||||
if (count($queryStringParameterParts) > 1) {
|
|
||||||
list($paramKey, $paramValue) = $queryStringParameterParts;
|
|
||||||
if (!self::isPlaceHolder($paramValue)) {
|
|
||||||
$parameters[$paramKey] = $paramValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static function isPlaceHolder($value) {
|
|
||||||
return preg_match(PLACEHOLDER_REGEX, $value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function generateClientHeaders() {
|
|
||||||
$headers = "User-Agent: mashape-php/1.0: ";
|
|
||||||
return $headers;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user