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

fix(init): using api sdk wrapper is better #1208

Merged
merged 4 commits into from Feb 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 2 additions & 23 deletions src/rubrix/client/rubrix_client.py
Expand Up @@ -19,7 +19,6 @@
import socket
from typing import Any, Dict, Iterable, List, Optional, Union

import httpx
import pandas
from tqdm.auto import tqdm

Expand Down Expand Up @@ -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.
frascuchon marked this conversation as resolved.
Show resolved Hide resolved
"""

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)

Expand Down
12 changes: 3 additions & 9 deletions src/rubrix/client/sdk/users/api.py
Expand Up @@ -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(
Expand All @@ -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")