Skip to content

Commit

Permalink
refactor: Rename clients
Browse files Browse the repository at this point in the history
  • Loading branch information
Simona Nemeckova committed Dec 11, 2024
1 parent 70bf8d2 commit 40e484e
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 73 deletions.
72 changes: 38 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,57 +48,61 @@ Async version:

```python
import asyncio
from rossum_api import ElisAPIClient
from rossum_api import AsyncRossumAPIClient

WORKSPACE = {
"name": "Rossum Client NG Test",
"organization": "https://elis.rossum.ai/api/v1/organizations/116390",
"name": "Rossum Client NG Test",
"organization": "https://elis.rossum.ai/api/v1/organizations/116390",
}


async def main_with_async_client():
client = ElisAPIClient(
os.environ["ELIS_USERNAME"],
os.environ["ELIS_PASSWORD"],
base_url="https://elis.rossum.ai/api/v1",
)
ws = await client.create_new_workspace(data=WORKSPACE)
workspace_id = ws.id
ws = await client.retrieve_workspace(workspace_id)
print("GET result:", ws)
print("LIST results:")
async for w in client.list_all_workspaces(ordering=["-id"], name=WORKSPACE["name"]):
print(w)
await client.delete_workspace(workspace_id)
print(f"Workspace {workspace_id} deleted.")
client = AsyncRossumAPIClient(
os.environ["ELIS_USERNAME"],
os.environ["ELIS_PASSWORD"],
base_url="https://elis.rossum.ai/api/v1",
)
ws = await client.create_new_workspace(data=WORKSPACE)
workspace_id = ws.id
ws = await client.retrieve_workspace(workspace_id)
print("GET result:", ws)
print("LIST results:")
async for w in client.list_all_workspaces(ordering=["-id"], name=WORKSPACE["name"]):
print(w)
await client.delete_workspace(workspace_id)
print(f"Workspace {workspace_id} deleted.")


asyncio.run(main_with_async_client())
```

Sync version:

```python
from rossum_api import ElisAPIClientSync
from rossum_api import SyncRossumAPIClient

WORKSPACE = {
"name": "Rossum Client NG Test",
"organization": "https://elis.rossum.ai/api/v1/organizations/116390",
"name": "Rossum Client NG Test",
"organization": "https://elis.rossum.ai/api/v1/organizations/116390",
}


def main_with_sync_client():
client = ElisAPIClientSync(
os.environ["ELIS_USERNAME"],
os.environ["ELIS_PASSWORD"],
base_url="https://elis.rossum.ai/api/v1",
)
ws = client.create_new_workspace(data=WORKSPACE)
workspace_id = ws.id
ws = client.retrieve_workspace(workspace_id)
print("GET result:", ws)
print("LIST results:")
for w in client.list_all_workspaces(ordering=["-id"], name=WORKSPACE["name"]):
print(w)
client.delete_workspace(workspace_id)
print(f"Workspace {workspace_id} deleted.")
client = SyncRossumAPIClient(
os.environ["ELIS_USERNAME"],
os.environ["ELIS_PASSWORD"],
base_url="https://elis.rossum.ai/api/v1",
)
ws = client.create_new_workspace(data=WORKSPACE)
workspace_id = ws.id
ws = client.retrieve_workspace(workspace_id)
print("GET result:", ws)
print("LIST results:")
for w in client.list_all_workspaces(ordering=["-id"], name=WORKSPACE["name"]):
print(w)
client.delete_workspace(workspace_id)
print(f"Workspace {workspace_id} deleted.")


main_with_sync_client()
```
Expand Down
10 changes: 5 additions & 5 deletions rossum_api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from __future__ import annotations

from rossum_api.api_client import APIClientError
from rossum_api.elis_api_client import ElisAPIClient, ExportFileFormats
from rossum_api.elis_api_client_sync import ElisAPIClientSync
from rossum_api.clients.external_async_client import AsyncRossumAPIClient, ExportFileFormats
from rossum_api.clients.external_sync_client import SyncRossumAPIClient
from rossum_api.clients.internal_async_client import APIClientError

