Skip to content

Commit

Permalink
bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
CameronSima committed Aug 28, 2024
1 parent 53cc0b6 commit b67004b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 17 deletions.
11 changes: 8 additions & 3 deletions src/ziplineio/html/jinja.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import inspect
from typing import Any

from ziplineio.response import JinjaResponse
from ziplineio.utils import call_handler

Expand All @@ -8,8 +8,13 @@ def jinja(env: Any, template_name: str):
template = env.get_template(template_name)

def decorator(handler):
async def wrapped_handler(*args, **kwargs):
context = await call_handler(handler, *args, **kwargs)
async def wrapped_handler(req, **kwargs):
# Pass all arguments directly to the handler
sig = inspect.signature(handler)
print(f"sig: {sig}")
# Filter kwargs to only pass those that the handler expects
filtered_kwargs = {k: v for k, v in kwargs.items() if k in sig.parameters}
context = await call_handler(handler, req, **filtered_kwargs)
rendered = template.render(context)
return JinjaResponse(rendered)

Expand Down
13 changes: 1 addition & 12 deletions src/ziplineio/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
import asyncio
from typing import Dict
from ziplineio.request import Request
from ziplineio.response import RawResponse, Response, format_response
from ziplineio.response import Response
from ziplineio.handler import Handler
from ziplineio.models import ASGIScope
from ziplineio.exception import BaseHttpException
from ziplineio import settings


async def call_handler(
Expand All @@ -23,22 +21,13 @@ async def call_handler(

except Exception as e:
response = e
print("EXCEPTION")
print(e)
# except Exception as e:
# print(e)
# response = BaseHttpException(e, 500)

print(f"response in call handler: {response}")

return response


def parse_scope(scope: ASGIScope) -> Request:
query_string = scope["query_string"].decode("utf-8")

print(f"query_string: {query_string}")

if query_string == "":
query_params = {}
else:
Expand Down
4 changes: 2 additions & 2 deletions test/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ async def test_handler_with_middleware(req: Request, ctx: dict):

# Call the route
handler, params = self.app._router.get_handler("GET", "/with-middleware")
response = await call_handler(handler, req, format=True)
response = await call_handler(handler, req)
response = format_response(response, settings.DEFAULT_HEADERS)

print(f"response: {response}")
Expand Down Expand Up @@ -117,7 +117,7 @@ async def test_handler_with_middleware(req: Request, ctx: dict):

# Call the route
handler, params = self.app._router.get_handler("GET", "/with-middleware")
response = await call_handler(handler, req, format=False)
response = await call_handler(handler, req)

print(response)

Expand Down

0 comments on commit b67004b

Please sign in to comment.