diff --git a/main/mashape/auth/Auth.php b/main/mashape/auth/Auth.php new file mode 100644 index 0000000..e45156a --- /dev/null +++ b/main/mashape/auth/Auth.php @@ -0,0 +1,7 @@ + diff --git a/main/mashape/auth/BasicAuth.php b/main/mashape/auth/BasicAuth.php new file mode 100644 index 0000000..571f8fc --- /dev/null +++ b/main/mashape/auth/BasicAuth.php @@ -0,0 +1,24 @@ +header = "Authorization: " . base64_encode($headerValue); + } + + public function handleHeader() { + return $this->header; + } + + public function handleParams() { + return null; + } +} +?> + + + diff --git a/main/mashape/auth/CustomHeaderAuth.php b/main/mashape/auth/CustomHeaderAuth.php new file mode 100644 index 0000000..67bea55 --- /dev/null +++ b/main/mashape/auth/CustomHeaderAuth.php @@ -0,0 +1,22 @@ +header = $headerName . ": " . $headerValue; + } + + public function handleHeader() { + return $this->header; + } + + public function handleParams() { + return null; + } +} +?> + + diff --git a/main/mashape/auth/HeaderAuth.php b/main/mashape/auth/HeaderAuth.php new file mode 100644 index 0000000..c23c0d7 --- /dev/null +++ b/main/mashape/auth/HeaderAuth.php @@ -0,0 +1,9 @@ + diff --git a/main/mashape/auth/MashapeAuth.php b/main/mashape/auth/MashapeAuth.php new file mode 100644 index 0000000..6485ff4 --- /dev/null +++ b/main/mashape/auth/MashapeAuth.php @@ -0,0 +1,18 @@ +header = AuthUtil::generateAuthenticationHeader($publicKey, $privateKey); + } + + public function handleHeader() { + return $this->header; + } +} +?> + diff --git a/main/mashape/auth/QueryAuth.php b/main/mashape/auth/QueryAuth.php new file mode 100644 index 0000000..26ad61a --- /dev/null +++ b/main/mashape/auth/QueryAuth.php @@ -0,0 +1,21 @@ +params = array($queryKey => $queryValue); + } + + public function handleHeader() { + return null; + } + + public function handleParams() { + return $this->params; + } +} +?> + diff --git a/main/mashape/http/HttpClient.php b/main/mashape/http/HttpClient.php index c526ba1..b5a8ef2 100755 --- a/main/mashape/http/HttpClient.php +++ b/main/mashape/http/HttpClient.php @@ -30,19 +30,22 @@ 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"); +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 { - 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 || $httpMethod == HttpMethod::POST || $httpMethod == HttpMethod::PUT)) { 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, $publicKey, $privateKey); + $response = self::execRequest($httpMethod, $url, $parameters, $authHandlers); if (!$encodeJson) { return $response; @@ -60,18 +63,26 @@ class HttpClient { return $jsonResponse; } - private static function execRequest($httpMethod, $url, $parameters, $publicKey, $privateKey) { + private static function execRequest($httpMethod, $url, $parameters, $authHandlers) { $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) { //$url = self::removeQueryString($url); $data = http_build_query($parameters); } - - //$headers = array(); - $headers = array( - ); - $headers[] = AuthUtil::generateAuthenticationHeader($publicKey, $privateKey); - $headers[] = UrlUtils::generateClientHeaders(); $ch = curl_init (); diff --git a/main/mashape/http/UrlUtils.php b/main/mashape/http/UrlUtils.php index 9449894..128703c 100755 --- a/main/mashape/http/UrlUtils.php +++ b/main/mashape/http/UrlUtils.php @@ -42,12 +42,6 @@ class UrlUtils { } } - if ($addRegularQueryStringParameters) { - // Get regular query string parameters - self::addRegularQueryStringParameters($url, $parameters); - } - - $finalUrl = $url; $matches = null; $match = preg_match_all(PLACEHOLDER_REGEX, $url, $matches); @@ -67,6 +61,17 @@ class UrlUtils { $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; }