| 1 |
<?php |
| 2 |
|
| 3 |
namespace Stripe\ApiOperations; |
| 4 |
|
| 5 |
/** |
| 6 |
* Trait for resources that need to make API requests. |
| 7 |
* |
| 8 |
* This trait should only be applied to classes that derive from StripeObject. |
| 9 |
*/ |
| 10 |
trait Request |
| 11 |
{ |
| 12 |
/** |
| 13 |
* @param null|array|mixed $params The list of parameters to validate |
| 14 |
* |
| 15 |
* @throws \Stripe\Exception\InvalidArgumentException if $params exists and is not an array |
| 16 |
*/ |
| 17 |
protected static function _validateParams($params = null) |
| 18 |
{ |
| 19 |
if ($params && !\is_array($params)) { |
| 20 |
$message = 'You must pass an array as the first argument to Stripe API ' |
| 21 |
. 'method calls. (HINT: an example call to create a charge ' |
| 22 |
. "would be: \"Stripe\\Charge::create(['amount' => 100, " |
| 23 |
. "'currency' => 'usd', 'source' => 'tok_1234'])\")"; |
| 24 |
|
| 25 |
throw new \Stripe\Exception\InvalidArgumentException($message); |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* @param string $method HTTP method ('get', 'post', etc.) |
| 31 |
* @param string $url URL for the request |
| 32 |
* @param array $params list of parameters for the request |
| 33 |
* @param null|array|string $options |
| 34 |
* |
| 35 |
* @throws \Stripe\Exception\ApiErrorException if the request fails |
| 36 |
* |
| 37 |
* @return array tuple containing (the JSON response, $options) |
| 38 |
*/ |
| 39 |
protected function _request($method, $url, $params = [], $options = null) |
| 40 |
{ |
| 41 |
$opts = $this->_opts->merge($options); |
| 42 |
list($resp, $options) = static::_staticRequest($method, $url, $params, $opts); |
| 43 |
$this->setLastResponse($resp); |
| 44 |
|
| 45 |
return [$resp->json, $options]; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* @param string $method HTTP method ('get', 'post', etc.) |
| 50 |
* @param string $url URL for the request |
| 51 |
* @param callable $readBodyChunk function that will receive chunks of data from a successful request body |
| 52 |
* @param array $params list of parameters for the request |
| 53 |
* @param null|array|string $options |
| 54 |
* |
| 55 |
* @throws \Stripe\Exception\ApiErrorException if the request fails |
| 56 |
*/ |
| 57 |
protected function _requestStream($method, $url, $readBodyChunk, $params = [], $options = null) |
| 58 |
{ |
| 59 |
$opts = $this->_opts->merge($options); |
| 60 |
static::_staticStreamingRequest($method, $url, $readBodyChunk, $params, $opts); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* @param string $method HTTP method ('get', 'post', etc.) |
| 65 |
* @param string $url URL for the request |
| 66 |
* @param array $params list of parameters for the request |
| 67 |
* @param null|array|string $options |
| 68 |
* |
| 69 |
* @throws \Stripe\Exception\ApiErrorException if the request fails |
| 70 |
* |
| 71 |
* @return array tuple containing (the JSON response, $options) |
| 72 |
*/ |
| 73 |
protected static function _staticRequest($method, $url, $params, $options) |
| 74 |
{ |
| 75 |
$opts = \Stripe\Util\RequestOptions::parse($options); |
| 76 |
$baseUrl = isset($opts->apiBase) ? $opts->apiBase : static::baseUrl(); |
| 77 |
$requestor = new \Stripe\ApiRequestor($opts->apiKey, $baseUrl); |
| 78 |
list($response, $opts->apiKey) = $requestor->request($method, $url, $params, $opts->headers); |
| 79 |
$opts->discardNonPersistentHeaders(); |
| 80 |
|
| 81 |
return [$response, $opts]; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* @param string $method HTTP method ('get', 'post', etc.) |
| 86 |
* @param string $url URL for the request |
| 87 |
* @param callable $readBodyChunk function that will receive chunks of data from a successful request body |
| 88 |
* @param array $params list of parameters for the request |
| 89 |
* @param null|array|string $options |
| 90 |
* |
| 91 |
* @throws \Stripe\Exception\ApiErrorException if the request fails |
| 92 |
*/ |
| 93 |
protected static function _staticStreamingRequest($method, $url, $readBodyChunk, $params, $options) |
| 94 |
{ |
| 95 |
$opts = \Stripe\Util\RequestOptions::parse($options); |
| 96 |
$baseUrl = isset($opts->apiBase) ? $opts->apiBase : static::baseUrl(); |
| 97 |
$requestor = new \Stripe\ApiRequestor($opts->apiKey, $baseUrl); |
| 98 |
$requestor->requestStream($method, $url, $readBodyChunk, $params, $opts->headers); |
| 99 |
} |
| 100 |
} |
| 101 |
|