Skip to content

Commit 78dcd55

Browse files
authored
Merge pull request #10 from payplug/accountingreports
Add AccountingReport object to handle new /accounting_reports endpoint
2 parents 8d83030 + 47bdd8d commit 78dcd55

File tree

7 files changed

+134
-4
lines changed

7 files changed

+134
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
1.3.0
2+
-----
3+
- Add AccountingReport class to handle the new /accounting\_reports API endpoint
4+
15
1.2.2
26
-----
37
- Add API version setting

payplug/__init__.py

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# -*- coding: utf-8 -*-
2-
from datetime import datetime
32
from six import string_types
4-
from payplug import config, exceptions, network, notifications, resources, routes
5-
from payplug.network import HttpClient, UrllibRequest
3+
from payplug import config, exceptions, resources, routes
4+
from payplug.network import HttpClient
65
from payplug.__version__ import __version__
76

87

@@ -29,6 +28,7 @@ def set_secret_key(token):
2928

3029
config.secret_key = token
3130

31+
3232
def set_api_version(version):
3333
"""
3434
Specify the PayPlug API version to use.
@@ -363,3 +363,37 @@ def list(customer, per_page=None, page=None):
363363
http_client = HttpClient()
364364
response, _ = http_client.get(routes.url(routes.CARD_RESOURCE, customer_id=customer, pagination=pagination))
365365
return resources.APIResourceCollection(resources.Card, **response)
366+
367+
368+
class AccountingReport:
369+
"""
370+
A DAO for resources.AccountingReport which provides a way to query accounting reports.
371+
"""
372+
@staticmethod
373+
def retrieve(report_id):
374+
"""
375+
Retrieve an accounting report from its id.
376+
377+
:param report_id: The report id
378+
:type report_id: string
379+
380+
:return: The accounting report resource
381+
:rtype: resources.AccountingReport
382+
"""
383+
http_client = HttpClient()
384+
response, __ = http_client.get(routes.url(routes.ACCOUNTING_REPORT_RESOURCE, resource_id=report_id))
385+
return resources.AccountingReport(**response)
386+
387+
@staticmethod
388+
def create(**data):
389+
"""
390+
Create an accounting report.
391+
392+
:param data: data required to create the report
393+
394+
:return: The accounting report resource
395+
:rtype resources.AccountingReport
396+
"""
397+
http_client = HttpClient()
398+
response, _ = http_client.post(routes.url(routes.ACCOUNTING_REPORT_RESOURCE), data)
399+
return resources.AccountingReport(**response)

payplug/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# -*- coding: utf-8 -*-
2-
__version__ = '1.2.2'
2+
__version__ = '1.3.0'

payplug/resources.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,3 +336,21 @@ def next(self):
336336

337337
def __getitem__(self, item):
338338
return self.data[item]
339+
340+
341+
class AccountingReport(APIResource, VerifiableAPIResource, ReconstituableAPIResource):
342+
"""
343+
An accounting report.
344+
"""
345+
object_type = 'accounting_report'
346+
347+
def get_consistent_resource(self):
348+
"""
349+
:return an accounting report that you can trust.
350+
:rtype AccountingReport
351+
"""
352+
http_client = HttpClient()
353+
response, _ = http_client.get(
354+
routes.url(routes.ACCOUNTING_REPORT_RESOURCE, resource_id=self.id)
355+
)
356+
return AccountingReport(**response)

payplug/routes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
REFUND_RESOURCE = PAYMENT_RESOURCE + '/{payment_id}/refunds'
77
CUSTOMER_RESOURCE = '/customers'
88
CARD_RESOURCE = CUSTOMER_RESOURCE + '/{customer_id}/cards'
9+
ACCOUNTING_REPORT_RESOURCE = '/accounting_reports'
910

1011
# API base url
1112
API_BASE_URL = 'https://api.payplug.com'
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# -*- coding: utf-8 -*-
2+
from mock import patch
3+
import payplug
4+
from payplug import resources
5+
from payplug.test import TestBase
6+
7+
8+
@patch('payplug.config.secret_key', 'a_secret_key')
9+
@patch.object(payplug.HttpClient, 'post', lambda *args, **kwargs: ({'id': 'accounting_report_id'}, 201))
10+
@patch.object(payplug.HttpClient, 'get', lambda *args, **kwargs: ({'id': 'accounting_report_id'}, 200))
11+
class TestAccountingReportCreateRetrieve(TestBase):
12+
def test_retrieve(self):
13+
report = payplug.AccountingReport.retrieve('accounting_report_id')
14+
15+
assert isinstance(report, resources.AccountingReport)
16+
assert report.id == 'accounting_report_id'
17+
18+
def test_create(self):
19+
report = payplug.AccountingReport.create(some='report', da='ta')
20+
21+
assert isinstance(report, resources.AccountingReport)
22+
assert report.id == 'accounting_report_id'
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# -*- coding: utf-8 -*-
2+
from mock import patch
3+
import payplug
4+
from payplug.resources import AccountingReport
5+
from payplug.test import TestBase
6+
7+
8+
@patch('payplug.config.secret_key', 'a_secret_key')
9+
class TestAccountingReportResource(TestBase):
10+
def test_initialize_accounting_report(self):
11+
report_attributes = {
12+
'start_date': '2020-01-01',
13+
'object': 'accounting_report',
14+
'notification_url': 'notification_url',
15+
'end_date': '2020-04-30',
16+
'id': 'ar_1GKEACvltTVXT5muBd3AQv',
17+
'file_available_until': 1588083743,
18+
'temporary_url': 'temporary_url',
19+
'is_live': True
20+
}
21+
22+
report_object = AccountingReport(**report_attributes)
23+
24+
assert report_object.id == 'ar_1GKEACvltTVXT5muBd3AQv'
25+
assert report_object.start_date == '2020-01-01'
26+
assert report_object.end_date == '2020-04-30'
27+
assert report_object.notification_url == 'notification_url'
28+
assert report_object.file_available_until == 1588083743
29+
assert report_object.temporary_url == 'temporary_url'
30+
assert report_object.is_live == True
31+
32+
33+
def report_fixture():
34+
return {
35+
"id": "ar_1GKEACvltTVXT5muBd3AQv",
36+
"object": "accounting_report",
37+
}
38+
39+
40+
@patch('payplug.config.secret_key', 'a_secret_key')
41+
@patch.object(payplug.HttpClient, 'get', lambda *args, **kwargs: (report_fixture(), 200))
42+
class TestConsistentAccountingReport(TestBase):
43+
@patch('payplug.resources.routes.url')
44+
def test_get_consistent_resource(self, routes_url_mock):
45+
unsafe_report = AccountingReport(id='ar_1GKEACvltTVXT5muBd3AQv_unsafe',
46+
object='accounting_report')
47+
safe_report = unsafe_report.get_consistent_resource()
48+
49+
assert isinstance(safe_report, AccountingReport)
50+
assert routes_url_mock.call_args[1]['resource_id'] == 'ar_1GKEACvltTVXT5muBd3AQv_unsafe'
51+
assert safe_report.id == 'ar_1GKEACvltTVXT5muBd3AQv'

0 commit comments

Comments
 (0)