Skip to content

Commit 8579ee1

Browse files
committed
POP-2430 added close line item function
1 parent 8c09824 commit 8579ee1

File tree

4 files changed

+71
-4
lines changed

4 files changed

+71
-4
lines changed

dynatademand/api.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,24 @@ def add_line_item(self, project_id, lineitem_data):
289289
)
290290
return response_data
291291

292+
def close_line_item(self, project_id, line_item_id):
293+
# Starts traffic to a line item.
294+
self.validator.validate_request(
295+
'close_line_item',
296+
path_data={
297+
'extProjectId': '{}'.format(project_id),
298+
'extLineItemId': '{}'.format(line_item_id),
299+
},
300+
)
301+
response_data = self._api_post('/projects/{}/lineItems/{}/close'.format(project_id, line_item_id), {})
302+
if response_data.get('status').get('message') != 'success':
303+
raise DemandAPIError(
304+
"Could not close line item. Demand API responded with: {}".format(
305+
response_data
306+
)
307+
)
308+
return response_data
309+
292310
def launch_line_item(self, project_id, line_item_id):
293311
# Starts traffic to a line item.
294312
self.validator.validate_request(
@@ -301,7 +319,7 @@ def launch_line_item(self, project_id, line_item_id):
301319
response_data = self._api_post('/projects/{}/lineItems/{}/launch'.format(project_id, line_item_id), {})
302320
if response_data.get('status').get('message') != 'success':
303321
raise DemandAPIError(
304-
"Could not close project. Demand API responded with: {}".format(
322+
"Could not launch line item. Demand API responded with: {}".format(
305323
response_data
306324
)
307325
)
@@ -319,7 +337,7 @@ def pause_line_item(self, project_id, line_item_id):
319337
response_data = self._api_post('/projects/{}/lineItems/{}/pause'.format(project_id, line_item_id), {})
320338
if response_data.get('status').get('message') != 'success':
321339
raise DemandAPIError(
322-
"Could not close project. Demand API responded with: {}".format(
340+
"Could not pause line item. Demand API responded with: {}".format(
323341
response_data
324342
)
325343
)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"type": "object",
3+
"properties": {
4+
"extProjectId": {
5+
"type": "string",
6+
"required": true
7+
},
8+
"extLineItemId": {
9+
"type": "string",
10+
"required": true
11+
}
12+
},
13+
"required": [
14+
"extProjectId",
15+
"extLineItemId"
16+
]
17+
}

dynatademand/validator.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
'get_project_detailed_report': ['path', ],
1818

1919
# Line items
20+
'close_line_item': ['path', ],
2021
'create_line_item': ['path', 'body', ],
2122
'get_line_item': ['path', ],
2223
'get_line_items': ['path', 'query'],

tests/test_line_items.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def setUp(self):
1818

1919
@responses.activate
2020
def test_get_line_item(self):
21+
# Tests getting a line item.
2122
with open('./tests/test_files/get_line_item.json', 'r') as line_item_file:
2223
line_item_json = json.load(line_item_file)
2324
responses.add(
@@ -31,6 +32,7 @@ def test_get_line_item(self):
3132

3233
@responses.activate
3334
def test_get_line_items(self):
35+
# Tests getting all line items for a project.
3436
with open('./tests/test_files/get_line_items.json', 'r') as line_item_file:
3537
line_item_json = json.load(line_item_file)
3638
responses.add(responses.GET, '{}/sample/v1/projects/1/lineItems'.format(BASE_HOST), json=line_item_json, status=200)
@@ -40,6 +42,7 @@ def test_get_line_items(self):
4042

4143
@responses.activate
4244
def test_get_line_item_detailed_report(self):
45+
# Tests getting a line item detailed report.
4346
with open('./tests/test_files/get_line_item_detailed_report.json', 'r') as line_item_detailed_report_file:
4447
line_item_detailed_report_json = json.load(line_item_detailed_report_file)
4548
responses.add(
@@ -53,7 +56,7 @@ def test_get_line_item_detailed_report(self):
5356

5457
@responses.activate
5558
def test_add_line_item(self):
56-
# Tests creating a project. This also tests validating the project data as part of `api.create_project`.
59+
# Tests creating a line item.
5760
with open('./tests/test_files/create_line_item.json', 'r') as new_lineitem_file:
5861
new_lineitem_data = json.load(new_lineitem_file)
5962
# Success response
@@ -76,9 +79,36 @@ def test_add_line_item(self):
7679
self.api.add_line_item(24, new_lineitem_data)
7780
self.assertEqual(len(responses.calls), 2)
7881

82+
@responses.activate
83+
def test_close_line_item(self):
84+
# Tests closing a line item.
85+
responses.add(
86+
responses.POST,
87+
'{}/sample/v1/projects/69/lineItems/1337/close'.format(BASE_HOST),
88+
json={'status': {'message': 'success'}},
89+
status=200
90+
)
91+
92+
# Response with error status
93+
responses.add(
94+
responses.POST,
95+
'{}/sample/v1/projects/69/lineItems/1337/close'.format(BASE_HOST),
96+
json={'status': {'message': 'error'}},
97+
status=200
98+
)
99+
100+
# Test successful response
101+
self.api.close_line_item(69, 1337)
102+
self.assertEqual(len(responses.calls), 1)
103+
104+
# Test error response
105+
with self.assertRaises(DemandAPIError):
106+
self.api.close_line_item(69, 1337)
107+
self.assertEqual(len(responses.calls), 2)
108+
79109
@responses.activate
80110
def test_launch_line_item(self):
81-
# Tests closing a project.
111+
# Tests launching a line item.
82112
responses.add(
83113
responses.POST,
84114
'{}/sample/v1/projects/24/lineItems/180/launch'.format(BASE_HOST),
@@ -131,6 +161,7 @@ def test_pause_line_item(self):
131161

132162
@responses.activate
133163
def test_update_line_item(self):
164+
# Tests updating a line item.
134165
with open('./tests/test_files/update_line_item.json', 'r') as new_lineitem_file:
135166
update_lineitem_data = json.load(new_lineitem_file)
136167

0 commit comments

Comments
 (0)