def dispatch(self, request, *args, **kwargs): """ the first entering point for http call, check method is allowed and cached ApiError and managed by cache manager """ _prepare_request(request, *args, **kwargs) self.request = request self.args = args self.kwargs = kwargs defaultCacheManager.start_session() success_status = 200 suppress_http_code = request.REQUEST.get('suppress_http_code', False) try: self.check_permissions(request, *args, **kwargs) method = request.method.lower() if method == "head" or method == "options": method = "get" crud_method = _CRUD_MAPPING[method] if (self.crud_method_names and crud_method in self.crud_method_names) or \ (self.crud_method_names is None and method in self.http_method_names): handler = getattr(self, crud_method, None) else: handler = None if crud_method == "create": success_status = 201 if not handler: raise RestMethodNotAllowed() response = handler(request, *args, **kwargs) except RestError as e: import traceback traceback.print_exc() response = e.to_response(suppress_http_code=suppress_http_code) except: import traceback traceback.print_exc() logger.error('Internal Server Error: %s', request.path, exc_info=sys.exc_info(), extra={'status_code': 500, 'request': request} ) response = RestRuntimeError().to_response(suppress_http_code=suppress_http_code) if isinstance(response, dict): if suppress_http_code: response['_http_status_code'] = success_status response = http_json_response(response) else: response = http_json_response(response, status=success_status) response["Access-Control-Allow-Origin"] = "*" if self.cache_max_age: patch_cache_control(response, max_age=self.cache_max_age) defaultCacheManager.end_session() return response
def to_response(self, suppress_http_code=False): if suppress_http_code: ret = self.to_dict() ret['_http_status_code'] = self.status return http_json_response(ret) return http_json_response(self.to_dict(), status=self.status)
def read(self, request, api_version): def load_cache(api_version="alpha"): manager = rest_api_manager(api_version) ret = {'title': manager.name, 'description': manager.description, 'apiVersion': manager.api_version, 'swaggerVersion': "1.2", 'basePath': manager.base_url, 'resourcePath': manager.base_url, 'info': manager.info, 'authorizations': swagger_authorizations_data()} apis = [] models = { "Error": { "id": "Error", "required": ['error'], "properties": { "error": { "type": "string" }, "field": { "type": "string" }, "message": { "type": "string" }, "resource": { "type": "string" } } } } for view_cls in manager.api_list: operations = [] global_params = [] path = view_cls.path() if path == "": continue if '{}' in path: path = path.replace('{}', '{pk}') global_params.append( { "paramType": "path", "name": 'pk', "description": 'primary key for object', "dataType": 'integer', "format": 'int64', "required": True, } ) responseMessages = [ { 'code': 404, "message": "not_found", "responseModel": "Error" }, { 'code': 500, "message": "internal_error", "responseModel": "Error" }, { 'code': 409, "message": "method_not_allowed", "responseModel": "Error" }, { 'code': 409, "message": "conflict", "responseModel": "Error" }, { 'code': 403, "message": "forbidden", "responseModel": "Error" }, { 'code': 401, "message": "permission_denied", "responseModel": "Error" }, { 'code': 401, "message": "unauthorized", "responseModel": "Error" }, { 'code': 400, "message": "form_invalid", "responseModel": "Error" }, { 'code': 400, "message": "form_required", "responseModel": "Error" }, { 'code': 400, "message": "bad_request", "responseModel": "Error" }, ] current_api = { 'path': path, 'description': view_cls.__doc__, } operations = [] if 'create' in view_cls.crud_method_names and hasattr(view_cls, 'create'): create_op = { 'method': 'POST', 'parameters': global_params, 'responseMessages': responseMessages, 'nickname': 'create ' + path, } operations.append(create_op) if 'read' in view_cls.crud_method_names and hasattr(view_cls, 'read'): op = { 'method': 'GET', 'responseMessages': responseMessages, 'nickname': 'read ' + path } params = global_params.copy() for each_permission in view_cls.permission_classes: if issubclass(each_permission, OAuth2Authenticated): params.append( { "paramType": "query", "name": 'access_token', "dataType": 'string', "required": True, } ) if hasattr(view_cls, 'read_safe_parameters'): for each in view_cls.read_safe_parameters: if isinstance(each, tuple): if each[1] == int: params.append( { "paramType": "query", "name": each[0], "dataType": 'int', "format": 'int64', "required": True, } ) elif each[1] == float: params.append( { "paramType": "query", "name": each[0], "dataType": 'float', "format": 'float', "required": True, } ) else: params.append( { "paramType": "query", "name": each[0], "dataType": 'string', "required": True, } ) else: params.append( { "paramType": "query", "name": each, "dataType": 'string', "required": True, } ) pass pass op['parameters'] = params operations.append(op) if 'update' in view_cls.crud_method_names and hasattr(view_cls, 'update'): op = { 'method': 'UPDATE', 'parameters': global_params, 'responseMessages': responseMessages, 'errorResponses': [], 'nickname': 'read ' + path, } operations.append(op) if 'delete' in view_cls.crud_method_names and hasattr(view_cls, 'delete'): op = { 'method': 'DELETE', 'parameters': global_params, 'responseMessages': responseMessages, 'errorResponses': [], 'nickname': 'read ' + path, } operations.append(op) current_api['operations'] = operations apis.append(current_api) ret['apis'] = apis ret["models"] = models return ret ret = SimpleCache(key_format="api-doc:%(api_version)s", duration=60 * 60 * 24, load_callback=load_cache).get(api_version=api_version) response = http_json_response(ret) response["Access-Control-Allow-Origin"] = "*" response["Access-Control-Allow-Methods"] = "GET" response["Access-Control-Max-Age"] = "1000" response["Access-Control-Allow-Headers"] = "*" return response
def post(self, request): try: return http_json_response(get_credential(request).to_dict()) except OAuth2Error as e: return http_json_response(e.to_dict(), status=400)