Skip to content

Commit

Permalink
added uri templates
Browse files Browse the repository at this point in the history
  • Loading branch information
mauro-balades committed Oct 2, 2021
1 parent d568df1 commit d439ef8
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
4 changes: 1 addition & 3 deletions expross/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@

from expross.main import Expross

from expross.response import html
from expross.response import json
from expross.response import json
from expross.response import *

from expross.types import ContentTypes

Expand Down
13 changes: 8 additions & 5 deletions expross/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,12 @@ def _check_for_method(self, method: str):
if not method in self.methods:
raise MethodNotAvailable(f"Method {method} is not avaiable for this route")

def _get_packed_result(self):
def _get_packed_result(self, **context):
"""Get a complete version of the function's response
Args:
context (str): extra context used in uri's templates
Returns:
any: The result the response will have as body
int: status code for the response
Expand All @@ -68,7 +71,7 @@ def _get_packed_result(self):
str: Content-type of the response
"""
# TODO: add arguments if the have a templated url
data = self.function()
data = self.function(**context)

# TODO: clean a bit this code.
try:
Expand All @@ -90,7 +93,7 @@ def _get_packed_result(self):

return res, code, content_type

def on_get(self, req: Request, resp: Response):
def on_get(self, req: Request, resp: Response, **context):
"""Triggered when a GET request has been requested
Args:
Expand All @@ -101,7 +104,7 @@ def on_get(self, req: Request, resp: Response):

self.app.req: Request = req
self.app.res: Response = resp
res, code, content_type = self._get_packed_result()
res, code, content_type = self._get_packed_result(**context)

# Get response's data in bytes
data, type = get_response(res)
Expand All @@ -121,7 +124,7 @@ def on_post(self, req: Request, resp: Response):

self.app.req: Request = req
self.app.res: Response = resp
res, code, content_type = self._get_packed_result()
res, code, content_type = self._get_packed_result(**context)

# Get response's data in bytes
data, type = get_response(res)
Expand Down
Binary file added test/public/test.pdf
Binary file not shown.
12 changes: 7 additions & 5 deletions test/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ def err():

@app.get("/")
def main():
print(app.req)
return {"hello": "test"}

if app.req.params.get('pdf', None) == "active":
return app.redirect("/public/test.pdf")

@app.post("/")
def test():
return "Hello post!"
return "<h1>Hello, world!</h1>"


@app.get("/test/{number:int}")
def test(number: int):
return f"this is number is a {number}"

app.listen()

0 comments on commit d439ef8

Please sign in to comment.