Support for authentication
This commit is contained in:
7
main/mashape/auth/Auth.php
Normal file
7
main/mashape/auth/Auth.php
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
<?php
|
||||||
|
interface Auth {
|
||||||
|
public function handleHeader();
|
||||||
|
|
||||||
|
public function handleParams();
|
||||||
|
}
|
||||||
|
?>
|
||||||
24
main/mashape/auth/BasicAuth.php
Normal file
24
main/mashape/auth/BasicAuth.php
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
require_once(dirname(__FILE__) . "/HeaderAuth.php");
|
||||||
|
|
||||||
|
class BasicAuth extends HeaderAuth {
|
||||||
|
|
||||||
|
private $header;
|
||||||
|
|
||||||
|
function __construct($username, $password) {
|
||||||
|
$headerValue = $username . ":" . $password;
|
||||||
|
$this->header = "Authorization: " . base64_encode($headerValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function handleHeader() {
|
||||||
|
return $this->header;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function handleParams() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
22
main/mashape/auth/CustomHeaderAuth.php
Normal file
22
main/mashape/auth/CustomHeaderAuth.php
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
<?php
|
||||||
|
require_once(dirname(__FILE__) . "/HeaderAuth.php");
|
||||||
|
|
||||||
|
class CustomHeaderAuth extends HeaderAuth {
|
||||||
|
|
||||||
|
private $header;
|
||||||
|
|
||||||
|
function __construct($headerName, $headerValue) {
|
||||||
|
$this->header = $headerName . ": " . $headerValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function handleHeader() {
|
||||||
|
return $this->header;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function handleParams() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
|
||||||
|
|
||||||
9
main/mashape/auth/HeaderAuth.php
Normal file
9
main/mashape/auth/HeaderAuth.php
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
<?php
|
||||||
|
require_once(dirname(__FILE__) . "/Auth.php");
|
||||||
|
|
||||||
|
abstract class HeaderAuth implements Auth {
|
||||||
|
public function handleParams() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
||||||
18
main/mashape/auth/MashapeAuth.php
Normal file
18
main/mashape/auth/MashapeAuth.php
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
<?php
|
||||||
|
require_once(dirname(__FILE__) . "/../http/AuthUtil.php");
|
||||||
|
require_once(dirname(__FILE__) . "/HeaderAuth.php");
|
||||||
|
|
||||||
|
class MashapeAuth extends HeaderAuth {
|
||||||
|
|
||||||
|
private $header;
|
||||||
|
|
||||||
|
function __construct($publicKey, $privateKey) {
|
||||||
|
$this->header = AuthUtil::generateAuthenticationHeader($publicKey, $privateKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function handleHeader() {
|
||||||
|
return $this->header;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
|
||||||
21
main/mashape/auth/QueryAuth.php
Normal file
21
main/mashape/auth/QueryAuth.php
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
require_once(dirname(__FILE__) . "/Auth.php");
|
||||||
|
|
||||||
|
class QueryAuth implements Auth {
|
||||||
|
|
||||||
|
private $params;
|
||||||
|
|
||||||
|
function __construct($queryKey, $queryValue) {
|
||||||
|
$this->params = array($queryKey => $queryValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function handleHeader() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function handleParams() {
|
||||||
|
return $this->params;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
|
||||||
@@ -30,19 +30,22 @@ 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__) . "/AuthUtil.php");
|
||||||
|
require_once(dirname(__FILE__) . "/../auth/HeaderAuth.php");
|
||||||
|
require_once(dirname(__FILE__) . "/../auth/BasicAuth.php");
|
||||||
|
require_once(dirname(__FILE__) . "/../auth/CustomHeaderAuth.php");
|
||||||
|
require_once(dirname(__FILE__) . "/../auth/MashapeAuth.php");
|
||||||
|
require_once(dirname(__FILE__) . "/../auth/QueryAuth.php");
|
||||||
|
|
||||||
class HttpClient {
|
class HttpClient {
|
||||||
|
|
||||||
public static function doRequest($httpMethod, $url, $parameters, $publicKey, $privateKey, $encodeJson = true) {
|
public static function doRequest($httpMethod, $url, $parameters, $authHandlers, $encodeJson = true) {
|
||||||
|
|
||||||
if (!($httpMethod == HttpMethod::DELETE || $httpMethod == HttpMethod::GET ||
|
if (!($httpMethod == HttpMethod::DELETE || $httpMethod == HttpMethod::GET ||
|
||||||
$httpMethod == HttpMethod::POST || $httpMethod == HttpMethod::PUT)) {
|
$httpMethod == HttpMethod::POST || $httpMethod == HttpMethod::PUT)) {
|
||||||
throw new MashapeClientException(EXCEPTION_NOTSUPPORTED_HTTPMETHOD, EXCEPTION_NOTSUPPORTED_HTTPMETHOD_CODE);
|
throw new MashapeClientException(EXCEPTION_NOTSUPPORTED_HTTPMETHOD, EXCEPTION_NOTSUPPORTED_HTTPMETHOD_CODE);
|
||||||
}
|
}
|
||||||
|
|
||||||
UrlUtils::prepareRequest($url, $parameters, ($httpMethod != HttpMethod::GET) ? true : false);
|
$response = self::execRequest($httpMethod, $url, $parameters, $authHandlers);
|
||||||
|
|
||||||
$response = self::execRequest($httpMethod, $url, $parameters, $publicKey, $privateKey);
|
|
||||||
|
|
||||||
if (!$encodeJson) {
|
if (!$encodeJson) {
|
||||||
return $response;
|
return $response;
|
||||||
@@ -60,18 +63,26 @@ class HttpClient {
|
|||||||
return $jsonResponse;
|
return $jsonResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static function execRequest($httpMethod, $url, $parameters, $publicKey, $privateKey) {
|
private static function execRequest($httpMethod, $url, $parameters, $authHandlers) {
|
||||||
$data = null;
|
$data = null;
|
||||||
|
|
||||||
|
$headers = array();
|
||||||
|
$headers[] = UrlUtils::generateClientHeaders();
|
||||||
|
// Authentication
|
||||||
|
foreach($authHandlers as $handler) {
|
||||||
|
if ($handler instanceof QueryAuth) {
|
||||||
|
$parameters = array_merge($parameters, $handler->handleParams());
|
||||||
|
} else if ($handler instanceof HeaderAuth) {
|
||||||
|
$headers[] = $handler->handleHeader();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
UrlUtils::prepareRequest($url, $parameters, ($httpMethod != HttpMethod::GET) ? true : false);
|
||||||
|
|
||||||
if ($httpMethod != HttpMethod::GET) {
|
if ($httpMethod != HttpMethod::GET) {
|
||||||
//$url = self::removeQueryString($url);
|
//$url = self::removeQueryString($url);
|
||||||
$data = http_build_query($parameters);
|
$data = http_build_query($parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
//$headers = array();
|
|
||||||
$headers = array(
|
|
||||||
);
|
|
||||||
$headers[] = AuthUtil::generateAuthenticationHeader($publicKey, $privateKey);
|
|
||||||
$headers[] = UrlUtils::generateClientHeaders();
|
|
||||||
|
|
||||||
$ch = curl_init ();
|
$ch = curl_init ();
|
||||||
|
|
||||||
|
|||||||
@@ -42,12 +42,6 @@ class UrlUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($addRegularQueryStringParameters) {
|
|
||||||
// Get regular query string parameters
|
|
||||||
self::addRegularQueryStringParameters($url, $parameters);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
$finalUrl = $url;
|
$finalUrl = $url;
|
||||||
$matches = null;
|
$matches = null;
|
||||||
$match = preg_match_all(PLACEHOLDER_REGEX, $url, $matches);
|
$match = preg_match_all(PLACEHOLDER_REGEX, $url, $matches);
|
||||||
@@ -67,6 +61,17 @@ class UrlUtils {
|
|||||||
|
|
||||||
$finalUrl = preg_replace("/\?&/", "?", $finalUrl);
|
$finalUrl = preg_replace("/\?&/", "?", $finalUrl);
|
||||||
$finalUrl = preg_replace("/\?$/", "", $finalUrl);
|
$finalUrl = preg_replace("/\?$/", "", $finalUrl);
|
||||||
|
|
||||||
|
if ($addRegularQueryStringParameters) {
|
||||||
|
// Get regular query string parameters
|
||||||
|
self::addRegularQueryStringParameters($finalUrl, $parameters);
|
||||||
|
} else {
|
||||||
|
foreach ($parameters as $paramKey => $paramValue) {
|
||||||
|
$delimiter = (strpos($finalUrl, "?") === false) ? "?" : "&";
|
||||||
|
$finalUrl .= $delimiter . $paramKey . "=" . $paramValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$url = $finalUrl;
|
$url = $finalUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user