From 25849b694f606ff8014b93ef97affcb6c288b8df Mon Sep 17 00:00:00 2001 From: Mohamed Magdy Date: Thu, 16 May 2019 17:38:59 +0200 Subject: [PATCH] Add "Automated Exports" API calls (#288) Add "Automated Exports" API calls --- recurly/__init__.py | 41 +++++++++++++++++ .../export-date-file-download-information.xml | 16 +++++++ .../export-date-files-list.xml | 18 ++++++++ tests/fixtures/export-date/export-date.xml | 18 ++++++++ tests/test_resources.py | 44 ++++++++++++++++--- 5 files changed, 130 insertions(+), 7 deletions(-) create mode 100644 tests/fixtures/export-date-files/export-date-file-download-information.xml create mode 100644 tests/fixtures/export-date-files/export-date-files-list.xml create mode 100644 tests/fixtures/export-date/export-date.xml diff --git a/recurly/__init__.py b/recurly/__init__.py index 77bfd064..38760f36 100644 --- a/recurly/__init__.py +++ b/recurly/__init__.py @@ -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()) diff --git a/tests/fixtures/export-date-files/export-date-file-download-information.xml b/tests/fixtures/export-date-files/export-date-file-download-information.xml new file mode 100644 index 00000000..ccabeeb3 --- /dev/null +++ b/tests/fixtures/export-date-files/export-date-file-download-information.xml @@ -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 + + + + 2019-05-09T14:00:00Z + https://api.recurly.com/download + \ No newline at end of file diff --git a/tests/fixtures/export-date-files/export-date-files-list.xml b/tests/fixtures/export-date-files/export-date-files-list.xml new file mode 100644 index 00000000..0a570c56 --- /dev/null +++ b/tests/fixtures/export-date-files/export-date-files-list.xml @@ -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 + + + + + churned_subscriptions_v2_expires.csv.gz + 75e1e5b77b8e667cc88e8a9992e88f3a + + \ No newline at end of file diff --git a/tests/fixtures/export-date/export-date.xml b/tests/fixtures/export-date/export-date.xml new file mode 100644 index 00000000..7bf8df00 --- /dev/null +++ b/tests/fixtures/export-date/export-date.xml @@ -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 + + + + + 2019-05-09 + + + \ No newline at end of file diff --git a/tests/test_resources.py b/tests/test_resources.py index 0b1ee8fd..0766758f 100644 --- a/tests/test_resources.py +++ b/tests/test_resources.py @@ -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' @@ -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()