diff --git a/LICENSE b/main/LICENSE similarity index 100% rename from LICENSE rename to main/LICENSE diff --git a/README b/main/README similarity index 77% rename from README rename to main/README index 3e63a66..ff3a326 100644 --- a/README +++ b/main/README @@ -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 diff --git a/mashapebase/mashapeClient.php b/main/mashape/MashapeClient.php similarity index 84% rename from mashapebase/mashapeClient.php rename to main/mashape/MashapeClient.php index 6c133a8..3986a4e 100644 --- a/mashapebase/mashapeClient.php +++ b/main/mashape/MashapeClient.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"); ?> diff --git a/mashapebase/exceptions/clientExceptionMessages.php b/main/mashape/exceptions/ExceptionConstants.php similarity index 86% rename from mashapebase/exceptions/clientExceptionMessages.php rename to main/mashape/exceptions/ExceptionConstants.php index 479d354..cc905b3 100644 --- a/mashapebase/exceptions/clientExceptionMessages.php +++ b/main/mashape/exceptions/ExceptionConstants.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"); ?> diff --git a/mashapebase/exceptions/mashapeClientException.php b/main/mashape/exceptions/MashapeClientException.php similarity index 93% rename from mashapebase/exceptions/mashapeClientException.php rename to main/mashape/exceptions/MashapeClientException.php index b468470..5fab1ca 100644 --- a/mashapebase/exceptions/mashapeClientException.php +++ b/main/mashape/exceptions/MashapeClientException.php @@ -24,7 +24,7 @@ * */ -require_once (dirname(__FILE__) . "/clientExceptionMessages.php"); +require_once (dirname(__FILE__) . "/ExceptionConstants.php"); class MashapeClientException extends Exception { diff --git a/main/mashape/http/HttpClient.php b/main/mashape/http/HttpClient.php new file mode 100644 index 0000000..d5b8352 --- /dev/null +++ b/main/mashape/http/HttpClient.php @@ -0,0 +1,62 @@ + + 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; + } + +} + + + +?> diff --git a/main/mashape/http/HttpMethod.php b/main/mashape/http/HttpMethod.php new file mode 100644 index 0000000..373bff4 --- /dev/null +++ b/main/mashape/http/HttpMethod.php @@ -0,0 +1,11 @@ + \ No newline at end of file diff --git a/main/mashape/http/TokenUtil.php b/main/mashape/http/TokenUtil.php new file mode 100644 index 0000000..76116c1 --- /dev/null +++ b/main/mashape/http/TokenUtil.php @@ -0,0 +1,26 @@ +$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); + } + } + +} + +?> \ No newline at end of file diff --git a/main/mashape/http/UrlUtils.php b/main/mashape/http/UrlUtils.php new file mode 100644 index 0000000..72bfda9 --- /dev/null +++ b/main/mashape/http/UrlUtils.php @@ -0,0 +1,101 @@ + 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 . "}"; + } + +} + +?> \ No newline at end of file diff --git a/mashapebase/init/json.php b/main/mashape/json/Json.php similarity index 94% rename from mashapebase/init/json.php rename to main/mashape/json/Json.php index 9c38a54..d950d06 100644 --- a/mashapebase/init/json.php +++ b/main/mashape/json/Json.php @@ -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); } diff --git a/mashapebase/json/jsonImpl.php b/main/mashape/json/Services_JSON.php similarity index 100% rename from mashapebase/json/jsonImpl.php rename to main/mashape/json/Services_JSON.php diff --git a/mashapebase/http/httpClient.php b/mashapebase/http/httpClient.php deleted file mode 100644 index 9f2787c..0000000 --- a/mashapebase/http/httpClient.php +++ /dev/null @@ -1,149 +0,0 @@ -. - * - * - * 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 - array( - 'ignore_errors' => true, - 'method' => $httpMethod, - 'content' => $data - ) - ); - - $context = stream_context_create($opts); - $response = @file_get_contents($finalUrl, false, $context); - return $response; - } - -} - -?> diff --git a/mashapebase/http/tokenUtil.php b/mashapebase/http/tokenUtil.php deleted file mode 100644 index 953aad0..0000000 --- a/mashapebase/http/tokenUtil.php +++ /dev/null @@ -1,50 +0,0 @@ -. - * - * - * 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); - } - } - -} - -?> diff --git a/mashapebase/http/urlUtils.php b/mashapebase/http/urlUtils.php deleted file mode 100644 index d16d559..0000000 --- a/mashapebase/http/urlUtils.php +++ /dev/null @@ -1,134 +0,0 @@ -. - * - * - * 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; - } - -} -?> \ No newline at end of file diff --git a/mashapebase/init/init.php b/mashapebase/init/init.php deleted file mode 100644 index cc1a5b4..0000000 --- a/mashapebase/init/init.php +++ /dev/null @@ -1,35 +0,0 @@ -. - * - * - * 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"); -?> diff --git a/test/bootstrap.php b/test/bootstrap.php new file mode 100644 index 0000000..1df2e56 --- /dev/null +++ b/test/bootstrap.php @@ -0,0 +1,5 @@ + \ No newline at end of file diff --git a/test/mashape/http/HttpClientTest.php b/test/mashape/http/HttpClientTest.php new file mode 100644 index 0000000..53089fe --- /dev/null +++ b/test/mashape/http/HttpClientTest.php @@ -0,0 +1,29 @@ +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); + } + +} + +?> diff --git a/test/mashape/http/TokenUtilTest.php b/test/mashape/http/TokenUtilTest.php new file mode 100644 index 0000000..9013382 --- /dev/null +++ b/test/mashape/http/TokenUtilTest.php @@ -0,0 +1,32 @@ +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()); + } + } + +} + +?> \ No newline at end of file diff --git a/test/mashape/http/UrlUtilsTest.php b/test/mashape/http/UrlUtilsTest.php new file mode 100644 index 0000000..ab54d9c --- /dev/null +++ b/test/mashape/http/UrlUtilsTest.php @@ -0,0 +1,146 @@ +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); + } + +} + +?> \ No newline at end of file diff --git a/test/phpunit.CI.xml b/test/phpunit.CI.xml new file mode 100644 index 0000000..520d78d --- /dev/null +++ b/test/phpunit.CI.xml @@ -0,0 +1,12 @@ + + + + + ./mashape + + + diff --git a/test/phpunit.xml b/test/phpunit.xml new file mode 100644 index 0000000..2d2f471 --- /dev/null +++ b/test/phpunit.xml @@ -0,0 +1,12 @@ + + + + + ./mashape + + +