async def test_unit__handle_exception_with_default_error_builder__ok__serpyco( self, test_client): from hapic.error.serpyco import SerpycoDefaultErrorBuilder from hapic.processor.serpyco import SerpycoProcessor app = AgnosticApp() hapic = Hapic() hapic.set_processor_class(SerpycoProcessor) hapic.set_context( AgnosticContext( app, default_error_builder=SerpycoDefaultErrorBuilder())) @hapic.with_api_doc() @hapic.handle_exception(ZeroDivisionError, http_code=400) def my_view(): 1 / 0 response = my_view() json_ = json.loads(response.body) assert { "code": None, "details": { "error_detail": {} }, "message": "division by zero", } == json_
def test_unit__handle_exception_with_default_error_builder__ok__marshmallow( self): app = AgnosticApp() hapic = Hapic() hapic.set_processor_class(MarshmallowProcessor) hapic.set_context( AgnosticContext( app, default_error_builder=MarshmallowDefaultErrorBuilder())) @hapic.with_api_doc() @hapic.handle_exception(ZeroDivisionError, http_code=400) def my_view(): 1 / 0 response = my_view() json_ = json.loads(response.body) assert { "code": None, "details": { "error_detail": {} }, "message": "division by zero", } == json_
import typing import aiohttp_autoreload import serpyco from aiohttp import web from hapic import Hapic, HapicData from hapic.error.serpyco import SerpycoDefaultErrorBuilder from hapic.ext.aiohttp.context import AiohttpContext from hapic.processor.serpyco import SerpycoProcessor from dataclasses import dataclass import utils hapic = Hapic(async_=True) hapic.set_processor_class(SerpycoProcessor) @dataclass class Location(object): def get_openstreetmap_url(obj: "Location") -> str: return f"https://www.openstreetmap.org/search?#map=13/{obj.lat}/{obj.lon}" lon: float = serpyco.number_field(cast_on_load=True) lat: float = serpyco.number_field(cast_on_load=True) url: typing.Optional[str] = serpyco.string_field( getter=get_openstreetmap_url, default=None) @dataclass class PartialSensor:
from example.usermanagement.userlib import UserLib from example.usermanagement.userlib import UserNotFound from hapic import Hapic from hapic import MarshmallowProcessor from hapic.data import HapicData from hapic.data import HapicFile from hapic.error.marshmallow import MarshmallowDefaultErrorBuilder from hapic.ext.bottle import BottleContext try: # Python 3.5+ from http import HTTPStatus except ImportError: from http import client as HTTPStatus hapic = Hapic() hapic.set_processor_class(MarshmallowProcessor) class BottleController(object): @hapic.with_api_doc() @hapic.output_body(AboutSchema()) def about(self): """ This endpoint allow to check that the API is running. This description is generated from the docstring of the method. """ return {"version": "1.2.3", "datetime": datetime.now()} @hapic.with_api_doc() @hapic.output_body(UserDigestSchema(many=True)) def get_users(self):