Skip to content

Commit

Permalink
Merge branch 'main' of github.com:sebastien/extra
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastien committed Sep 5, 2024
2 parents ff2acd1 + a696720 commit 77e4f7e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
25 changes: 18 additions & 7 deletions src/py/extra/http/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def notModified(self):
def fail(
self,
content: str | None = None,
*,
status: int = 500,
contentType: str = "text/plain",
):
Expand All @@ -87,8 +88,10 @@ def redirect(self, url: str, permanent: bool = False):
def returns(
self,
value: Any,
contentType: str = "application/json",
headers: dict[str, str] | None = None,
*,
status: int = 200,
contentType: str = "application/json",
):
if isinstance(value, bytes):
try:
Expand All @@ -101,18 +104,25 @@ def returns(
contentType=contentType,
contentLength=len(payload),
headers=headers,
status=status,
)

def respondText(
self, content: str | bytes | Iterator[str | bytes], contentType="text/plain"
self,
content: str | bytes | Iterator[str | bytes],
contentType="text/plain",
status: int = 200,
):
return self.respond(content=content, contentType=contentType)
return self.respond(content=content, contentType=contentType, status=status)

def respondHTML(self, html: str | bytes | Iterator[str | bytes]):
return self.respond(content=html, contentType="text/html")
def respondHTML(self, html: str | bytes | Iterator[str | bytes], status: int = 200):
return self.respond(content=html, contentType="text/html", status=status)

def respondFile(
self, path: Path | str, status: int = 200, headers: dict[str, str] | None = None
self,
path: Path | str,
headers: dict[str, str] | None = None,
status: int = 200,
):
# TODO: We should have a much more detailed file handling, supporting ranges, etags, etc.
p: Path = path if isinstance(path, Path) else Path(path)
Expand All @@ -128,8 +138,9 @@ def respondFile(
def respondError(
self,
content: str | None = None,
status: int = 500,
contentType: str = "text/plain",
*,
status: int = 500,
):
return self.error(status, content, contentType)

Expand Down
4 changes: 2 additions & 2 deletions src/py/extra/services/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ def renderDir(
*nodes,
),
),
doctype="html",
)
),
doctype="html",
)
)

Expand Down

0 comments on commit 77e4f7e

Please sign in to comment.