Skip to content

Commit adc601f

Browse files
authored
[CLEAN] general (#11)
1 parent 060da84 commit adc601f

File tree

3 files changed

+27
-28
lines changed

3 files changed

+27
-28
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
![Python](https://github.com/raphaelauv/fastAPI-aiohttp-example/workflows/Python/badge.svg?branch=master)
22

3-
# Full exemple of [FastAPI](https://github.com/tiangolo/fastapi) with an [aiohttp](https://github.com/aio-libs/aiohttp) client
3+
# Full example of [FastAPI](https://github.com/tiangolo/fastapi) with an [aiohttp](https://github.com/aio-libs/aiohttp) client
4+
5+
### This is an example with FastAPI, but you can use this logic with any async ( ASGI ) web framework
6+
7+
8+
### [EXAMPLE FOR HTTPX](https://github.com/raphaelauv/fastAPI-httpx-example/)
49

5-
### This is an exemple with FastAPI but you can use this logic with any async ( ASGI ) web framework
6-
#
710

811
#### Implemented logic :
912

src/fastAPI_aiohttp/fastAPI.py

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import json
21
import asyncio
32
from collections.abc import Coroutine
43
from socket import AF_INET
54
from typing import List, Optional, Any, Dict
65

76
import aiohttp
8-
from aioresponses import aioresponses
97
from fastapi import FastAPI
108
from fastapi.logger import logger as fastAPI_logger # convenient name
119
from fastapi.requests import Request
@@ -65,29 +63,20 @@ async def on_shutdown() -> None:
6563
@app.get('/endpoint')
6664
async def endpoint() -> Any:
6765
url = "http://localhost:8080/test"
68-
69-
with aioresponses() as mock_server: # mock answer , remove in real
70-
mock_server.post(url=url, status=200, body=json.dumps({"succes": 1}))
71-
72-
rst = await SingletonAiohttp.query_url(url)
73-
return rst
66+
return await SingletonAiohttp.query_url(url)
7467

7568

7669
@app.get('/endpoint_multi')
7770
async def endpoint_multi() -> Dict[str, int]:
7871
url = "http://localhost:8080/test"
7972

80-
with aioresponses() as mock_server: # mock answer , remove in real
81-
mock_server.post(url=url, status=200, body=json.dumps({"succes": 1}))
82-
mock_server.post(url=url, status=200, body=json.dumps({"succes": 2}))
83-
84-
async_calls: List[Coroutine[Any, Any, Any]] = list() # store all async operations
73+
async_calls: List[Coroutine[Any, Any, Any]] = list() # store all async operations
8574

86-
async_calls.append(SingletonAiohttp.query_url(url))
87-
async_calls.append(SingletonAiohttp.query_url(url))
75+
async_calls.append(SingletonAiohttp.query_url(url))
76+
async_calls.append(SingletonAiohttp.query_url(url))
8877

89-
all_results: List[Dict[Any, Any]] = await asyncio.gather(*async_calls) # wait for all async operations
90-
return {'succes': sum([x['succes'] for x in all_results])}
78+
all_results: List[Dict[Any, Any]] = await asyncio.gather(*async_calls) # wait for all async operations
79+
return {'success': sum([x['success'] for x in all_results])}
9180

9281

9382
@app.post("/endpoint_stream/")

src/tests/test_fastAPI_aiohttp.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@
77

88
from fastAPI_aiohttp.fastAPI import app, SingletonAiohttp
99

10-
url = "test/toto"
11-
1210

1311
@pytest.fixture
1412
def client_aio():
1513
with aioresponses() as m:
16-
m.post(url=url,
14+
m.post(url="test/toto",
1715
status=200,
1816
body=json.dumps({"result": 2}))
1917
yield m
@@ -26,24 +24,33 @@ def client_fastAPI():
2624

2725
@pytest.mark.asyncio
2826
async def test_query_url(client_aio):
29-
rst = await SingletonAiohttp.query_url(url)
27+
rst = await SingletonAiohttp.query_url("test/toto")
3028
assert rst == {"result": 2}
3129

3230

3331
def test_endpoint(client_fastAPI):
34-
result: httpx.Response = client_fastAPI.get(url='/endpoint/')
32+
url = "http://localhost:8080/test"
33+
with aioresponses() as mock_server:
34+
mock_server.post(url=url, status=200, body=json.dumps({"success": 1}))
35+
36+
result: httpx.Response = client_fastAPI.get(url='/endpoint/')
3537
assert result is not None
3638

3739
result_json = result.json()
38-
assert result_json == {'succes': 1}
40+
assert result_json == {'success': 1}
3941

4042

4143
def test_endpoint_multi(client_fastAPI):
42-
result: httpx.Response = client_fastAPI.get(url='/endpoint_multi/')
44+
url = "http://localhost:8080/test"
45+
with aioresponses() as mock_server:
46+
mock_server.post(url=url, status=200, body=json.dumps({"success": 1}))
47+
mock_server.post(url=url, status=200, body=json.dumps({"success": 2}))
48+
49+
result: httpx.Response = client_fastAPI.get(url='/endpoint_multi/')
4350
assert result is not None
4451

4552
result_json = result.json()
46-
assert result_json == {'succes': 3}
53+
assert result_json == {'success': 3}
4754

4855

4956
def test_endpoint_stream(client_fastAPI):

0 commit comments

Comments
 (0)