Skip to content

Commit

Permalink
Add "Automated Exports" API calls (#288)
Browse files Browse the repository at this point in the history
Add "Automated Exports" API calls
  • Loading branch information
mohamagdy authored and Aaron Suarez committed May 16, 2019
1 parent fee2afd commit 25849b6
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 7 deletions.
41 changes: 41 additions & 0 deletions recurly/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1504,6 +1504,47 @@ class CreditPayment(Resource):
'voided_at',
)


class ExportDate(Resource):
nodename = 'export_date'
collection_path = 'export_dates'

attributes = (
'date',
'export_files'
)

def files(self, date):
"""
Fetch files for a given date.
:param date: The date to fetch the export files for
:return: A list of exported files for that given date or an empty list if not file exists for that date
"""
url = urljoin(recurly.base_uri() + self.collection_path, '/%s/export_files' % date)
return ExportDateFile.paginated(url)


class ExportDateFile(Resource):
nodename = 'export_file'
collection_path = 'export_files'

attributes = (
'name',
'md5sum',
'expires_at',
'download_url'
)

def download_information(self):
"""
Download an export file
:return: The download information of the file that includes the download URL as well as the expiry time of the
download URL
"""
_response, element = ExportDateFile.element_for_url(self._url)
return ExportDateFile.from_element(element)


Resource._learn_nodenames(locals().values())


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
GET https://api.recurly.com/v2/export_dates/2019-05-09/export_files/churned_subscriptions_v2_expires.csv.gz HTTP/1.1
X-Api-Version: {api-version}
Accept: application/xml
Authorization: Basic YXBpa2V5Og==
User-Agent: {user-agent}


HTTP/1.1 200 OK
X-Records: 1
Content-Type: application/xml; charset=utf-8

<?xml version="1.0" encoding="UTF-8"?>
<export_file href="https://api.recurly.com/v2/export_dates/2019-05-09/export_files/churned_subscriptions_v2_expires.csv.gz">
<expires_at type="datetime">2019-05-09T14:00:00Z</expires_at>
<download_url>https://api.recurly.com/download</download_url>
</export_file>
18 changes: 18 additions & 0 deletions tests/fixtures/export-date-files/export-date-files-list.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
GET https://api.recurly.com/v2/export_dates/2019-05-09/export_files HTTP/1.1
X-Api-Version: {api-version}
Accept: application/xml
Authorization: Basic YXBpa2V5Og==
User-Agent: {user-agent}


HTTP/1.1 200 OK
X-Records: 1
Content-Type: application/xml; charset=utf-8

<?xml version="1.0" encoding="UTF-8"?>
<export_files type="array" href="https://api.recurly.com/v2/export_dates/2019-05-09/export_files">
<export_file href="https://api.recurly.com/v2/export_dates/2019-05-09/export_files/churned_subscriptions_v2_expires.csv.gz">
<name>churned_subscriptions_v2_expires.csv.gz</name>
<md5sum>75e1e5b77b8e667cc88e8a9992e88f3a</md5sum>
</export_file>
</export_files>
18 changes: 18 additions & 0 deletions tests/fixtures/export-date/export-date.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
GET https://api.recurly.com/v2/export_dates HTTP/1.1
X-Api-Version: {api-version}
Accept: application/xml
Authorization: Basic YXBpa2V5Og==
User-Agent: {user-agent}


HTTP/1.1 200 OK
X-Records: 1
Content-Type: application/xml; charset=utf-8

<?xml version="1.0" encoding="UTF-8"?>
<export_dates type="array">
<export_date>
<date>2019-05-09</date>
<export_files href="https://api.recurly.com/v2/export_dates/2019-05-09/export_files"/>
</export_date>
</export_dates>
44 changes: 37 additions & 7 deletions tests/test_resources.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import collections
from recurly import recurly_logging as logging
import time
from datetime import datetime

import six
import recurly

from six import StringIO
from six.moves import urllib, http_client
from six.moves.urllib.parse import urljoin

from recurly import Account, AddOn, Address, Adjustment, BillingInfo, Coupon, Plan, Redemption, Subscription, SubscriptionAddOn, Transaction, MeasuredUnit, Usage, GiftCard, Delivery, ShippingAddress, AccountAcquisition, Purchase, Invoice, InvoiceCollection, CreditPayment, CustomField
import recurly
from recurly import Account, AddOn, Address, Adjustment, BillingInfo, Coupon, Plan, Redemption, Subscription, \
SubscriptionAddOn, Transaction, MeasuredUnit, Usage, GiftCard, Delivery, ShippingAddress, AccountAcquisition, \
Purchase, Invoice, InvoiceCollection, CreditPayment, CustomField, ExportDate, ExportDateFile
from recurly import Money, NotFoundError, ValidationError, BadRequestError, PageError
from recurlytests import RecurlyTest, xml
from recurly import recurly_logging as logging
from recurlytests import RecurlyTest

recurly.SUBDOMAIN = 'api'

Expand Down Expand Up @@ -1821,6 +1820,37 @@ def test_gift_cards_redeem_with_url(self):

self.assertTrue(gift_card.redeemed_at is not None)

def test_export_date(self):
with self.mock_request('export-date/export-date.xml'):
export_dates = ExportDate.all()

self.assertEqual(len(export_dates), 1)
self.assertEqual(export_dates[0].date, "2019-05-09")

def test_export_date_files(self):
export_date = ExportDate()

with self.mock_request('export-date-files/export-date-files-list.xml'):
export_date_files = export_date.files("2019-05-09")

self.assertEqual(len(export_date_files), 1)
self.assertEqual(export_date_files[0].name, "churned_subscriptions_v2_expires.csv.gz")

def test_export_date_files_download_information(self):
export_date = ExportDate()

with self.mock_request('export-date-files/export-date-files-list.xml'):
export_date_files = export_date.files("2019-05-09")

with self.mock_request('export-date-files/export-date-file-download-information.xml'):
export_date_file_download_information = export_date_files[0].download_information()

self.assertEqual(
export_date_file_download_information.expires_at.strftime("%Y-%m-%d %H:%M:%S"), "2019-05-09 14:00:00"
)
self.assertEqual(export_date_file_download_information.download_url, "https://api.recurly.com/download")


if __name__ == '__main__':
import unittest
unittest.main()

0 comments on commit 25849b6

Please sign in to comment.