Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions aiopenapi3/extra/cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ def info(self) -> email.message.Message:
def __init__(
self, cookiejar: http.cookiejar.CookieJar = None, policy: Literal["jar", "securitySchemes"] = "jar"
) -> None:
"""

:param cookiejar: a cookiejar.CookieJar instance
:param policy: There are two policies:

* securitySchemes - only cookies whose name is in the securitySchemes are used. To match authentication \
requirements credentials are set via OpenAPI.authenticate(name=value)
* jar - all cookies are used, basically like a browser would do. Cookies not mentioned in securitySchemes \
are set besides OpenAPI.authenticate() to allow using them without adjusting the description document.
"""
self.cookiejar: http.cookiejar.CookieJar = cookiejar or http.cookiejar.CookieJar()
self.policy: Literal["jar", "securitySchemes"] = policy
self.schemes: dict[str, str] = dict()
Expand Down
20 changes: 13 additions & 7 deletions aiopenapi3/v30/glue.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,25 +599,31 @@

if content_type.lower() == "application/json":
data = ctx.received
expected_type = getattr(expected_media.schema_, "_target", expected_media.schema_)

try:
data = json.loads(data)
except json.decoder.JSONDecodeError:
raise ResponseDecodingError(self.operation, data, result)
data = self.api.plugins.message.parsed(
ctx = self.api.plugins.message.parsed(
request=self,
operationId=self.operation.operationId,
headers=headers,
parsed=data,
expected_type=getattr(expected_media.schema_, "_target", expected_media.schema_),
expected_type=expected_type,
status_code=status_code,
).parsed
)

data = ctx.parsed
expected_type = ctx.expected_type

if expected_media.schema_ is None:
raise ResponseSchemaError(self.operation, expected_media, expected_media.schema_, result, None)
if expected_type is None:
raise ResponseSchemaError(self.operation, expected_media, expected_type, result, None)

Check warning on line 621 in aiopenapi3/v30/glue.py

View check run for this annotation

Codecov / codecov/patch

aiopenapi3/v30/glue.py#L621

Added line #L621 was not covered by tests

try:
data = expected_media.schema_.model(data)
data = expected_type.model(data)
except pydantic.ValidationError as e:
raise ResponseSchemaError(self.operation, expected_media, expected_media.schema_, result, e)
raise ResponseSchemaError(self.operation, expected_media, expected_type, result, e)

data = self.api.plugins.message.unmarshalled(
request=self, operationId=self.operation.operationId, unmarshalled=data
Expand Down
23 changes: 15 additions & 8 deletions docs/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,11 @@ Cull & Reduce
Reduce & Cull are Plugins limiting the models built to the minimum required to match the requirements of the supplied Operations
Code below will eleminate all schemas not required to serve the operations identified by the pattern/string match and http methods associated.

.. currentmodule:: aiopenapi3.extra
.. autoclass:: Reduce
:members: __init__
.. autoclass:: Cull

.. code:: python

api = OpenAPI.load_sync(
Expand All @@ -392,20 +397,20 @@ Code below will eleminate all schemas not required to serve the operations ident
],
)

.. currentmodule:: aiopenapi3.extra
.. autoclass:: Reduce
:members: __init__
.. autoclass:: Cull


.. inheritance-diagram:: aiopenapi3.plugin.Init aiopenapi3.plugin.Document aiopenapi3.extra.Cull aiopenapi3.extra.Reduce
:top-classes: aiopenapi3.plugin.Plugin
:parts: -2


Cookies
-------

This plugin deals with cookies from responses and adds in requests based on a policy.

There are two policies:
* securitySchemes - only cookies whose name is in the securitySchemes are used. To match authentication requirements credentials are set via OpenAPI.authenticate(name=value)
* jar - all cookies are used, basically like a browser would do. Cookies not mentioned in securitySchemes are set besides OpenAPI.authenticate() to allow using them without adjusting the description document.
.. autoclass:: Cookies
:members: __init__

.. code:: python

Expand All @@ -416,4 +421,6 @@ There are two policies:
],
)

.. autoclass:: Cookies
.. inheritance-diagram:: aiopenapi3.plugin.Plugin aiopenapi3.plugin.Init aiopenapi3.plugin.Message aiopenapi3.extra.cookies.Cookies
:top-classes: aiopenapi3.plugin.Plugin
:parts: -2
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ packages = [
"aiopenapi3",
"aiopenapi3.v20",
"aiopenapi3.v30",
"aiopenapi3.v31"
"aiopenapi3.v31",
"aiopenapi3.extra"
]

[tool.setuptools.dynamic]
Expand Down
Loading