__version__ = "0.20.0"

__all__ = (
"APIClientError",
"ElisAPIClient",
"ElisAPIClientSync",
"AsyncRossumAPIClient",
"SyncRossumAPIClient",
"ExportFileFormats",
)
Empty file added rossum_api/clients/__init__.py
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import aiofiles

from rossum_api.api_client import APIClient
from rossum_api.clients.internal_async_client import InternalAsyncClient
from rossum_api.domain_logic.annotations import (
is_annotation_imported,
validate_list_annotations_params,
Expand Down Expand Up @@ -53,14 +53,14 @@ class Sideload:
pass


class ElisAPIClient:
class AsyncRossumAPIClient:
def __init__(
self,
username: Optional[str] = None,
password: Optional[str] = None,
token: Optional[str] = None,
base_url: str = DEFAULT_BASE_URL,
http_client: Optional[APIClient] = None,
http_client: Optional[InternalAsyncClient] = None,
deserializer: Optional[Deserializer] = None,
):
"""
Expand All @@ -72,7 +72,7 @@ def __init__(
deserializer
pass a custom deserialization callable if different model classes should be returned
"""
self._http_client = http_client or APIClient(base_url, username, password, token)
self._http_client = http_client or InternalAsyncClient(base_url, username, password, token)
self._deserializer = deserializer or deserialize_default

# ##### QUEUE #####
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from typing import Any, Callable, Iterator, Optional, Sequence, Tuple, Union, cast

from rossum_api import ExportFileFormats
from rossum_api.api_client import Resource
from rossum_api.clients.internal_async_client import Resource
from rossum_api.clients.internal_sync_client import InternalSyncClient
from rossum_api.domain_logic.annotations import (
get_http_method_for_annotation_export,
is_annotation_imported,
Expand All @@ -17,7 +18,6 @@
from rossum_api.domain_logic.upload import build_upload_files
from rossum_api.domain_logic.urls import build_upload_url, parse_resource_id_from_url
from rossum_api.dtos import Token, UserCredentials
from rossum_api.internal_sync_client import InternalSyncRossumAPIClient
from rossum_api.models import (
Annotation,
Connector,
Expand All @@ -40,15 +40,15 @@
from rossum_api.models.task import TaskStatus


class ElisAPIClientSync:
class SyncRossumAPIClient:
def __init__(
self,
base_url: str,
credentials: UserCredentials | Token,
deserializer: Optional[Deserializer] = None,
):
self._deserializer = deserializer or deserialize_default
self.internal_client = InternalSyncRossumAPIClient(base_url, credentials)
self.internal_client = InternalSyncClient(base_url, credentials)

# ##### QUEUES #####

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def authenticate_if_needed(method):
"""

@functools.wraps(method)
async def authenticate_if_needed(self: APIClient, *args, **kwargs):
async def authenticate_if_needed(self: InternalAsyncClient, *args, **kwargs):
# Authenticate if there is no token, no need to fire the request only to get 401 and retry
if self.token is None:
await self._authenticate()
Expand Down Expand Up @@ -81,7 +81,7 @@ def authenticate_generator_if_needed(method):
"""

@functools.wraps(method)
async def authenticate_if_needed(self: APIClient, *args, **kwargs):
async def authenticate_if_needed(self: InternalAsyncClient, *args, **kwargs):
# Authenticate if there is no token, no need to fire the request only to get 401 and retry
if self.token is None:
await self._authenticate()
Expand All @@ -99,7 +99,7 @@ async def authenticate_if_needed(self: APIClient, *args, **kwargs):
return authenticate_if_needed


class APIClient:
class InternalAsyncClient:
"""Perform CRUD operations over resources provided by Elis API.
Requests will be retried up to `n_retries` times with exponential backoff.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import tenacity

from rossum_api import APIClientError
from rossum_api.api_client import Resource
from rossum_api.clients.internal_async_client import Resource
from rossum_api.domain_logic.pagination import build_pagination_params
from rossum_api.domain_logic.retry import AlwaysRetry, should_retry
from rossum_api.domain_logic.sideloads import build_sideload_params, embed_sideloads
Expand All @@ -16,7 +16,7 @@
from rossum_api.utils import enforce_domain


class InternalSyncRossumAPIClient:
class InternalSyncClient:
def __init__(
self,
base_url: str,
Expand Down
2 changes: 1 addition & 1 deletion rossum_api/domain_logic/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import re
from typing import TYPE_CHECKING

from rossum_api.api_client import Resource
from rossum_api.clients.internal_async_client import Resource

if TYPE_CHECKING:
from rossum_api.models import Resource
Expand Down
10 changes: 5 additions & 5 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

import aiofiles

from rossum_api import ElisAPIClient, ElisAPIClientSync
from rossum_api.api_client import APIClient
from rossum_api import AsyncRossumAPIClient, SyncRossumAPIClient
from rossum_api.clients.internal_async_client import InternalAsyncClient

logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
Expand Down Expand Up @@ -51,7 +51,7 @@


async def main():
client = APIClient(
client = InternalAsyncClient(
os.environ["ELIS_USERNAME"],
os.environ["ELIS_PASSWORD"],
base_url="https://elis.develop.r8.lol/api/v1",
Expand Down Expand Up @@ -112,7 +112,7 @@ async def main():


async def main_with_async_client():
client = ElisAPIClient(
client = AsyncRossumAPIClient(
os.environ["ELIS_USERNAME"],
os.environ["ELIS_PASSWORD"],
base_url="https://elis.develop.r8.lol/api/v1",
Expand Down Expand Up @@ -146,7 +146,7 @@ async def main_with_async_client():


def main_with_sync_client():
client = ElisAPIClientSync(
client = SyncRossumAPIClient(
os.environ["ELIS_USERNAME"],
os.environ["ELIS_PASSWORD"],
base_url="https://elis.develop.r8.lol/api/v1",
Expand Down
10 changes: 5 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@
import pytest
import pytest_asyncio

from rossum_api import ElisAPIClient, ElisAPIClientSync
from rossum_api.api_client import APIClient
from rossum_api import AsyncRossumAPIClient, SyncRossumAPIClient
from rossum_api.clients.internal_async_client import InternalAsyncClient


@pytest.fixture
def http_client():
return MagicMock(APIClient)
return MagicMock(InternalAsyncClient)


@pytest_asyncio.fixture
def elis_client(http_client):
client = ElisAPIClient(username="", password="", base_url=None, http_client=http_client)
client = AsyncRossumAPIClient(username="", password="", base_url=None, http_client=http_client)
return (client, http_client)


@pytest.fixture
def elis_client_sync(http_client):
client = ElisAPIClientSync(username="", password="", base_url=None, http_client=http_client)
client = SyncRossumAPIClient(username="", password="", base_url=None, http_client=http_client)
return (client, http_client)


Expand Down
6 changes: 3 additions & 3 deletions tests/e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import pytest
from aiofiles import os as aios

from rossum_api import ElisAPIClient
from rossum_api import AsyncRossumAPIClient
from rossum_api.domain_logic.resources import Resource

if TYPE_CHECKING:
Expand All @@ -44,7 +44,7 @@ async def test_import_document(self):
queue: Optional[Queue] = None
schema: Optional[Schema] = None

client = ElisAPIClient(
client = AsyncRossumAPIClient(
token=os.environ["ROSSUM_TOKEN"],
base_url=os.environ["ROSSUM_BASE_URL"],
)
Expand Down Expand Up @@ -89,7 +89,7 @@ async def test_create_upload(self):
queue: Optional[Queue] = None
schema: Optional[Schema] = None

client = ElisAPIClient(
client = AsyncRossumAPIClient(
token=os.environ["ROSSUM_TOKEN"],
base_url=os.environ["ROSSUM_BASE_URL"],
)
Expand Down
Loading

0 comments on commit 40e484e

Please sign in to comment.