Skip to content

Commit 4e5fb1c

Browse files
committed
Updated pause line item function with validation and fixed test.
1 parent 4e56fa9 commit 4e5fb1c

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

dynatademand/api.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,13 @@ def get_project_detailed_report(self, project_id):
170170

171171
def pause_line_item(self, project_id, line_item_id):
172172
# Stops traffic to a line item.
173+
self.validator.validate_request(
174+
'pause_line_item',
175+
path_data={
176+
'extProjectId': '{}'.format(project_id),
177+
'extLineItemId': '{}'.format(line_item_id),
178+
},
179+
)
173180
response_data = self._api_post('/projects/{}/lineItems/{}/pause'.format(project_id, line_item_id), {})
174181
if response_data.get('status').get('message') != 'success':
175182
raise DemandAPIError(
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: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import json
2+
import jsonschema
3+
4+
ENDPOINTS = {
5+
# Authorization
6+
'obtain_access_token': ['body', ],
7+
'refresh_access_token': ['body', ],
8+
'logout': ['body', ],
9+
10+
# Projects
11+
'create_project': ['body', ],
12+
'get_projects': ['query', ],
13+
'get_project': ['path', ],
14+
'update_project': ['path', 'body', ],
15+
'get_project_detailed_report': ['path', ],
16+
17+
# Line items
18+
'get_line_item': ['path', ],
19+
'get_line_items': ['path', 'query'],
20+
'get_line_item_detailed_report': ['path', ],
21+
'pause_line_item': ['path', ],
22+
'update_line_item': ['path', 'body'],
23+
24+
# Events
25+
'get_events': ['query', ],
26+
'get_event': ['path', ],
27+
28+
# Attributes
29+
'get_attributes': ['path', 'query', ],
30+
31+
# Categories
32+
'get_survey_topics': ['query', ],
33+
34+
# Countries & Languages
35+
'get_countries': ['query', ],
36+
37+
# Pricing & Feasibility
38+
'get_feasibility': ['path', ],
39+
40+
# Supplier Sources
41+
'get_sources': [],
42+
}
43+
44+
45+
class DemandAPIValidator(object):
46+
def __init__(self, ):
47+
self.schemas = {
48+
'path': {},
49+
'query': {},
50+
'body': {},
51+
}
52+
for endpoint_name, schemas in ENDPOINTS.items():
53+
for schema in schemas:
54+
with open('dynatademand/schemas/request/{}/{}.json'.format(schema, endpoint_name), 'r') as schema_file:
55+
self.schemas[schema][endpoint_name] = json.load(schema_file)
56+
57+
def _validate_object(self, schema_type, endpoint_name, data):
58+
jsonschema.validate(schema=self.schemas[schema_type][endpoint_name], instance=data)
59+
60+
def validate_request(self, endpoint_name, path_data={}, query_params={}, request_body={}):
61+
'''
62+
# TODO: None of the path schemas from the documentation are currently valid.
63+
if 'path' in ENDPOINTS[endpoint_name]:
64+
self._validate_object('path', endpoint_name, path_data)
65+
'''
66+
if 'query' in ENDPOINTS[endpoint_name]:
67+
self._validate_object('query', endpoint_name, query_params)
68+
if 'body' in ENDPOINTS[endpoint_name]:
69+
self._validate_object('body', endpoint_name, request_body)

0 commit comments

Comments
 (0)