Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REFACTOR] Redefine some property methods #5114

Merged
merged 3 commits into from
Jul 3, 2024
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
18 changes: 6 additions & 12 deletions argilla/src/argilla/_api/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

__all__ = ["APIClient"]


ARGILLA_API_URL = os.getenv(key="ARGILLA_API_URL", default=_DEFAULT_API_URL)
ARGILLA_API_KEY = os.getenv(key="ARGILLA_API_KEY", default=_DEFAULT_API_KEY)
DEFAULT_HTTP_CONFIG = HTTPClientConfig(api_url=ARGILLA_API_URL, api_key=ARGILLA_API_KEY)
Expand Down Expand Up @@ -106,24 +105,19 @@ def __init__(
timeout: int = DEFAULT_HTTP_CONFIG.timeout,
**http_client_args,
):
http_client_args = http_client_args or {}
http_client_args["timeout"] = timeout

self.api_url = api_url
self.api_key = api_key
self._http_client_args = http_client_args

@property
def http_client(self) -> httpx.Client:
frascuchon marked this conversation as resolved.
Show resolved Hide resolved
return create_http_client(
http_client_args = http_client_args or {}
http_client_args["timeout"] = timeout

self.http_client = create_http_client(
api_url=self.api_url, # type: ignore
api_key=self.api_key, # type: ignore
**self._http_client_args,
**http_client_args,
)

@property
def api(self) -> "ArgillaAPI":
return ArgillaAPI(http_client=self.http_client)
self.api = ArgillaAPI(self.http_client)
frascuchon marked this conversation as resolved.
Show resolved Hide resolved

##############################
# Utility methods
Expand Down
12 changes: 8 additions & 4 deletions argilla/src/argilla/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import warnings
from abc import abstractmethod
from collections.abc import Sequence
from functools import cached_property
from typing import TYPE_CHECKING, overload, List, Optional, Union

from argilla import _api
Expand All @@ -28,7 +29,6 @@
from argilla import Dataset
from argilla import User


__all__ = ["Argilla"]


Expand All @@ -43,6 +43,11 @@ class Argilla(_api.APIClient):

"""

workspaces: "Workspaces"
datasets: "Datasets"
users: "Users"
me: "User"

# Default instance of Argilla
_default_client: Optional["Argilla"] = None

Expand Down Expand Up @@ -72,10 +77,9 @@ def users(self) -> "Users":
"""A collection of users on the server."""
return Users(client=self)

@property
@cached_property
frascuchon marked this conversation as resolved.
Show resolved Hide resolved
def me(self) -> "User":
"""The current user."""
from argilla import User
from argilla.users import User

return User(client=self, _model=self.api.users.get_me())

Expand Down
10 changes: 5 additions & 5 deletions argilla/tests/unit/test_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ def test_default_client(self):
assert client.api_key == "admin.apikey"

def test_multiple_clients(self):
with mock.patch("argilla.client._api.APIClient.http_client"):
local_client = rg.Argilla(api_url="http://localhost:6900", api_key="admin.apikey")
remote_client = rg.Argilla(api_url="http://argilla.production.net", api_key="admin.apikey")
assert local_client.api_url == "http://localhost:6900"
assert remote_client.api_url == "http://argilla.production.net"
local_client = rg.Argilla(api_url="http://localhost:6900", api_key="admin.apikey")
remote_client = rg.Argilla(api_url="http://argilla.production.net", api_key="admin.apikey")

assert local_client.api_url == "http://localhost:6900"
assert remote_client.api_url == "http://argilla.production.net"
Loading