Skip to content

Commit

Permalink
Merge pull request #2 from lazy-labs/0.0.14
Browse files Browse the repository at this point in the history
v0.0.14
  • Loading branch information
wvolkov authored Feb 24, 2021
2 parents 700a322 + ab5a9ca commit 238f6c3
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 14 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ venv.bak/
.spyderproject
.spyproject

#Visual Code Settings
.vscode

# Rope project settings
.ropeproject

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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')
Expand All @@ -59,8 +59,8 @@ class Echo(Method):
meta = Operation(tag='default',
description='echo')
response_schema = EchoInput

async def execute(self, query_params: query(EchoInput)):
self.status_code = 201 # Configurable Respone Http Status Code
return query_params


Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion star_resty/method/method.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
18 changes: 9 additions & 9 deletions star_resty/method/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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:
Expand Down

0 comments on commit 238f6c3

Please sign in to comment.