Skip to content

Commit

Permalink
fix all issues with 0.0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
kheina committed Jul 13, 2024
1 parent 2653070 commit cf5db64
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 81 deletions.
23 changes: 2 additions & 21 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,26 +1,10 @@
# credentials
**/credentials.*
**/credentials
credentials

# executables
**/*.o
# python virtual environment
.venv

# config files
.vscode
.DS_Store

# database files
**/*.db
**/*.sql

# images
**/*.jpg
**/*.png
**/*.gif
**/*.ico
**/*.svg

# python files
**/__pycache__
**/Pipfile
Expand All @@ -34,9 +18,6 @@ server.py
!requirements.txt
!requirements*.txt

# back ups of files hosted remotely
**/backups

# experimental things
ignore*

Expand Down
101 changes: 53 additions & 48 deletions avrofastapi/routing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
from contextlib import AsyncExitStack
import json
from asyncio import Lock
from collections import OrderedDict
Expand Down Expand Up @@ -28,7 +29,7 @@
from starlette.requests import Request
from starlette.responses import JSONResponse
from starlette.routing import BaseRoute
from starlette.types import ASGIApp, Receive, Scope, Send
from starlette.types import ASGIApp, Lifespan, Receive, Scope, Send

from avrofastapi.handshake import MD5, AvroMessage, AvroProtocol, CallRequest, CallResponse, HandshakeMatch, HandshakeRequest, HandshakeResponse
from avrofastapi.models import Error, ValidationError, ValidationErrorDetail
Expand Down Expand Up @@ -364,60 +365,62 @@ async def app(request: Request) -> Response :
error=True,
)

solved_result = await solve_dependencies(
request=request,
dependant=dependant,
body=body,
dependency_overrides_provider=dependency_overrides_provider,
)
values, errors, background_tasks, sub_response, _ = solved_result
# print(values, errors, background_tasks, sub_response, actual_response_class, self.response_class)

