Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add paginated requests helper function and use in Search and All #1630

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
helenye-stripe marked this conversation as resolved.
Show resolved Hide resolved
}
}
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);
}
}