Skip to content

Commit ab3e39a

Browse files
seanp1c2u
sean
authored andcommitted
Add docs for aiohttp integration and some comments to the test suite.
1 parent 35d15e7 commit ab3e39a

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

docs/integrations.rst

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,3 +336,33 @@ You can use ``WerkzeugOpenAPIResponse`` as a Werkzeug response factory:
336336
337337
openapi_response = WerkzeugOpenAPIResponse(werkzeug_response)
338338
result = unmarshal_response(openapi_request, openapi_response, spec=spec)
339+
340+
341+
aiohttp.web
342+
---------
343+
344+
This section describes integration with `aiohttp.web <https://docs.aiohttp.org/en/stable/web.html>`__ framework.
345+
346+
Low level
347+
~~~~~~~~~
348+
349+
You can use ``AIOHTTPOpenAPIWebRequest`` as an aiohttp request factory:
350+
351+
.. code-block:: python
352+
353+
from openapi_core import unmarshal_request
354+
from openapi_core.contrib.aiohttp import AIOHTTPOpenAPIWebRequest
355+
356+
request_body = await aiohttp_request.text()
357+
openapi_request = AIOHTTPOpenAPIWebRequest(aiohttp_request, body=request_body)
358+
result = unmarshal_request(openapi_request, spec=spec)
359+
360+
You can use ``AIOHTTPOpenAPIWebRequest`` as an aiohttp response factory:
361+
362+
.. code-block:: python
363+
364+
from openapi_core import unmarshal_response
365+
from openapi_core.contrib.starlette import AIOHTTPOpenAPIWebRequest
366+
367+
openapi_response = StarletteOpenAPIResponse(aiohttp_response)
368+
result = unmarshal_response(openapi_request, openapi_response, spec=spec)

tests/integration/contrib/aiohttp/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def spec(factory):
2222

2323
@pytest.fixture
2424
def response_getter() -> mock.MagicMock:
25+
# Using a mock here allows us to control the return value for different scenarios.
2526
return mock.MagicMock(return_value={"data": "data"})
2627

2728

tests/integration/contrib/aiohttp/test_aiohttp_validation.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ async def test_aiohttp_integration_valid_input(client: TestClient):
2626
headers=given_headers,
2727
)
2828
response_data = await response.json()
29+
# Then
2930
assert response.status == expected_status_code
3031
assert response_data == expected_response_data
3132

@@ -52,5 +53,6 @@ async def test_aiohttp_integration_invalid_input(
5253
headers=given_headers,
5354
)
5455
response_data = await response.json()
56+
# Then
5557
assert response.status == expected_status_code
5658
assert response_data == expected_response_data

0 commit comments

Comments
 (0)