Skip to content

Commit

Permalink
OPT: use self.__combadge_backend__ instead of capturing backend f…
Browse files Browse the repository at this point in the history
…rom outside a bound method ♻️
  • Loading branch information
eigenein committed Nov 28, 2024
1 parent b15990a commit 72edee1
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
11 changes: 10 additions & 1 deletion combadge/core/binder.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class BoundService(BaseBoundService, from_protocol): # type: ignore[misc, valid
signature = Signature.from_method(method)
bound_method: ServiceMethod = bind_method(signature) # generate implementation by the backend
update_wrapper(bound_method, method)
bound_method = _wrap(bound_method, signature.method_markers) # apply user decorators
bound_method = _wrap(bound_method, signature.method_markers)
bound_method = override(bound_method) # no functional change, just possibly setting `__override__`
setattr(BoundService, name, bound_method)

Expand All @@ -63,6 +63,15 @@ class BoundService(BaseBoundService, from_protocol): # type: ignore[misc, valid


def _wrap(method: Callable[..., Any], with_markers: Iterable[MethodMarker]) -> Callable[..., Any]:
"""
Apply method markers.
Method markers may wrap or modify the bound method.
Args:
method: bound method in the protocol implementation
with_markers: method markers to apply
"""
for marker in with_markers:
method = marker.wrap(method)
return method
Expand Down
5 changes: 2 additions & 3 deletions combadge/support/httpx/backends/async_.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,20 @@ def __init__(
ServiceContainer.__init__(self)

def bind_method(self, signature: Signature) -> ServiceMethod[HttpxBackend]: # noqa: D102
backend = self
response_type: TypeAdapter[Any] = TypeAdapter(signature.return_type)

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

return bound_method # type: ignore[return-value]
Expand Down
5 changes: 2 additions & 3 deletions combadge/support/httpx/backends/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,20 @@ def __init__(
ServiceContainer.__init__(self)

def bind_method(self, signature: Signature) -> ServiceMethod[HttpxBackend]: # noqa: D102
backend = self
response_type: TypeAdapter[Any] = TypeAdapter(signature.return_type)

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

return bound_method # type: ignore[return-value]
Expand Down
5 changes: 2 additions & 3 deletions combadge/support/zeep/backends/async_.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,14 @@ def __init__(

def bind_method(self, signature: Signature) -> ServiceMethod[ZeepBackend]: # noqa: D102
response_type, fault_type = self._adapt_response_type(signature.return_type)
backend = self

async def bound_method(self: BaseBoundService[ZeepBackend], *args: Any, **kwargs: Any) -> Any:
request = signature.build_request(Request, self, args, kwargs)
operation = backend._get_operation(request.get_operation_name())
operation = self.__combadge_backend__._get_operation(request.get_operation_name())
try:
response = await operation(**(request.payload or {}), _soapheaders=request.soap_header)
except Fault as e:
return backend._parse_soap_fault(e, fault_type)
return self.__combadge_backend__._parse_soap_fault(e, fault_type)
except Exception as e:
raise BackendError(e) from e
else:
Expand Down
5 changes: 2 additions & 3 deletions combadge/support/zeep/backends/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,14 @@ def __init__(

def bind_method(self, signature: Signature) -> ServiceMethod[ZeepBackend]: # noqa: D102
response_type, fault_type = self._adapt_response_type(signature.return_type)
backend = self

def bound_method(self: BaseBoundService[ZeepBackend], *args: Any, **kwargs: Any) -> Any:
request = signature.build_request(Request, self, args, kwargs)
operation = backend._get_operation(request.get_operation_name())
operation = self.__combadge_backend__._get_operation(request.get_operation_name())
try:
response = operation(**(request.payload or {}), _soapheaders=request.soap_header)
except Fault as e:
return backend._parse_soap_fault(e, fault_type)
return self.__combadge_backend__._parse_soap_fault(e, fault_type)
except Exception as e:
raise BackendError(e) from e
else:
Expand Down

0 comments on commit 72edee1

Please sign in to comment.