|
1 |
| -from typing import ParamSpecArgs, ParamSpecKwargs |
2 |
| - |
3 |
| -from marshmallow import Schema |
4 |
| -from marshmallow.decorators import post_dump |
5 |
| -from marshmallow_jsonschema import JSONSchema |
6 | 1 |
|
7 | 2 | import asyncio
|
8 | 3 | import dataclasses
|
|
19 | 14 | from martin_eden.openapi import OpenApiBuilder
|
20 | 15 | from martin_eden.routing import ControllerDefinitionError, get_controller, register_route
|
21 | 16 | from martin_eden.utils import get_argument_names
|
| 17 | +from martin_eden.base import Controller |
22 | 18 |
|
23 | 19 | db = DataBase()
|
24 | 20 |
|
| 21 | + |
25 | 22 | @register_route('/schema/', 'get')
|
26 | 23 | async def get_openapi_schema() -> str:
|
27 | 24 | return json.dumps(OpenApiBuilder().openapi_object)
|
28 | 25 |
|
29 | 26 |
|
30 |
| -class Controller: |
31 |
| - """The class needs only as type hint""" |
32 |
| - request_schema: Schema |
33 |
| - response_schema: Schema |
34 |
| - query_params: dict |
35 |
| - |
36 |
| - def __call__( |
37 |
| - self, *args: ParamSpecArgs, **kwargs: ParamSpecKwargs, |
38 |
| - ) -> None: |
39 |
| - pass |
40 |
| - |
41 |
| - |
42 |
| -class CustomSchema(Schema): |
43 |
| - def __init__( |
44 |
| - self, |
45 |
| - *args: ParamSpecArgs, |
46 |
| - json_schema_name: str = None, |
47 |
| - **kwargs: ParamSpecKwargs, |
48 |
| - ) -> None: |
49 |
| - super().__init__(*args, **kwargs) |
50 |
| - self.json_schema_name = json_schema_name |
51 |
| - self.__name__ = json_schema_name |
52 |
| - |
53 |
| - |
54 |
| -class CustomJsonSchema(JSONSchema): |
55 |
| - @post_dump |
56 |
| - def wrap(self, data: dict, **_) -> dict[str, Any]: |
57 |
| - """Wrap this with the root schema definitions.""" |
58 |
| - if self.nested: # no need to wrap, will be in outer defs |
59 |
| - return data |
60 |
| - |
61 |
| - schema_name = self.obj.json_schema_name |
62 |
| - |
63 |
| - data["additionalProperties"] = False |
64 |
| - |
65 |
| - self._nested_schema_classes[schema_name] = data |
66 |
| - root = { |
67 |
| - "$schema": "http://json-schema.org/draft-07/schema#", |
68 |
| - "definitions": self._nested_schema_classes, |
69 |
| - "$ref": f"#/definitions/{schema_name}", |
70 |
| - } |
71 |
| - return root |
72 |
| - |
73 |
| - |
74 | 27 | class HttpRequestHandler:
|
75 | 28 | def __init__(
|
76 | 29 | self, event_loop: AbstractEventLoop, client_socket: socket.socket,
|
|
0 commit comments