-
Notifications
You must be signed in to change notification settings - Fork 436
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 support for SearchResult. #777
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from __future__ import absolute_import, division, print_function | ||
|
||
from stripe import api_requestor, util | ||
from stripe.api_resources.abstract.api_resource import APIResource | ||
|
||
|
||
class SearchableAPIResource(APIResource): | ||
@classmethod | ||
def _search( | ||
cls, | ||
search_url, | ||
api_key=None, | ||
stripe_version=None, | ||
stripe_account=None, | ||
**params | ||
): | ||
requestor = api_requestor.APIRequestor( | ||
api_key, | ||
api_base=cls.api_base(), | ||
api_version=stripe_version, | ||
account=stripe_account, | ||
) | ||
response, api_key = requestor.request("get", search_url, params) | ||
stripe_object = util.convert_to_stripe_object( | ||
response, api_key, stripe_version, stripe_account | ||
) | ||
stripe_object._retrieve_params = params | ||
return stripe_object |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
from __future__ import absolute_import, division, print_function | ||
|
||
from stripe import api_requestor, six, util | ||
from stripe.stripe_object import StripeObject | ||
|
||
|
||
class SearchResultObject(StripeObject): | ||
OBJECT_NAME = "search_result" | ||
|
||
def search( | ||
self, api_key=None, stripe_version=None, stripe_account=None, **params | ||
): | ||
stripe_object = self._request( | ||
"get", | ||
self.get("url"), | ||
api_key=api_key, | ||
stripe_version=stripe_version, | ||
stripe_account=stripe_account, | ||
**params | ||
) | ||
stripe_object._retrieve_params = params | ||
return stripe_object | ||
|
||
def _request( | ||
self, | ||
method_, | ||
url_, | ||
api_key=None, | ||
idempotency_key=None, | ||
stripe_version=None, | ||
stripe_account=None, | ||
**params | ||
): | ||
api_key = api_key or self.api_key | ||
stripe_version = stripe_version or self.stripe_version | ||
stripe_account = stripe_account or self.stripe_account | ||
|
||
requestor = api_requestor.APIRequestor( | ||
api_key, api_version=stripe_version, account=stripe_account | ||
) | ||
headers = util.populate_headers(idempotency_key) | ||
response, api_key = requestor.request(method_, url_, params, headers) | ||
stripe_object = util.convert_to_stripe_object( | ||
response, api_key, stripe_version, stripe_account | ||
) | ||
return stripe_object | ||
|
||
def __getitem__(self, k): | ||
if isinstance(k, six.string_types): | ||
return super(SearchResultObject, self).__getitem__(k) | ||
else: | ||
raise KeyError( | ||
"You tried to access the %s index, but SearchResultObject types " | ||
"only support string keys. (HINT: Search calls return an object " | ||
"with a 'data' (which is the data array). You likely want to " | ||
"call .data[%s])" % (repr(k), repr(k)) | ||
) | ||
|
||
def __iter__(self): | ||
return getattr(self, "data", []).__iter__() | ||
|
||
def __len__(self): | ||
return getattr(self, "data", []).__len__() | ||
|
||
def auto_paging_iter(self): | ||
page = self | ||
|
||
while True: | ||
for item in page: | ||
yield item | ||
page = page.next_search_result_page() | ||
|
||
if page.is_empty: | ||
break | ||
|
||
@classmethod | ||
def empty_search_result( | ||
cls, api_key=None, stripe_version=None, stripe_account=None | ||
): | ||
return cls.construct_from( | ||
{"data": [], "has_more": False, "next_page": None}, | ||
key=api_key, | ||
stripe_version=stripe_version, | ||
stripe_account=stripe_account, | ||
last_response=None, | ||
) | ||
|
||
@property | ||
def is_empty(self): | ||
return not self.data | ||
|
||
def next_search_result_page( | ||
self, api_key=None, stripe_version=None, stripe_account=None, **params | ||
): | ||
if not self.has_more: | ||
return self.empty_search_result( | ||
api_key=api_key, | ||
stripe_version=stripe_version, | ||
stripe_account=stripe_account, | ||
) | ||
|
||
params_with_filters = self._retrieve_params.copy() | ||
params_with_filters.update({"page": self.next_page}) | ||
params_with_filters.update(params) | ||
|
||
return self.search( | ||
api_key=api_key, | ||
stripe_version=stripe_version, | ||
stripe_account=stripe_account, | ||
**params_with_filters | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
tests/api_resources/abstract/test_searchable_api_resource.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from __future__ import absolute_import, division, print_function | ||
|
||
import stripe | ||
|
||
|
||
class TestSearchableAPIResource(object): | ||
class MySearchable(stripe.api_resources.abstract.SearchableAPIResource): | ||
OBJECT_NAME = "mysearchable" | ||
|
||
@classmethod | ||
def search(cls, *args, **kwargs): | ||
return cls._search( | ||
search_url="/v1/mysearchables/search", *args, **kwargs | ||
) | ||
|
||
def test_search(self, request_mock): | ||
request_mock.stub_request( | ||
"get", | ||
"/v1/mysearchables/search", | ||
{ | ||
"object": "list", | ||
"data": [ | ||
{"object": "charge", "name": "jose"}, | ||
{"object": "charge", "name": "curly"}, | ||
], | ||
"url": "/v1/charges", | ||
"has_more": False, | ||
}, | ||
rheaders={"request-id": "req_id"}, | ||
) | ||
|
||
res = self.MySearchable.search(query='currency:"CAD"') | ||
request_mock.assert_requested("get", "/v1/mysearchables/search", {}) | ||
assert len(res.data) == 2 | ||
assert all(isinstance(obj, stripe.Charge) for obj in res.data) | ||
assert res.data[0].name == "jose" | ||
assert res.data[1].name == "curly" | ||
|
||
assert res.last_response is not None | ||
assert res.last_response.request_id == "req_id" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to add a second page here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!