Skip to content

Commit

Permalink
Merge pull request #233 from recurly/api_version_2_11
Browse files Browse the repository at this point in the history
API v2.11 changes
  • Loading branch information
bhelx authored Apr 5, 2018
2 parents 3c11d99 + 9f12435 commit 1ec65cc
Show file tree
Hide file tree
Showing 8 changed files with 235 additions and 2 deletions.
33 changes: 31 additions & 2 deletions recurly/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
API_KEY = None
"""The API key to use when authenticating API requests."""

API_VERSION = '2.10'
API_VERSION = '2.11'
"""The API version to use when making API requests."""

CA_CERTS_FILE = None
Expand Down Expand Up @@ -147,7 +147,9 @@ class Account(Resource):
'has_active_subscription',
'has_future_subscription',
'has_canceled_subscription',
'has_paused_subscription',
'has_past_due_invoice',
'preferred_locale',
)

_classes_for_nodename = { 'address': Address }
Expand Down Expand Up @@ -647,9 +649,12 @@ class Adjustment(Resource):
'updated_at',
'type',
'revenue_schedule_type',
'shipping_address',
'shipping_address_id',
)
xml_attribute_attributes = ('type',)
_classes_for_nodename = {'tax_detail': TaxDetail,}
_classes_for_nodename = {'tax_detail': TaxDetail, 'shipping_address':
ShippingAddress}

# This can be removed when the `original_adjustment_uuid` is moved to a link
def __getattr__(self, name):
Expand Down Expand Up @@ -866,6 +871,7 @@ class Purchase(Resource):
'customer_notes',
'terms_and_conditions',
'vat_reverse_charge_notes',
'shipping_address_id',
)

def invoice(self):
Expand Down Expand Up @@ -983,6 +989,8 @@ class Subscription(Resource):
'converted_at',
'no_billing_info_reason',
'imported_trial',
'remaining_pause_cycles',
'paused_at',
)
sensitive_attributes = ('number', 'verification_value', 'bulk')

Expand All @@ -1001,6 +1009,27 @@ def update_notes(self, **kwargs):
url = urljoin(self._url, '%s/notes' % self.uuid)
self.put(url)

def pause(self, remaining_pause_cycles):
"""Pause a subscription"""
url = urljoin(self._url, '%s/pause' % self.uuid)
elem = ElementTree.Element(self.nodename)
elem.append(Resource.element_for_value('remaining_pause_cycles',
remaining_pause_cycles))
body = ElementTree.tostring(elem, encoding='UTF-8')

response = self.http_request(url, 'PUT', body, { 'Content-Type':
'application/xml; charset=utf-8' })

if response.status not in (200, 201, 204):
self.raise_http_error(response)

self.update_from_element(ElementTree.fromstring(response.read()))

def resume(self):
"""Resume a subscription"""
url = urljoin(self._url, '%s/resume' % self.uuid)
self.put(url)

def _update(self):
if not hasattr(self, 'timeframe'):
self.timeframe = 'now'
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/account/created.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Content-Type: application/xml; charset=utf-8
<account>
<account_code>testmock</account_code>
<vat_number>444444-UK</vat_number>
<preferred_locale>en-US</preferred_locale>
</account>

