|
| 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