-
Notifications
You must be signed in to change notification settings - Fork 432
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e7e2eba
commit ce5d34a
Showing
5 changed files
with
204 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
from __future__ import absolute_import, division, print_function | ||
|
||
import stripe | ||
from stripe import api_requestor | ||
from stripe import util | ||
from stripe.api_resources.abstract import CreateableAPIResource | ||
from stripe.api_resources.abstract import ListableAPIResource | ||
from stripe.api_resources.abstract import UpdateableAPIResource | ||
from stripe.api_resources.abstract import custom_method | ||
from stripe.six.moves.urllib.parse import quote_plus | ||
|
||
|
||
@custom_method("accept", http_verb="post") | ||
@custom_method("cancel", http_verb="post") | ||
@custom_method("finalize_quote", http_verb="post", http_path="finalize") | ||
@custom_method("list_line_items", http_verb="get", http_path="line_items") | ||
class Quote(CreateableAPIResource, ListableAPIResource, UpdateableAPIResource): | ||
OBJECT_NAME = "quote" | ||
|
||
def accept(self, idempotency_key=None, **params): | ||
url = self.instance_url() + "/accept" | ||
headers = util.populate_headers(idempotency_key) | ||
self.refresh_from(self.request("post", url, params, headers)) | ||
return self | ||
|
||
def cancel(self, idempotency_key=None, **params): | ||
url = self.instance_url() + "/cancel" | ||
headers = util.populate_headers(idempotency_key) | ||
self.refresh_from(self.request("post", url, params, headers)) | ||
return self | ||
|
||
def finalize_quote(self, idempotency_key=None, **params): | ||
url = self.instance_url() + "/finalize" | ||
headers = util.populate_headers(idempotency_key) | ||
self.refresh_from(self.request("post", url, params, headers)) | ||
return self | ||
|
||
def list_line_items(self, idempotency_key=None, **params): | ||
url = self.instance_url() + "/line_items" | ||
headers = util.populate_headers(idempotency_key) | ||
self.refresh_from(self.request("get", url, params, headers)) | ||
return self | ||
|
||
@classmethod | ||
def _cls_pdf( | ||
cls, | ||
sid, | ||
api_key=None, | ||
idempotency_key=None, | ||
stripe_version=None, | ||
stripe_account=None, | ||
**params | ||
): | ||
url = "%s/%s/%s" % ( | ||
cls.class_url(), | ||
quote_plus(util.utf8(sid)), | ||
"pdf", | ||
) | ||
requestor = api_requestor.APIRequestor( | ||
api_key, | ||
api_base=stripe.upload_api_base, | ||
api_version=stripe_version, | ||
account=stripe_account, | ||
) | ||
headers = util.populate_headers(idempotency_key) | ||
response, _ = requestor.request_stream("get", url, params, headers) | ||
return response | ||
|
||
@util.class_method_variant("_cls_pdf") | ||
def pdf( | ||
self, | ||
api_key=None, | ||
api_version=None, | ||
stripe_version=None, | ||
stripe_account=None, | ||
**params | ||
): | ||
version = api_version or stripe_version | ||
requestor = api_requestor.APIRequestor( | ||
api_key, | ||
api_base=stripe.upload_api_base, | ||
api_version=version, | ||
account=stripe_account, | ||
) | ||
url = self.instance_url() + "/pdf" | ||
return requestor.request_stream("get", url, params=params) |
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,115 @@ | ||
from __future__ import absolute_import, division, print_function | ||
|
||
import stripe | ||
import pytest | ||
|
||
|
||
TEST_RESOURCE_ID = "qt_123" | ||
|
||
|
||
class TestQuote(object): | ||
@pytest.fixture(scope="function") | ||
def setup_upload_api_base(self): | ||
stripe.upload_api_base = stripe.api_base | ||
yield | ||
stripe.api_base = stripe.upload_api_base | ||
stripe.upload_api_base = "https://files.stripe.com" | ||
|
||
def test_is_listable(self, request_mock): | ||
resources = stripe.Quote.list() | ||
request_mock.assert_requested("get", "/v1/quotes") | ||
assert isinstance(resources.data, list) | ||
assert isinstance(resources.data[0], stripe.Quote) | ||
|
||
def test_is_retrievable(self, request_mock): | ||
resource = stripe.Quote.retrieve(TEST_RESOURCE_ID) | ||
request_mock.assert_requested( | ||
"get", "/v1/quotes/%s" % TEST_RESOURCE_ID | ||
) | ||
assert isinstance(resource, stripe.Quote) | ||
|
||
def test_is_creatable(self, request_mock): | ||
resource = stripe.Quote.create(customer="cus_123") | ||
request_mock.assert_requested("post", "/v1/quotes") | ||
assert isinstance(resource, stripe.Quote) | ||
|
||
def test_is_saveable(self, request_mock): | ||
resource = stripe.Quote.retrieve(TEST_RESOURCE_ID) | ||
resource.metadata["key"] = "value" | ||
resource.save() | ||
request_mock.assert_requested( | ||
"post", "/v1/quotes/%s" % TEST_RESOURCE_ID | ||
) | ||
|
||
def test_is_modifiable(self, request_mock): | ||
resource = stripe.Quote.modify( | ||
TEST_RESOURCE_ID, metadata={"key": "value"} | ||
) | ||
request_mock.assert_requested( | ||
"post", "/v1/quotes/%s" % TEST_RESOURCE_ID | ||
) | ||
assert isinstance(resource, stripe.Quote) | ||
|
||
def test_can_finalize_quote(self, request_mock): | ||
resource = stripe.Quote.retrieve(TEST_RESOURCE_ID) | ||
resource = resource.finalize_quote() | ||
request_mock.assert_requested( | ||
"post", "/v1/quotes/%s/finalize" % TEST_RESOURCE_ID | ||
) | ||
assert isinstance(resource, stripe.Quote) | ||
|
||
def test_can_finalize_quote_classmethod(self, request_mock): | ||
resource = stripe.Quote.finalize_quote(TEST_RESOURCE_ID) | ||
request_mock.assert_requested( | ||
"post", "/v1/quotes/%s/finalize" % TEST_RESOURCE_ID | ||
) | ||
assert isinstance(resource, stripe.Quote) | ||
|
||
def test_can_cancel(self, request_mock): | ||
resource = stripe.Quote.retrieve(TEST_RESOURCE_ID) | ||
resource = resource.cancel() | ||
request_mock.assert_requested( | ||
"post", "/v1/quotes/%s/cancel" % TEST_RESOURCE_ID | ||
) | ||
assert isinstance(resource, stripe.Quote) | ||
|
||
def test_can_cancel_classmethod(self, request_mock): | ||
resource = stripe.Quote.cancel(TEST_RESOURCE_ID) | ||
request_mock.assert_requested( | ||
"post", "/v1/quotes/%s/cancel" % TEST_RESOURCE_ID | ||
) | ||
assert isinstance(resource, stripe.Quote) | ||
|
||
def test_can_accept(self, request_mock): | ||
resource = stripe.Quote.retrieve(TEST_RESOURCE_ID) | ||
resource = resource.accept() | ||
request_mock.assert_requested( | ||
"post", "/v1/quotes/%s/accept" % TEST_RESOURCE_ID | ||
) | ||
assert isinstance(resource, stripe.Quote) | ||
|
||
def test_can_accept_classmethod(self, request_mock): | ||
resource = stripe.Quote.accept(TEST_RESOURCE_ID) | ||
request_mock.assert_requested( | ||
"post", "/v1/quotes/%s/accept" % TEST_RESOURCE_ID | ||
) | ||
assert isinstance(resource, stripe.Quote) | ||
|
||
def test_can_pdf(self, setup_upload_api_base, request_mock): | ||
resource = stripe.Quote.retrieve(TEST_RESOURCE_ID) | ||
stream, _ = resource.pdf() | ||
request_mock.assert_api_base(stripe.upload_api_base) | ||
request_mock.assert_requested_stream( | ||
"get", "/v1/quotes/%s/pdf" % TEST_RESOURCE_ID | ||
) | ||
content = stream.io.read() | ||
assert content == b"Stripe binary response" | ||
|
||
def test_can_pdf_classmethod(self, setup_upload_api_base, request_mock): | ||
stream = stripe.Quote.pdf(TEST_RESOURCE_ID) | ||
request_mock.assert_api_base(stripe.upload_api_base) | ||
request_mock.assert_requested_stream( | ||
"get", "/v1/quotes/%s/pdf" % TEST_RESOURCE_ID | ||
) | ||
content = stream.io.read() | ||
assert content == b"Stripe binary response" |
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