HTTP/1.1 201 Created
Expand Down Expand Up @@ -41,4 +42,5 @@ Location: https://api.recurly.com/v2/accounts/testmock
</address>
<accept_language nil="nil"></accept_language>
<vat_location_enabled type="boolean">true</vat_location_enabled>
<preferred_locale>en-US</preferred_locale>
</account>
12 changes: 12 additions & 0 deletions tests/fixtures/purchase/authorized.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ Content-Type: application/xml; charset=utf-8
<account>
<account_code>testmock</account_code>
<email>[email protected]</email>
<shipping_addresses>
<shipping_address>
<address1>123 Main St</address1>
<city>New Orleans</city>
<country>US</country>
<first_name>Verena</first_name>
<last_name>Example</last_name>
<nickname>Work</nickname>
<state>LA</state>
<zip>70114</zip>
</shipping_address>
</shipping_addresses>
<billing_info>
<first_name>Verena</first_name>
<last_name>Example</last_name>
Expand Down
42 changes: 42 additions & 0 deletions tests/fixtures/purchase/invoiced.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ Content-Type: application/xml; charset=utf-8
<purchase>
<account>
<account_code>testmock</account_code>
<shipping_addresses>
<shipping_address>
<address1>123 Main St</address1>
<city>New Orleans</city>
<country>US</country>
<first_name>Verena</first_name>
<last_name>Example</last_name>
<nickname>Work</nickname>
<state>LA</state>
<zip>70114</zip>
</shipping_address>
</shipping_addresses>
<billing_info>
<first_name>Verena</first_name>
<last_name>Example</last_name>
Expand Down Expand Up @@ -105,6 +117,16 @@ Location: https://api.recurly.com/v2/invoices/1021
<created_at type="datetime">2017-10-06T21:25:56Z</created_at>
<updated_at type="datetime">2017-10-06T21:25:56Z</updated_at>
<revenue_schedule_type>evenly</revenue_schedule_type>
<shipping_address>
<address1>123 Main St</address1>
<address2 nil="nil"></address2>
<city>New Orleans</city>
<state>LA</state>
<zip>70114</zip>
<country>US</country>
<nickname>Work</nickname>
<phone nil="nil"></phone>
</shipping_address>
</adjustment>
<adjustment href="https://api.recurly.com/v2/adjustments/40625fda8b836c4ff51e764ef795fb87" type="charge">
<account href="https://api.recurly.com/v2/accounts/03f2dad7-e0d3-4856-acbb-fa7a7ce26683"/>
Expand All @@ -127,6 +149,16 @@ Location: https://api.recurly.com/v2/invoices/1021
<created_at type="datetime">2017-10-06T21:25:56Z</created_at>
<updated_at type="datetime">2017-10-06T21:25:56Z</updated_at>
<revenue_schedule_type></revenue_schedule_type>
<shipping_address>
<address1>123 Main St</address1>
<address2 nil="nil"></address2>
<city>New Orleans</city>
<state>LA</state>
<zip>70114</zip>
<country>US</country>
<nickname>Work</nickname>
<phone nil="nil"></phone>
</shipping_address>
</adjustment>
<adjustment href="https://api.recurly.com/v2/adjustments/40625fda8ea4dbb77b2b8d4a5d8a9ac2" type="charge">
<account href="https://api.recurly.com/v2/accounts/03f2dad7-e0d3-4856-acbb-fa7a7ce26683"/>
Expand All @@ -149,6 +181,16 @@ Location: https://api.recurly.com/v2/invoices/1021
<created_at type="datetime">2017-10-06T21:25:56Z</created_at>
<updated_at type="datetime">2017-10-06T21:25:56Z</updated_at>
<revenue_schedule_type></revenue_schedule_type>
<shipping_address>
<address1>123 Main St</address1>
<address2 nil="nil"></address2>
<city>New Orleans</city>
<state>LA</state>
<zip>70114</zip>
<country>US</country>
<nickname>Work</nickname>
<phone nil="nil"></phone>
</shipping_address>
</adjustment>
</line_items>
<transactions type="array">
Expand Down
12 changes: 12 additions & 0 deletions tests/fixtures/purchase/previewed.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ Content-Type: application/xml; charset=utf-8
<purchase>
<account>
<account_code>testmock</account_code>
<shipping_addresses>
<shipping_address>
<address1>123 Main St</address1>
<city>New Orleans</city>
<country>US</country>
<first_name>Verena</first_name>
<last_name>Example</last_name>
<nickname>Work</nickname>
<state>LA</state>
<zip>70114</zip>
</shipping_address>
</shipping_addresses>
<billing_info>
<first_name>Verena</first_name>
<last_name>Example</last_name>
Expand Down
55 changes: 55 additions & 0 deletions tests/fixtures/subscription/pause.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
PUT https://api.recurly.com/v2/subscriptions/123456789012345678901234567890ab/pause HTTP/1.1
X-Api-Version: {api-version}
Accept: application/xml
Authorization: Basic YXBpa2V5Og==
User-Agent: {user-agent}
Content-Type: application/xml; charset=utf-8

