diff --git a/src/turbosloth/__main__.py b/src/turbosloth/__main__.py index f3ca4cd..29b01e6 100644 --- a/src/turbosloth/__main__.py +++ b/src/turbosloth/__main__.py @@ -116,7 +116,7 @@ class TestResp: @dataclass class TestResp1(TestResp): - hehe = 'haha' + hehe: str = 'haha' @dataclass @@ -175,14 +175,54 @@ async def stoplight() -> BasicResponse: - + diff --git a/src/turbosloth/app.py b/src/turbosloth/app.py index 3b0ece6..55141a4 100644 --- a/src/turbosloth/app.py +++ b/src/turbosloth/app.py @@ -70,11 +70,16 @@ class HTTPApp(ASGIApp): contenttype_header = req.headers.get('Accept') if contenttype_header is None: return ct + ctypes = set(map(str.strip, contenttype_header.split(','))) + if (self.serialize_selector.default_content_type in ctypes + and self.serialize_selector.default_content_type is not None): + contenttype = self.serialize_selector.default_content_type + charset = None + else: + properties: dict[str, str] + contenttype, properties = parse_content_type(contenttype_header) - properties: dict[str, str] - contenttype, properties = parse_content_type(contenttype_header) - - charset = properties.get('charset') + charset = properties.get('charset') if charset is None: # TODO: extract default charsets based on content type @@ -467,7 +472,7 @@ class SlothApp(HTTPApp, WSApp, LifespanApp, MethodRoutersApp): def decorator(fn: HandlerType): path_substs = self.router.find_pattern_substs(path_pattern) - fork_with, fn_type_hints = self._integrate_func(fn, path_substs, ok_return_code=ok_return_code) + config, fork_with, fn_type_hints = self._integrate_func(fn, path_substs, ok_return_code=ok_return_code) tmp_repo = self.inj_repo.fork(fork_with) p = tmp_repo.create_pipeline( (Send, BasicRequest), @@ -496,18 +501,32 @@ class SlothApp(HTTPApp, WSApp, LifespanApp, MethodRoutersApp): create_di_autodoc_handler(method, path_pattern, p, depgraph)) if self.openapi_app is not None: + responses = {} + if config.return_schema is not None: + for k, v in config.return_schema.code_map.items(): + schema = self.schema_from_type(v.body) + self.openapi_app.register_component(v.body) + content = {} + for ctype in self.serialize_selector.serializers.keys(): + content[ctype] = MediaType(schema=schema) + headers = None + if v.headers_provided: + pass + responses[k] = Response( + description=f'{v.body}', + content=content, + headers=headers + ) + req_body = {} + req_schema = self.construct_req_body(p) + for ctype in self.serialize_selector.serializers.keys(): + req_body[ctype] = MediaType(req_schema) self.openapi_app.register_endpoint( method, path_pattern, self.construct_req_params(p), - { - '200': Response('desc', content={'application/json': MediaType(schema=Schema(type='boolean'))}) - }, - request_body=OpenApiRequestBody( - { - 'application/json': MediaType(self.construct_req_body(p)) - } - ) + responses, + request_body=OpenApiRequestBody(req_body) ) self.register_endpoint_components(p) @@ -645,10 +664,10 @@ class SlothApp(HTTPApp, WSApp, LifespanApp, MethodRoutersApp): setattr(func, '__turbosloth_config__', config) - return fork_with, fn_type_hints + return config, fork_with, fn_type_hints def add_injector(self, func: Callable): - fork_with, _ = self._integrate_func(func) + _, fork_with, _ = self._integrate_func(func) self.inj_repo.add_conversion_points(fork_with) def mark_injector(self):