Skip to content

Commit

Permalink
[OPIK-515] implement opik client auth_check method (#805)
Browse files Browse the repository at this point in the history
* Add new check_auth method

* Add docstring

* Fix lint errors

* Temporary change opik installation path to check timeouts behavior

* Update retry policy

* Fix lint errors

* Add comment to documentation cookbook workflow

* Add e2e test for auth check
  • Loading branch information
alexkuzmik authored Dec 4, 2024
1 parent 63d1dbc commit d5e1ac9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .github/workflows/documentation_cookbook_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ jobs:
run: |
python -m pip install --upgrade pip
python -m pip install -U ipython nbconvert
# - name: Install opik # Uncomment if you want to run cookbooks with local version of opik
# run: pip install sdks/python
- name: Prepare env variables
run: |
directory=$(dirname -- "${NOTEBOOK_TO_TEST}")
Expand Down
10 changes: 10 additions & 0 deletions sdks/python/src/opik/api_objects/opik_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
)

LOGGER = logging.getLogger(__name__)
OPIK_API_REQUESTS_TIMEOUT_SECONDS = 5.0


class Opik:
Expand Down Expand Up @@ -90,6 +91,7 @@ def _initialize_streamer(
base_url=base_url,
httpx_client=httpx_client_,
)
self._rest_client._client_wrapper._timeout = OPIK_API_REQUESTS_TIMEOUT_SECONDS # See https://github.com/fern-api/fern/issues/5321
rest_client_configurator.configure(self._rest_client)
self._streamer = streamer_constructors.construct_online_streamer(
n_consumers=workers,
Expand Down Expand Up @@ -118,6 +120,14 @@ def _display_created_dataset_url(self, workspace: str, dataset_name: str) -> Non

LOGGER.info(f'Created a "{dataset_name}" dataset at {dataset_url}.')

def auth_check(self) -> None:
"""
Checks if current API key user has an access to the configured workspace and its content.
"""
self._rest_client.check.access(
request={}
) # empty body for future backward compatibility

def trace(
self,
id: Optional[str] = None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
(
httpx.RemoteProtocolError, # handle retries for expired connections
httpx.ConnectError,
httpx.ConnectTimeout,
httpx.TimeoutException,
)
),
)
17 changes: 17 additions & 0 deletions sdks/python/tests/e2e/test_backend_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import opik
import pytest
from opik.rest_api.core import api_error


def test_auth_check__happyflow(opik_client: opik.Opik):
# Assuming opik client is correctly configured for tests, no
# exceptions must be raised.
assert opik_client.auth_check() is None


def test_auth_check__not_existing_workspace__api_error_raised():
opik_client = opik.Opik(
workspace="workspace-that-does-not-exist-in-any-installation"
)
with pytest.raises(api_error.ApiError):
opik_client.auth_check()

0 comments on commit d5e1ac9

Please # to comment.