mini-aiohttp 0.1.0__tar.gz → 0.1.2__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.
- {mini_aiohttp-0.1.0 → mini_aiohttp-0.1.2}/PKG-INFO +2 -2
- mini_aiohttp-0.1.2/mini_aiohttp/__init__.py +16 -0
- mini_aiohttp-0.1.2/mini_aiohttp/batch.py +72 -0
- {mini_aiohttp-0.1.0 → mini_aiohttp-0.1.2}/mini_aiohttp/response.py +1 -0
- {mini_aiohttp-0.1.0 → mini_aiohttp-0.1.2}/mini_aiohttp/server.py +71 -7
- {mini_aiohttp-0.1.0 → mini_aiohttp-0.1.2}/mini_aiohttp.egg-info/PKG-INFO +2 -2
- {mini_aiohttp-0.1.0 → mini_aiohttp-0.1.2}/mini_aiohttp.egg-info/SOURCES.txt +3 -0
- {mini_aiohttp-0.1.0 → mini_aiohttp-0.1.2}/pyproject.toml +5 -3
- mini_aiohttp-0.1.2/tests/test_batch.py +59 -0
- mini_aiohttp-0.1.2/tests/test_routing.py +66 -0
- mini_aiohttp-0.1.0/mini_aiohttp/__init__.py +0 -7
- {mini_aiohttp-0.1.0 → mini_aiohttp-0.1.2}/LICENSE +0 -0
- {mini_aiohttp-0.1.0 → mini_aiohttp-0.1.2}/README.md +0 -0
- {mini_aiohttp-0.1.0 → mini_aiohttp-0.1.2}/mini_aiohttp/client.py +0 -0
- {mini_aiohttp-0.1.0 → mini_aiohttp-0.1.2}/mini_aiohttp.egg-info/dependency_links.txt +0 -0
- {mini_aiohttp-0.1.0 → mini_aiohttp-0.1.2}/mini_aiohttp.egg-info/requires.txt +0 -0
- {mini_aiohttp-0.1.0 → mini_aiohttp-0.1.2}/mini_aiohttp.egg-info/top_level.txt +0 -0
- {mini_aiohttp-0.1.0 → mini_aiohttp-0.1.2}/setup.cfg +0 -0
- {mini_aiohttp-0.1.0 → mini_aiohttp-0.1.2}/tests/test_client.py +0 -0
- {mini_aiohttp-0.1.0 → mini_aiohttp-0.1.2}/tests/test_server.py +0 -0
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: mini-aiohttp
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.2
|
|
4
4
|
Summary: A small, dependency-free async HTTP/1.1 client and server built on asyncio
|
|
5
|
-
License: MIT
|
|
5
|
+
License-Expression: MIT
|
|
6
6
|
Requires-Python: >=3.9
|
|
7
7
|
Description-Content-Type: text/markdown
|
|
8
8
|
License-File: LICENSE
|
|
@@ -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.2"
|
|
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
|
+
)
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"""Asyncio HTTP/1.1 server with keep-alive,
|
|
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,27 @@ 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 _param_count(pattern: list) -> int:
|
|
33
|
+
return sum(1 for seg in pattern if seg.startswith("{") and seg.endswith("}"))
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _match_pattern(pattern: list, segments: list):
|
|
37
|
+
"""Return a dict of captured params if the path matches the pattern, else None."""
|
|
38
|
+
if len(pattern) != len(segments):
|
|
39
|
+
return None
|
|
40
|
+
params = {}
|
|
41
|
+
for p, s in zip(pattern, segments):
|
|
42
|
+
if p.startswith("{") and p.endswith("}"):
|
|
43
|
+
params[p[1:-1]] = s
|
|
44
|
+
elif p != s:
|
|
45
|
+
return None
|
|
46
|
+
return params
|
|
47
|
+
|
|
48
|
+
|
|
28
49
|
def _check_fd_limit(needed: int) -> None:
|
|
29
50
|
"""Warn early if the OS will refuse sockets before we hit max_connections."""
|
|
30
51
|
try:
|
|
@@ -49,7 +70,8 @@ class Server:
|
|
|
49
70
|
max_body_size: int = 10 * 1024 * 1024,
|
|
50
71
|
backlog: int = 4096,
|
|
51
72
|
):
|
|
52
|
-
self.routes:
|
|
73
|
+
self.routes: list = [] # (method, pattern_segments, handler)
|
|
74
|
+
self.middleware: list = [] # async fn(request, call_next) -> Response
|
|
53
75
|
self.max_connections = max_connections
|
|
54
76
|
self.read_timeout = read_timeout
|
|
55
77
|
self.max_body_size = max_body_size
|
|
@@ -60,23 +82,59 @@ class Server:
|
|
|
60
82
|
|
|
61
83
|
# ---------- routing ----------
|
|
62
84
|
def route(self, path: str, method: str = "GET"):
|
|
85
|
+
"""Register a handler. Use {name} in the path to capture a segment: '/users/{id}'."""
|
|
63
86
|
def decorator(handler):
|
|
64
|
-
self.routes
|
|
87
|
+
self.routes.append((method.upper(), _split_path(path), handler))
|
|
88
|
+
# Routes with fewer {params} are tried first, so '/users/me' beats '/users/{id}'
|
|
89
|
+
# no matter which one was registered first. sort() is stable, so ties keep order.
|
|
90
|
+
self.routes.sort(key=lambda r: _param_count(r[1]))
|
|
65
91
|
return handler
|
|
66
92
|
return decorator
|
|
67
93
|
|
|
94
|
+
def middleware_(self, fn):
|
|
95
|
+
"""Register middleware. It runs for every routed request, in the order added.
|
|
96
|
+
|
|
97
|
+
@app.middleware_
|
|
98
|
+
async def log(req, call_next):
|
|
99
|
+
resp = await call_next(req)
|
|
100
|
+
return resp
|
|
101
|
+
"""
|
|
102
|
+
self.middleware.append(fn)
|
|
103
|
+
return fn
|
|
104
|
+
|
|
105
|
+
def _match(self, method: str, path: str):
|
|
106
|
+
segments = _split_path(path)
|
|
107
|
+
path_seen = False
|
|
108
|
+
for route_method, pattern, handler in self.routes:
|
|
109
|
+
params = _match_pattern(pattern, segments)
|
|
110
|
+
if params is None:
|
|
111
|
+
continue
|
|
112
|
+
path_seen = True
|
|
113
|
+
if route_method == method:
|
|
114
|
+
return handler, params, True
|
|
115
|
+
return None, None, path_seen
|
|
116
|
+
|
|
68
117
|
async def _dispatch(self, req: Request) -> Response:
|
|
69
|
-
handler = self.
|
|
118
|
+
handler, params, path_seen = self._match(req.method, req.path)
|
|
70
119
|
if handler is None:
|
|
71
|
-
if
|
|
120
|
+
if path_seen:
|
|
72
121
|
return Response(405, "Method Not Allowed")
|
|
73
122
|
return Response(404, "Not Found")
|
|
123
|
+
req.params = params
|
|
124
|
+
|
|
125
|
+
async def call_handler(request: Request) -> Response:
|
|
126
|
+
result = await handler(request)
|
|
127
|
+
return result if isinstance(result, Response) else Response(200, result)
|
|
128
|
+
|
|
129
|
+
chain = call_handler
|
|
130
|
+
for mw in reversed(self.middleware):
|
|
131
|
+
chain = _wrap(mw, chain)
|
|
132
|
+
|
|
74
133
|
try:
|
|
75
|
-
|
|
134
|
+
return await chain(req)
|
|
76
135
|
except Exception:
|
|
77
136
|
log.exception("handler failed: %s %s", req.method, req.path)
|
|
78
137
|
return Response(500, "Internal Server Error")
|
|
79
|
-
return result if isinstance(result, Response) else Response(200, result)
|
|
80
138
|
|
|
81
139
|
# ---------- connection handling ----------
|
|
82
140
|
async def _on_connect(self, reader, writer):
|
|
@@ -199,3 +257,9 @@ class Server:
|
|
|
199
257
|
writer.close()
|
|
200
258
|
if self._server is not None:
|
|
201
259
|
await self._server.wait_closed()
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
def _wrap(mw, nxt):
|
|
263
|
+
async def inner(req: Request) -> Response:
|
|
264
|
+
return await mw(req, nxt)
|
|
265
|
+
return inner
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: mini-aiohttp
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.2
|
|
4
4
|
Summary: A small, dependency-free async HTTP/1.1 client and server built on asyncio
|
|
5
|
-
License: MIT
|
|
5
|
+
License-Expression: MIT
|
|
6
6
|
Requires-Python: >=3.9
|
|
7
7
|
Description-Content-Type: text/markdown
|
|
8
8
|
License-File: LICENSE
|
|
@@ -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
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
[build-system]
|
|
2
|
-
requires = ["setuptools>=
|
|
2
|
+
requires = ["setuptools>=77", "wheel"]
|
|
3
3
|
build-backend = "setuptools.build_meta"
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "mini-aiohttp"
|
|
7
|
-
version = "0.1.
|
|
7
|
+
version = "0.1.2"
|
|
8
8
|
description = "A small, dependency-free async HTTP/1.1 client and server built on asyncio"
|
|
9
9
|
readme = "README.md"
|
|
10
|
-
license =
|
|
10
|
+
license = "MIT"
|
|
11
|
+
license-files = ["LICENSE"]
|
|
11
12
|
requires-python = ">=3.9"
|
|
12
13
|
dependencies = []
|
|
13
14
|
|
|
@@ -19,4 +20,5 @@ include = ["mini_aiohttp*"]
|
|
|
19
20
|
|
|
20
21
|
[tool.pytest.ini_options]
|
|
21
22
|
asyncio_mode = "auto"
|
|
23
|
+
asyncio_default_fixture_loop_scope = "function"
|
|
22
24
|
testpaths = ["tests"]
|
|
@@ -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
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|