Skip to content

Commit

Permalink
BREAK: remove the request_with functionality 💥
Browse files Browse the repository at this point in the history
It used to be a workaround in the early versions and is not needed now
  • Loading branch information
eigenein committed Nov 13, 2023
1 parent e8daf6e commit 2fd20bb
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 86 deletions.
29 changes: 11 additions & 18 deletions combadge/support/httpx/backends/async_.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from __future__ import annotations

from contextlib import AbstractAsyncContextManager
from types import TracebackType
from typing import Any, Callable
from typing import Any

from httpx import AsyncClient, Response
from pydantic import TypeAdapter
Expand All @@ -14,32 +13,27 @@
from combadge.core.signature import Signature
from combadge.support.http.request import Request
from combadge.support.httpx.backends.base import BaseHttpxBackend
from combadge.support.shared.async_ import SupportsRequestWith
from combadge.support.shared.contextlib import asyncnullcontext


class HttpxBackend(BaseHttpxBackend[AsyncClient], SupportsRequestWith[Request], ServiceContainer):
class HttpxBackend(BaseHttpxBackend[AsyncClient], ServiceContainer):
"""Async HTTPX backend."""

__slots__ = ("_client", "_request_with", "_service_cache", "_raise_for_status")
__slots__ = ("_client", "_service_cache", "_raise_for_status")

def __init__(
self,
client: AsyncClient,
*,
request_with: Callable[[Any], AbstractAsyncContextManager] = asyncnullcontext,
raise_for_status: bool = True,
) -> None:
"""
Instantiate the backend.
Args:
client: [HTTPX client](https://www.python-httpx.org/advanced/#client-instances)
request_with: an optional context manager getter to wrap each request into
raise_for_status: automatically call `raise_for_status()`
"""
BaseHttpxBackend.__init__(self, client, raise_for_status=raise_for_status)
SupportsRequestWith.__init__(self, request_with)
ServiceContainer.__init__(self)

def bind_method(self, signature: Signature) -> ServiceMethod[HttpxBackend]: # noqa: D102
Expand All @@ -48,15 +42,14 @@ def bind_method(self, signature: Signature) -> ServiceMethod[HttpxBackend]: # n

async def bound_method(self: BaseBoundService[HttpxBackend], *args: Any, **kwargs: Any) -> Any:
request = signature.build_request(Request, self, args, kwargs)
async with self.backend._request_with(request):
response: Response = await backend._client.request(
request.get_method(),
request.get_url_path(),
json=request.payload,
data=request.form_data,
params=request.query_params,
headers=request.headers,
)
response: Response = await backend._client.request(
request.get_method(),
request.get_url_path(),
json=request.payload,
data=request.form_data,
params=request.query_params,
headers=request.headers,
)
return signature.apply_response_markers(response, backend._parse_response(response), response_type)

return bound_method # type: ignore[return-value]
Expand Down
28 changes: 11 additions & 17 deletions combadge/support/httpx/backends/sync.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from __future__ import annotations

from contextlib import AbstractContextManager, nullcontext
from types import TracebackType
from typing import Any, Callable
from typing import Any

from httpx import Client, Response
from pydantic import TypeAdapter
Expand All @@ -14,31 +13,27 @@
from combadge.core.signature import Signature
from combadge.support.http.request import Request
from combadge.support.httpx.backends.base import BaseHttpxBackend
from combadge.support.shared.sync import SupportsRequestWith


class HttpxBackend(BaseHttpxBackend[Client], SupportsRequestWith[Request], ServiceContainer):
class HttpxBackend(BaseHttpxBackend[Client], ServiceContainer):
"""Sync HTTPX backend."""

__slots__ = ("_client", "_request_with", "_service_cache", "_raise_for_status")
__slots__ = ("_client", "_service_cache", "_raise_for_status")

def __init__(
self,
client: Client,
*,
request_with: Callable[[Any], AbstractContextManager] = nullcontext,
raise_for_status: bool = True,
) -> None:
"""
Instantiate the backend.
Args:
client: [HTTPX client](https://www.python-httpx.org/advanced/#client-instances)
request_with: an optional context manager getter to wrap each request into
raise_for_status: automatically call `raise_for_status()`
"""
BaseHttpxBackend.__init__(self, client, raise_for_status=raise_for_status)
SupportsRequestWith.__init__(self, request_with)
ServiceContainer.__init__(self)

def bind_method(self, signature: Signature) -> ServiceMethod[HttpxBackend]: # noqa: D102
Expand All @@ -47,15 +42,14 @@ def bind_method(self, signature: Signature) -> ServiceMethod[HttpxBackend]: # n

def bound_method(self: BaseBoundService[HttpxBackend], *args: Any, **kwargs: Any) -> Any:
request = signature.build_request(Request, self, args, kwargs)
with self.backend._request_with(request):
response: Response = backend._client.request(
request.get_method(),
request.get_url_path(),
json=request.payload,
data=request.form_data,
params=request.query_params,
headers=request.headers,
)
response: Response = backend._client.request(
request.get_method(),
request.get_url_path(),
json=request.payload,
data=request.form_data,
params=request.query_params,
headers=request.headers,
)
return signature.apply_response_markers(response, backend._parse_response(response), response_type)