if errors :
serializer: AvroSerializer
_, _, serializer = request.scope['router']._server_protocol
error = ValidationError(detail=[ValidationErrorDetail(**e) for e in errors[0].exc.errors()])
return AvroJsonResponse(
model=error,
serializer=serializer,
error=True,
async with AsyncExitStack() as async_exit_stack:
solved_result = await solve_dependencies(
request=request,
dependant=dependant,
body=body,
dependency_overrides_provider=dependency_overrides_provider,
async_exit_stack=async_exit_stack,
)
values, errors, background_tasks, sub_response, _ = solved_result
# print(values, errors, background_tasks, sub_response, actual_response_class, self.response_class)

if errors :
serializer: AvroSerializer
_, _, serializer = request.scope['router']._server_protocol
error = ValidationError(detail=[ValidationErrorDetail(**e) for e in errors[0].exc.errors()])
return AvroJsonResponse(
model=error,
serializer=serializer,
error=True,
)

else :
raw_response = await run_endpoint_function(
dependant=dependant, values=values, is_coroutine=is_coroutine
)
else :
raw_response = await run_endpoint_function(
dependant=dependant, values=values, is_coroutine=is_coroutine
)

if isinstance(raw_response, Response) :
if raw_response.background is None :
raw_response.background = background_tasks
return raw_response

response_data = await serialize_response(
field=response_field,
response_content=raw_response,
include=response_model_include,
exclude=response_model_exclude,
by_alias=response_model_by_alias,
exclude_unset=response_model_exclude_unset,
exclude_defaults=response_model_exclude_defaults,
exclude_none=response_model_exclude_none,
is_coroutine=is_coroutine,
)
response_args: Dict[str, Any] = { 'background': background_tasks }
if isinstance(raw_response, Response) :
if raw_response.background is None :
raw_response.background = background_tasks
return raw_response

response_data = await serialize_response(
field=response_field,
response_content=raw_response,
include=response_model_include,
exclude=response_model_exclude,
by_alias=response_model_by_alias,
exclude_unset=response_model_exclude_unset,
exclude_defaults=response_model_exclude_defaults,
exclude_none=response_model_exclude_none,
is_coroutine=is_coroutine,
)
response_args: Dict[str, Any] = { 'background': background_tasks }

# If status_code was set, use it, otherwise use the default from the
# response class, in the case of redirect it's 307
if status_code is not None :
response_args['status_code'] = status_code
# If status_code was set, use it, otherwise use the default from the
# response class, in the case of redirect it's 307
if status_code is not None :
response_args['status_code'] = status_code

response = actual_response_class(serializable_body=response_data, model=raw_response, **response_args)
response.headers.raw.extend(sub_response.headers.raw)
response = actual_response_class(serializable_body=response_data, model=raw_response, **response_args)
response.headers.raw.extend(sub_response.headers.raw)

if sub_response.status_code :
response.status_code = sub_response.status_code
if sub_response.status_code :
response.status_code = sub_response.status_code

return response
return response

return app

Expand All @@ -440,6 +443,7 @@ def __init__(
route_class: Type[APIRoute] = AvroRoute,
on_startup: Optional[Sequence[Callable[[], Any]]] = None,
on_shutdown: Optional[Sequence[Callable[[], Any]]] = None,
lifespan: Optional[Lifespan[Any]] = None,
deprecated: Optional[bool] = None,
include_in_schema: bool = True,
generate_unique_id_function: Callable[[APIRoute], str] = Default(
Expand All @@ -460,6 +464,7 @@ def __init__(
route_class=route_class,
on_startup=on_startup,
on_shutdown=on_shutdown,
lifespan=lifespan,
deprecated=deprecated,
include_in_schema=include_in_schema,
generate_unique_id_function=generate_unique_id_function,
Expand Down
9 changes: 9 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.PHONY: venv
venv:
python3 -m venv ./.venv
.venv/bin/python3 -m pip install -r requirements.lock --no-deps \
&& echo && echo "Done. run 'source .venv/bin/activate' to enter python virtual environment"

.PHONY: lock
lock:
python3 -m pip freeze > requirements.lock
1 change: 0 additions & 1 deletion pyproject.toml

This file was deleted.

15 changes: 15 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[tool.isort]
line_length = 999
lines_after_imports = 2

[tool.ruff.format]
exclude = ["*"] #
indent-style = "tab"
line-ending = "lf"

[tool.ruff.lint]
fixable = []

[tool.ruff.lint.isort]
force-single-line = true
lines-after-imports = 2
81 changes: 81 additions & 0 deletions requirements.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
aiohttp==3.7.4.post0
annotated-types==0.7.0
anyio==4.4.0
async-timeout==3.0.1
attrs==23.2.0
avro==1.11.3
certifi==2024.7.4
cffi==1.16.0
chardet==4.0.0
charset-normalizer==3.3.2
click==8.1.7
colorama==0.4.6
coverage==7.6.0
cryptography==42.0.8
dnspython==2.6.1
docutils==0.21.2
email_validator==2.2.0
fastapi==0.111.0
fastapi-cli==0.0.4
h11==0.14.0
httpcore==0.16.3
httptools==0.6.1
httpx==0.23.3
idna==3.7
importlib_metadata==8.0.0
iniconfig==2.0.0
isort==5.11.5
jaraco.classes==3.4.0
jaraco.context==5.3.0
jaraco.functools==4.0.1
jeepney==0.8.0
Jinja2==3.1.4
keyring==25.2.1
markdown-it-py==3.0.0
MarkupSafe==2.1.5
mdurl==0.1.2
more-itertools==10.3.0
multidict==6.0.5
nh3==0.2.18
orjson==3.10.6
packaging==24.1
pkginfo==1.11.1
pluggy==1.5.0
py==1.11.0
pycparser==2.22
pydantic==1.10.17
pydantic_core==2.20.1
Pygments==2.18.0
pytest==6.2.5
pytest-aiohttp==0.3.0
pytest-asyncio==0.14.0
pytest-cov==3.0.0
pytest-env==0.6.2
pytest-mock==3.4.0
python-dotenv==1.0.1
python-multipart==0.0.9
PyYAML==6.0.1
readme_renderer==44.0
requests==2.32.3
requests-toolbelt==1.0.0
rfc3986==1.5.0
rich==13.7.1
SecretStorage==3.3.3
setuptools==66.1.1
shellingham==1.5.4
sniffio==1.3.1
starlette==0.37.2
toml==0.10.2
tqdm==4.66.4
twine==3.4.2
typer==0.12.3
typing_extensions==4.12.2
ujson==5.5.0
urllib3==2.2.2
uvicorn==0.30.1
uvloop==0.19.0
watchfiles==0.22.0
websockets==12.0
wheel==0.38.4
yarl==1.9.4
zipp==3.19.2
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ avro~=1.11.1
setuptools~=66.1.1
ujson~=5.5.0
aiohttp~=3.7.3
fastapi~=0.89.1
pydantic~=1.9.0
fastapi~=0.111.0
pydantic~=1.10.17
18 changes: 9 additions & 9 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ class BasicModelBaseTypes(BaseModel) :

class BasicEnum(Enum) :
__use_enum_names__: bool = False
test1: str = 'TEST1'
test2: str = 'TEST2'
test3: str = 'TEST3'
test1 = 'TEST1'
test2 = 'TEST2'
test3 = 'TEST3'


class BasicModelAdvancedTypes(BaseModel) :
Expand Down Expand Up @@ -59,9 +59,9 @@ class BasicModelCustomNestedNamespace(BaseModel) :

class BasicEnumUsesNames(Enum) :
__use_enum_names__: bool = True
test1: str = 'TEST1'
test2: str = 'TEST2'
test3: str = 'TEST3'
test1 = 'TEST1'
test2 = 'TEST2'
test3 = 'TEST3'


class BasicModelDefaultValues(BaseModel) :
Expand Down Expand Up @@ -146,9 +146,9 @@ class BasicModelInvalidType6(BaseModel) :


class BasicEnumInvalidType7(Enum) :
test1: str = 'TEST1'
test2: str = 'TEST2'
test3: str = 'TEST1'
test1 = 'TEST1'
test2 = 'TEST2'
test3 = 'TEST1'


class NestedModelInvalidNamespace1(BaseModel) :
Expand Down

0 comments on commit cf5db64

Please sign in to comment.