Skip to content

Commit

Permalink
Support more datasources APIs (#55)
Browse files Browse the repository at this point in the history
Add support for:

- sync_datasource
- list_datasources_objects_states
  • Loading branch information
corafid authored Dec 4, 2024
1 parent ca4438e commit 46cd001
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 5 deletions.
2 changes: 1 addition & 1 deletion compass_sdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
ValidatedModel,
)

__version__ = "0.6.0"
__version__ = "0.7.0"


class ProcessFileParameters(ValidatedModel):
Expand Down
52 changes: 49 additions & 3 deletions compass_sdk/clients/compass.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
CreateDataSource,
DataSource,
Document,
DocumentStatus,
PaginatedList,
ParseableDocument,
PushDocumentsInput,
Expand Down Expand Up @@ -119,6 +120,8 @@ def __init__(
"list_datasources": self.session.get,
"delete_datasources": self.session.delete,
"get_datasource": self.session.get,
"sync_datasource": self.session.post,
"list_datasources_objects_states": self.session.get,
}
self.api_endpoint = {
"create_index": "/api/v1/indexes/{index_name}",
Expand All @@ -138,6 +141,8 @@ def __init__(
"list_datasources": "/api/v1/datasources",
"delete_datasources": "/api/v1/datasources/{datasource_id}",
"get_datasource": "/api/v1/datasources/{datasource_id}",
"sync_datasource": "/api/v1/datasources/{datasource_id}/_sync",
"list_datasources_objects_states": "/api/v1/datasources/{datasource_id}/documents?skip={skip}&limit={limit}",
}

def create_index(self, *, index_name: str):
Expand Down Expand Up @@ -460,7 +465,7 @@ def list_datasources(
*,
max_retries: int = DEFAULT_MAX_RETRIES,
sleep_retry_seconds: int = DEFAULT_SLEEP_RETRY_SECONDS,
):
) -> Union[PaginatedList[DataSource], str]:
result = self._send_request(
api_name="list_datasources",
max_retries=max_retries,
Expand All @@ -469,7 +474,7 @@ def list_datasources(

if result.error:
return result.error
return PaginatedList.model_validate(result.result)
return PaginatedList[DataSource].model_validate(result.result)

def get_datasource(
self,
Expand Down Expand Up @@ -507,6 +512,46 @@ def delete_datasource(
return result.error
return result.result

def sync_datasource(
self,
*,
datasource_id: str,
max_retries: int = DEFAULT_MAX_RETRIES,
sleep_retry_seconds: int = DEFAULT_SLEEP_RETRY_SECONDS,
):
result = self._send_request(
api_name="sync_datasource",
datasource_id=datasource_id,
max_retries=max_retries,
sleep_retry_seconds=sleep_retry_seconds,
)

if result.error:
return result.error
return result.result

def list_datasources_objects_states(
self,
*,
datasource_id: str,
skip: int = 0,
limit: int = 100,
max_retries: int = DEFAULT_MAX_RETRIES,
sleep_retry_seconds: int = DEFAULT_SLEEP_RETRY_SECONDS,
) -> Union[PaginatedList[DocumentStatus], str]:
result = self._send_request(
api_name="list_datasources_objects_states",
datasource_id=datasource_id,
max_retries=max_retries,
sleep_retry_seconds=sleep_retry_seconds,
skip=str(skip),
limit=str(limit),
)

if result.error:
return result.error
return PaginatedList[DocumentStatus].model_validate(result.result)

@staticmethod
def _get_request_blocks(
docs: Iterator[CompassDocument],
Expand Down Expand Up @@ -680,7 +725,8 @@ def _send_request_with_retry():

if response.ok:
error = None
return RetryResult(result=response.json(), error=None)
result = response.json() if response.text else None
return RetryResult(result=result, error=None)
else:
response.raise_for_status()

Expand Down
11 changes: 11 additions & 0 deletions compass_sdk/models/datasources.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

class PaginatedList(pydantic.BaseModel, typing.Generic[T]):
value: typing.List[T]
skip: typing.Optional[int]
limit: typing.Optional[int]


class OneDriveConfig(pydantic.BaseModel):
Expand Down Expand Up @@ -47,3 +49,12 @@ class DataSource(pydantic.BaseModel):
class CreateDataSource(pydantic.BaseModel):
datasource: DataSource
state_key: typing.Optional[str] = None


class DocumentStatus(pydantic.BaseModel):
document_id: str
source_id: typing.Optional[str]
state: str
destinations: typing.List[str]
created_at: datetime.datetime
updated_at: typing.Optional[datetime.datetime]
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "compass-sdk"
version = "0.6.0"
version = "0.7.0"
authors = []
description = "Compass SDK"

Expand Down

0 comments on commit 46cd001

Please sign in to comment.