Skip to content

Commit

Permalink
Add version to API requests
Browse files Browse the repository at this point in the history
  • Loading branch information
mjvogelsong committed Nov 10, 2023
1 parent 6b978ad commit 66d7e2d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
11 changes: 2 additions & 9 deletions src/groundlight/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@
# Imports from our code
from .client import Groundlight
from .binary_labels import Label
from .version import get_version

try:
import importlib.metadata

# Copy the version number from where it's set in pyproject.toml
__version__ = importlib.metadata.version("groundlight")
except ModuleNotFoundError:
# importlib.metadata was only added in py3.8
# We're still supporting py3.7
__version__ = "(version number available in python 3.8+)"
__version__ = get_version()
16 changes: 8 additions & 8 deletions src/groundlight/internalapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from groundlight.images import ByteStreamWrapper
from groundlight.status_codes import is_ok
from groundlight.version import get_version

logger = logging.getLogger("groundlight.sdk")

Expand All @@ -36,10 +37,8 @@ def sanitize_endpoint_url(endpoint: Optional[str] = None) -> str:
parts = urlsplit(endpoint)
if (parts.scheme not in ("http", "https")) or (not parts.netloc):
raise ValueError(
(
f"Invalid API endpoint {endpoint}. Unsupported scheme: {parts.scheme}. Must be http or https, e.g."
" https://api.groundlight.ai/"
),
f"Invalid API endpoint {endpoint}. Unsupported scheme: {parts.scheme}. Must be http or https, e.g."
" https://api.groundlight.ai/",
)
if parts.query or parts.fragment:
raise ValueError(f"Invalid API endpoint {endpoint}. Cannot have query or fragment.")
Expand Down Expand Up @@ -138,10 +137,8 @@ def decorated(*args, **kwargs): # pylint: disable=inconsistent-return-statement
status_code = e.status
if status_code in self.status_code_range:
logger.warning(
(
f"Current HTTP response status: {status_code}. "
f"Remaining retries: {self.max_retries - retry_count}"
),
f"Current HTTP response status: {status_code}. "
f"Remaining retries: {self.max_retries - retry_count}",
exc_info=True,
)
# This is implementing a full jitter strategy
Expand Down Expand Up @@ -186,6 +183,9 @@ def _headers(self) -> dict:
"Content-Type": "application/json",
"x-api-token": self.configuration.api_key["ApiToken"],
"X-Request-Id": request_id,
# This metadata helps us debug issues with specific SDK versions.
"x-sdk-version": get_version(),
"x-sdk-language": "python",
}

@RequestsRetryDecorator()
Expand Down
10 changes: 10 additions & 0 deletions src/groundlight/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def get_version() -> str:
try:
import importlib.metadata # pylint: disable=import-outside-toplevel

# Copy the version number from where it's set in pyproject.toml
return importlib.metadata.version("groundlight")
except ModuleNotFoundError:
# importlib.metadata was only added in py3.8
# We're still supporting py3.7
return "(version number available in python 3.8+)"

0 comments on commit 66d7e2d

Please sign in to comment.