def post(self): request_data = request.get_json(force=True) logger.info("Received login request") if not validate_login_request(request_data): return error_response(400, "Bad Request") response = validate_user(request_data) if response: return ok_response(response) else: return error_response( 403, "The username or password you entered is incorrect")
def put(self): logger.info("Received Movie Put request from : {}".format( session.get("user_id"))) request_body = request.get_json(force=True) if not validate_movie_put_request(request_body): return error_response(400, "Bad Request") if not update_movie_data(request_body): return error_response(500, message="Internal Server Error") return ok_response({}, message="Movie Updated Successfully")
def post(self): try: get_session_user() except: return error_response(400, "logout called for an invalid session") logout_user() return ok_response({},message="logout successfully")
def delete(self, movie_id=None): logger.info("Received Movie Delete request from : {}".format( session.get("user_id"))) if not movie_id: return error_response(400, "Bad Request") remove_movie(movie_id) return ok_response({}, message="Movie Deleted Successfully")
def post(self): logger.info("Received User Post request from : {}".format( session.get("user_id"))) request_body = request.get_json(force=True) if not validate_user_post_request(request_body): return error_response(400, "Bad Request") if user_exist_with_username(request_body['username']): return error_response(409, message="User Alreay Exist with username") if user_exist_with_email(request_body['email']): return error_response(409, message="User Alreay Exist with email") if not create_user(request_body): return error_response(500, message="Internal Server Error") return ok_response({}, message="User Created Successfully")
def get(self, movie_id=None): logger.info("Received Movie Get request from : {}".format( session.get("user_id"))) if movie_id: response = get_movie_json_by_id(movie_id) return ok_response(response) try: params = request.args.to_dict() limit = int(params.get('limit', 20)) offset = int(params.get('offset', 0)) if params.get('genre'): response = get_movies_by_genre(params['genre'], limit, offset) return ok_response(response) elif params.get('search_term'): response = get_movies_by_search_term(params['search_term'], limit, offset) return ok_response(response) else: return error_response(400, "Bad Request") except: return error_response(500, "Internal Server Error")
def wrapped_f(*args, **kwargs): try: start = datetime.now() output = f(*args, **kwargs) logger.info("Time taken for %s is %s", self.api_key, datetime.now() - start) return output except KeyError as e: exception_trace = traceback.format_exc() parsed_request = parse_request_for_error(request) logger.error("Exception in %s API:\n%s Request: \n%s", self.api_key, exception_trace, parsed_request) logger.error("Missing key : " + e.__repr__()) return error_response(400, "{} is required".format(e.__repr__())) except (ValidationError, TypeError) as e: exception_trace = traceback.format_exc() parsed_request = parse_request_for_error(request) logger.error("Exception in %s API:\n%s Request: \n%s", self.api_key, exception_trace, parsed_request) logger.error("Missing key : " + e.__repr__()) return error_response( 400, "Bad Request object. Missing Parameters or wrong endpoint") except ValueError as e: exception_trace = traceback.format_exc() parsed_request = parse_request_for_error(request) logger.error("Exception in %s API:\n%s Request: \n%s", self.api_key, exception_trace, parsed_request) logger.error("Missing key : " + e.__repr__()) return error_response( 400, "Bad Request. Incorrect Parameter types") except (ObjectDoesNotExist, MultipleObjectsReturned) as e: exception_trace = traceback.format_exc() parsed_request = parse_request_for_error(request) logger.error("Exception in %s API:\n%s Request: \n%s", self.api_key, exception_trace, parsed_request) logger.error("Missing key : " + e.__repr__()) return error_response( 422, "Unprocessable Entity, no records found or multiple records found" ) except PermissionDenied as e: exception_trace = traceback.format_exc() parsed_request = parse_request_for_error(request) logger.error("Exception in %s API:\n%s Request: \n%s", self.api_key, exception_trace, parsed_request) logger.error("Missing key : " + e.__repr__()) return error_response(403, "Permission Denied") except CustomError as error: exception_trace = traceback.format_exc() parsed_request = parse_request_for_error(request) logger.error("Exception in %s API: %s\n%s \nRequest: %s", self.api_key, error.get_message(), exception_trace, parsed_request) return error_response(error.get_error_code(), error.get_message()) except IntegrityError as e: exception_trace = traceback.format_exc() parsed_request = parse_request_for_error(request) logger.error("Exception in %s API:\n%s Request: \n%s", self.api_key, exception_trace, parsed_request) db_err_code, _ = e.args if db_err_code == 1062: return error_response(400, "Duplicate Entry") return error_response(500, "Internal Server Error") except Exception as e: exception_trace = traceback.format_exc() parsed_request = parse_request_for_error(request) logger.error("Exception in %s API:\n%s Request: \n%s", self.api_key, exception_trace, parsed_request) logger.error("Missing key : " + e.__repr__()) return error_response(500, "Internal Server Error") finally: close_old_connections()