Skip to content
Merged
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
5 changes: 4 additions & 1 deletion centraldogma/_async/base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ def __init__(
self.headers = self._get_headers(token)
self.patch_headers = self._get_patch_headers(token)

async def __aenter__(self):
return self

async def __aexit__(self, *_: Any) -> None:
await self.client.aclose()

Expand All @@ -75,7 +78,7 @@ async def request(
wait=wait_exponential(max=60),
reraise=True,
)
return retryer(self._request, method, path, handler, **kwargs)
return await retryer(self._request, method, path, handler, **kwargs)

def _set_request_headers(self, method: str, **kwargs) -> Dict:
default_headers = self.patch_headers if method == "patch" else self.headers
Expand Down
3 changes: 3 additions & 0 deletions centraldogma/_sync/base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ def __init__(
self.headers = self._get_headers(token)
self.patch_headers = self._get_patch_headers(token)

def __enter__(self):
return self

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

Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ dev = [
"black",
"codecov",
"pytest",
"pytest-asyncio>=1.2.0",
"pytest-cov",
"pytest-mock",
"respx",
Expand Down
3 changes: 3 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ markers =
log_format = %(asctime)s %(levelname)s %(message)s
log_date_format = %Y-%m-%d %H:%M:%S
log_cli=true

asyncio_mode = auto
asyncio_default_fixture_loop_scope = function
205 changes: 205 additions & 0 deletions tests/test_async_base_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
# Copyright 2025 LINE Corporation
#
# LINE Corporation licenses this file to you under the Apache License,
# version 2.0 (the "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at:
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

from typing import AsyncGenerator

from http import HTTPStatus
from httpx import ConnectError, NetworkError, Response
import json
import pytest

from centraldogma._async.base_client import BaseClient
from centraldogma.exceptions import UnauthorizedException, NotFoundException

base_url = "http://baseurl"
ok_handler = {HTTPStatus.OK: lambda resp: resp}


@pytest.fixture
async def client() -> AsyncGenerator[BaseClient, None]:
async with BaseClient(base_url, "token", retries=0) as async_client:
yield async_client


@pytest.fixture
async def client_with_configs() -> AsyncGenerator[BaseClient, None]:
configs = {
"timeout": 5,
"retries": 10,
"max_connections": 50,
"max_keepalive_connections": 10,
"trust_env": True,
}
async with BaseClient(base_url, "token", **configs) as async_client:
yield async_client


@pytest.mark.asyncio
async def test_set_request_headers(client_with_configs):
for method in ["get", "post", "delete", "patch"]:
kwargs = client_with_configs._set_request_headers(
method, params={"a": "b"}, follow_redirects=True
)
content_type = (
"application/json-patch+json" if method == "patch" else "application/json"
)
assert kwargs["headers"] == {
"Authorization": "bearer token",
"Content-Type": content_type,
}
assert kwargs["params"] == {"a": "b"}
assert kwargs["follow_redirects"]
assert "limits" not in kwargs
assert "event_hooks" not in kwargs
assert "transport" not in kwargs
assert "app" not in kwargs


@pytest.mark.asyncio
async def test_request_with_configs(respx_mock, client, client_with_configs):
methods = ["get", "post", "put", "delete", "patch", "options"]
for method in methods:
getattr(respx_mock, method)(f"{base_url}/api/v1/path").mock(
return_value=Response(200, text="success")
)

await client.request(
method,
"/path",
timeout=5,
cookies=None,
auth=None,
)
await client.request(method, "/path", timeout=(3.05, 27))
await client_with_configs.request(method, "/path")

assert respx_mock.calls.call_count == len(methods) * 3


@pytest.mark.asyncio
async def test_delete(respx_mock, client):
route = respx_mock.delete(f"{base_url}/api/v1/path").mock(
return_value=Response(200, text="success")
)
resp = await client.request("delete", "/path", params={"a": "b"})

assert route.called
assert resp.request.headers["Authorization"] == "bearer token"
assert resp.request.headers["Content-Type"] == "application/json"
assert resp.request.url.params.multi_items() == [("a", "b")]


@pytest.mark.asyncio
async def test_delete_exception_authorization(respx_mock, client):
with pytest.raises(UnauthorizedException):
respx_mock.delete(f"{base_url}/api/v1/path").mock(return_value=Response(401))
await client.request("delete", "/path", handler=ok_handler)


@pytest.mark.asyncio
async def test_get(respx_mock, client):
route = respx_mock.get(f"{base_url}/api/v1/path").mock(
return_value=Response(200, text="success")
)
resp = await client.request("get", "/path", params={"a": "b"}, handler=ok_handler)

assert route.called
assert route.call_count == 1
assert resp.request.headers["Authorization"] == "bearer token"
assert resp.request.headers["Content-Type"] == "application/json"
assert resp.request.url.params.multi_items() == [("a", "b")]


@pytest.mark.asyncio
async def test_get_exception_authorization(respx_mock, client):
with pytest.raises(UnauthorizedException):
respx_mock.get(f"{base_url}/api/v1/path").mock(return_value=Response(401))
await client.request("get", "/path", handler=ok_handler)


@pytest.mark.asyncio
async def test_get_exception_not_found(respx_mock, client):
with pytest.raises(NotFoundException):
respx_mock.get(f"{base_url}/api/v1/path").mock(return_value=Response(404))
await client.request("get", "/path", handler=ok_handler)


@pytest.mark.asyncio
async def test_get_with_retry_by_response(respx_mock):
async with BaseClient(base_url, "token", retries=2) as retry_client:
route = respx_mock.get(f"{base_url}/api/v1/path").mock(
side_effect=[Response(503), Response(404), Response(200)],
)

await retry_client.request("get", "/path", handler=ok_handler)

assert route.called
assert route.call_count == 3


@pytest.mark.asyncio
async def test_get_with_retry_by_client(respx_mock):
async with BaseClient(base_url, "token", retries=10) as retry_client:
route = respx_mock.get(f"{base_url}/api/v1/path").mock(
side_effect=[ConnectError, ConnectError, NetworkError, Response(200)],
)

await retry_client.request("get", "/path", handler=ok_handler)

assert route.called
assert route.call_count == 4


@pytest.mark.asyncio
async def test_patch(respx_mock, client):
route = respx_mock.patch(f"{base_url}/api/v1/path").mock(
return_value=Response(200, text="success")
)
given = {"a": "b"}
resp = await client.request("patch", "/path", json=given, handler=ok_handler)

assert route.called
assert resp.request.headers["Authorization"] == "bearer token"
assert resp.request.headers["Content-Type"] == "application/json-patch+json"
got = json.loads(resp.request.content)
assert got == given


@pytest.mark.asyncio
async def test_patch_exception_authorization(respx_mock, client):
with pytest.raises(UnauthorizedException):
respx_mock.patch(f"{base_url}/api/v1/path").mock(return_value=Response(401))
await client.request("patch", "/path", json={"a": "b"}, handler=ok_handler)


@pytest.mark.asyncio
async def test_post(respx_mock, client):
route = respx_mock.post(f"{base_url}/api/v1/path").mock(
return_value=Response(200, text="success")
)
given = {"a": "b"}
resp = await client.request("post", "/path", json=given, handler=ok_handler)

assert route.called
assert resp.request.headers["Authorization"] == "bearer token"
assert resp.request.headers["Content-Type"] == "application/json"
got = json.loads(resp.request.content)
assert got == given


@pytest.mark.asyncio
async def test_post_exception_authorization(respx_mock, client):
with pytest.raises(UnauthorizedException):
respx_mock.post(f"{base_url}/api/v1/path").mock(return_value=Response(401))
await client.request("post", "/path", handler=ok_handler)
1 change: 1 addition & 0 deletions utils/run-unasync.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def main(check: bool = False):
"AsyncClient": "Client",
"AsyncDogma": "Dogma",
"AsyncRetrying": "Retrying",
"__aenter__": "__enter__",
"__aexit__": "__exit__",
"aclose": "close",
},
Expand Down
60 changes: 57 additions & 3 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading