Skip to content

Commit 22670a3

Browse files
authored
Merge pull request #26 from dynata/POP-2429
POP-2429 added get invoice pdf endpoint
2 parents 86d8243 + 4033912 commit 22670a3

File tree

5 files changed

+53
-0
lines changed

5 files changed

+53
-0
lines changed

dynatademand/api.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ def _api_get(self, uri, query_params=None):
6868
raise DemandAPIError('Demand API request to {} failed with status {}. Response: {}'.format(
6969
url, response.status_code, response.content
7070
))
71+
if response.headers['content-type'] == 'application/pdf':
72+
return response.content
7173
return response.json()
7274

7375
def authenticate(self):
@@ -187,6 +189,13 @@ def create_project(self, project_data):
187189
)
188190
return response_data
189191

192+
def get_invoice(self, project_id):
193+
self.validator.validate_request(
194+
'get_event',
195+
path_data={'extProjectId': '{}'.format(project_id)},
196+
)
197+
return self._api_get('/projects/{}/invoices'.format(project_id))
198+
190199
def buy_project(self, project_id, buy_data):
191200
'''
192201
Buy the line items for a project, agreeing to the price. A Line Item
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"type": "object",
3+
"properties": {
4+
"extProjectId": {
5+
"type": "string",
6+
"required": true
7+
}
8+
},
9+
"required": [
10+
"extProjectId"
11+
]
12+
}

dynatademand/validator.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
'buy_project': ['path', 'body', ],
1717
'get_project_detailed_report': ['path', ],
1818

19+
# Invoices
20+
'get_invoice': ['path', ],
21+
1922
# Line items
2023
'close_line_item': ['path', ],
2124
'create_line_item': ['path', 'body', ],

tests/test_files/get_invoice.pdf

18.5 KB
Binary file not shown.

tests/test_invoices.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# encoding: utf-8
2+
from __future__ import unicode_literals, print_function
3+
4+
import unittest
5+
import responses
6+
7+
from dynatademand.api import DemandAPIClient
8+
9+
BASE_HOST = "http://test-url.example"
10+
11+
12+
class TestInvoiceEndpoints(unittest.TestCase):
13+
def setUp(self):
14+
self.api = DemandAPIClient(client_id='test', username='testuser', password='testpass', base_host=BASE_HOST)
15+
self.api._access_token = 'Bearer testtoken'
16+
17+
@responses.activate
18+
def test_get_invoice(self):
19+
with open('./tests/test_files/get_invoice.pdf', 'rb') as invoice_file:
20+
responses.add(
21+
responses.GET,
22+
'{}/sample/v1/projects/1337/invoices'.format(BASE_HOST),
23+
body=invoice_file.read(),
24+
content_type='application/pdf',
25+
stream=True,
26+
status=200)
27+
self.api.get_invoice(1337)
28+
self.assertEqual(len(responses.calls), 1)
29+
self.assertEqual(responses.calls[0].response.headers['content-type'], 'application/pdf')

0 commit comments

Comments
 (0)