From 2edece25021d67e09497aae1e3fb11664da6a4fd Mon Sep 17 00:00:00 2001 From: Francisco Aranda Date: Mon, 28 Feb 2022 11:14:26 +0100 Subject: [PATCH 1/4] fix(init): using api sdk wrapper is better --- src/rubrix/client/rubrix_client.py | 25 ++----------------------- src/rubrix/client/sdk/users/api.py | 12 +++--------- 2 files changed, 5 insertions(+), 32 deletions(-) diff --git a/src/rubrix/client/rubrix_client.py b/src/rubrix/client/rubrix_client.py index e1771e3c49..60124f3712 100644 --- a/src/rubrix/client/rubrix_client.py +++ b/src/rubrix/client/rubrix_client.py @@ -19,7 +19,6 @@ import socket from typing import Any, Dict, Iterable, List, Optional, Union -import httpx import pandas from tqdm.auto import tqdm @@ -104,34 +103,14 @@ def __init__( api_url: Address from which the API is serving. api_key: Authentication token. workspace: Active workspace for this client session. - timeout: Seconds to considered a connection timeout. + timeout: Seconds to consider a connection timeout. """ - self._client = None # Variable to store the client after the init - - try: - response = httpx.get(url=f"{api_url}/api/docs/spec.json") - except ConnectionRefusedError: - raise Exception("Connection Refused: cannot connect to the API.") - - if response.status_code != 200: - raise Exception( - "Connection error: Undetermined error connecting to the Rubrix Server. " - "The API answered with a {} code: {}".format( - response.status_code, response.content - ) - ) self._client = AuthenticatedClient( base_url=api_url, token=api_key, timeout=timeout ) - response = whoami(client=self._client) - - whoami_response_status = response.status_code - if whoami_response_status == 401: - raise Exception("Authentication error: invalid credentials.") - - self.__current_user__: User = response.parsed + self.__current_user__: User = whoami(client=self._client) if workspace: self.set_workspace(workspace) diff --git a/src/rubrix/client/sdk/users/api.py b/src/rubrix/client/sdk/users/api.py index ac2ceecf32..21ae70adbb 100644 --- a/src/rubrix/client/sdk/users/api.py +++ b/src/rubrix/client/sdk/users/api.py @@ -2,11 +2,10 @@ from rubrix.client.sdk.client import AuthenticatedClient from rubrix.client.sdk.commons.errors_handler import handle_response_error -from rubrix.client.sdk.commons.models import Response from rubrix.client.sdk.users.models import User -def whoami(client: AuthenticatedClient): +def whoami(client: AuthenticatedClient) -> User: url = "{}/api/me".format(client.base_url) response = httpx.get( @@ -17,11 +16,6 @@ def whoami(client: AuthenticatedClient): ) if response.status_code == 200: - return Response( - status_code=response.status_code, - content=response.content, - headers=response.headers, - parsed=User(**response.json()), - ) + return User(**response.json()) - return handle_response_error(response, msg="Invalid credentials") + handle_response_error(response, msg="Invalid credentials") From 7c9874f960529f20071280be6964a50a4d09bc46 Mon Sep 17 00:00:00 2001 From: Francisco Aranda Date: Mon, 28 Feb 2022 13:15:20 +0100 Subject: [PATCH 2/4] Update src/rubrix/client/rubrix_client.py Co-authored-by: David Fidalgo --- src/rubrix/client/rubrix_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rubrix/client/rubrix_client.py b/src/rubrix/client/rubrix_client.py index 60124f3712..5ec1e0d55d 100644 --- a/src/rubrix/client/rubrix_client.py +++ b/src/rubrix/client/rubrix_client.py @@ -103,7 +103,7 @@ def __init__( api_url: Address from which the API is serving. api_key: Authentication token. workspace: Active workspace for this client session. - timeout: Seconds to consider a connection timeout. + timeout: Seconds to wait before raising a connection timeout. """ self._client = AuthenticatedClient( From 6ada847901f5c5464c5c36248f8ac429090f9d6b Mon Sep 17 00:00:00 2001 From: Francisco Aranda Date: Mon, 28 Feb 2022 13:41:17 +0100 Subject: [PATCH 3/4] test: fix and add missing tests --- tests/client/sdk/users/test_api.py | 25 +++++++++++++++++-------- tests/test_init.py | 12 +++--------- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/tests/client/sdk/users/test_api.py b/tests/client/sdk/users/test_api.py index 824ba560dc..d8bb7fa4d5 100644 --- a/tests/client/sdk/users/test_api.py +++ b/tests/client/sdk/users/test_api.py @@ -3,19 +3,28 @@ from rubrix import DEFAULT_API_KEY from rubrix.client.sdk.client import AuthenticatedClient +from rubrix.client.sdk.commons.errors import UnauthorizedApiError from rubrix.client.sdk.users.api import whoami from rubrix.client.sdk.users.models import User -@pytest.fixture -def sdk_client(): - return AuthenticatedClient(base_url="http://localhost:6900", token=DEFAULT_API_KEY) +def test_whoami(mocked_client): + sdk_client = AuthenticatedClient( + base_url="http://localhost:6900", token=DEFAULT_API_KEY + ) + user = whoami(client=sdk_client) + assert isinstance(user, User) -def test_whoami(mocked_client, sdk_client, monkeypatch): - monkeypatch.setattr(httpx, "get", mocked_client.get) +def test_whoami_with_auth_error(mocked_client): + with pytest.raises(UnauthorizedApiError): + whoami( + AuthenticatedClient(base_url="http://localhost:6900", token="wrong-apikey") + ) - response = whoami(client=sdk_client) - assert response.status_code == 200 - assert isinstance(response.parsed, User) +def test_whoami_with_connection_error(): + with pytest.raises(httpx.ConnectError): + whoami( + AuthenticatedClient(base_url="http://localhost:6900", token="wrong-apikey") + ) diff --git a/tests/test_init.py b/tests/test_init.py index 4bafcc9751..7403a7d45a 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -23,7 +23,7 @@ import rubrix from rubrix.client import RubrixClient from rubrix.client.sdk.client import AuthenticatedClient -from rubrix.client.sdk.commons.errors import UnauthorizedApiError +from rubrix.client.sdk.commons.errors import GenericApiError, UnauthorizedApiError @pytest.fixture @@ -145,10 +145,7 @@ def test_init_incorrect(mock_response_500): """ rubrix._client = None # assert empty client - with pytest.raises( - Exception, - match="Connection error: Undetermined error connecting to the Rubrix Server. The API answered with a 500 code: b", - ): + with pytest.raises(GenericApiError): rubrix.init() @@ -179,10 +176,7 @@ def test_init_token_incorrect(mock_response_500): Mocked correct http response """ rubrix._client = None # assert empty client - with pytest.raises( - Exception, - match="Connection error: Undetermined error connecting to the Rubrix Server. The API answered with a 500 code: b", - ): + with pytest.raises(GenericApiError): rubrix.init(api_key="422") From f0b395912a8175b1dd9df9a80427a660e9db2ebf Mon Sep 17 00:00:00 2001 From: Francisco Aranda Date: Mon, 28 Feb 2022 14:13:55 +0100 Subject: [PATCH 4/4] fix: update dataset name --- tests/functional_tests/test_log_for_token_classification.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/functional_tests/test_log_for_token_classification.py b/tests/functional_tests/test_log_for_token_classification.py index 8e49d688a9..658744c8bd 100644 --- a/tests/functional_tests/test_log_for_token_classification.py +++ b/tests/functional_tests/test_log_for_token_classification.py @@ -450,7 +450,7 @@ def test_search_keywords(mocked_client): dataset = "test_search_keywords" from datasets import load_dataset - dataset_ds = load_dataset("rubrix/gutenberg_spacy-ner_sm", split="train") + dataset_ds = load_dataset("rubrix/gutenberg_spacy-ner", split="train") dataset_rb = rubrix.read_datasets(dataset_ds, task="TokenClassification") rubrix.delete(dataset)