Skip to content

Commit 63eb7d2

Browse files
authored
feat: create InsufficientCreditError to handle credit paywall (#5318)
* feat: create `InsufficientCreditError` to handle credit paywall * chore: update `lock` file * fix: failing `tests` * fix: address pr `feedback`
1 parent a78e7e3 commit 63eb7d2

File tree

6 files changed

+15
-5
lines changed

6 files changed

+15
-5
lines changed

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

warehouse/pyoso/pyoso/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99

1010
from .analytics import DataAnalytics
1111
from .client import Client, ClientConfig, QueryResponse
12-
from .exceptions import HTTPError, OsoError
12+
from .exceptions import HTTPError, InsufficientCreditError, OsoError

warehouse/pyoso/pyoso/client.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from pydantic import BaseModel
1010
from pyoso.analytics import DataAnalytics, DataStatus
1111
from pyoso.constants import DEFAULT_BASE_URL, OSO_API_KEY
12-
from pyoso.exceptions import OsoError, OsoHTTPError
12+
from pyoso.exceptions import InsufficientCreditError, OsoError, OsoHTTPError
1313
from pyoso.semantic import create_registry
1414
from sqlglot import parse
1515

@@ -182,6 +182,9 @@ def _make_sql_request(
182182
response.iter_lines(chunk_size=None)
183183
)
184184
except requests.HTTPError as e:
185+
if e.response.status_code == 402:
186+
error_message = e.response.json()["error"]
187+
raise InsufficientCreditError(error_message) from None
185188
raise OsoHTTPError(e, response=e.response) from None
186189

187190
def to_pandas(self, query: str):

warehouse/pyoso/pyoso/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@ def __str__(self):
1010
str = super().__str__()
1111
response = self.response.json()
1212
return f"{str} - {response['error'] if response['error'] else response}"
13+
14+
15+
class InsufficientCreditError(OsoError):
16+
"""Raised when the user has insufficient credits to execute a query."""

warehouse/pyoso/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "pyoso"
3-
version = "0.7.0"
3+
version = "0.7.1"
44
description = ""
55
readme = "README.md"
66
requires-python = ">=3.8"

warehouse/pyoso/tests/test_client.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ def test_to_pandas_with_default_api_key(
9696
def test_to_pandas_http_error(self, mock_post: mock.Mock, mock_registry: mock.Mock):
9797
mock_registry.return_value = Registry()
9898
mock_response = mock.Mock()
99-
mock_response.raise_for_status.side_effect = requests.HTTPError("HTTP Error")
99+
mock_response.status_code = 500
100+
http_error = requests.HTTPError("HTTP Error")
101+
http_error.response = mock_response
102+
mock_response.raise_for_status.side_effect = http_error
100103
mock_post.return_value = mock_response
101104

102105
client = Client(api_key=self.CUSTOM_API_KEY)

0 commit comments

Comments
 (0)