Skip to content

Commit

Permalink
Expose useful configurations with agnostic way (#47)
Browse files Browse the repository at this point in the history
* Improve `BaseClient` by making it agnostic

* Upgrade Github action

* Fix tests

* Remove 3.7 test on MacOS

* Change default values
  • Loading branch information
hexoul authored Sep 13, 2024
1 parent 045d77a commit 935a447
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/gh-pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
18 changes: 9 additions & 9 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
Expand All @@ -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:
Expand All @@ -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"
Expand All @@ -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"
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

---
Expand Down
28 changes: 25 additions & 3 deletions centraldogma/base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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,
Expand Down
Empty file added tests/integration/__init__.py
Empty file.
6 changes: 4 additions & 2 deletions tests/test_base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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,
Expand Down

0 comments on commit 935a447

Please sign in to comment.