Skip to content

Commit

Permalink
Paginate requests
Browse files Browse the repository at this point in the history
  • Loading branch information
helenye-stripe committed Jan 16, 2024
1 parent 3d92509 commit 863b876
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 28 deletions.
13 changes: 1 addition & 12 deletions lib/ApiOperations/All.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,8 @@ trait All
*/
public static function all($params = null, $opts = null)
{
self::_validateParams($params);
$url = static::classUrl();

list($response, $opts) = static::_staticRequest('get', $url, $params, $opts);
$obj = \Stripe\Util\Util::convertToStripeObject($response->json, $opts);
if (!($obj instanceof \Stripe\Collection)) {
throw new \Stripe\Exception\UnexpectedValueException(
'Expected type ' . \Stripe\Collection::class . ', got "' . \get_class($obj) . '" instead.'
);
}
$obj->setLastResponse($response);
$obj->setFilters($params);

return $obj;
return static::_requestPage($url, \Stripe\Collection::class, $params, $opts);
}
}
34 changes: 31 additions & 3 deletions lib/ApiOperations/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ protected static function _validateParams($params = null)
{
if ($params && !\is_array($params)) {
$message = 'You must pass an array as the first argument to Stripe API '
. 'method calls. (HINT: an example call to create a charge '
. "would be: \"Stripe\\Charge::create(['amount' => 100, "
. "'currency' => 'usd', 'source' => 'tok_1234'])\")";
. 'method calls. (HINT: an example call to create a charge '
. "would be: \"Stripe\\Charge::create(['amount' => 100, "
. "'currency' => 'usd', 'source' => 'tok_1234'])\")";

throw new \Stripe\Exception\InvalidArgumentException($message);
}
Expand All @@ -46,6 +46,34 @@ protected function _request($method, $url, $params = [], $options = null, $usage
return [$resp->json, $options];
}

/**
* @param string $url URL for the request
* @param class-string< \Stripe\SearchResult|\Stripe\Collection > $resultClass indicating what type of paginated result is returned
* @param null|array $params list of parameters for the request
* @param null|array|string $options
* @param string[] $usage names of tracked behaviors associated with this request
*
* @throws \Stripe\Exception\ApiErrorException if the request fails
*
* @return \Stripe\Collection|\Stripe\SearchResult
*/
protected static function _requestPage($url, $resultClass, $params = null, $options = null, $usage = [])
{
self::_validateParams($params);

list($response, $opts) = static::_staticRequest('get', $url, $params, $options, $usage);
$obj = \Stripe\Util\Util::convertToStripeObject($response->json, $opts);
if (!($obj instanceof $resultClass)) {
throw new \Stripe\Exception\UnexpectedValueException(
'Expected type ' . $resultClass . ', got "' . \get_class($obj) . '" instead.'
);
}
$obj->setLastResponse($response);
$obj->setFilters($params);

return $obj;
}

/**
* @param 'delete'|'get'|'post' $method HTTP method ('get', 'post', etc.)
* @param string $url URL for the request
Expand Down
14 changes: 1 addition & 13 deletions lib/ApiOperations/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,6 @@ trait Search
*/
protected static function _searchResource($searchUrl, $params = null, $opts = null)
{
self::_validateParams($params);

list($response, $opts) = static::_staticRequest('get', $searchUrl, $params, $opts);
$obj = \Stripe\Util\Util::convertToStripeObject($response->json, $opts);
if (!($obj instanceof \Stripe\SearchResult)) {
throw new \Stripe\Exception\UnexpectedValueException(
'Expected type ' . \Stripe\SearchResult::class . ', got "' . \get_class($obj) . '" instead.'
);
}
$obj->setLastResponse($response);
$obj->setFilters($params);

return $obj;
return static::_requestPage($searchUrl, \Stripe\SearchResult::class, $params, $opts);
}
}

0 comments on commit 863b876

Please sign in to comment.