Skip to content

Commit

Permalink
use as context managers
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-emelin committed Feb 12, 2024
1 parent 0178983 commit 64e39ee
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 10 deletions.
6 changes: 6 additions & 0 deletions cent/client/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,9 @@ async def __call__(
:return: Centrifugo response
"""
return await self._session(self._api_key, request, timeout=request_timeout)

async def __aenter__(self) -> "AsyncClient":
return self

async def __aexit__(self, *kwargs: Any) -> None:
await self.close()
8 changes: 7 additions & 1 deletion cent/client/grpc_client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional, Dict, TypeVar, List
from typing import Optional, Dict, TypeVar, List, Any

from cent.client.session import GrpcSession
from cent.base import CentRequest
Expand Down Expand Up @@ -244,3 +244,9 @@ async def __call__(
:return: Centrifugo response
"""
return await self._session(request, request_timeout)

async def __aenter__(self) -> "GrpcClient":
return self

async def __aexit__(self, *kwargs: Any) -> None:
await self.close()
3 changes: 2 additions & 1 deletion cent/client/session/aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ async def make_request(
timeout: Optional[float] = None,
) -> CentType:
session = self._session
session.headers["X-API-Key"] = api_key
if api_key:
session.headers["X-API-Key"] = api_key

if isinstance(request, BatchRequest):
json_data = self.get_batch_json_data(request)
Expand Down
8 changes: 1 addition & 7 deletions cent/client/session/base_http_async.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from abc import ABC, abstractmethod
from typing import Any, Optional, cast
from typing import Optional, cast

from cent.client.session.base_http import BaseHttpSession
from cent.base import CentType, CentRequest
Expand Down Expand Up @@ -36,9 +36,3 @@ async def __call__(
timeout: Optional[float] = None,
) -> CentType:
return cast(CentType, await self.make_request(api_key, request, timeout))

async def __aenter__(self) -> "BaseHttpAsyncSession":
return self

async def __aexit__(self, *kwargs: Any) -> None:
await self.close()
3 changes: 2 additions & 1 deletion cent/client/session/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ def make_request(
request: CentRequest[CentType],
timeout: Optional[float] = None,
) -> CentType:
self._session.headers["X-API-Key"] = api_key
if api_key:
self._session.headers["X-API-Key"] = api_key
if isinstance(request, BatchRequest):
json_data = self.get_batch_json_data(request)
else:
Expand Down
6 changes: 6 additions & 0 deletions cent/client/sync_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,9 @@ def __call__(self, request: CentRequest[T], request_timeout: Optional[float] = N
:return: Centrifugo response
"""
return self._session(self._api_key, request, timeout=request_timeout)

def __enter__(self) -> "Client":
return self

def __exit__(self, *kwargs: Any) -> None:
self.close()

0 comments on commit 64e39ee

Please sign in to comment.