version 0.4
This commit is contained in:
@@ -25,6 +25,5 @@
|
||||
*/
|
||||
|
||||
require_once(dirname(__FILE__) . "/http/HttpClient.php");
|
||||
require_once(dirname(__FILE__) . "/http/TokenUtil.php");
|
||||
|
||||
?>
|
||||
|
||||
@@ -28,7 +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_JSONDECODE_REQUEST", "Can't deserialize the response JSON from the component. The method returned an invalid JSON value: %s");
|
||||
define("EXCEPTION_EMPTY_RESPONSE", "Can't deserialize the response JSON from the component. The method returned an empty value.");
|
||||
define("EXCEPTION_JSONDECODE_REQUEST", "Can't deserialize the response JSON from the component. Maybe you tried to make an HTTPS request but PHP is not compiled with OpenSSL support, or the request returned an invalid JSON value: %s");
|
||||
|
||||
?>
|
||||
|
||||
@@ -1,25 +1,47 @@
|
||||
<?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__) . "/../exceptions/MashapeClientException.php");
|
||||
require_once(dirname(__FILE__) . "/HttpMethod.php");
|
||||
require_once(dirname(__FILE__) . "/UrlUtils.php");
|
||||
require_once(dirname(__FILE__) . "/AuthUtil.php");
|
||||
|
||||
class HttpClient {
|
||||
|
||||
public static function doRequest($httpMethod, $url, $parameters, $token) {
|
||||
public static function doRequest($httpMethod, $url, $parameters, $mashapeAuthentication, $publicKey, $privateKey) {
|
||||
|
||||
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);
|
||||
if (empty($response)) {
|
||||
throw new MashapeClientException(EXCEPTION_EMPTY_RESPONSE, EXCEPTION_SYSTEM_ERROR_CODE);
|
||||
}
|
||||
$response = self::execRequest($httpMethod, $url, $parameters, $mashapeAuthentication, $publicKey, $privateKey);
|
||||
|
||||
$jsonResponse = json_decode($response);
|
||||
if (empty($jsonResponse)) {
|
||||
@@ -30,18 +52,22 @@ class HttpClient {
|
||||
|
||||
}
|
||||
|
||||
private static function execRequest($httpMethod, $url, $parameters) {
|
||||
private static function execRequest($httpMethod, $url, $parameters, $mashapeAuthentication, $publicKey, $privateKey) {
|
||||
$data = null;
|
||||
if ($httpMethod != HttpMethod::GET) {
|
||||
$url = self::removeQueryString($url);
|
||||
$data = http_build_query($parameters);
|
||||
}
|
||||
|
||||
|
||||
$headers = ($mashapeAuthentication) ? AuthUtil::generateAuthenticationHeader($publicKey, $privateKey) : "";
|
||||
$headers .= UrlUtils::generateClientHeaders();
|
||||
|
||||
$opts = array('http' =>
|
||||
array(
|
||||
'ignore_errors' => true,
|
||||
'method' => $httpMethod,
|
||||
'content' => $data
|
||||
'content' => $data,
|
||||
'header' => $headers
|
||||
)
|
||||
);
|
||||
|
||||
|
||||
@@ -1,5 +1,29 @@
|
||||
<?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 HttpMethod
|
||||
{
|
||||
const DELETE = "DELETE";
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -1,11 +1,31 @@
|
||||
<?php
|
||||
|
||||
define("CLIENT_LIBRARY_LANGUAGE", "PHP");
|
||||
define("CLIENT_LIBRARY_VERSION", "V03");
|
||||
/*
|
||||
* 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
|
||||
*
|
||||
*/
|
||||
|
||||
define("TOKEN", "_token");
|
||||
define("LANGUAGE", "_language");
|
||||
define("VERSION", "_version");
|
||||
define("CLIENT_LIBRARY_LANGUAGE", "PHP");
|
||||
define("CLIENT_LIBRARY_VERSION", "V04");
|
||||
|
||||
define("PLACEHOLDER_REGEX", "/\{([\w\.]+)\}/");
|
||||
class UrlUtils {
|
||||
@@ -73,27 +93,9 @@ class UrlUtils {
|
||||
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 . "}";
|
||||
public static function generateClientHeaders() {
|
||||
$headers = "X-Mashape-Language: " . CLIENT_LIBRARY_LANGUAGE . "\r\n" . "X-Mashape-Version: " . CLIENT_LIBRARY_VERSION . "\r\n";
|
||||
return $headers;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user