return bound_method # type: ignore[return-value]
Expand Down
9 changes: 0 additions & 9 deletions combadge/support/shared/async_.py

This file was deleted.

10 changes: 0 additions & 10 deletions combadge/support/shared/contextlib.py

This file was deleted.

9 changes: 0 additions & 9 deletions combadge/support/shared/sync.py

This file was deleted.

15 changes: 3 additions & 12 deletions combadge/support/zeep/backends/async_.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from __future__ import annotations

from collections.abc import Collection
from contextlib import AbstractAsyncContextManager
from os import PathLike, fspath
from ssl import SSLContext
from types import TracebackType
from typing import Any, Callable
from typing import Any

import httpx
from typing_extensions import Self
Expand All @@ -20,20 +19,17 @@
from combadge.core.binder import BaseBoundService
from combadge.core.interfaces import ServiceMethod
from combadge.core.signature import Signature
from combadge.support.shared.async_ import SupportsRequestWith
from combadge.support.shared.contextlib import asyncnullcontext
from combadge.support.soap.request import Request
from combadge.support.zeep.backends.base import BaseZeepBackend, ByBindingName, ByServiceName


class ZeepBackend(
BaseZeepBackend[AsyncServiceProxy, AsyncOperationProxy],
SupportsRequestWith[Request],
ServiceContainer,
):
"""Asynchronous Zeep service."""

__slots__ = ("_service", "_request_with", "_service_cache")
__slots__ = ("_service", "_service_cache")

@classmethod
def with_params(
Expand Down Expand Up @@ -90,18 +86,14 @@ def with_params(
def __init__(
self,
service: AsyncServiceProxy,
*,
request_with: Callable[[Any], AbstractAsyncContextManager] = asyncnullcontext,
) -> None:
"""
Instantiate the backend.
Args:
service: [service proxy object](https://docs.python-zeep.org/en/master/client.html#the-serviceproxy-object)
request_with: an optional context manager getter to wrap each request into
"""
BaseZeepBackend.__init__(self, service)
SupportsRequestWith.__init__(self, request_with)
ServiceContainer.__init__(self)

def bind_method(self, signature: Signature) -> ServiceMethod[ZeepBackend]: # noqa: D102
Expand All @@ -112,8 +104,7 @@ async def bound_method(self: BaseBoundService[ZeepBackend], *args: Any, **kwargs
request = signature.build_request(Request, self, args, kwargs)
operation = backend._get_operation(request.get_operation_name())
try:
async with self.backend._request_with(request):
response = await operation(**(request.payload or {}))
response = await operation(**(request.payload or {}))
except Fault as e:
return backend._parse_soap_fault(e, fault_type)
else:
Expand Down
15 changes: 4 additions & 11 deletions combadge/support/zeep/backends/sync.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from __future__ import annotations

from contextlib import AbstractContextManager, nullcontext
from os import PathLike, fspath
from types import TracebackType
from typing import Any, Callable, Collection
from typing import Any, Collection

from typing_extensions import Self
from zeep import Client, Plugin, Transport
Expand All @@ -16,15 +15,14 @@
from combadge.core.binder import BaseBoundService
from combadge.core.interfaces import ServiceMethod
from combadge.core.signature import Signature
from combadge.support.shared.sync import SupportsRequestWith
from combadge.support.soap.request import Request
from combadge.support.zeep.backends.base import BaseZeepBackend, ByBindingName, ByServiceName


class ZeepBackend(BaseZeepBackend[ServiceProxy, OperationProxy], SupportsRequestWith[Request], ServiceContainer):
class ZeepBackend(BaseZeepBackend[ServiceProxy, OperationProxy], ServiceContainer):
"""Synchronous Zeep service."""

__slots__ = ("_service", "_request_with", "_service_cache")
__slots__ = ("_service", "_service_cache")

@classmethod
def with_params(
Expand Down Expand Up @@ -69,18 +67,14 @@ def with_params(
def __init__(
self,
service: ServiceProxy,
*,
request_with: Callable[[Any], AbstractContextManager] = nullcontext,
) -> None:
"""
Instantiate the backend.
Args:
service: [service proxy object](https://docs.python-zeep.org/en/master/client.html#the-serviceproxy-object)
request_with: an optional context manager getter to wrap each request into
"""
BaseZeepBackend.__init__(self, service)
SupportsRequestWith.__init__(self, request_with)
ServiceContainer.__init__(self)

def bind_method(self, signature: Signature) -> ServiceMethod[ZeepBackend]: # noqa: D102
Expand All @@ -91,8 +85,7 @@ def bound_method(self: BaseBoundService[ZeepBackend], *args: Any, **kwargs: Any)
request = signature.build_request(Request, self, args, kwargs)
operation = backend._get_operation(request.get_operation_name())
try:
with self.backend._request_with(request):
response = operation(**(request.payload or {}))
response = operation(**(request.payload or {}))
except Fault as e:
return backend._parse_soap_fault(e, fault_type)
else:
Expand Down

0 comments on commit 2fd20bb

Please sign in to comment.