<?xml version="1.0" encoding="UTF-8"?>
<subscription>
<remaining_pause_cycles type="integer">1</remaining_pause_cycles>
</subscription>

HTTP/1.1 200 OK
Content-Type: application/xml; charset=utf-8
Location: https://api.recurly.com/v2/subscriptions/123456789012345678901234567890ab

<?xml version="1.0" encoding="UTF-8"?>
<subscription
href="https://api.recurly.com/v2/subscriptions/123456789012345678901234567890ab">
<redemptions href="https://api.recurly.com/v2/subscriptions/123456789012345678901234567890ab/redemptions" />
<uuid>123456789012345678901234567890ab</uuid>
<account href="https://api.recurly.com/v2/accounts/subscribemock"/>
<plan href="https://api.recurly.com/v2/plans/basicplan">
<plan_code>basicplan</plan_code>
<name>Basic Plan</name>
</plan>
<state>active</state>
<quantity type="integer">1</quantity>
<currency>EUR</currency>
<unit_amount_in_cents type="integer">1000</unit_amount_in_cents>
<activated_at type="datetime">2011-05-27T07:00:00Z</activated_at>
<canceled_at nil="nil"></canceled_at>
<expires_at nil="nil"></expires_at>
<current_period_started_at type="datetime">2011-06-27T07:00:00Z</current_period_started_at>
<current_period_ends_at type="datetime">2010-07-27T07:00:00Z</current_period_ends_at>
<trial_started_at nil="nil"></trial_started_at>
<trial_ends_at nil="nil"></trial_ends_at>
<tax_in_cents type="integer">0</tax_in_cents>
<tax_type>usst</tax_type>
<no_billing_info_reason>plan_free_trial</no_billing_info_reason>
<paused_at type="datetime">2019-07-27T07:00:00Z</paused_at>
<remaining_pause_cycles type="integer">1</remaining_pause_cycles>>
<subscription_add_ons type="array">
<subscription_add_on>
<add_on_type>usage</add_on_type>
<measured_unit href="https://api.recurly.com/v2/measured_units/123456"/>
<usage href="https://api.recurly.com/v2/subscriptions/123456789012345678901234567890ab/add_ons/marketing_emails/usage"/>
<add_on_code>marketing_emails</add_on_code>
<unit_amount_in_cents type="integer">5</unit_amount_in_cents>
<quantity type="integer">1</quantity>
<usage_type>price</usage_type>
<usage_percentage nil="nil"/>
</subscription_add_on>
</subscription_add_ons>
</subscription>
52 changes: 52 additions & 0 deletions tests/fixtures/subscription/resume.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
PUT https://api.recurly.com/v2/subscriptions/123456789012345678901234567890ab/resume HTTP/1.1
X-Api-Version: {api-version}
Accept: application/xml
Authorization: Basic YXBpa2V5Og==
User-Agent: {user-agent}
Content-Type: application/xml; charset=utf-8

<subscription />

HTTP/1.1 200 OK
Content-Type: application/xml; charset=utf-8
Location: https://api.recurly.com/v2/subscriptions/123456789012345678901234567890ab

