def patch_book(id): try: patch_payload = request.get_json() book_validator.validate_patch_payload(patch_payload) book_service.patch(id, patch_payload) return res_success(status=204) except ValidationError as exception: return res_error(exception, status=400) except ValueError as exception: return res_error(exception, status=404)
def update_book(id): try: updated_book = request.get_json() book_validator.validate_book(updated_book) book_service.update(id, updated_book) return res_success() except ValidationError as exception: return res_error(exception, status=400) except ValueError as exception: return res_error(exception, status=404)
def wrapper(*args, **kwargs): token_header = request.headers.get('Authorization', None) if not token_header: return res_error('Authroization header is required', status=403) if not token_header.startswith(AUTH_TYPE): return res_error('unsupported authorisation type', status=400) token = token_header.split(AUTH_TYPE)[1] if not auth_service.is_authorized(token): return res_error('not authorised', status=403) return f(*args, **kwargs)
def register(): try: new_user = request.get_json() auth_service.register(new_user) return res_success(status=201) except ValueError as error: return res_error(error, status=409)
def add_book(): try: new_book = request.get_json() book_validator.validate_book(new_book) id = book_service.add(new_book) return res_success({'id': id}, status=201) except (ValidationError, ValueError) as exception: return res_error(exception)
def login(): try: request_data = request.get_json() token = auth_service.login(request_data['username'], request_data['password']) message = {'token': f'Bearer {token}'} return res_success(message) except ValueError: return res_error('username or password is incorrect', status=400)
def delete(id): try: book_service.delete(id) return res_success(status=204) except ValueError as exception: return res_error(exception, status=404)
def find_book(id): try: book = book_service.get_one(id) return jsonify(book) except ValueError as exception: return res_error(exception, status=404)