-
Notifications
You must be signed in to change notification settings - Fork 4
implement datasets sdk #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| __version__ = "0.2.1" | ||
| __version__ = "0.2.2" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| """ | ||
| Endpoint for creating a new dataset item | ||
| """ | ||
| from dataclasses import dataclass | ||
| from typing import Any, Dict, Optional, Tuple | ||
|
|
||
| from ..utils.dtos import DatasetRowDTO, CreateDatasetItemDTO | ||
|
|
||
| @dataclass | ||
| class CreateDatasetItemEndpointResponse: | ||
| """ | ||
| Response from the create dataset item endpoint | ||
| """ | ||
| datasetRow: DatasetRowDTO | ||
| warning: Optional[str] = None | ||
| error: Optional[str] = None | ||
|
|
||
| @classmethod | ||
| def from_dict(cls, data: Dict[str, Any]) -> "CreateDatasetItemEndpointResponse": | ||
| """ | ||
| Create an instance of CreateDatasetItemEndpointResponse from a dictionary. | ||
|
|
||
| Args: | ||
| data (Dict[str, Any]): The dictionary containing the response data. | ||
|
|
||
| Returns: | ||
| CreateDatasetItemEndpointResponse | ||
| """ | ||
| if "error" in data: | ||
| return cls(datasetRow=None, error=data["error"]) | ||
|
|
||
| return cls( | ||
| datasetRow=DatasetRowDTO.from_dict(data["datasetRow"]), | ||
| warning=data.get("warning"), | ||
| error=None | ||
| ) | ||
|
|
||
|
|
||
| class CreateDatasetItemEndpoint: | ||
| """ | ||
| Endpoint class for creating a dataset item. | ||
| """ | ||
| @staticmethod | ||
| def prepare_request(dto: CreateDatasetItemDTO) -> Dict[str, Any]: | ||
| """ | ||
| Prepare the request dictionary for the CreateDatasetItem endpoint. | ||
|
|
||
| Args: | ||
| dto (CreateDatasetItemDTO): The DTO containing dataset item data. | ||
|
|
||
| Returns: | ||
| The path, method, and body for creating a dataset item on the API. | ||
| """ | ||
| body = { | ||
| "values": dto.values | ||
| } | ||
|
|
||
| if dto.name: | ||
| body["name"] = dto.name | ||
|
|
||
| if dto.idealOutput: | ||
| body["idealOutput"] = dto.idealOutput | ||
|
|
||
| if dto.metadata: | ||
| body["metadata"] = dto.metadata | ||
|
|
||
| return { | ||
| "path": f"/datasets/{dto.slug}/items", | ||
| "method": "POST", | ||
| "body": body | ||
| } | ||
|
|
||
| @staticmethod | ||
| def decode_response(response: dict) -> Tuple[Optional[Exception], Optional[CreateDatasetItemEndpointResponse]]: | ||
| """ | ||
| Decode the response returned from the API | ||
|
|
||
| Args: | ||
| response (dict): The JSON response to encode into a CreateDatasetItemEndpointResponse | ||
|
|
||
| Returns: | ||
| A tuple containing an optional exception and an optional CreateDatasetItemEndpointResponse. | ||
| """ | ||
| try: | ||
| return None, CreateDatasetItemEndpointResponse.from_dict(response) | ||
| except Exception as e: | ||
| return e, None | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| """ | ||
| Endpoint for fetching a specific dataset by slug | ||
| """ | ||
| from dataclasses import dataclass | ||
| from typing import Any, Dict, Optional, Tuple | ||
|
|
||
| from ..utils.dtos import DatasetDTO, GetDatasetDTO | ||
|
|
||
| @dataclass | ||
| class GetDatasetEndpointResponse: | ||
| """ | ||
| Response from the get dataset endpoint | ||
| """ | ||
| dataset: DatasetDTO | ||
| error: Optional[str] = None | ||
|
Comment on lines
+14
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix type inconsistency in dataclass field. The - dataset: DatasetDTO
+ dataset: Optional[DatasetDTO]
error: Optional[str] = NoneThis ensures type consistency throughout the error handling logic. Also applies to: 28-29 🤖 Prompt for AI Agents |
||
|
|
||
| @classmethod | ||
| def from_dict(cls, data: Dict[str, Any]) -> "GetDatasetEndpointResponse": | ||
| """ | ||
| Create an instance of GetDatasetEndpointResponse from a dictionary. | ||
|
|
||
| Args: | ||
| data (Dict[str, Any]): The dictionary containing the response data. | ||
|
|
||
| Returns: | ||
| GetDatasetEndpointResponse | ||
| """ | ||
| if "error" in data: | ||
| return cls(dataset=None, error=data["error"]) | ||
|
|
||
| return cls( | ||
| dataset=DatasetDTO.from_dict(data["dataset"]), | ||
| error=None | ||
| ) | ||
|
|
||
|
|
||
| class GetDatasetEndpoint: | ||
| """ | ||
| Endpoint class for fetching a specific dataset. | ||
| """ | ||
| @staticmethod | ||
| def prepare_request(dto: GetDatasetDTO) -> Dict[str, Any]: | ||
| """ | ||
| Prepare the request dictionary for the GetDataset endpoint. | ||
|
|
||
| Args: | ||
| dto (GetDatasetDTO): The DTO containing dataset slug. | ||
|
|
||
| Returns: | ||
| The path, method, and query parameters for getting a dataset on the API. | ||
| """ | ||
| return { | ||
| "path": f"/datasets/{dto.slug}", | ||
| "method": "GET", | ||
| "query": {} | ||
| } | ||
|
|
||
| @staticmethod | ||
| def decode_response(response: dict) -> Tuple[Optional[Exception], Optional[GetDatasetEndpointResponse]]: | ||
| """ | ||
| Decode the response returned from the API | ||
|
|
||
| Args: | ||
| response (dict): The JSON response to encode into a GetDatasetEndpointResponse | ||
|
|
||
| Returns: | ||
| A tuple containing an optional exception and an optional GetDatasetEndpointResponse. | ||
| """ | ||
| try: | ||
| return None, GetDatasetEndpointResponse.from_dict(response) | ||
| except Exception as e: | ||
| return e, None | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,64 @@ | ||||||||||||||
| """ | ||||||||||||||
| Endpoint for listing all datasets | ||||||||||||||
| """ | ||||||||||||||
| from dataclasses import dataclass | ||||||||||||||
| from typing import Any, Dict, List, Optional, Tuple | ||||||||||||||
|
|
||||||||||||||
| from ..utils.dtos import DatasetDTO, ListDatasetsDTO | ||||||||||||||
|
|
||||||||||||||
| @dataclass | ||||||||||||||
| class ListDatasetsEndpointResponse: | ||||||||||||||
| """ | ||||||||||||||
| Response from the list datasets endpoint | ||||||||||||||
| """ | ||||||||||||||
| datasets: List[DatasetDTO] | ||||||||||||||
|
|
||||||||||||||
| @classmethod | ||||||||||||||
| def from_dict(cls, data: Dict[str, Any]) -> "ListDatasetsEndpointResponse": | ||||||||||||||
| """ | ||||||||||||||
| Create an instance of ListDatasetsEndpointResponse from a dictionary. | ||||||||||||||
|
|
||||||||||||||
| Args: | ||||||||||||||
| data (Dict[str, Any]): The dictionary containing the response data. | ||||||||||||||
|
|
||||||||||||||
| Returns: | ||||||||||||||
| ListDatasetsEndpointResponse | ||||||||||||||
| """ | ||||||||||||||
| return cls( | ||||||||||||||
| datasets=[DatasetDTO.from_dict(dataset) for dataset in data["datasets"]], | ||||||||||||||
| ) | ||||||||||||||
|
Comment on lines
+27
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add error handling for missing datasets key. The method assumes the "datasets" key exists in the response data, which could cause a KeyError if the API response format changes or is malformed. return cls(
- datasets=[DatasetDTO.from_dict(dataset) for dataset in data["datasets"]],
+ datasets=[DatasetDTO.from_dict(dataset) for dataset in data.get("datasets", [])],
)📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| class ListDatasetsEndpoint: | ||||||||||||||
| """ | ||||||||||||||
| Endpoint class for fetching all datasets. | ||||||||||||||
| """ | ||||||||||||||
| @staticmethod | ||||||||||||||
| def prepare_request(dto: ListDatasetsDTO) -> Dict[str, Any]: | ||||||||||||||
| """ | ||||||||||||||
| Prepare the request dictionary for the ListDatasets endpoint. | ||||||||||||||
|
|
||||||||||||||
| Returns: | ||||||||||||||
| The path, method, and query parameters for getting datasets on the API. | ||||||||||||||
| """ | ||||||||||||||
| return { | ||||||||||||||
| "path": "/datasets", | ||||||||||||||
| "method": "GET", | ||||||||||||||
| "query": {} | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| @staticmethod | ||||||||||||||
| def decode_response(response: dict) -> Tuple[Optional[Exception], Optional[ListDatasetsEndpointResponse]]: | ||||||||||||||
| """ | ||||||||||||||
| Decode the response returned from the API | ||||||||||||||
|
|
||||||||||||||
| Args: | ||||||||||||||
| response (dict): The JSON response to encode into a ListDatasetsEndpointResponse | ||||||||||||||
|
|
||||||||||||||
| Returns: | ||||||||||||||
| A tuple containing an optional exception and an optional ListDatasetsEndpointResponse. | ||||||||||||||
| """ | ||||||||||||||
| try: | ||||||||||||||
| return None, ListDatasetsEndpointResponse.from_dict(response) | ||||||||||||||
| except Exception as e: | ||||||||||||||
| return e, None | ||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,92 @@ | ||||||||||
| """ | ||||||||||
| Dataset object for Basalt SDK | ||||||||||
| """ | ||||||||||
|
|
||||||||||
| from dataclasses import dataclass, field | ||||||||||
| from typing import Dict, List, Any, Optional | ||||||||||
|
|
||||||||||
|
|
||||||||||
| @dataclass | ||||||||||
| class DatasetRow: | ||||||||||
| """ | ||||||||||
| A row in a dataset with values and metadata | ||||||||||
| """ | ||||||||||
| values: Dict[str, str] | ||||||||||
| name: Optional[str] = None | ||||||||||
| ideal_output: Optional[str] = None | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Inconsistent naming convention with other dataset classes. This class uses - ideal_output: Optional[str] = None
+ idealOutput: Optional[str] = NoneAnd update the corresponding references: - "idealOutput": self.ideal_output
+ "idealOutput": self.idealOutput- ideal_output=data.get("idealOutput", None),
+ idealOutput=data.get("idealOutput", None),Also applies to: 24-25, 50-50 🤖 Prompt for AI Agents |
||||||||||
| metadata: Dict[str, Any] = field(default_factory=dict) | ||||||||||
|
|
||||||||||
| def to_dict(self) -> Dict[str, Any]: | ||||||||||
| """Convert the DatasetRow to a dictionary for API requests""" | ||||||||||
| result = { | ||||||||||
| "values": self.values, | ||||||||||
| "metadata": self.metadata, | ||||||||||
| "name": self.name, | ||||||||||
| "idealOutput": self.ideal_output | ||||||||||
|
Comment on lines
+24
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix indentation issues and mixed tabs/spaces. Lines 24-25 contain mixed tabs and spaces which will cause syntax errors. The indentation is also incorrect for the dictionary structure. - "name": self.name,
- "idealOutput": self.ideal_output
+ "name": self.name,
+ "idealOutput": self.ideal_output📝 Committable suggestion
Suggested change
🧰 Tools🪛 Flake8 (7.2.0)[error] 24-24: indentation contains mixed spaces and tabs (E101) [error] 24-24: continuation line unaligned for hanging indent (E131) [error] 25-25: indentation contains mixed spaces and tabs (E101) 🤖 Prompt for AI Agents |
||||||||||
| } | ||||||||||
|
|
||||||||||
| # if self.name: | ||||||||||
| # result["name"] = self.name | ||||||||||
|
|
||||||||||
| # if self.ideal_output: | ||||||||||
| # result["idealOutput"] = self.ideal_output | ||||||||||
|
|
||||||||||
| return result | ||||||||||
|
|
||||||||||
| @classmethod | ||||||||||
| def from_dict(cls, data: Dict[str, Any]) -> "DatasetRow": | ||||||||||
| """ | ||||||||||
| Create a DatasetRow instance from a dictionary | ||||||||||
|
|
||||||||||
| Args: | ||||||||||
| data: Dictionary containing dataset row data | ||||||||||
|
|
||||||||||
| Returns: | ||||||||||
| DatasetRow: A new DatasetRow instance | ||||||||||
| """ | ||||||||||
| return cls( | ||||||||||
| values=data.get("values", {}), | ||||||||||
| name=data.get("name", None), | ||||||||||
| ideal_output=data.get("idealOutput", None), | ||||||||||
| metadata=data.get("metadata", {}) | ||||||||||
| ) | ||||||||||
|
|
||||||||||
|
|
||||||||||
| @dataclass | ||||||||||
| class Dataset: | ||||||||||
| """ | ||||||||||
| A dataset with rows and metadata | ||||||||||
| """ | ||||||||||
| slug: str | ||||||||||
| name: str | ||||||||||
| columns: List[str] | ||||||||||
| rows: List[DatasetRow] = field(default_factory=list) | ||||||||||
|
|
||||||||||
| def to_dict(self) -> Dict[str, Any]: | ||||||||||
| """Convert the Dataset to a dictionary for API responses""" | ||||||||||
| return { | ||||||||||
| "slug": self.slug, | ||||||||||
| "name": self.name, | ||||||||||
| "columns": self.columns, | ||||||||||
| "rows": [row.to_dict() for row in self.rows] | ||||||||||
| } | ||||||||||
|
|
||||||||||
| @classmethod | ||||||||||
| def from_dict(cls, data: Dict[str, Any]) -> "Dataset": | ||||||||||
| """ | ||||||||||
| Create a Dataset instance from a dictionary | ||||||||||
|
|
||||||||||
| Args: | ||||||||||
| data: Dictionary containing dataset data | ||||||||||
|
|
||||||||||
| Returns: | ||||||||||
| Dataset: A new Dataset instance | ||||||||||
| """ | ||||||||||
| rows = [DatasetRow.from_dict(row) for row in data.get("rows", [])] | ||||||||||
|
|
||||||||||
| return cls( | ||||||||||
| slug=data["slug"], | ||||||||||
| name=data["name"], | ||||||||||
| columns=data["columns"], | ||||||||||
| rows=rows | ||||||||||
| ) | ||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| """ | ||
| Datasets resource module | ||
| """ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix type consistency in error handling.
The
datasetRowfield is typed asDatasetRowDTO(non-optional) but gets set toNonewhen there's an error. This creates a type inconsistency.Also applies to: 29-30
🤖 Prompt for AI Agents