Request.php 3.9 KB

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