Skip to content

Commit ba2ba55

Browse files
committed
Merge branch 'dev' into POP-2279
2 parents 65255fa + 7bd93d9 commit ba2ba55

File tree

7 files changed

+175
-2
lines changed

7 files changed

+175
-2
lines changed

dynatademand/api.py

Lines changed: 52 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',
@@ -253,6 +269,42 @@ def add_line_item(self, project_id, lineitem_data):
253269
)
254270
return response_data
255271

272+
def launch_line_item(self, project_id, line_item_id):
273+
# Starts traffic to a line item.
274+
self.validator.validate_request(
275+
'launch_line_item',
276+
path_data={
277+
'extProjectId': '{}'.format(project_id),
278+
'extLineItemId': '{}'.format(line_item_id),
279+
},
280+
)
281+
response_data = self._api_post('/projects/{}/lineItems/{}/launch'.format(project_id, line_item_id), {})
282+
if response_data.get('status').get('message') != 'success':
283+
raise DemandAPIError(
284+
"Could not close project. Demand API responded with: {}".format(
285+
response_data
286+
)
287+
)
288+
return response_data
289+
290+
def pause_line_item(self, project_id, line_item_id):
291+
# Stops traffic to a line item.
292+
self.validator.validate_request(
293+
'pause_line_item',
294+
path_data={
295+
'extProjectId': '{}'.format(project_id),
296+
'extLineItemId': '{}'.format(line_item_id),
297+
},
298+
)
299+
response_data = self._api_post('/projects/{}/lineItems/{}/pause'.format(project_id, line_item_id), {})
300+
if response_data.get('status').get('message') != 'success':
301+
raise DemandAPIError(
302+
"Could not close project. Demand API responded with: {}".format(
303+
response_data
304+
)
305+
)
306+
return response_data
307+
256308
def get_line_item(self, project_id, line_item_id):
257309
self.validator.validate_request(
258310
'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+
}
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: 3 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,8 @@
1920
'get_line_item': ['path', ],
2021
'get_line_items': ['path', 'query'],
2122
'get_line_item_detailed_report': ['path', ],
23+
'launch_line_item': ['path', ],
24+
'pause_line_item': ['path', ],
2225
'update_line_item': ['path', 'body'],
2326

2427
# Events

tests/test_line_items.py

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,61 @@ def test_add_line_item(self):
7272
self.api.add_line_item(24, new_lineitem_data)
7373
self.assertEqual(len(responses.calls), 1)
7474

75-
# Test error response
7675
with self.assertRaises(DemandAPIError):
7776
self.api.add_line_item(24, new_lineitem_data)
78-
self.assertEqual(len(responses.calls), 2)
77+
self.assertEqual(len(responses.calls), 2)
78+
79+
def test_launch_line_item(self):
80+
# Tests closing a project.
81+
responses.add(
82+
responses.POST,
83+
'{}/sample/v1/projects/24/lineItems/180/launch'.format(BASE_HOST),
84+
json={'status': {'message': 'success'}},
85+
status=200
86+
)
87+
88+
# Response with error status
89+
responses.add(
90+
responses.POST,
91+
'{}/sample/v1/projects/24/lineItems/180/launch'.format(BASE_HOST),
92+
json={'status': {'message': 'error'}},
93+
status=200
94+
)
95+
96+
# Test successful response
97+
self.api.launch_line_item(24, 180)
98+
self.assertEqual(len(responses.calls), 1)
99+
100+
# Test error response
101+
with self.assertRaises(DemandAPIError):
102+
self.api.launch_line_item(24, 180)
103+
self.assertEqual(len(responses.calls), 2)
104+
105+
@responses.activate
106+
def test_pause_line_item(self):
107+
# Tests pausing a line item.
108+
responses.add(
109+
responses.POST,
110+
'{}/sample/v1/projects/24/lineItems/180/pause'.format(BASE_HOST),
111+
json={'status': {'message': 'success'}},
112+
status=200
113+
)
114+
# Response with error
115+
responses.add(
116+
responses.POST,
117+
'{}/sample/v1/projects/24/lineItems/180/pause'.format(BASE_HOST),
118+
json={'status': {'message': 'error'}},
119+
status=200
120+
)
121+
122+
# Test successful response
123+
self.api.pause_line_item(24, 180)
124+
self.assertEqual(len(responses.calls), 1)
125+
126+
# Test error response
127+
with self.assertRaises(DemandAPIError):
128+
self.api.pause_line_item(24, 180)
129+
self.assertEqual(len(responses.calls), 2)
79130

80131
@responses.activate
81132
def test_update_line_item(self):

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)