mini-aiohttp 0.1.0__tar.gz → 0.1.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mini-aiohttp
3
- Version: 0.1.0
3
+ Version: 0.1.1
4
4
  Summary: A small, dependency-free async HTTP/1.1 client and server built on asyncio
5
5
  License: MIT
6
6
  Requires-Python: >=3.9
@@ -0,0 +1,16 @@
1
+ """mini-aiohttp: a small, dependency-free async HTTP/1.1 client and server."""
2
+ from .batch import fetch_all, gather_requests
3
+ from .client import ClientError, ClientSession
4
+ from .response import Request, Response
5
+ from .server import Server
6
+
7
+ __version__ = "0.1.1"
8
+ __all__ = [
9
+ "ClientSession",
10
+ "ClientError",
11
+ "Server",
12
+ "Request",
13
+ "Response",
14
+ "gather_requests",
15
+ "fetch_all",
16
+ ]
@@ -0,0 +1,72 @@
1
+ """Send many requests concurrently, with a limit on how many run at once."""
2
+ from __future__ import annotations
3
+
4
+ import asyncio
5
+
6
+ from .client import ClientSession
7
+
8
+
9
+ def _normalize(spec):
10
+ """Accept a URL string, a (method, url[, kwargs]) tuple, or a dict with 'url'."""
11
+ if isinstance(spec, str):
12
+ return "GET", spec, {}
13
+ if isinstance(spec, tuple):
14
+ method, url = spec[0], spec[1]
15
+ kwargs = dict(spec[2]) if len(spec) > 2 else {}
16
+ return method, url, kwargs
17
+ if isinstance(spec, dict):
18
+ spec = dict(spec)
19
+ method = spec.pop("method", "GET")
20
+ url = spec.pop("url")
21
+ return method, url, spec
22
+ raise TypeError(f"Unsupported request spec: {spec!r}")
23
+
24
+
25
+ async def gather_requests(
26
+ session: ClientSession,
27
+ requests,
28
+ *,
29
+ concurrency: int = 100,
30
+ return_exceptions: bool = True,
31
+ ) -> list:
32
+ """Run many requests on one session, like asyncio.gather but bounded.
33
+
34
+ Each item in `requests` can be:
35
+ - a URL string -> GET
36
+ - (method, url) -> e.g. ("GET", url)
37
+ - (method, url, {kwargs}) -> kwargs go to session.request (json=, data=, headers=...)
38
+ - {"method": ..., "url": ..., "json": ...}
39
+
40
+ Results come back in the same order as the input. With return_exceptions=True
41
+ (default), a failed request is returned as its exception object, so one
42
+ failure never cancels the others.
43
+ """
44
+ sem = asyncio.Semaphore(concurrency)
45
+
46
+ async def run(spec):
47
+ method, url, kwargs = _normalize(spec)
48
+ async with sem:
49
+ return await session.request(method, url, **kwargs)
50
+
51
+ tasks = [run(spec) for spec in requests]
52
+ return await asyncio.gather(*tasks, return_exceptions=return_exceptions)
53
+
54
+
55
+ async def fetch_all(
56
+ requests,
57
+ *,
58
+ concurrency: int = 100,
59
+ timeout: float = 10.0,
60
+ return_exceptions: bool = True,
61
+ ) -> list:
62
+ """One-liner: open a session, run all requests, close the session.
63
+
64
+ results = await fetch_all(["https://a.com", "https://b.com"])
65
+ """
66
+ async with ClientSession(timeout=timeout, max_per_host=concurrency) as session:
67
+ return await gather_requests(
68
+ session,
69
+ requests,
70
+ concurrency=concurrency,
71
+ return_exceptions=return_exceptions,
72
+ )
@@ -31,6 +31,7 @@ class Request:
31
31
  version: str
32
32
  headers: dict = field(default_factory=dict)
33
33
  body: bytes = b""
34
+ params: dict = field(default_factory=dict) # filled by the router, e.g. {"id": "42"}
34
35
 
35
36
  def text(self) -> str:
36
37
  return self.body.decode("utf-8")
@@ -1,4 +1,4 @@
1
- """Asyncio HTTP/1.1 server with keep-alive, a connection cap, body limits and timeouts."""
1
+ """Asyncio HTTP/1.1 server: routing with path params, middleware, keep-alive, limits."""
2
2
  from __future__ import annotations
3
3
 
4
4
  import asyncio
@@ -25,6 +25,23 @@ async def _readline(reader: asyncio.StreamReader) -> bytes:
25
25
  raise BadRequest(431, "Line too long")
26
26
 
27
27
 
28
+ def _split_path(path: str) -> list:
29
+ return [seg for seg in path.split("/") if seg]
30
+
31
+
32
+ def _match_pattern(pattern: list, segments: list):
33
+ """Return a dict of captured params if the path matches the pattern, else None."""
34
+ if len(pattern) != len(segments):
35
+ return None
36
+ params = {}
37
+ for p, s in zip(pattern, segments):
38
+ if p.startswith("{") and p.endswith("}"):
39
+ params[p[1:-1]] = s
40
+ elif p != s:
41
+ return None
42
+ return params
43
+
44
+
28
45
  def _check_fd_limit(needed: int) -> None:
29
46
  """Warn early if the OS will refuse sockets before we hit max_connections."""
30
47
  try:
@@ -49,7 +66,8 @@ class Server:
49
66
  max_body_size: int = 10 * 1024 * 1024,
50
67
  backlog: int = 4096,
51
68
  ):
52
- self.routes: dict = {}
69
+ self.routes: list = [] # (method, pattern_segments, handler)
70
+ self.middleware: list = [] # async fn(request, call_next) -> Response
53
71
  self.max_connections = max_connections
54
72
  self.read_timeout = read_timeout
55
73
  self.max_body_size = max_body_size
@@ -60,23 +78,62 @@ class Server:
60
78
 
61
79
  # ---------- routing ----------
62
80
  def route(self, path: str, method: str = "GET"):
81
+ """Register a handler. Use {name} in the path to capture a segment: '/users/{id}'."""
63
82
  def decorator(handler):
64
- self.routes[(method.upper(), path)] = handler
83
+ self.routes.append((method.upper(), _split_path(path), handler))
65
84
  return handler
66
85
  return decorator
67
86
 
87
+ def middleware_(self, fn):
88
+ """Register middleware. It runs for every matched request, in the order added.
89
+
90
+ @app.middleware_
91
+ async def log(req, call_next):
92
+ resp = await call_next(req)
93
+ return resp
94
+ """
95
+ self.middleware.append(fn)
96
+ return fn
97
+
98
+ def _match(self, method: str, path: str):
99
+ """Pick the most specific route: fewest path params wins, so /users/me beats /users/{id}."""
100
+ segments = _split_path(path)
101
+ path_seen = False
102
+ best = None # (param_count, handler, params)
103
+ for route_method, pattern, handler in self.routes:
104
+ params = _match_pattern(pattern, segments)
105
+ if params is None:
106
+ continue
107
+ path_seen = True
108
+ if route_method != method:
109
+ continue
110
+ if best is None or len(params) < best[0]:
111
+ best = (len(params), handler, params)
112
+ if best is None:
113
+ return None, None, path_seen
114
+ return best[1], best[2], True
115
+
68
116
  async def _dispatch(self, req: Request) -> Response:
69
- handler = self.routes.get((req.method, req.path))
117
+ handler, params, path_seen = self._match(req.method, req.path)
70
118
  if handler is None:
71
- if any(path == req.path for _, path in self.routes):
119
+ if path_seen:
72
120
  return Response(405, "Method Not Allowed")
73
121
  return Response(404, "Not Found")
122
+ req.params = params
123
+
124
+ async def call_handler(request: Request) -> Response:
125
+ result = await handler(request)
126
+ return result if isinstance(result, Response) else Response(200, result)
127
+
128
+ chain = call_handler
129
+ for mw in reversed(self.middleware):
130
+ chain = _wrap(mw, chain)
131
+
74
132
  try:
75
- result = await handler(req)
133
+ return await chain(req)
76
134
  except Exception:
77
135
  log.exception("handler failed: %s %s", req.method, req.path)
78
136
  return Response(500, "Internal Server Error")
79
- return result if isinstance(result, Response) else Response(200, result)
80
137
 
81
138
  # ---------- connection handling ----------
82
139
  async def _on_connect(self, reader, writer):
@@ -199,3 +256,9 @@ class Server:
199
256
  writer.close()
200
257
  if self._server is not None:
201
258
  await self._server.wait_closed()
259
+
260
+
261
+ def _wrap(mw, nxt):
262
+ async def inner(req: Request) -> Response:
263
+ return await mw(req, nxt)
264
+ return inner
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mini-aiohttp
3
- Version: 0.1.0
3
+ Version: 0.1.1
4
4
  Summary: A small, dependency-free async HTTP/1.1 client and server built on asyncio
5
5
  License: MIT
6
6
  Requires-Python: >=3.9
@@ -2,6 +2,7 @@ LICENSE
2
2
  README.md
3
3
  pyproject.toml
4
4
  mini_aiohttp/__init__.py
5
+ mini_aiohttp/batch.py
5
6
  mini_aiohttp/client.py
6
7
  mini_aiohttp/response.py
7
8
  mini_aiohttp/server.py
@@ -10,5 +11,7 @@ mini_aiohttp.egg-info/SOURCES.txt
10
11
  mini_aiohttp.egg-info/dependency_links.txt
11
12
  mini_aiohttp.egg-info/requires.txt
12
13
  mini_aiohttp.egg-info/top_level.txt
14
+ tests/test_batch.py
13
15
  tests/test_client.py
16
+ tests/test_routing.py
14
17
  tests/test_server.py
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "mini-aiohttp"
7
- version = "0.1.0"
7
+ version = "0.1.1"
8
8
  description = "A small, dependency-free async HTTP/1.1 client and server built on asyncio"
9
9
  readme = "README.md"
10
10
  license = {text = "MIT"}
