Skip to content

Commit 63e6693

Browse files
Merge branch 'dev' into feature/launch-line-item
2 parents 33295a5 + afa775b commit 63e6693

File tree

6 files changed

+113
-1
lines changed

6 files changed

+113
-1
lines changed

dynatademand/api.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,22 @@ def create_project(self, project_data):
187187
)
188188
return response_data
189189

190+
def close_project(self, project_id):
191+
# Closes the requested project. Once a project is closed, all traffic
192+
# is stopped, and the project is automatically sent for invoicing.
193+
self.validator.validate_request(
194+
'close_project',
195+
path_data={'extProjectId': '{}'.format(project_id)},
196+
)
197+
response_data = self._api_post('/projects/{}/close'.format(project_id), {})
198+
if response_data.get('status').get('message') != 'success':
199+
raise DemandAPIError(
200+
"Could not close project. Demand API responded with: {}".format(
201+
response_data
202+
)
203+
)
204+
return response_data
205+
190206
def get_project(self, project_id):
191207
self.validator.validate_request(
192208
'get_project',
@@ -244,6 +260,24 @@ def launch_line_item(self, project_id, line_item_id):
244260
)
245261
return response_data
246262

263+
def pause_line_item(self, project_id, line_item_id):
264+
# Stops traffic to a line item.
265+
self.validator.validate_request(
266+
'pause_line_item',
267+
path_data={
268+
'extProjectId': '{}'.format(project_id),
269+
'extLineItemId': '{}'.format(line_item_id),
270+
},
271+
)
272+
response_data = self._api_post('/projects/{}/lineItems/{}/pause'.format(project_id, line_item_id), {})
273+
if response_data.get('status').get('message') != 'success':
274+
raise DemandAPIError(
275+
"Could not close project. Demand API responded with: {}".format(
276+
response_data
277+
)
278+
)
279+
return response_data
280+
247281
def get_line_item(self, project_id, line_item_id):
248282
self.validator.validate_request(
249283
'get_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+
}
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
'create_project': ['body', ],
1212
'get_projects': ['query', ],
1313
'get_project': ['path', ],
14+
'close_project': ['path', ],
1415
'update_project': ['path', 'body', ],
1516
'get_project_detailed_report': ['path', ],
1617

@@ -19,6 +20,7 @@
1920
'get_line_items': ['path', 'query'],
2021
'get_line_item_detailed_report': ['path', ],
2122
'launch_line_item': ['path', ],
23+
'pause_line_item': ['path', ],
2224
'update_line_item': ['path', 'body'],
2325

2426
# Events

tests/test_line_items.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def test_launch_lineitem(self):
6565
responses.add(
6666
responses.POST,
6767
'{}/sample/v1/projects/24/lineItems/180/launch'.format(BASE_HOST),
68-
json={'status': {'message': 'error'}},
68+
json={'status': {'message': 'error'}},
6969
status=200
7070
)
7171

@@ -78,6 +78,32 @@ def test_launch_lineitem(self):
7878
self.api.launch_line_item(24, 180)
7979
self.assertEqual(len(responses.calls), 2)
8080

81+
@responses.activate
82+
def test_pause_line_item(self):
83+
# Tests pausing a line item.
84+
responses.add(
85+
responses.POST,
86+
'{}/sample/v1/projects/24/lineItems/180/pause'.format(BASE_HOST),
87+
json={'status': {'message': 'success'}},
88+
status=200
89+
)
90+
# Response with error
91+
responses.add(
92+
responses.POST,
93+
'{}/sample/v1/projects/24/lineItems/180/pause'.format(BASE_HOST),
94+
json={'status': {'message': 'error'}},
95+
status=200
96+
)
97+
98+
# Test successful response
99+
self.api.pause_line_item(24, 180)
100+
self.assertEqual(len(responses.calls), 1)
101+
102+
# Test error response
103+
with self.assertRaises(DemandAPIError):
104+
self.api.pause_line_item(24, 180)
105+
self.assertEqual(len(responses.calls), 2)
106+
81107
@responses.activate
82108
def test_update_line_item(self):
83109
with open('./tests/test_files/update_line_item.json', 'r') as new_lineitem_file:

tests/test_projects.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,27 @@ def test_create_project(self):
6060
self.api.create_project(new_project_data)
6161
self.assertEqual(len(responses.calls), 1)
6262

63+
@responses.activate
64+
def test_close_project(self):
65+
# Tests closing a project.
66+
responses.add(
67+
responses.POST,
68+
'{}/sample/v1/projects/24/close'.format(BASE_HOST),
69+
json={'status': {'message': 'success'}},
70+
status=200
71+
)
72+
responses.add(
73+
responses.POST,
74+
'{}/sample/v1/projects/24/close'.format(BASE_HOST),
75+
json={'status': {'message': 'error'}},
76+
status=200
77+
)
78+
self.api.close_project(24)
79+
self.assertEqual(len(responses.calls), 1)
80+
with self.assertRaises(DemandAPIError):
81+
self.api.close_project(24)
82+
self.assertEqual(len(responses.calls), 2)
83+
6384
@responses.activate
6485
def test_update_project(self):
6586
# Tests creating a project. This also tests validating the project data as part of `api.create_project`.

0 commit comments

Comments
 (0)