Skip to content
Open
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
2 changes: 2 additions & 0 deletions pysirix/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
DeleteDiff,
Metadata,
MetaNode,
PathSummaryNode,
)


Expand Down Expand Up @@ -66,4 +67,5 @@ async def sirix_async(
"Metadata",
"MetaNode",
"TimeAxisShift",
"PathSummaryNode",
]
80 changes: 76 additions & 4 deletions pysirix/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async def create_database(self, name: str, db_type: DBType) -> None:
resp.raise_for_status()

async def get_database_info(self, name: str) -> Dict:
resp = await self.client.get(name)
resp = await self.client.get(name, headers={"Accept": "application/json"})
with include_response_text_in_errors():
resp.raise_for_status()
return resp.json()
Expand Down Expand Up @@ -74,12 +74,18 @@ async def create_resource(
hash_type: str = "ROLLING",
use_dewey_ids: bool = False,
hash_kind: str = None,
commit_message: str = None,
commit_timestamp: str = None,
) -> str:
params = {"hashType": hash_type}
if use_dewey_ids:
params["useDeweyIDs"] = "true"
if hash_kind is not None:
params["hashKind"] = hash_kind
if commit_message is not None:
params["commitMessage"] = commit_message
if commit_timestamp is not None:
params["commitTimestamp"] = commit_timestamp
resp = await self.client.put(
f"{db_name}/{name}",
headers={"Content-Type": db_type.value},
Expand All @@ -90,6 +96,31 @@ async def create_resource(
resp.raise_for_status()
return resp.text

async def create_resources(
self,
db_name: str,
db_type: DBType,
resources: Dict[str, BytesLikeAsync],
hash_type: str = "ROLLING",
use_dewey_ids: bool = False,
) -> str:
content_type = db_type.value
files = [
("resource", (name, data, content_type))
for name, data in resources.items()
]
params = {"hashType": hash_type}
if use_dewey_ids:
params["useDeweyIDs"] = "true"
resp = await self.client.post(
db_name,
files=files,
params=params,
)
with include_response_text_in_errors():
resp.raise_for_status()
return resp.text

async def read_resource(
self,
db_name: str,
Expand All @@ -107,14 +138,48 @@ async def read_resource(
else:
return ET.fromstring(resp.text)

async def history(self, db_name: str, db_type: DBType, name: str) -> List[Commit]:
async def history(
self,
db_name: str,
db_type: DBType,
name: str,
revisions: int = None,
start_revision: int = None,
end_revision: int = None,
) -> List[Commit]:
params = {}
if revisions is not None:
params["revisions"] = revisions
if start_revision is not None:
params["startRevision"] = start_revision
if end_revision is not None:
params["endRevision"] = end_revision
resp = await self.client.get(
f"{db_name}/{name}/history", headers={"Accept": db_type.value}
f"{db_name}/{name}/history", params=params, headers={"Accept": db_type.value}
)
with include_response_text_in_errors():
resp.raise_for_status()
return resp.json()["history"]

async def path_summary(
self,
db_name: str,
db_type: DBType,
name: str,
revision: int = None,
) -> Dict:
params = {}
if revision is not None:
params["revision"] = revision
resp = await self.client.get(
f"{db_name}/{name}/pathSummary",
params=params,
headers={"Accept": db_type.value},
)
with include_response_text_in_errors():
resp.raise_for_status()
return resp.json()

async def diff(
self, db_name: str, name: str, params: Dict[str, str]
) -> List[Dict[str, Union[InsertDiff, ReplaceDiff, UpdateDiff, int]]]:
Expand Down Expand Up @@ -152,12 +217,19 @@ async def update(
data: BytesLikeAsync,
insert: Insert,
etag: Union[str, None],
commit_message: str = None,
commit_timestamp: str = None,
) -> str:
if not etag:
etag = await self.get_etag(db_name, db_type, name, {"nodeId": node_id})
params = {"nodeId": node_id, "insert": insert.value}
if commit_message is not None:
params["commitMessage"] = commit_message
if commit_timestamp is not None:
params["commitTimestamp"] = commit_timestamp
resp = await self.client.post(
f"{db_name}/{name}",
params={"nodeId": node_id, "insert": insert.value},
params=params,
headers={"ETag": etag, "Content-Type": db_type.value},
content=data,
)
Expand Down
23 changes: 23 additions & 0 deletions pysirix/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from pysirix.sync_client import SyncClient
from pysirix.async_client import AsyncClient
from pysirix.resource import Resource
from pysirix.types import BytesLike


class Database:
Expand Down Expand Up @@ -82,6 +83,28 @@ def json_store(self, name: str, root: str = ""):
else:
return JsonStoreSync(self.database_name, name, self._client, self._auth, root)

def create_resources(
self,
resources: Dict[str, Union[str, BytesLike]],
hash_type: str = "ROLLING",
use_dewey_ids: bool = False,
):
"""
Create multiple resources in this database via multipart upload.

:param resources: a dict mapping resource names to their data content.
:param hash_type: the hash type to use.
:param use_dewey_ids: whether to use DeweyIDs for node identification.
:return: a ``str`` response.
"""
return self._client.create_resources(
self.database_name,
self.database_type,
resources,
hash_type,
use_dewey_ids,
)

def delete(self) -> Union[Awaitable[None], None]:
"""
Delete the database with the name of this :py:class:`Database` instance.
Expand Down
Loading