Skip to content

Commit

Permalink
fix format
Browse files Browse the repository at this point in the history
  • Loading branch information
atimin committed Jul 26, 2023
1 parent 944adc3 commit 5dd2db9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 29 deletions.
24 changes: 13 additions & 11 deletions reduct/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ class Client:
"""HTTP Client for Reduct Storage HTTP API"""

def __init__(
self,
url: str,
api_token: Optional[str] = None,
timeout: Optional[float] = None,
extra_headers: Optional[Dict[str, str]] = None,
session: Optional[ClientSession] = None,
self,
url: str,
api_token: Optional[str] = None,
timeout: Optional[float] = None,
extra_headers: Optional[Dict[str, str]] = None,
session: Optional[ClientSession] = None,
):
"""
Constructor
Expand All @@ -115,7 +115,9 @@ def __init__(
>>> client = Client("http://127.0.0.1:8383")
>>> info = await client.info()
"""
self._http = HttpClient(url.rstrip("/"), api_token, timeout, extra_headers, session)
self._http = HttpClient(
url.rstrip("/"), api_token, timeout, extra_headers, session
)

async def __aenter__(self):
self._http._session = ClientSession(timeout=self._http._timeout)
Expand Down Expand Up @@ -163,10 +165,10 @@ async def get_bucket(self, name: str) -> Bucket:
return Bucket(name, self._http)

async def create_bucket(
self,
name: str,
settings: Optional[BucketSettings] = None,
exist_ok: bool = False,
self,
name: str,
settings: Optional[BucketSettings] = None,
exist_ok: bool = False,
) -> Bucket:
"""
Create a new bucket
Expand Down
40 changes: 23 additions & 17 deletions reduct/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ class HttpClient:
FILE_SIZE_FOR_100_CONTINUE = 256_000

def __init__(
self,
url: str,
api_token: Optional[str] = None,
timeout: Optional[float] = None,
extra_headers: Optional[Dict[str, str]] = None,
session: Optional[aiohttp.ClientSession] = None,
self,
url: str,
api_token: Optional[str] = None,
timeout: Optional[float] = None,
extra_headers: Optional[Dict[str, str]] = None,
session: Optional[aiohttp.ClientSession] = None,
):
self._url = url + API_PREFIX
self._api_token = api_token
Expand All @@ -38,7 +38,7 @@ def __init__(

@asynccontextmanager
async def request(
self, method: str, path: str = "", **kwargs
self, method: str, path: str = "", **kwargs
) -> AsyncIterator[ClientResponse]:
"""HTTP request with ReductError exception"""

Expand Down Expand Up @@ -67,23 +67,29 @@ async def request(
if self._session is None:
connector = aiohttp.TCPConnector(force_close=True)
async with aiohttp.ClientSession(
timeout=self._timeout, connector=connector
timeout=self._timeout, connector=connector
) as session:
async with self._request(expect100, extra_headers, kwargs, method, path, session) as response:
async with self._request(
expect100, extra_headers, kwargs, method, path, session
) as response:
yield response
else:
async with self._request(expect100, extra_headers, kwargs, method, path, self._session) as response:
async with self._request(
expect100, extra_headers, kwargs, method, path, self._session
) as response:
yield response

@asynccontextmanager
async def _request(self, expect100, extra_headers, kwargs, method, path, session) -> AsyncIterator[ClientResponse]:
async def _request(
self, expect100, extra_headers, kwargs, method, path, session
) -> AsyncIterator[ClientResponse]:
try:
async with session.request(
method,
f"{self._url}{path.strip()}",
headers=dict(self._headers, **extra_headers),
expect100=expect100,
**kwargs,
method,
f"{self._url}{path.strip()}",
headers=dict(self._headers, **extra_headers),
expect100=expect100,
**kwargs,
) as response:
if self._api_version is None:
self._api_version = response.headers.get("x-reduct-api")
Expand All @@ -108,7 +114,7 @@ async def request_all(self, method: str, path: str = "", **kwargs) -> bytes:
return await response.read()

async def request_chunked(
self, method: str, path: str = "", chunk_size=1024, **kwargs
self, method: str, path: str = "", chunk_size=1024, **kwargs
) -> AsyncIterator[bytes]:
"""Http request"""
async with self.request(method, path, **kwargs) as response:
Expand Down
2 changes: 1 addition & 1 deletion tests/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,4 @@ async def test__me(client):
async def test__with(url, api_token):
async with Client(url, api_token=api_token) as client:
bucket = await client.create_bucket("bucket-1", exist_ok=True)
await bucket.info()
await bucket.info()
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def _token() -> Optional[str]:
api_token = os.getenv("RS_API_TOKEN", default=None)
return api_token


@pytest_asyncio.fixture(name="client")
async def _make_client(url, api_token):
client = Client(url, api_token=api_token)
Expand Down

0 comments on commit 5dd2db9

Please sign in to comment.