@@ -0,0 +1,59 @@
1
+ import pytest
2
+
3
+ from mini_aiohttp import ClientSession, Response, Server, fetch_all, gather_requests
4
+
5
+
6
+ @pytest.fixture
7
+ async def server():
8
+ srv = Server()
9
+
10
+ @srv.route("/a")
11
+ async def a(req):
12
+ return Response(200, "A")
13
+
14
+ @srv.route("/b")
15
+ async def b(req):
16
+ return Response(200, "B")
17
+
18
+ @srv.route("/echo", method="POST")
19
+ async def echo(req):
20
+ return Response(200, req.body)
21
+
22
+ await srv.start("127.0.0.1", 0)
23
+ yield srv
24
+ await srv.close()
25
+
26
+
27
+ async def test_fetch_all_keeps_input_order(server):
28
+ base = f"http://127.0.0.1:{server.port}"
29
+ results = await fetch_all([f"{base}/a", f"{base}/b", f"{base}/a"])
30
+ assert [r.body for r in results] == [b"A", b"B", b"A"]
31
+
32
+
33
+ async def test_mixed_specs(server):
34
+ base = f"http://127.0.0.1:{server.port}"
35
+ specs = [
36
+ {"method": "POST", "url": f"{base}/echo", "data": "hi"},
37
+ f"{base}/missing",
38
+ ("GET", f"{base}/b"),
39
+ ]
40
+ async with ClientSession() as s:
41
+ results = await gather_requests(s, specs, concurrency=2)
42
+ assert results[0].body == b"hi"
43
+ assert results[1].status == 404
44
+ assert results[2].body == b"B"
45
+
46
+
47
+ async def test_one_failure_does_not_stop_others(server):
48
+ base = f"http://127.0.0.1:{server.port}"
49
+ # Port 1 refuses connections, so that request fails on its own.
50
+ results = await fetch_all(["http://127.0.0.1:1/", f"{base}/a"], timeout=3)
51
+ assert isinstance(results[0], Exception)
52
+ assert results[1].body == b"A"
53
+
54
+
55
+ async def test_many_requests_bounded_concurrency(server):
56
+ base = f"http://127.0.0.1:{server.port}"
57
+ results = await fetch_all([f"{base}/a"] * 500, concurrency=50)
58
+ assert len(results) == 500
59
+ assert all(r.status == 200 for r in results)
@@ -0,0 +1,66 @@
1
+ import pytest
2
+
3
+ from mini_aiohttp import ClientSession, Response, Server
4
+
5
+
6
+ @pytest.fixture
7
+ async def server():
8
+ srv = Server()
9
+
10
+ @srv.route("/users/{id}")
11
+ async def get_user(req):
12
+ return Response(200, f"user={req.params['id']}")
13
+
14
+ @srv.route("/users/{uid}/posts/{pid}")
15
+ async def get_post(req):
16
+ return Response(200, f"{req.params['uid']}:{req.params['pid']}")
17
+
18
+ @srv.route("/users/me")
19
+ async def me(req):
20
+ return Response(200, "me")
21
+
22
+ @srv.route("/users/{id}", method="DELETE")
23
+ async def delete_user(req):
24
+ return Response(200, "deleted")
25
+
26
+ @srv.middleware_
27
+ async def add_header(req, call_next):
28
+ resp = await call_next(req)
29
+ resp.headers["x-powered-by"] = "mini-aiohttp"
30
+ return resp
31
+
32
+ await srv.start("127.0.0.1", 0)
33
+ yield srv
34
+ await srv.close()
35
+
36
+
37
+ async def test_path_param_is_captured(server):
38
+ async with ClientSession() as s:
39
+ r = await s.get(f"http://127.0.0.1:{server.port}/users/42")
40
+ assert r.body == b"user=42"
41
+
42
+
43
+ async def test_multiple_path_params(server):
44
+ async with ClientSession() as s:
45
+ r = await s.get(f"http://127.0.0.1:{server.port}/users/7/posts/99")
46
+ assert r.body == b"7:99"
47
+
48
+
49
+ async def test_literal_route_beats_param_route(server):
50
+ async with ClientSession() as s:
51
+ r = await s.get(f"http://127.0.0.1:{server.port}/users/me")
52
+ assert r.body == b"me"
53
+
54
+
55
+ async def test_same_path_different_method(server):
56
+ async with ClientSession() as s:
57
+ r = await s.request("DELETE", f"http://127.0.0.1:{server.port}/users/5")
58
+ assert r.body == b"deleted"
59
+
60
+
61
+ async def test_middleware_runs_on_every_response(server):
62
+ async with ClientSession() as s:
63
+ ok = await s.get(f"http://127.0.0.1:{server.port}/users/1")
64
+ missing = await s.get(f"http://127.0.0.1:{server.port}/nope")
65
+ assert ok.headers["x-powered-by"] == "mini-aiohttp"
66
+ assert missing.status == 404
@@ -1,7 +0,0 @@
1
- """mini-aiohttp: a small, dependency-free async HTTP/1.1 client and server."""
2
- from .client import ClientError, ClientSession
3
- from .response import Request, Response
4
- from .server import Server
5
-
6
- __version__ = "0.1.0"
7
- __all__ = ["ClientSession", "ClientError", "Server", "Request", "Response"]
File without changes
File without changes
File without changes