refactor(request): request body management is now externalized to helper methods
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
require_once dirname(__FILE__) . '/Unirest/File.php';
|
||||
require_once dirname(__FILE__) . '/Unirest/Method.php';
|
||||
require_once dirname(__FILE__) . '/Unirest/Response.php';
|
||||
require_once dirname(__FILE__) . '/Unirest/Request.php';
|
||||
require_once dirname(__FILE__) . '/Unirest/Request/Body.php';
|
||||
|
||||
5
src/Unirest/Exception.php
Normal file
5
src/Unirest/Exception.php
Normal file
@@ -0,0 +1,5 @@
|
||||
<?php
|
||||
|
||||
namespace Unirest;
|
||||
|
||||
class Exception extends \Exception {}
|
||||
@@ -1,22 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Unirest;
|
||||
|
||||
class File
|
||||
{
|
||||
/**
|
||||
* Prepares a file for upload. To be used inside the parameters declaration for a request.
|
||||
* @param string $filename The file path
|
||||
* @param string $mimetype MIME type
|
||||
* @param string $postname the file name
|
||||
* @return string|\CURLFile
|
||||
*/
|
||||
public static function add($filename, $mimetype = '', $postname = '')
|
||||
{
|
||||
if (function_exists('curl_file_create')) {
|
||||
return curl_file_create($filename, $mimetype, $postname);
|
||||
} else {
|
||||
return sprintf('@%s;filename=%s;type=%s', $filename, $postname ?: basename($filename), $mimetype);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace Unirest;
|
||||
|
||||
use Unirest\Exception as Exception;
|
||||
|
||||
class Request
|
||||
{
|
||||
private static $cookie = null;
|
||||
@@ -228,7 +230,7 @@ class Request
|
||||
* @param mixed $parameters parameters to send in the querystring
|
||||
* @param string $username Authentication username (deprecated)
|
||||
* @param string $password Authentication password (deprecated)
|
||||
* @return \Unirest\Response
|
||||
* @return Unirest\Response
|
||||
*/
|
||||
public static function get($url, $headers = array(), $parameters = null, $username = null, $password = null)
|
||||
{
|
||||
@@ -242,7 +244,7 @@ class Request
|
||||
* @param mixed $parameters parameters to send in the querystring
|
||||
* @param string $username Basic Authentication username (deprecated)
|
||||
* @param string $password Basic Authentication password (deprecated)
|
||||
* @return \Unirest\Response
|
||||
* @return Unirest\Response
|
||||
*/
|
||||
public static function head($url, $headers = array(), $parameters = null, $username = null, $password = null)
|
||||
{
|
||||
@@ -256,7 +258,7 @@ class Request
|
||||
* @param mixed $parameters parameters to send in the querystring
|
||||
* @param string $username Basic Authentication username
|
||||
* @param string $password Basic Authentication password
|
||||
* @return \Unirest\Response
|
||||
* @return Unirest\Response
|
||||
*/
|
||||
public static function options($url, $headers = array(), $parameters = null, $username = null, $password = null)
|
||||
{
|
||||
@@ -270,7 +272,7 @@ class Request
|
||||
* @param mixed $parameters parameters to send in the querystring
|
||||
* @param string $username Basic Authentication username (deprecated)
|
||||
* @param string $password Basic Authentication password (deprecated)
|
||||
* @return \Unirest\Response
|
||||
* @return Unirest\Response
|
||||
*/
|
||||
public static function connect($url, $headers = array(), $parameters = null, $username = null, $password = null)
|
||||
{
|
||||
@@ -284,7 +286,7 @@ class Request
|
||||
* @param mixed $body POST body data
|
||||
* @param string $username Basic Authentication username (deprecated)
|
||||
* @param string $password Basic Authentication password (deprecated)
|
||||
* @return \Unirest\Response response
|
||||
* @return Unirest\Response response
|
||||
*/
|
||||
public static function post($url, $headers = array(), $body = null, $username = null, $password = null)
|
||||
{
|
||||
@@ -298,7 +300,7 @@ class Request
|
||||
* @param mixed $body DELETE body data
|
||||
* @param string $username Basic Authentication username (deprecated)
|
||||
* @param string $password Basic Authentication password (deprecated)
|
||||
* @return \Unirest\Response
|
||||
* @return Unirest\Response
|
||||
*/
|
||||
public static function delete($url, $headers = array(), $body = null, $username = null, $password = null)
|
||||
{
|
||||
@@ -312,7 +314,7 @@ class Request
|
||||
* @param mixed $body PUT body data
|
||||
* @param string $username Basic Authentication username (deprecated)
|
||||
* @param string $password Basic Authentication password (deprecated)
|
||||
* @return \Unirest\Response
|
||||
* @return Unirest\Response
|
||||
*/
|
||||
public static function put($url, $headers = array(), $body = null, $username = null, $password = null)
|
||||
{
|
||||
@@ -326,7 +328,7 @@ class Request
|
||||
* @param mixed $body PATCH body data
|
||||
* @param string $username Basic Authentication username (deprecated)
|
||||
* @param string $password Basic Authentication password (deprecated)
|
||||
* @return \Unirest\Response
|
||||
* @return Unirest\Response
|
||||
*/
|
||||
public static function patch($url, $headers = array(), $body = null, $username = null, $password = null)
|
||||
{
|
||||
@@ -340,7 +342,7 @@ class Request
|
||||
* @param mixed $body TRACE body data
|
||||
* @param string $username Basic Authentication username (deprecated)
|
||||
* @param string $password Basic Authentication password (deprecated)
|
||||
* @return \Unirest\Response
|
||||
* @return Unirest\Response
|
||||
*/
|
||||
public static function trace($url, $headers = array(), $body = null, $username = null, $password = null)
|
||||
{
|
||||
@@ -381,14 +383,14 @@ class Request
|
||||
|
||||
/**
|
||||
* Send a cURL request
|
||||
* @param \Unirest\Method|string $method HTTP method to use
|
||||
* @param Unirest\Method|string $method HTTP method to use
|
||||
* @param string $url URL to send the request to
|
||||
* @param mixed $body request body
|
||||
* @param array $headers additional headers to send
|
||||
* @param string $username Authentication username (deprecated)
|
||||
* @param string $password Authentication password (deprecated)
|
||||
* @throws \Exception if a cURL error occurs
|
||||
* @return \Unirest\Response
|
||||
* @throws Unirest\Exception if a cURL error occurs
|
||||
* @return Unirest\Response
|
||||
*/
|
||||
public static function send($method, $url, $body = null, $headers = array(), $username = null, $password = null)
|
||||
{
|
||||
@@ -397,11 +399,7 @@ class Request
|
||||
if ($method !== Method::GET) {
|
||||
curl_setopt(self::$handle, CURLOPT_CUSTOMREQUEST, $method);
|
||||
|
||||
if (is_array($body) || $body instanceof \Traversable) {
|
||||
curl_setopt(self::$handle, CURLOPT_POSTFIELDS, self::buildHTTPCurlQuery($body));
|
||||
} else {
|
||||
curl_setopt(self::$handle, CURLOPT_POSTFIELDS, $body);
|
||||
}
|
||||
curl_setopt(self::$handle, CURLOPT_POSTFIELDS, $body);
|
||||
} elseif (is_array($body)) {
|
||||
if (strpos($url, '?') !== false) {
|
||||
$url .= '&';
|
||||
@@ -472,7 +470,7 @@ class Request
|
||||
$info = self::getInfo();
|
||||
|
||||
if ($error) {
|
||||
throw new \Exception($error);
|
||||
throw new Exception($error);
|
||||
}
|
||||
|
||||
// Split the full response in its headers and body
|
||||
|
||||
66
src/Unirest/Request/Body.php
Normal file
66
src/Unirest/Request/Body.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace Unirest\Request;
|
||||
|
||||
use Unirest\Request as Request;
|
||||
use Unirest\Exception as Exception;
|
||||
|
||||
class Body
|
||||
{
|
||||
/**
|
||||
* Prepares a file for upload. To be used inside the parameters declaration for a request.
|
||||
* @param string $filename The file path
|
||||
* @param string $mimetype MIME type
|
||||
* @param string $postname the file name
|
||||
* @return string|\CURLFile
|
||||
*/
|
||||
public static function File($filename, $mimetype = '', $postname = '')
|
||||
{
|
||||
if (class_exists('CURLFile')) {
|
||||
return new \CURLFile($filename, $mimetype, $postname);
|
||||
}
|
||||
|
||||
if (function_exists('curl_file_create')) {
|
||||
return curl_file_create($filename, $mimetype, $postname);
|
||||
}
|
||||
|
||||
return sprintf('@%s;filename=%s;type=%s', $filename, $postname ?: basename($filename), $mimetype);
|
||||
}
|
||||
|
||||
public static function Json($data)
|
||||
{
|
||||
if (!function_exists('json_encode')) {
|
||||
throw new Exception('JSON Extension not available');
|
||||
}
|
||||
|
||||
return json_encode($data);
|
||||
}
|
||||
|
||||
public static function Form($data)
|
||||
{
|
||||
if (is_array($data) || is_object($data) || $data instanceof \Traversable) {
|
||||
return http_build_query(Request::buildHTTPCurlQuery($data));
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
public static function Multipart($data, $files = false)
|
||||
{
|
||||
if (is_object($data)) {
|
||||
return get_object_vars($data);
|
||||
}
|
||||
|
||||
if (!is_array($data)) {
|
||||
return array($data);
|
||||
}
|
||||
|
||||
if ($files !== false) {
|
||||
foreach ($files as $name => $file) {
|
||||
$data[$name] = call_user_func(array(__CLASS__, 'File'), $file);
|
||||
}
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user