Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@ build/
htmlcov/
.claude/
.DS_Store
tests/
.benchmarks/
9 changes: 9 additions & 0 deletions src/sharpapi/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@
RETRY_MAX_DELAY = 4.0


def normalize_base_url(base_url: str) -> str:
"""Return the API origin URL, accepting values with a trailing /api/v1."""
cleaned = base_url.rstrip("/")
suffix = "/api/v1"
if cleaned.endswith(suffix):
return cleaned[: -len(suffix)]
return cleaned


def should_retry(response: httpx.Response | None, exc: Exception | None) -> bool:
"""True for transient upstream failures worth retrying."""
if exc is not None:
Expand Down
3 changes: 2 additions & 1 deletion src/sharpapi/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
AuthMethod,
handle_errors,
make_headers,
normalize_base_url,
parse_rate_limit,
parse_response,
retry_delay,
Expand Down Expand Up @@ -89,7 +90,7 @@ def __init__(

self._api_key = api_key
self._auth_method: AuthMethod = auth_method
self._base_url = base_url.rstrip("/")
self._base_url = normalize_base_url(base_url)
self._timeout = timeout
self._http = httpx.AsyncClient(
base_url=f"{self._base_url}/api/v1",
Expand Down
3 changes: 2 additions & 1 deletion src/sharpapi/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
AuthMethod,
handle_errors,
make_headers,
normalize_base_url,
parse_rate_limit,
parse_response,
retry_delay,
Expand Down Expand Up @@ -97,7 +98,7 @@ def __init__(

self._api_key = api_key
self._auth_method: AuthMethod = auth_method
self._base_url = base_url.rstrip("/")
self._base_url = normalize_base_url(base_url)
self._timeout = timeout
self._http = httpx.Client(
base_url=f"{self._base_url}/api/v1",
Expand Down
23 changes: 23 additions & 0 deletions tests/test_base_url.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from sharpapi import AsyncSharpAPI, SharpAPI


def test_sync_client_accepts_api_v1_base_url_without_double_prefix():
client = SharpAPI("sk_test", base_url="https://api.sharpapi.io/api/v1/")

assert str(client._http.base_url) == "https://api.sharpapi.io/api/v1/"
assert client._base_url == "https://api.sharpapi.io"

stream = client.stream.odds()
assert stream._url.startswith("https://api.sharpapi.io/api/v1/stream?")
assert "/api/v1/api/v1/" not in stream._url

client.close()


async def test_async_client_accepts_api_v1_base_url_without_double_prefix():
client = AsyncSharpAPI("sk_test", base_url="https://api.sharpapi.io/api/v1/")

assert str(client._http.base_url) == "https://api.sharpapi.io/api/v1/"
assert client._base_url == "https://api.sharpapi.io"

await client.close()