Skip to content

Commit

Permalink
Add support for studio_token and studio_repo_url to be provided a…
Browse files Browse the repository at this point in the history
…s args. (#29)
  • Loading branch information
daavoo authored Apr 20, 2023
1 parent 0b86738 commit 66e25cf
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/dvc_studio_client/post_live_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ def get_studio_repo_url() -> Optional[str]:
return None


def get_studio_token_and_repo_url():
studio_token = getenv(DVC_STUDIO_TOKEN) or getenv(STUDIO_TOKEN)
def get_studio_token_and_repo_url(studio_token=None, studio_repo_url=None):
studio_token = studio_token or getenv(DVC_STUDIO_TOKEN) or getenv(STUDIO_TOKEN)
if studio_token is None:
logger.debug(
f"{DVC_STUDIO_TOKEN} not found. Skipping `post_studio_live_metrics`"
)
return None, None

studio_repo_url = getenv(STUDIO_REPO_URL, None)
studio_repo_url = studio_repo_url or getenv(STUDIO_REPO_URL, None)
if studio_repo_url is None:
logger.debug(f"`{STUDIO_REPO_URL}` not found. Trying to automatically find it.")
studio_repo_url = get_studio_repo_url()
Expand All @@ -79,6 +79,8 @@ def post_live_metrics(
params: Optional[Dict[str, Any]] = None,
plots: Optional[Dict[str, Any]] = None,
step: Optional[int] = None,
studio_token: Optional[str] = None,
studio_repo_url: Optional[str] = None,
) -> Optional[bool]:
"""Post `event_type` to Studio's `api/live`.
Expand Down Expand Up @@ -143,14 +145,18 @@ def post_live_metrics(
Usually comes from DVCLive `Live.step` property.
Required in when `event_type="data"`.
Defaults to `None`.
studio_token (Optional[str]): Studio access token obtained from the UI.
studio_repo_url (Optional[str]): URL of the Git repository that has been
imported into Studio UI.
Returns:
Optional[bool]:
`True` - if received status code 200 from Studio.
`False` - if received other status code or RequestException raised.
`None`- if prerequisites weren't met and the request was not sent.
"""
studio_token, studio_repo_url = get_studio_token_and_repo_url()
studio_token, studio_repo_url = get_studio_token_and_repo_url(
studio_token, studio_repo_url
)

if any(x is None for x in (studio_token, studio_repo_url)):
return None
Expand Down
33 changes: 33 additions & 0 deletions tests/test_post_live_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,36 @@ def test_get_studio_token_and_repo_url_skip_repo_url(monkeypatch):
token, repo_url = get_studio_token_and_repo_url()
assert token is None
assert repo_url is None # Skipped call to get_repo_url


def test_post_live_metrics_token_and_repo_url_args(mocker, monkeypatch):
monkeypatch.setenv(DVC_STUDIO_URL, "https://0.0.0.0")

mocked_response = mocker.MagicMock()
mocked_response.status_code = 200
mocked_post = mocker.patch("requests.post", return_value=mocked_response)

assert post_live_metrics(
"start",
"f" * 40,
"fooname",
"fooclient",
studio_token="FOO_TOKEN",
studio_repo_url="FOO_REPO_URL",
)

mocked_post.assert_called_with(
"https://0.0.0.0/api/live",
json={
"type": "start",
"repo_url": "FOO_REPO_URL",
"baseline_sha": "f" * 40,
"name": "fooname",
"client": "fooclient",
},
headers={
"Authorization": "token FOO_TOKEN",
"Content-type": "application/json",
},
timeout=5,
)

0 comments on commit 66e25cf

Please sign in to comment.