PHP library now supports multipart/form-data content-type and binary fields

This commit is contained in:
Evan Seguin
2012-08-28 14:58:21 -07:00
parent eff530a5fa
commit b6939ad164
15 changed files with 168 additions and 103 deletions

View File

@@ -1,9 +0,0 @@
<?php
require_once(dirname(__FILE__) . "/Auth.php");
abstract class HeaderAuth implements Auth {
public function handleParams() {
return null;
}
}
?>

View File

@@ -1,18 +0,0 @@
<?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;
}
}
?>

View File

@@ -1,5 +1,5 @@
<?php
interface Auth {
interface Authentication {
public function handleHeader();
public function handleParams();

View File

@@ -24,7 +24,7 @@
*
*/
class AuthUtil {
class AuthenticationUtil {
public static function generateAuthenticationHeader($publicKey, $privateKey) {
$header = "";

View File

@@ -1,7 +1,7 @@
<?php
require_once(dirname(__FILE__) . "/HeaderAuth.php");
require_once(dirname(__FILE__) . "/HeaderAuthentication.php");
class BasicAuth extends HeaderAuth {
class BasicAuthentication extends HeaderAuthentication {
private $header;

View File

@@ -1,7 +1,7 @@
<?php
require_once(dirname(__FILE__) . "/HeaderAuth.php");
require_once(dirname(__FILE__) . "/HeaderAuthentication.php");
class CustomHeaderAuth extends HeaderAuth {
class CustomHeaderAuthentication extends HeaderAuthentication {
private $header;

View File

@@ -0,0 +1,9 @@
<?php
require_once(dirname(__FILE__) . "/Authentication.php");
abstract class HeaderAuthentication implements Authentication {
public function handleParams() {
return null;
}
}
?>

View File

@@ -0,0 +1,18 @@
<?php
require_once(dirname(__FILE__) . "/AuthenticationUtil.php");
require_once(dirname(__FILE__) . "/HeaderAuthentication.php");
class MashapeAuthentication extends HeaderAuthentication {
private $header;
function __construct($publicKey, $privateKey) {
$this->header = AuthenticationUtil::generateAuthenticationHeader($publicKey, $privateKey);
}
public function handleHeader() {
return $this->header;
}
}
?>

View File

@@ -1,7 +1,7 @@
<?php
require_once(dirname(__FILE__) . "/Auth.php");
require_once(dirname(__FILE__) . "/Authentication.php");
class QueryAuth implements Auth {
class QueryAuthentication implements Authentication {
private $params;

View File

@@ -27,6 +27,12 @@
define("EXCEPTION_NOTSUPPORTED_HTTPMETHOD_CODE", 1003);
define("EXCEPTION_NOTSUPPORTED_HTTPMETHOD", "HTTP method not supported. Only DELETE, GET, POST, PUT are supported");
define("EXCEPTION_NOTSUPPORTED_CONTENTTYPE_CODE", 415);
define("EXCEPTION_NOTSUPPORTED_CONTENTTYPE", "Content Type not supported. Currently only application/x-www-form-urlencoded and multipart/form-data are supported");
define("EXCEPTION_GET_INVALID_CONTENTTYPE_CODE", 415);
define("EXCEPTION_GET_INVALID_CONTENTTYPE", "A GET request must have a content type of application/x-www-form-urlencoded");
define("EXCEPTION_SYSTEM_ERROR_CODE", 2000);
define("EXCEPTION_JSONDECODE_REQUEST", "Can't deserialize the response JSON: %s");

View File

@@ -0,0 +1,34 @@
<?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 ContentType
{
const FORM = "FORM";
const MULTIPART = "MULTIPART";
const JSON = "JSON";
}
?>

View File

@@ -26,62 +26,85 @@
require_once(dirname(__FILE__) . "/../exceptions/MashapeClientException.php");
require_once(dirname(__FILE__) . "/HttpMethod.php");
require_once(dirname(__FILE__) . "/ContentType.php");
require_once(dirname(__FILE__) . "/UrlUtils.php");
require_once(dirname(__FILE__) . "/MashapeResponse.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");
require_once(dirname(__FILE__) . "/../authentication/HeaderAuthentication.php");
require_once(dirname(__FILE__) . "/../authentication/BasicAuthentication.php");
require_once(dirname(__FILE__) . "/../authentication/CustomHeaderAuthentication.php");
require_once(dirname(__FILE__) . "/../authentication/MashapeAuthentication.php");
require_once(dirname(__FILE__) . "/../authentication/QueryAuthentication.php");
class HttpClient {
public static function doRequest($httpMethod, $url, $parameters, $authHandlers, $encodeJson = true) {
public static function doRequest($httpMethod, $url, $parameters, $authHandlers, $contentType = ContentType::FORM, $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);
}
$response = self::execRequest($httpMethod, $url, $parameters, $authHandlers);
$response = self::execRequest($httpMethod, $url, $parameters, $authHandlers, $contentType);
if ($encodeJson) {
$response->parseBodyAsJson();
}
return $response;
}
private static function execRequest($httpMethod, $url, $parameters, $authHandlers) {
private static function execRequest($httpMethod, $url, $parameters, $authHandlers, $contentType) {
$data = null;
if ($parameters == null) {
$parameters = array();
}
if ($authHandlers == null) {
$authHandlers = array();
}
$headers = array();
$headers[] = UrlUtils::generateClientHeaders();
// Authentication
foreach($authHandlers as $handler) {
if ($handler instanceof QueryAuth) {
if ($handler instanceof QueryAuthentication) {
$parameters = array_merge($parameters, $handler->handleParams());
} else if ($handler instanceof HeaderAuth) {
} else if ($handler instanceof HeaderAuthentication) {
$headers[] = $handler->handleHeader();
}
}
UrlUtils::prepareRequest($url, $parameters, ($httpMethod != HttpMethod::GET) ? true : false);
if ($httpMethod != HttpMethod::GET) {
$data = http_build_query($parameters);
switch ($contentType) {
case ContentType::FORM:
$data = http_build_query($parameters);
break;
case ContentType::MULTIPART:
$data = $parameters;
break;
case ContentType::JSON:
// TODO support json
default:
throw new MashapeClientException(
EXCEPTION_NOTSUPPORTED_CONTENTTYPE,
EXCEPTION_NOTSUPPORTED_CONTENTTYPE_CODE);
}
} else if ($contentType != ContentType::FORM) {
// if we have a GET request that is anything other than urlencoded
// form data, we shouldn't allow it.
throw new MashapeClientException(
EXCEPTION_GET_INVALID_CONTENTTYPE,
EXCEPTION_GET_INVALID_CONTENTTYPE_CODE);
}
$ch = curl_init ();
// prepare the request
curl_setopt ($ch, CURLOPT_URL , $url);
curl_setopt ($ch, CURLOPT_URL , $url);
if ($httpMethod != HttpMethod::GET) {
curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, $httpMethod);
//curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $parameters);
}
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_HTTPHEADER, $headers);
@@ -90,7 +113,7 @@ class HttpClient {
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$responseHeaders = curl_getinfo($ch, CURLINFO_HEADER_OUT);
curl_close($ch);
return new MashapeResponse($response, $httpCode, $responseHeaders);
}
}

View File

@@ -44,7 +44,7 @@ class MashapeResponse {
$this->body = json_decode($this->rawBody);
if (empty($this->body) && ($this->statusCode == 200)) {
// It may be a chunked response
$this->body = json_decode(http_chunked_decode($this->rawBody));
//$this->body = json_decode(http_chunked_decode($this->rawBody));
if (empty($this->body)) {
throw new MashapeClientException(
sprintf(EXCEPTION_JSONDECODE_REQUEST, $this->rawBody),