Skip to content

Commit 0e46619

Browse files
authored
Fixing bad breaking bug in endpoint url sanitization (#30)
* Fixing bad breaking bug in url-sanitization. * If you accidentally set the endpoint to a blank string, it works. * Fixing imagequery prefix. Changing default test env to prod. * Fixing integ tests to point at integ. * Explicitly dealing with blank environment variable.
1 parent e194d75 commit 0e46619

File tree

6 files changed

+21
-13
lines changed

6 files changed

+21
-13
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ generate: install ## Generate the SDK from our public openapi spec
1313

1414

1515
test-local: install ## Run integration tests against an API server running at http://localhost:8000/device-api (needs GROUNDLIGHT_API_TOKEN)
16-
GROUNDLIGHT_TEST_API_ENDPOINT="http://localhost:8000/device-api" poetry run pytest --cov=src test --log-cli-level INFO
16+
GROUNDLIGHT_ENDPOINT="http://localhost:8000/" poetry run pytest --cov=src test --log-cli-level INFO
1717

1818

1919
test-integ: install ## Run integration tests against the integ API server (needs GROUNDLIGHT_API_TOKEN)
20-
GROUNDLIGHT_TEST_API_ENDPOINT="https://api.integ.groundlight.ai/device-api" poetry run pytest --cov=src test --log-cli-level INFO
20+
GROUNDLIGHT_ENDPOINT="https://api.integ.groundlight.ai/" poetry run pytest --cov=src test --log-cli-level INFO

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "groundlight"
3-
version = "0.7.0"
3+
version = "0.7.1"
44
license = "MIT"
55
readme = "README.md"
66
homepage = "https://www.groundlight.ai"

src/groundlight/client.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ class Groundlight:
4343
POLLING_EXPONENTIAL_BACKOFF = 1.3 # This still has the nice backoff property that the max number of requests
4444
# is O(log(time)), but with 1.3 the guarantee is that the call will return no more than 30% late
4545

46-
def __init__(self, endpoint: str = DEFAULT_ENDPOINT, api_token: str = None):
46+
def __init__(self, endpoint: Optional[str] = None, api_token: str = None):
4747
"""
4848
:param endpoint: optionally specify a different endpoint
4949
:param api_token: use this API token for your API calls. If unset, fallback to the
5050
environment variable "GROUNDLIGHT_API_TOKEN".
5151
"""
5252
# Specify the endpoint
5353
self.endpoint = sanitize_endpoint_url(endpoint)
54-
configuration = Configuration(host=endpoint)
54+
configuration = Configuration(host=self.endpoint)
5555

5656
if api_token is None:
5757
try:
@@ -201,6 +201,7 @@ def add_label(self, image_query: Union[ImageQuery, str], label: str):
201201
image_query_id = image_query.id
202202
else:
203203
image_query_id = str(image_query)
204+
# Some old imagequery id's started with "chk_"
204205
if not (image_query_id.startswith("chk_") or image_query_id.startswith("iq_")):
205206
raise ValueError(f"Invalid image query id {image_query_id}")
206207
api_label = convert_display_label_to_internal(image_query_id, label)

src/groundlight/config.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import os
2-
31
API_TOKEN_WEB_URL = "https://app.groundlight.ai/reef/my-account/api-tokens"
42
API_TOKEN_VARIABLE_NAME = "GROUNDLIGHT_API_TOKEN"
53

6-
DEFAULT_ENDPOINT = os.environ.get("GROUNDLIGHT_ENDPOINT", "https://api.groundlight.ai/")
4+
DEFAULT_ENDPOINT = "https://api.groundlight.ai/"
75

86
__all__ = [
97
"API_TOKEN_WEB_URL",

src/groundlight/internalapi.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import logging
2+
import os
23
import time
3-
import uuid
44
from typing import Dict
55
from urllib.parse import urlsplit, urlunsplit
6+
import uuid
7+
8+
from groundlight.config import DEFAULT_ENDPOINT
69

710
import model
811
import requests
@@ -17,6 +20,11 @@ def sanitize_endpoint_url(endpoint: str) -> str:
1720
This allows people to leave that off entirely, or add a trailing slash.
1821
Also some future-proofing by allowing "v1" or "v2" or "v3" paths.
1922
"""
23+
if not endpoint:
24+
endpoint = os.environ.get("GROUNDLIGHT_ENDPOINT", "")
25+
if not endpoint:
26+
# Because sometimes people set an environment variable to a blank string by mistake
27+
endpoint = "https://api.groundlight.ai/"
2028
parts = urlsplit(endpoint)
2129
if (parts.scheme not in ("http", "https")) or (not parts.netloc):
2230
raise ValueError(

test/integration/test_groundlight.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
@pytest.fixture
1313
def gl() -> Groundlight:
1414
"""Creates a Groundlight client object for testing."""
15-
endpoint = os.environ.get("GROUNDLIGHT_TEST_API_ENDPOINT", "http://localhost:8000/device-api")
16-
return Groundlight(endpoint=endpoint)
15+
return Groundlight()
1716

1817

1918
@pytest.fixture
@@ -26,7 +25,8 @@ def detector(gl: Groundlight) -> Detector:
2625

2726
@pytest.fixture
2827
def image_query(gl: Groundlight, detector: Detector) -> ImageQuery:
29-
return gl.submit_image_query(detector=detector.id, image="test/assets/dog.jpeg")
28+
iq = gl.submit_image_query(detector=detector.id, image="test/assets/dog.jpeg")
29+
return iq
3030

3131

3232
def test_create_detector(gl: Groundlight):
@@ -121,7 +121,8 @@ def test_add_label_to_object(gl: Groundlight, image_query: ImageQuery):
121121

122122
def test_add_label_by_id(gl: Groundlight, image_query: ImageQuery):
123123
iqid = image_query.id
124-
assert iqid.startswith("chk_") # someday we'll probably change this to iq_
124+
# TODO: Fully deprecate chk_ prefix
125+
assert iqid.startswith("chk_") or iqid.startswith("iq_")
125126
gl.add_label(iqid, "No")
126127

127128

0 commit comments

Comments
 (0)