<?xml version="1.0" encoding="UTF-8"?>
<subscription
href="https://api.recurly.com/v2/subscriptions/123456789012345678901234567890ab">
<redemptions href="https://api.recurly.com/v2/subscriptions/123456789012345678901234567890ab/redemptions" />
<uuid>123456789012345678901234567890ab</uuid>
<account href="https://api.recurly.com/v2/accounts/subscribemock"/>
<plan href="https://api.recurly.com/v2/plans/basicplan">
<plan_code>basicplan</plan_code>
<name>Basic Plan</name>
</plan>
<state>active</state>
<quantity type="integer">1</quantity>
<currency>EUR</currency>
<unit_amount_in_cents type="integer">1000</unit_amount_in_cents>
<activated_at type="datetime">2011-05-27T07:00:00Z</activated_at>
<canceled_at nil="nil"></canceled_at>
<expires_at nil="nil"></expires_at>
<current_period_started_at type="datetime">2011-06-27T07:00:00Z</current_period_started_at>
<current_period_ends_at type="datetime">2010-07-27T07:00:00Z</current_period_ends_at>
<trial_started_at nil="nil"></trial_started_at>
<trial_ends_at nil="nil"></trial_ends_at>
<tax_in_cents type="integer">0</tax_in_cents>
<tax_type>usst</tax_type>
<no_billing_info_reason>plan_free_trial</no_billing_info_reason>
<paused_at type="datetime">2019-07-27T07:00:00Z</paused_at>
<remaining_pause_cycles type="integer">1</remaining_pause_cycles>>
<subscription_add_ons type="array">
<subscription_add_on>
<add_on_type>usage</add_on_type>
<measured_unit href="https://api.recurly.com/v2/measured_units/123456"/>
<usage href="https://api.recurly.com/v2/subscriptions/123456789012345678901234567890ab/add_ons/marketing_emails/usage"/>
<add_on_code>marketing_emails</add_on_code>
<unit_amount_in_cents type="integer">5</unit_amount_in_cents>
<quantity type="integer">1</quantity>
<usage_type>price</usage_type>
<usage_percentage nil="nil"/>
</subscription_add_on>
</subscription_add_ons>
</subscription>
29 changes: 29 additions & 0 deletions tests/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ def test_purchase(self):
currency = 'USD',
account = Account(
account_code = account_code,
shipping_addresses = [
ShippingAddress(
first_name = 'Verena',
last_name = 'Example',
address1 = '123 Main St',
city = 'New Orleans',
state = 'LA',
zip = '70114',
country = 'US',
nickname = 'Work'
)
],
billing_info = BillingInfo(
first_name = 'Verena',
last_name = 'Example',
Expand Down Expand Up @@ -97,6 +109,8 @@ def test_purchase(self):
self.assertIsInstance(collection.charge_invoice, Invoice)
self.assertIsInstance(collection.credit_invoices, list)
self.assertIsInstance(collection.credit_invoices[0], Invoice)
self.assertIsInstance(collection.charge_invoice.line_items[0].shipping_address,
ShippingAddress)
with self.mock_request('purchase/previewed.xml'):
collection = purchase.preview()
self.assertIsInstance(collection, InvoiceCollection)
Expand All @@ -115,13 +129,15 @@ def test_account(self):

account = Account(account_code=account_code)
account.vat_number = '444444-UK'
account.preferred_locale = 'en-US'
with self.mock_request('account/created.xml'):
account.save()
self.assertEqual(account._url, urljoin(recurly.base_uri(), 'accounts/%s' % account_code))
self.assertEqual(account.vat_number, '444444-UK')
self.assertEqual(account.vat_location_enabled, True)
self.assertEqual(account.cc_emails,
'[email protected],[email protected]')
self.assertEqual(account.preferred_locale, 'en-US')

with self.mock_request('account/list-active.xml'):
active = Account.all_active()
Expand Down Expand Up @@ -1172,6 +1188,19 @@ def test_measured_unit(self):
self.assertEqual(measured_unit.description, 'Unit of Marketing Email')
self.assertEqual(measured_unit.id, 123456)

def test_subscription_pause_resume(self):
with self.mock_request('subscription/show.xml'):
sub = Subscription.get('123456789012345678901234567890ab')

with self.mock_request('subscription/pause.xml'):
sub.pause(1)

self.assertIsInstance(sub.paused_at, datetime)
self.assertEqual(sub.remaining_pause_cycles, 1)

with self.mock_request('subscription/resume.xml'):
sub.resume()

def test_usage(self):
usage = Usage()
usage.amount = 100 # record 100 emails
Expand Down

0 comments on commit 1ec65cc

Please sign in to comment.