-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make datacosmos sdk not rely on private external packages; update rea…
…dme file
- Loading branch information
1 parent
eb36654
commit a3d8781
Showing
13 changed files
with
201 additions
and
1,059 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""Http response and url utils for datacosmos.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""Validates an API response.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
"""Validates an API response and raises a DatacosmosException if an error occurs.""" | ||
|
||
from pydantic import ValidationError | ||
from requests import Response | ||
|
||
from datacosmos.exceptions.datacosmos_exception import DatacosmosException | ||
from datacosmos.utils.http_response.models.datacosmos_response import DatacosmosResponse | ||
|
||
|
||
def check_api_response(response: Response) -> None: | ||
"""Validates an API response and raises a DatacosmosException if an error occurs. | ||
Args: | ||
resp (requests.Response): The response object. | ||
Raises: | ||
DatacosmosException: If the response status code indicates an error. | ||
""" | ||
if 200 <= response.status_code < 400: | ||
return | ||
|
||
try: | ||
response = DatacosmosResponse.model_validate_json(response.text) | ||
msg = response.errors[0].human_readable() | ||
if len(response.errors) > 1: | ||
msg = "\n * " + "\n * ".join( | ||
error.human_readable() for error in response.errors | ||
) | ||
raise DatacosmosException(msg, response=response) | ||
|
||
except ValidationError: | ||
raise DatacosmosException( | ||
f"HTTP {response.status_code}: {response.text}", response=response | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""Models for validation of API response.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
"""Structured API error message for Datacosmos.""" | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
class DatacosmosError(BaseModel): | ||
"""Structured API error message for Datacosmos.""" | ||
|
||
message: str | ||
field: str | None = None | ||
type: str | None = None | ||
source: str | None = None | ||
trace_id: str | None = None | ||
|
||
def human_readable(self) -> str: | ||
"""Formats the error message into a readable format.""" | ||
msg = self.message | ||
if self.type: | ||
msg += f" (type: {self.type})" | ||
if self.field: | ||
msg += f" (field: {self.field})" | ||
if self.source: | ||
msg += f" (source: {self.source})" | ||
if self.trace_id: | ||
msg += f" (trace_id: {self.trace_id})" | ||
return msg |
11 changes: 11 additions & 0 deletions
11
datacosmos/utils/http_response/models/datacosmos_response.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
"""Structured response for Datacosmos handling multiple API errors.""" | ||
|
||
from pydantic import BaseModel | ||
|
||
from datacosmos.utils.http_response.models.datacosmos_error import DatacosmosError | ||
|
||
|
||
class DatacosmosResponse(BaseModel): | ||
"""Structured response for Datacosmos handling multiple API errors.""" | ||
|
||
errors: list[DatacosmosError] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
"""URL utility class for building and handling URLs in the SDK.""" | ||
|
||
|
||
class URL: | ||
"""Class to represent and build URLs in a convenient way.""" | ||
|
||
def __init__(self, protocol: str, host: str, port: int, base: str): | ||
"""Creates a new basis to build URLs. | ||
Args: | ||
protocol (str): Protocol to use in the URL (http/https). | ||
host (str): Hostname (e.g., example.com). | ||
port (int): Port number. | ||
base (str): Base path (e.g., /api/v1). | ||
""" | ||
self.protocol = protocol | ||
self.host = host | ||
self.port = port | ||
self.base = base | ||
|
||
def string(self) -> str: | ||
"""Returns the full URL as a string.""" | ||
port = "" if self.port in [80, 443] else f":{self.port}" | ||
base = f"/{self.base.lstrip('/')}" if self.base else "" | ||
return f"{self.protocol}://{self.host}{port}{base}" | ||
|
||
def with_suffix(self, suffix: str) -> str: | ||
"""Appends a suffix to the URL, ensuring proper formatting. | ||
Args: | ||
suffix (str): The path to append. | ||
Returns: | ||
str: Full URL with the suffix. | ||
""" | ||
base = self.string() | ||
return f"{base.rstrip('/')}/{suffix.lstrip('/')}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.