From acec24e0922b259c85a4975ef561a9e3c8f82974 Mon Sep 17 00:00:00 2001 From: Vladimir Volkov Date: Mon, 15 Feb 2021 19:10:24 +0300 Subject: [PATCH 1/2] fix status_code change at Method --- .gitignore | 3 +++ README.md | 6 +++--- setup.py | 2 +- star_resty/method/method.py | 2 +- star_resty/method/render.py | 18 +++++++++--------- 5 files changed, 17 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index fdda8ce..8a18d49 100644 --- a/.gitignore +++ b/.gitignore @@ -94,6 +94,9 @@ venv.bak/ .spyderproject .spyproject +#Visual Code Settings +.vscode + # Rope project settings .ropeproject diff --git a/README.md b/README.md index 1e1d749..e629677 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ from typing import Optional from marshmallow import Schema, fields, post_load, ValidationError from starlette.applications import Starlette -from starlette.responses import UJSONResponse +from starlette.responses import JSONResponse from star_resty import Method, Operation, endpoint, json_schema, query, setup_spec @@ -50,7 +50,7 @@ app = Starlette(debug=True) @app.exception_handler(ValidationError) def register_error(request, e: ValidationError): - return UJSONResponse(e.normalized_messages(), status_code=400) + return JSONResponse(e.normalized_messages(), status_code=400) @app.route('/echo') @@ -59,7 +59,7 @@ class Echo(Method): meta = Operation(tag='default', description='echo') response_schema = EchoInput - + self.status_code = 201 # Configurable Respone Http Status Code async def execute(self, query_params: query(EchoInput)): return query_params diff --git a/setup.py b/setup.py index 11477c4..ccea59f 100644 --- a/setup.py +++ b/setup.py @@ -29,7 +29,7 @@ def get_packages(package): 'starlette<1', 'apispec<4', ], - version='0.0.13', + version='0.0.14', url='https://github.com/slv0/start_resty', license='BSD', description='The web framework', diff --git a/star_resty/method/method.py b/star_resty/method/method.py index 4a68995..c4abee8 100644 --- a/star_resty/method/method.py +++ b/star_resty/method/method.py @@ -34,7 +34,7 @@ async def execute(self, *args, **kwargs): async def dispatch(self) -> Response: params = await self.__parser__.parse(self.request) content = await self.execute(**params) - return self.__render__(content) + return self.__render__(content, self) @classmethod def as_endpoint(cls): diff --git a/star_resty/method/render.py b/star_resty/method/render.py index 2861ec9..e478e48 100644 --- a/star_resty/method/render.py +++ b/star_resty/method/render.py @@ -22,11 +22,11 @@ def create_render(method) -> 'Render': if response_schema is not None: if inspect.isclass(response_schema): response_schema = response_schema() - renders.append(dump_content(response_schema)) + renders.append(dump_content(response_schema, None)) serializer = getattr(method, 'serializer', None) if serializer is not None: - renders.append(render_bytes(serializer, method.status_code or 200)) + renders.append(render_bytes(serializer, method)) return Render(renders) @@ -37,24 +37,24 @@ class Render: def __init__(self, renders: Sequence): self._renders = renders - def __call__(self, content: Any): + def __call__(self, content: Any, method): for r in self._renders: - content = r(content) + content = r(content, method) return content -def render_bytes(serializer, status_code): - def render(content): +def render_bytes(serializer, method): + def render(content, method): return Response(serializer.render(content), media_type=serializer.media_type, - status_code=status_code) + status_code=method.status_code) return render -def dump_content(response_schema: Schema) -> Callable: - def dump(content): +def dump_content(response_schema: Schema, _) -> Callable: + def dump(content, _): try: return response_schema.dump(content) except MarshmallowError as e: From ab5a9ca29f0395df465236fe156630806912caf2 Mon Sep 17 00:00:00 2001 From: Valdis Sheinkman <49549154+wvolkov@users.noreply.github.com> Date: Wed, 24 Feb 2021 21:01:54 +0300 Subject: [PATCH 2/2] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e629677..dc5c5c3 100644 --- a/README.md +++ b/README.md @@ -59,8 +59,8 @@ class Echo(Method): meta = Operation(tag='default', description='echo') response_schema = EchoInput - self.status_code = 201 # Configurable Respone Http Status Code async def execute(self, query_params: query(EchoInput)): + self.status_code = 201 # Configurable Respone Http Status Code return query_params