Skip to content

Commit

Permalink
replaced msgspec with pydantic and blacksheep with quart (#43)
Browse files Browse the repository at this point in the history
* replaced msgspec with pydantic and blacksheep with quart

* updated type from BaseException -> Exception

* Revert "updated type from BaseException -> Exception"

This reverts commit d97c1b6.

* added arbitrary_types_allowed=True to pydantic model config

* updated test server response

* removed arg
  • Loading branch information
fullerzz authored Nov 9, 2024
1 parent 3757eb9 commit 07de1ec
Show file tree
Hide file tree
Showing 5 changed files with 329 additions and 197 deletions.
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ dependencies = [
"aiodns>=3.2.0",
"aiohttp>=3.10.10",
"loguru>=0.7.2",
"msgspec>=0.18.6",
"pydantic>=2.9.2",
]

[project.optional-dependencies]
Expand All @@ -19,7 +19,6 @@ uvloop = [

[dependency-groups]
dev = [
"blacksheep==2.0.6",
"httpx>=0.27.2",
"mypy>=1.13.0",
"pip-audit>=2.7.3",
Expand All @@ -28,6 +27,7 @@ dev = [
"pytest-asyncio>=0.24.0",
"ruff>=0.7.3",
"uvicorn>=0.32.0",
"quart>=0.19.8",
]

[build-system]
Expand Down
8 changes: 5 additions & 3 deletions src/loamy/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from typing import Literal

import aiohttp
import msgspec
from loguru import logger
from pydantic import BaseModel, ConfigDict

# Disable the logger. If a consuming app wishes to see loamy's logs, they can enable() it again.
logger.disable("loamy")
Expand All @@ -19,7 +19,7 @@
logger.debug("Using asyncio for async operations")


class RequestMap(msgspec.Struct):
class RequestMap(BaseModel):
"""
Class containing information about a single HTTP request to be sent.
"""
Expand All @@ -31,11 +31,13 @@ class RequestMap(msgspec.Struct):
headers: dict[str, str] | None = None


class RequestResponse(msgspec.Struct):
class RequestResponse(BaseModel):
"""
Class containing information about the result of an HTTP request.
"""

model_config = ConfigDict(arbitrary_types_allowed=True)

request_map: RequestMap
status_code: int
body: dict | None = None
Expand Down
23 changes: 13 additions & 10 deletions tests/bin/test_server/server.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
from blacksheep import Application, Request, Response, get, json, post
from quart import Quart, jsonify, Response, Request

app = Application()
app = Quart(__name__)


@get("/")
@app.get("/")
async def index() -> Response:
return json({"message": "Hello, world!"})
return jsonify({"message": "Hello, world!"})


@post("/foo")
async def post_foo(request: Request) -> Response:
data = await request.json()
resp: Response = json(data)
resp.add_header(b"X-Test", b"Test")
@app.post("/foo")
async def post_foo() -> Response:
resp = jsonify({"foo": "bar"})
resp.headers["X-Test"] = "Test"
return resp


@get("/exception")
@app.get("/exception")
async def mock_exception() -> Response:
raise Exception("Mock exception")


if __name__ == "__main__":
app.run()
1 change: 1 addition & 0 deletions tests/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def test_send_requests(request_map_collection: List[RequestMap]) -> None:
assert "x-test" in response.headers
assert "Test" == response.headers["x-test"]


def test_send_requests_with_exceptions(
request_map_collection: List[RequestMap],
request_map_to_trigger_exception: RequestMap,
Expand Down
Loading

0 comments on commit 07de1ec

Please sign in to comment.