Request.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 'delete'|'get'|'post' $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. * @param string[] $usage names of tracked behaviors associated with this request
  31. *
  32. * @throws \Stripe\Exception\ApiErrorException if the request fails
  33. *
  34. * @return array tuple containing (the JSON response, $options)
  35. */
  36. protected function _request($method, $url, $params = [], $options = null, $usage = [])
  37. {
  38. $opts = $this->_opts->merge($options);
  39. list($resp, $options) = static::_staticRequest($method, $url, $params, $opts, $usage);
  40. $this->setLastResponse($resp);
  41. return [$resp->json, $options];
  42. }
  43. /**
  44. * @param string $url URL for the request
  45. * @param class-string< \Stripe\SearchResult|\Stripe\Collection > $resultClass indicating what type of paginated result is returned
  46. * @param null|array $params list of parameters for the request
  47. * @param null|array|string $options
  48. * @param string[] $usage names of tracked behaviors associated with this request
  49. *
  50. * @throws \Stripe\Exception\ApiErrorException if the request fails
  51. *
  52. * @return \Stripe\Collection|\Stripe\SearchResult
  53. */
  54. protected static function _requestPage($url, $resultClass, $params = null, $options = null, $usage = [])
  55. {
  56. self::_validateParams($params);
  57. list($response, $opts) = static::_staticRequest('get', $url, $params, $options, $usage);
  58. $obj = \Stripe\Util\Util::convertToStripeObject($response->json, $opts);
  59. if (!($obj instanceof $resultClass)) {
  60. throw new \Stripe\Exception\UnexpectedValueException(
  61. 'Expected type ' . $resultClass . ', got "' . \get_class($obj) . '" instead.'
  62. );
  63. }
  64. $obj->setLastResponse($response);
  65. $obj->setFilters($params);
  66. return $obj;
  67. }
  68. /**
  69. * @param 'delete'|'get'|'post' $method HTTP method ('get', 'post', etc.)
  70. * @param string $url URL for the request
  71. * @param callable $readBodyChunk function that will receive chunks of data from a successful request body
  72. * @param array $params list of parameters for the request
  73. * @param null|array|string $options
  74. * @param string[] $usage names of tracked behaviors associated with this request
  75. *
  76. * @throws \Stripe\Exception\ApiErrorException if the request fails
  77. */
  78. protected function _requestStream($method, $url, $readBodyChunk, $params = [], $options = null, $usage = [])
  79. {
  80. $opts = $this->_opts->merge($options);
  81. static::_staticStreamingRequest($method, $url, $readBodyChunk, $params, $opts, $usage);
  82. }
  83. /**
  84. * @param 'delete'|'get'|'post' $method HTTP method ('get', 'post', etc.)
  85. * @param string $url URL for the request
  86. * @param array $params list of parameters for the request
  87. * @param null|array|string $options
  88. * @param string[] $usage names of tracked behaviors associated with this request
  89. *
  90. * @throws \Stripe\Exception\ApiErrorException if the request fails
  91. *
  92. * @return array tuple containing (the JSON response, $options)
  93. */
  94. protected static function _staticRequest($method, $url, $params, $options, $usage = [])
  95. {
  96. $opts = \Stripe\Util\RequestOptions::parse($options);
  97. $baseUrl = isset($opts->apiBase) ? $opts->apiBase : static::baseUrl();
  98. $requestor = new \Stripe\ApiRequestor($opts->apiKey, $baseUrl);
  99. list($response, $opts->apiKey) = $requestor->request($method, $url, $params, $opts->headers, $usage);
  100. $opts->discardNonPersistentHeaders();
  101. return [$response, $opts];
  102. }
  103. /**
  104. * @param 'delete'|'get'|'post' $method HTTP method ('get', 'post', etc.)
  105. * @param string $url URL for the request
  106. * @param callable $readBodyChunk function that will receive chunks of data from a successful request body
  107. * @param array $params list of parameters for the request
  108. * @param null|array|string $options
  109. * @param string[] $usage names of tracked behaviors associated with this request
  110. *
  111. * @throws \Stripe\Exception\ApiErrorException if the request fails
  112. */
  113. protected static function _staticStreamingRequest($method, $url, $readBodyChunk, $params, $options, $usage = [])
  114. {
  115. $opts = \Stripe\Util\RequestOptions::parse($options);
  116. $baseUrl = isset($opts->apiBase) ? $opts->apiBase : static::baseUrl();
  117. $requestor = new \Stripe\ApiRequestor($opts->apiKey, $baseUrl);
  118. $requestor->requestStream($method, $url, $readBodyChunk, $params, $opts->headers);
  119. }
  120. }