From 935a4475ac661a77ca14b856d76e31675b516244 Mon Sep 17 00:00:00 2001 From: Seunggon Kim Date: Fri, 13 Sep 2024 12:19:01 +0900 Subject: [PATCH] Expose useful configurations with agnostic way (#47) * Improve `BaseClient` by making it agnostic * Upgrade Github action * Fix tests * Remove 3.7 test on MacOS * Change default values --- .github/workflows/gh-pages.yml | 2 +- .github/workflows/publish.yml | 4 ++-- .github/workflows/test.yml | 18 +++++++++--------- README.md | 8 ++++++++ centraldogma/base_client.py | 28 +++++++++++++++++++++++++--- tests/integration/__init__.py | 0 tests/test_base_client.py | 6 ++++-- 7 files changed, 49 insertions(+), 17 deletions(-) create mode 100644 tests/integration/__init__.py diff --git a/.github/workflows/gh-pages.yml b/.github/workflows/gh-pages.yml index 2761a88..5b31608 100644 --- a/.github/workflows/gh-pages.yml +++ b/.github/workflows/gh-pages.yml @@ -9,7 +9,7 @@ jobs: docs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: ammaraskar/sphinx-action@master with: diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index c467ba5..d454bff 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -9,9 +9,9 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Set up Python - uses: actions/setup-python@v4 + uses: actions/setup-python@v5 with: python-version: "3.x" cache: "pip" diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 7f061a7..4e7a2a8 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -10,7 +10,7 @@ jobs: docs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: ammaraskar/sphinx-action@master with: docs-folder: "docs/" @@ -20,7 +20,7 @@ jobs: name: black --check runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: rickstaa/action-black@v1 id: action_black with: @@ -34,12 +34,12 @@ jobs: python-version: ["3.7", "3.8", "3.9", "3.10", "3.11", "pypy3.8", "pypy3.9"] steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Start Central Dogma - run: docker-compose -f "docker-compose.yml" up -d --build + run: docker compose -f "docker-compose.yml" up -d --build - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v4 + uses: actions/setup-python@v5 with: python-version: ${{ matrix.python-version }} cache: "pip" @@ -58,19 +58,19 @@ jobs: - name: Stop containers if: always() - run: docker-compose -f "docker-compose.yml" down + run: docker compose -f "docker-compose.yml" down test: runs-on: ${{ matrix.os }} strategy: matrix: os: [macos-latest, windows-latest] - python-version: ["3.7", "3.8", "3.9", "3.10", "3.11", "pypy3.8", "pypy3.9"] + python-version: ["3.8", "3.9", "3.10", "3.11", "pypy3.8", "pypy3.9"] steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v4 + uses: actions/setup-python@v5 with: python-version: ${{ matrix.python-version }} cache: "pip" diff --git a/README.md b/README.md index 2308d1b..2e81f28 100644 --- a/README.md +++ b/README.md @@ -11,12 +11,20 @@ $ pip install centraldogma-python ``` ## Getting started +Only URL indicating CentralDogma server and access token are required. ```pycon >>> from centraldogma.dogma import Dogma >>> dogma = Dogma("https://dogma.yourdomain.com", "token") >>> dogma.list_projects() [] ``` + +It supports client configurations. +```pycon +>>> retries, max_connections = 5, 10 +>>> dogma = Dogma("https://dogma.yourdomain.com", "token", retries=retries, max_connections=max_connections) +``` + Please see [`examples` folder](https://github.com/line/centraldogma-python/tree/main/examples) for more detail. --- diff --git a/centraldogma/base_client.py b/centraldogma/base_client.py index bfd125a..1729c71 100644 --- a/centraldogma/base_client.py +++ b/centraldogma/base_client.py @@ -13,7 +13,7 @@ # under the License. from typing import Dict, Union, Callable, TypeVar, Optional -from httpx import Client, Response +from httpx import Client, HTTPTransport, Limits, Response from centraldogma.exceptions import to_exception @@ -23,15 +23,37 @@ class BaseClient: PATH_PREFIX = "/api/v1" - def __init__(self, base_url: str, token: str, http2: bool = True, **configs): + def __init__( + self, + base_url: str, + token: str, + http2: bool = True, + retries: int = 1, + max_connections: int = 10, + max_keepalive_connections: int = 2, + **configs, + ): base_url = base_url[:-1] if base_url[-1] == "/" else base_url + + for key in ["transport", "limits"]: + if key in configs: + del configs[key] + self.client = Client( - base_url=f"{base_url}{self.PATH_PREFIX}", http2=http2, **configs + base_url=f"{base_url}{self.PATH_PREFIX}", + http2=http2, + transport=HTTPTransport(retries=retries), + limits=Limits( + max_connections=max_connections, + max_keepalive_connections=max_keepalive_connections, + ), + **configs, ) self.token = token self.headers = self._get_headers(token) self.patch_headers = self._get_patch_headers(token) + # TODO(@hexoul): Support automatic retry with `tenacity` even if an exception occurs. def request( self, method: str, diff --git a/tests/integration/__init__.py b/tests/integration/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_base_client.py b/tests/test_base_client.py index 8fd951b..48b0260 100644 --- a/tests/test_base_client.py +++ b/tests/test_base_client.py @@ -15,7 +15,7 @@ from centraldogma.exceptions import UnauthorizedException, NotFoundException from centraldogma.base_client import BaseClient -from httpx import Limits, Response +from httpx import Response import pytest client = BaseClient("http://baseurl", "token") @@ -28,7 +28,9 @@ "proxies": None, "mounts": None, "timeout": 5, - "limits": Limits(max_connections=100, max_keepalive_connections=20), + "retries": 10, + "max_connections": 50, + "max_keepalive_connections": 10, "max_redirects": 1, "event_hooks": None, "transport": None,