version 0.3
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
Mashape PHP client library v0.2
|
||||
Mashape PHP client library v0.3
|
||||
Copyright (C) 2011 Mashape, Inc.
|
||||
|
||||
For the documentation, please visit http://www.mashape.com/guide/consume/php
|
||||
@@ -24,8 +24,7 @@
|
||||
*
|
||||
*/
|
||||
|
||||
require_once(dirname(__FILE__) . "/init/init.php");
|
||||
require_once(dirname(__FILE__) . "/http/httpClient.php");
|
||||
require_once(dirname(__FILE__) . "/http/tokenUtil.php");
|
||||
require_once(dirname(__FILE__) . "/http/HttpClient.php");
|
||||
require_once(dirname(__FILE__) . "/http/TokenUtil.php");
|
||||
|
||||
?>
|
||||
@@ -28,8 +28,6 @@ define("EXCEPTION_NOTSUPPORTED_HTTPMETHOD_CODE", 1003);
|
||||
define("EXCEPTION_NOTSUPPORTED_HTTPMETHOD", "HTTP method not supported. Only DELETE, GET, POST, PUT are supported");
|
||||
|
||||
define("EXCEPTION_SYSTEM_ERROR_CODE", 2000);
|
||||
|
||||
define("EXCEPTION_EMPTY_REQUEST", "A request attempt was made to the component, but the response was empty. The component's URL may be wrong or the firewall may be blocking your outbound HTTP requests.");
|
||||
define("EXCEPTION_JSONDECODE_REQUEST", "Can't deserialize the response JSON from the component. The json_decode function is missing on your server or the method returned an invalid JSON value: %s");
|
||||
|
||||
?>
|
||||
@@ -24,7 +24,7 @@
|
||||
*
|
||||
*/
|
||||
|
||||
require_once (dirname(__FILE__) . "/clientExceptionMessages.php");
|
||||
require_once (dirname(__FILE__) . "/ExceptionConstants.php");
|
||||
|
||||
class MashapeClientException extends Exception {
|
||||
|
||||
62
main/mashape/http/HttpClient.php
Normal file
62
main/mashape/http/HttpClient.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
require_once(dirname(__FILE__) . "/../json/Json.php");
|
||||
require_once(dirname(__FILE__) . "/../exceptions/MashapeClientException.php");
|
||||
require_once(dirname(__FILE__) . "/HttpMethod.php");
|
||||
require_once(dirname(__FILE__) . "/UrlUtils.php");
|
||||
|
||||
class HttpClient {
|
||||
|
||||
public static function doRequest($httpMethod, $url, $parameters, $token) {
|
||||
if (!($httpMethod == HttpMethod::DELETE || $httpMethod == HttpMethod::GET ||
|
||||
$httpMethod == HttpMethod::POST || $httpMethod == HttpMethod::PUT)) {
|
||||
throw new MashapeClientException(EXCEPTION_NOTSUPPORTED_HTTPMETHOD, EXCEPTION_NOTSUPPORTED_HTTPMETHOD_CODE);
|
||||
}
|
||||
|
||||
UrlUtils::addClientParameters($url, $parameters, $token);
|
||||
UrlUtils::prepareRequest($url, $parameters, ($httpMethod != HttpMethod::GET) ? true : false);
|
||||
|
||||
$response = self::execRequest($httpMethod, $url, $parameters);
|
||||
|
||||
$jsonResponse = json_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) {
|
||||
$data = null;
|
||||
if ($httpMethod != HttpMethod::GET) {
|
||||
$url = self::removeQueryString($url);
|
||||
$data = http_build_query($parameters);
|
||||
}
|
||||
|
||||
$opts = array('http' =>
|
||||
array(
|
||||
'ignore_errors' => true,
|
||||
'method' => $httpMethod,
|
||||
'content' => $data
|
||||
)
|
||||
);
|
||||
|
||||
$context = stream_context_create($opts);
|
||||
$response = @file_get_contents($url, false, $context);
|
||||
return $response;
|
||||
}
|
||||
|
||||
private static function removeQueryString($url) {
|
||||
$pos = strpos($url, "?");
|
||||
if ($pos !== false) {
|
||||
return substr($url, 0, $pos);
|
||||
}
|
||||
return $url;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
?>
|
||||
11
main/mashape/http/HttpMethod.php
Normal file
11
main/mashape/http/HttpMethod.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
class HttpMethod
|
||||
{
|
||||
const DELETE = "DELETE";
|
||||
const GET = "GET";
|
||||
const POST = "POST";
|
||||
const PUT = "PUT";
|
||||
}
|
||||
|
||||
?>
|
||||
26
main/mashape/http/TokenUtil.php
Normal file
26
main/mashape/http/TokenUtil.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
require_once(dirname(__FILE__) . "/../exceptions/MashapeClientException.php");
|
||||
require_once(dirname(__FILE__) . "/HttpMethod.php");
|
||||
require_once(dirname(__FILE__) . "/HttpClient.php");
|
||||
|
||||
define("TOKEN_URL", "https://api.mashape.com/requestToken?devkey={devkey}");
|
||||
|
||||
class TokenUtil {
|
||||
|
||||
public static function requestToken($developerKey) {
|
||||
$parameters = array("devkey"=>$developerKey);
|
||||
|
||||
$response = HttpClient::doRequest(HttpMethod::POST, TOKEN_URL, $parameters, null);
|
||||
|
||||
if (empty($response->errors)) {
|
||||
$token = $response->token;
|
||||
return $token;
|
||||
} else {
|
||||
throw new MashapeClientException($response->errors[0]->message, $response->errors[0]->code);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
101
main/mashape/http/UrlUtils.php
Normal file
101
main/mashape/http/UrlUtils.php
Normal file
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
define("CLIENT_LIBRARY_LANGUAGE", "PHP");
|
||||
define("CLIENT_LIBRARY_VERSION", "V03");
|
||||
|
||||
define("TOKEN", "_token");
|
||||
define("LANGUAGE", "_language");
|
||||
define("VERSION", "_version");
|
||||
|
||||
define("PLACEHOLDER_REGEX", "/\{([\w\.]+)\}/");
|
||||
class UrlUtils {
|
||||
|
||||
public static function prepareRequest(&$url, &$parameters, $addRegularQueryStringParameters = false) {
|
||||
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];
|
||||
}
|
||||
}
|
||||
|
||||
if ($addRegularQueryStringParameters) {
|
||||
// Get regular query string parameters
|
||||
self::addRegularQueryStringParameters($url, $parameters);
|
||||
}
|
||||
|
||||
|
||||
$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) {
|
||||
if (array_key_exists($key, $parameters)) {
|
||||
$finalUrl = preg_replace("/(\?.+)\{" . $key . "\}/", '${1}' . urlencode($parameters[$key]), $finalUrl);
|
||||
$finalUrl = preg_replace("/\{" . $key . "\}/", rawurlencode($parameters[$key]), $finalUrl);
|
||||
} else {
|
||||
$finalUrl = preg_replace("/&?[\w]*=?\{" . $key . "\}/", "", $finalUrl);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$finalUrl = preg_replace("/\?&/", "?", $finalUrl);
|
||||
$finalUrl = preg_replace("/\?$/", "", $finalUrl);
|
||||
$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) {
|
||||
if (!self::isPlaceHolder($queryStringParameterParts[1])) {
|
||||
$parameters[$queryStringParameterParts[0]] = $queryStringParameterParts[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static function isPlaceHolder($value) {
|
||||
return preg_match(PLACEHOLDER_REGEX, $value);
|
||||
}
|
||||
|
||||
public static function addClientParameters(&$url, &$parameters, $token) {
|
||||
if ($parameters == null) {
|
||||
$parameters = array();
|
||||
}
|
||||
|
||||
if (strpos($url, "?") === false) {
|
||||
$url .= "?";
|
||||
} else {
|
||||
$url .= "&";
|
||||
}
|
||||
|
||||
$url .= self::addClientParameter(TOKEN);
|
||||
$parameters[TOKEN] = $token;
|
||||
$url .= "&" . self::addClientParameter(LANGUAGE);
|
||||
$parameters[LANGUAGE] = CLIENT_LIBRARY_LANGUAGE;
|
||||
$url .= "&" . self::addClientParameter(VERSION);
|
||||
$parameters[VERSION] = CLIENT_LIBRARY_VERSION;
|
||||
}
|
||||
|
||||
private static function addClientParameter($parameter) {
|
||||
return $parameter . "={" . $parameter . "}";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -26,7 +26,7 @@
|
||||
|
||||
if (!function_exists('json_decode')) {
|
||||
function json_decode($content, $assoc=false) {
|
||||
require_once(dirname(__FILE__) . "/../json/jsonImpl.php");
|
||||
require_once(dirname(__FILE__) . "/Services_JSON.php");
|
||||
if ($assoc) {
|
||||
$json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
|
||||
}
|
||||
@@ -1,149 +0,0 @@
|
||||
<?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__) . "/../init/init.php");
|
||||
require_once(dirname(__FILE__) . "/../exceptions/mashapeClientException.php");
|
||||
require_once(dirname(__FILE__) . "/urlUtils.php");
|
||||
|
||||
class HttpMethod
|
||||
{
|
||||
const DELETE = 0;
|
||||
const GET = 1;
|
||||
const POST = 2;
|
||||
const PUT = 3;
|
||||
}
|
||||
|
||||
class HttpClient {
|
||||
|
||||
public static function call($url, $httpMethod, $token, $parameters) {
|
||||
if (empty($parameters)) {
|
||||
$parameters = array();
|
||||
} else {
|
||||
// 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 {
|
||||
// Convert every value to a string value
|
||||
$parameters[$key] = (string)$parameters[$key];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$parameters[TOKEN] = $token;
|
||||
$parameters[LANGUAGE] = CLIENT_LIBRARY_LANGUAGE;
|
||||
$parameters[VERSION] = CLIENT_LIBRARY_VERSION;
|
||||
|
||||
$url = UrlUtils::addClientParameters($url);
|
||||
|
||||
$response;
|
||||
switch($httpMethod) {
|
||||
case HttpMethod::DELETE:
|
||||
$response = self::doDelete($url, $parameters);
|
||||
break;
|
||||
case HttpMethod::GET:
|
||||
$response = self::doGet($url, $parameters);
|
||||
break;
|
||||
case HttpMethod::POST:
|
||||
$response = self::doPost($url, $parameters);
|
||||
break;
|
||||
case HttpMethod::PUT:
|
||||
$response = self::doPut($url, $parameters);
|
||||
break;
|
||||
default:
|
||||
throw new MashapeClientException(EXCEPTION_NOTSUPPORTED_HTTPMETHOD, EXCEPTION_NOTSUPPORTED_HTTPMETHOD_CODE);
|
||||
}
|
||||
if (empty($response)) {
|
||||
throw new MashapeClientException(EXCEPTION_EMPTY_REQUEST, EXCEPTION_SYSTEM_ERROR_CODE);
|
||||
}
|
||||
$responseObject = json_decode($response);
|
||||
if (empty($responseObject)) {
|
||||
throw new MashapeClientException(sprintf(EXCEPTION_JSONDECODE_REQUEST, $response), EXCEPTION_SYSTEM_ERROR_CODE);
|
||||
}
|
||||
return $responseObject;
|
||||
}
|
||||
|
||||
private static function replaceParameters($url, $parameters) {
|
||||
$finalUrl = UrlUtils::getCleanUrl($url, $parameters);
|
||||
if (!empty($parameters)) {
|
||||
$keys = array_keys($parameters);
|
||||
for ($i = 0;$i<count($keys);$i++) {
|
||||
$key = $keys[$i];
|
||||
$finalUrl = str_replace("{" . $key . "}", urlencode($parameters[$key]), $finalUrl);
|
||||
}
|
||||
}
|
||||
return $finalUrl;
|
||||
}
|
||||
|
||||
private static function doGet($url, $parameters) {
|
||||
$finalUrl = self::replaceParameters($url, $parameters);
|
||||
$response = self::makeRequest($finalUrl, "GET", null);
|
||||
return $response;
|
||||
}
|
||||
|
||||
public static function doPost($url, $parameters) {
|
||||
$response = self::makeRequest($url, "POST", $parameters);
|
||||
return $response;
|
||||
}
|
||||
|
||||
private static function doPut($url, $parameters) {
|
||||
$response = self::makeRequest($url, "PUT", $parameters);
|
||||
return $response;
|
||||
}
|
||||
|
||||
private static function doDelete($url, $parameters) {
|
||||
$response = self::makeRequest($url, "DELETE", $parameters);
|
||||
return $response;
|
||||
}
|
||||
|
||||
private static function makeRequest($url, $httpMethod, $parameters) {
|
||||
$data = null;
|
||||
$finalUrl = $url;
|
||||
if (!(empty($parameters))) {
|
||||
// It's a POST/PUT/DELETE request
|
||||
$data = http_build_query(array_merge($parameters, UrlUtils::getQueryStringParameters($url)));
|
||||
$finalUrl = self::replaceParameters($url, $parameters);
|
||||
$finalUrl = UrlUtils::removeQueryString($finalUrl);
|
||||
}
|
||||
|
||||
$opts = array('http' =>
|
||||
array(
|
||||
'ignore_errors' => true,
|
||||
'method' => $httpMethod,
|
||||
'content' => $data
|
||||
)
|
||||
);
|
||||
|
||||
$context = stream_context_create($opts);
|
||||
$response = @file_get_contents($finalUrl, false, $context);
|
||||
return $response;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -1,50 +0,0 @@
|
||||
<?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__) . "/httpClient.php");
|
||||
require_once(dirname(__FILE__) . "/../exceptions/mashapeClientException.php");
|
||||
|
||||
define("TOKEN_URL", "https://api.mashape.com/requestToken");
|
||||
|
||||
class TokenUtil {
|
||||
|
||||
public static function requestToken($devKey) {
|
||||
$parameters = array("devkey"=>$devKey);
|
||||
|
||||
$response = HttpClient::doPost(TOKEN_URL, $parameters);
|
||||
|
||||
$jsonResponse = json_decode($response);
|
||||
if (empty($jsonResponse->errors)) {
|
||||
$token = $jsonResponse->token;
|
||||
return $token;
|
||||
} else {
|
||||
throw new MashapeClientException($jsonResponse->errors[0]->message, $jsonResponse->errors[0]->code);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -1,134 +0,0 @@
|
||||
<?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__) . "/../init/init.php");
|
||||
|
||||
class UrlUtils {
|
||||
|
||||
private static function addRouteParameter($url, $parameterName) {
|
||||
$result = $url;
|
||||
$pos = strpos($url, "?");
|
||||
if ($pos === false) {
|
||||
$result .= "?";
|
||||
}
|
||||
if (substr($result, strlen($result) - 1, 1) != "?") {
|
||||
$result .= "&";
|
||||
}
|
||||
$result .= $parameterName . "={" . $parameterName . "}";
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function addClientParameters($url) {
|
||||
$result = self::addRouteParameter($url, TOKEN);
|
||||
$result = self::addRouteParameter($result, LANGUAGE);
|
||||
$result = self::addRouteParameter($result, VERSION);
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function getCleanUrl($url, $parameters) {
|
||||
if ($parameters == null) {
|
||||
$parameters = array();
|
||||
}
|
||||
|
||||
$finalUrl = "";
|
||||
|
||||
for($i=0;$i < strlen($url);$i++) {
|
||||
$curchar = substr($url, $i, 1);
|
||||
|
||||
if ($curchar == "{") {
|
||||
// It may be a placeholder
|
||||
|
||||
$pos = strpos($url, "}", $i);
|
||||
if ($pos !== false) {
|
||||
// It's a placeholder
|
||||
|
||||
$placeHolder = substr($url, $i + 1, $pos - 1 - $i); // Get the placeholder name without {..}
|
||||
if (array_key_exists($placeHolder, $parameters) === false) {
|
||||
// If it doesn't exist in the array, remove it
|
||||
|
||||
if (substr($url, $i - 1, 1) == "=") {
|
||||
// It's a query string placeholder, remove also its name
|
||||
|
||||
for ($t = strlen($finalUrl) - 1;$t>=0;$t--) {
|
||||
$backChar = substr($finalUrl, $t, 1);
|
||||
if ($backChar == "?" || $backChar == "&") {
|
||||
$finalUrl = substr($finalUrl, 0, ($backChar == "?") ? $t + 1 : $t);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$i = $pos;
|
||||
continue;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
$finalUrl .= $curchar;
|
||||
|
||||
}
|
||||
|
||||
return str_replace("?&", "?", $finalUrl);
|
||||
|
||||
}
|
||||
|
||||
public static function removeQueryString($url) {
|
||||
$urlParts = explode("?", $url);
|
||||
return $urlParts[0];
|
||||
}
|
||||
|
||||
public static function getQueryStringParameters($url) {
|
||||
$result = array();
|
||||
$urlParts = explode("?", $url);
|
||||
if (count($urlParts) > 1) {
|
||||
$queryString = $urlParts[1];
|
||||
$parameters = explode("&", $queryString);
|
||||
foreach($parameters as $parameter) {
|
||||
$p = explode("=", $parameter);
|
||||
if (count($p) > 1) {
|
||||
if (self::isPlaceHolder($p[1]) == false) {
|
||||
$result[$p[0]] = $p[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
private static function isPlaceholder($val) {
|
||||
if (!empty($val)) {
|
||||
if (strlen($val) >= 2) {
|
||||
if (substr($val, 0, 1) == "{" && substr($val, strlen($val) - 1, 1) == "}") {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
?>
|
||||
@@ -1,35 +0,0 @@
|
||||
<?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.php");
|
||||
|
||||
define("CLIENT_LIBRARY_LANGUAGE", "PHP");
|
||||
define("CLIENT_LIBRARY_VERSION", "V02");
|
||||
|
||||
define("TOKEN", "_token");
|
||||
define("LANGUAGE", "_language");
|
||||
define("VERSION", "_version");
|
||||
?>
|
||||
5
test/bootstrap.php
Normal file
5
test/bootstrap.php
Normal file
@@ -0,0 +1,5 @@
|
||||
<?php
|
||||
|
||||
define('MASHAPE_CLIENT_LIBRAY_PATH', dirname(dirname(__FILE__)) . '/main/mashape');
|
||||
|
||||
?>
|
||||
29
test/mashape/http/HttpClientTest.php
Normal file
29
test/mashape/http/HttpClientTest.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
require_once(MASHAPE_CLIENT_LIBRAY_PATH . "/http/HttpClient.php");
|
||||
|
||||
class HttpClientTest extends PHPUnit_Framework_TestCase {
|
||||
|
||||
function testDoRequest() {
|
||||
|
||||
try {
|
||||
HttpClient::doRequest("ciao", "http://www.ciao.com", null, null);
|
||||
$this->assertFalse(true);
|
||||
} catch (MashapeClientException $e) {
|
||||
$this->assertEquals(1003, $e->getCode());
|
||||
}
|
||||
|
||||
try {
|
||||
HttpClient::doRequest(HttpMethod::GET, "http://www.google.com", null, null);
|
||||
$this->assertFalse(true);
|
||||
} catch (MashapeClientException $e) {
|
||||
$this->assertEquals(2000, $e->getCode());
|
||||
}
|
||||
|
||||
$response = HttpClient::doRequest(HttpMethod::POST, "https://api.mashape.com/requestToken", null, null);
|
||||
$this->assertEquals(2001, $response->errors[0]->code);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
32
test/mashape/http/TokenUtilTest.php
Normal file
32
test/mashape/http/TokenUtilTest.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
require_once(MASHAPE_CLIENT_LIBRAY_PATH . "/http/TokenUtil.php");
|
||||
|
||||
class TokenUtilTest extends PHPUnit_Framework_TestCase {
|
||||
|
||||
function testRequestToken() {
|
||||
try {
|
||||
TokenUtil::requestToken(null);
|
||||
$this->assertFalse(true);
|
||||
} catch (MashapeClientException $e) {
|
||||
$this->assertEquals(2001, $e->getCode());
|
||||
}
|
||||
|
||||
try {
|
||||
TokenUtil::requestToken("");
|
||||
$this->assertFalse(true);
|
||||
} catch (MashapeClientException $e) {
|
||||
$this->assertEquals(2001, $e->getCode());
|
||||
}
|
||||
|
||||
try {
|
||||
TokenUtil::requestToken("bla");
|
||||
$this->assertFalse(true);
|
||||
} catch (MashapeClientException $e) {
|
||||
$this->assertEquals(2001, $e->getCode());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
146
test/mashape/http/UrlUtilsTest.php
Normal file
146
test/mashape/http/UrlUtilsTest.php
Normal file
@@ -0,0 +1,146 @@
|
||||
<?php
|
||||
|
||||
require_once(MASHAPE_CLIENT_LIBRAY_PATH . "/http/UrlUtils.php");
|
||||
|
||||
class UrlUtilsTest extends PHPUnit_Framework_TestCase {
|
||||
|
||||
function testPrepareRequest() {
|
||||
$url = "http://www.ciao.com";
|
||||
$parameters = null;
|
||||
UrlUtils::prepareRequest($url, $parameters);
|
||||
$this->assertEquals("http://www.ciao.com", $url);
|
||||
$this->assertEquals(array(), $parameters);
|
||||
|
||||
$url = "http://www.ciao.com";
|
||||
$parameters = array();
|
||||
UrlUtils::prepareRequest($url, $parameters);
|
||||
$this->assertEquals("http://www.ciao.com", $url);
|
||||
$this->assertEquals(array(), $parameters);
|
||||
|
||||
$url = "http://www.ciao.com/{id}";
|
||||
$parameters = null;
|
||||
UrlUtils::prepareRequest($url, $parameters);
|
||||
$this->assertEquals("http://www.ciao.com/", $url);
|
||||
$this->assertEquals(array(), $parameters);
|
||||
|
||||
$url = "http://www.ciao.com/{id}?name={name}";
|
||||
$parameters = null;
|
||||
UrlUtils::prepareRequest($url, $parameters);
|
||||
$this->assertEquals("http://www.ciao.com/", $url);
|
||||
$this->assertEquals(array(), $parameters);
|
||||
|
||||
$url = "http://www.ciao.com/{id}?name={name}";
|
||||
$parameters = array("id"=>12);
|
||||
UrlUtils::prepareRequest($url, $parameters);
|
||||
$this->assertEquals("http://www.ciao.com/12", $url);
|
||||
$this->assertEquals(array("id"=>"12"), $parameters);
|
||||
|
||||
$url = "http://www.ciao.com/{id}?name={name}";
|
||||
$parameters = array("id"=>12, "name"=>"tom");
|
||||
UrlUtils::prepareRequest($url, $parameters);
|
||||
$this->assertEquals("http://www.ciao.com/12?name=tom", $url);
|
||||
$this->assertEquals(array("id"=>12, "name"=>"tom"), $parameters);
|
||||
|
||||
$url = "http://www.ciao.com/{id}?name={name}&opt=1";
|
||||
$parameters = array("id"=>12, "name"=>"tom");
|
||||
UrlUtils::prepareRequest($url, $parameters);
|
||||
$this->assertEquals("http://www.ciao.com/12?name=tom&opt=1", $url);
|
||||
$this->assertEquals(array("id"=>12, "name"=>"tom"), $parameters);
|
||||
|
||||
$url = "http://www.ciao.com/{id}?name={name}&opt=1";
|
||||
$parameters = array("id"=>12, "name"=>"tom jerry");
|
||||
UrlUtils::prepareRequest($url, $parameters);
|
||||
$this->assertEquals("http://www.ciao.com/12?name=tom+jerry&opt=1", $url);
|
||||
$this->assertEquals(array("id"=>12, "name"=>"tom jerry"), $parameters);
|
||||
|
||||
$url = "http://www.ciao.com/{id}?name={name}&opt=1&nick={nick}";
|
||||
$parameters = array("id"=>12, "name"=>"tom jerry");
|
||||
UrlUtils::prepareRequest($url, $parameters);
|
||||
$this->assertEquals("http://www.ciao.com/12?name=tom+jerry&opt=1", $url);
|
||||
$this->assertEquals(array("id"=>12, "name"=>"tom jerry"), $parameters);
|
||||
|
||||
$url = "http://www.ciao.com/{id}?name={name}&opt={opt}&nick={nick}";
|
||||
$parameters = array("id"=>12, "name"=>"tom jerry", "nick"=>"sinz");
|
||||
UrlUtils::prepareRequest($url, $parameters);
|
||||
$this->assertEquals("http://www.ciao.com/12?name=tom+jerry&nick=sinz", $url);
|
||||
$this->assertEquals(array("id"=>12, "name"=>"tom jerry", "nick"=>"sinz"), $parameters);
|
||||
|
||||
$url = "http://www.ciao.com/{id}?name={name}&opt={opt}&nick={nick}";
|
||||
$parameters = array("id"=>12, "name"=>"tom jerry", "opt"=>"yes", "nick"=>"sinz");
|
||||
UrlUtils::prepareRequest($url, $parameters);
|
||||
$this->assertEquals("http://www.ciao.com/12?name=tom+jerry&opt=yes&nick=sinz", $url);
|
||||
$this->assertEquals(array("id"=>12, "name"=>"tom jerry", "opt"=>"yes", "nick"=>"sinz"), $parameters);
|
||||
|
||||
$url = "http://www.ciao.com/{id}?name={name}&opt={opt}&nick={nick}";
|
||||
$parameters = array("id"=>12, "opt"=>"yes", "nick"=>"sinz");
|
||||
UrlUtils::prepareRequest($url, $parameters);
|
||||
$this->assertEquals("http://www.ciao.com/12?opt=yes&nick=sinz", $url);
|
||||
$this->assertEquals(array("id"=>12, "opt"=>"yes", "nick"=>"sinz"), $parameters);
|
||||
|
||||
$url = "http://www.ciao.com/{id}?name={name}&opt={opt}&nick={nick}";
|
||||
$parameters = array("id"=>12, "opt"=>"yes");
|
||||
UrlUtils::prepareRequest($url, $parameters);
|
||||
$this->assertEquals("http://www.ciao.com/12?opt=yes", $url);
|
||||
$this->assertEquals(array("id"=>12, "opt"=>"yes"), $parameters);
|
||||
|
||||
$url = "http://www.ciao.com/{id}?name={name}&opt={opt}&nick={nick}";
|
||||
$parameters = array("id"=>12, "nick"=>"sinz");
|
||||
UrlUtils::prepareRequest($url, $parameters);
|
||||
$this->assertEquals("http://www.ciao.com/12?nick=sinz", $url);
|
||||
$this->assertEquals(array("id"=>12, "nick"=>"sinz"), $parameters);
|
||||
|
||||
$url = "http://www.ciao.com/{id}?name={name}&opt={opt}&nick={nick}";
|
||||
$parameters = array("id"=>12);
|
||||
UrlUtils::prepareRequest($url, $parameters);
|
||||
$this->assertEquals("http://www.ciao.com/12", $url);
|
||||
$this->assertEquals(array("id"=>12), $parameters);
|
||||
|
||||
$url = "http://www.ciao.com/{id}?name={name}&opt={opt}&nick={nick}";
|
||||
$parameters = array("id"=>12, "pippo"=>null);
|
||||
UrlUtils::prepareRequest($url, $parameters);
|
||||
$this->assertEquals("http://www.ciao.com/12", $url);
|
||||
$this->assertEquals(array("id"=>12), $parameters);
|
||||
|
||||
$url = "http://www.ciao.com/{id}?name={name}&opt={opt}&nick=some+nick";
|
||||
$parameters = array("id"=>"ciao marco", "name"=>"ciao pippo", "opt"=>"2");
|
||||
UrlUtils::prepareRequest($url, $parameters);
|
||||
$this->assertEquals("http://www.ciao.com/ciao%20marco?name=ciao+pippo&opt=2&nick=some+nick", $url);
|
||||
$this->assertEquals(array("id"=>"ciao marco", "name"=>"ciao pippo", "opt"=>"2"), $parameters);
|
||||
|
||||
$url = "http://www.ciao.com/{id}?name={name}&opt={opt}&nick=some+nick";
|
||||
$parameters = array("id"=>"ciao marco", "name"=>"ciao pippo", "opt"=>"{this is opt}");
|
||||
UrlUtils::prepareRequest($url, $parameters);
|
||||
$this->assertEquals("http://www.ciao.com/ciao%20marco?name=ciao+pippo&opt=%7Bthis+is+opt%7D&nick=some+nick", $url);
|
||||
$this->assertEquals(array("id"=>"ciao marco", "name"=>"ciao pippo", "opt"=>"{this is opt}"), $parameters);
|
||||
}
|
||||
|
||||
function testAddClientParameters() {
|
||||
$url = "http://www.ciao.com";
|
||||
$parameters = array();
|
||||
UrlUtils::addClientParameters($url, $parameters, null);
|
||||
$this->assertEquals("http://www.ciao.com?_token={_token}&_language={_language}&_version={_version}", $url);
|
||||
$this->assertEquals(array("_token"=>null, "_language"=>"PHP", "_version"=>"V03"), $parameters);
|
||||
|
||||
$url = "http://www.ciao.com?name={name}";
|
||||
$parameters = array("name"=>"Marco");
|
||||
UrlUtils::addClientParameters($url, $parameters, null);
|
||||
$this->assertEquals("http://www.ciao.com?name={name}&_token={_token}&_language={_language}&_version={_version}", $url);
|
||||
$this->assertEquals(array("name"=>"Marco", "_token"=>null, "_language"=>"PHP", "_version"=>"V03"), $parameters);
|
||||
|
||||
$url = "http://www.ciao.com?name={name}";
|
||||
$parameters = array("name"=>"Marco");
|
||||
UrlUtils::addClientParameters($url, $parameters, "a-random-token");
|
||||
$this->assertEquals("http://www.ciao.com?name={name}&_token={_token}&_language={_language}&_version={_version}", $url);
|
||||
$this->assertEquals(array("name"=>"Marco", "_token"=>"a-random-token", "_language"=>"PHP", "_version"=>"V03"), $parameters);
|
||||
|
||||
$url = "http://www.ciao.com?name={name}";
|
||||
$parameters = array("name"=>"Marco");
|
||||
UrlUtils::addClientParameters($url, $parameters, "a-random-token");
|
||||
UrlUtils::prepareRequest($url, $parameters);
|
||||
$this->assertEquals("http://www.ciao.com?name=Marco&_token=a-random-token&_language=PHP&_version=V03", $url);
|
||||
$this->assertEquals(array("name"=>"Marco", "_token"=>"a-random-token", "_language"=>"PHP", "_version"=>"V03"), $parameters);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
12
test/phpunit.CI.xml
Normal file
12
test/phpunit.CI.xml
Normal file
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit
|
||||
bootstrap="./bootstrap.php"
|
||||
colors="false"
|
||||
backupGlobals="false"
|
||||
backupStaticAttributes="false">
|
||||
|
||||
<testsuite name="Mashape Client Test Suite">
|
||||
<directory>./mashape</directory>
|
||||
</testsuite>
|
||||
|
||||
</phpunit>
|
||||
12
test/phpunit.xml
Normal file
12
test/phpunit.xml
Normal file
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit
|
||||
bootstrap="./bootstrap.php"
|
||||
colors="true"
|
||||
backupGlobals="false"
|
||||
backupStaticAttributes="false">
|
||||
|
||||
<testsuite name="Mashape Client Test Suite">
|
||||
<directory>./mashape</directory>
|
||||
</testsuite>
|
||||
|
||||
</phpunit>
|
||||
Reference in New Issue
Block a user