Skip to content

Commit

Permalink
Add routine for parsing HTML response for datalab API URL redirects f…
Browse files Browse the repository at this point in the history
…rom UI
  • Loading branch information
ml-evs committed May 27, 2024
1 parent 2bda4cd commit 97a654a
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/datalab_api/_base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import functools
import logging
import os
import re
import warnings
from importlib.metadata import version
from typing import Any, Optional

Expand Down Expand Up @@ -98,6 +100,8 @@ def __init__(self, datalab_api_url: str, log_level: str = "WARNING"):
self._http_client = httpx.Client
self._headers["User-Agent"] = f"Datalab Python API/{__version__}"

self._detect_api_url()

info_json = self.get_info()

self._datalab_api_versions: list[str] = info_json["data"]["attributes"][
Expand All @@ -110,6 +114,27 @@ def __init__(self, datalab_api_url: str, log_level: str = "WARNING"):

self._find_api_key()

def _detect_api_url(self) -> None:
"""Perform a handshake with the chosen URL to ascertain the correct API URL.
If a datalab UI URL is passed, the client will attempt to resolve the API URL by
inspecting the HTML meta tags.
Do not use the session for this, so we are not passing the API key to arbitrary URLs.
"""
response = httpx.get(self.datalab_api_url)
match = re.search(
r'<meta name="x_datalab_api_url" content="(.*?)"\s*/>',
response.text,
re.IGNORECASE,
)
if match:
self.datalab_api_url = match.group(1)
warnings.warn(
f"Found API URL {self.datalab_api_url} in HTML meta tag. Creating client with this URL instead."
)

def get_info(self) -> dict[str, Any]:
raise NotImplementedError

Expand Down

0 comments on commit 97a654a

Please sign in to comment.