Skip to content
Merged
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
103 changes: 76 additions & 27 deletions cwms/catalog/blobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"office-id": "SWT",
"id": "MYFILE_OR_BLOB_ID.TXT",
"description": "Your description here",
"media-type-id": "application/octet-stream",
"media-type-id": "text/plain",
"value": "STRING of content or BASE64_ENCODED_STRING"
}
"""
Expand All @@ -19,16 +19,14 @@ def get_blob(blob_id: str, office_id: str) -> str:
"""Get a single BLOB (Binary Large Object).

Parameters
----------
blob_id: string
Specifies the id of the blob. ALL blob ids are UPPERCASE.
office_id: string
Specifies the office of the blob.
blob_id: string
Specifies the id of the blob. ALL blob ids are UPPERCASE.
office_id: string
Specifies the office of the blob.


Returns
-------
str: the value returned based on the content-type it was stored with as a string
Returns
str: the value returned based on the content-type it was stored with as a string
"""

endpoint = f"blobs/{blob_id}"
Expand All @@ -44,18 +42,16 @@ def get_blobs(
) -> Data:
"""Get a subset of Blobs

Parameters
----------
office_id: Optional[string]
Specifies the office of the blob.
page_sie: Optional[Integer]
How many entries per page returned. Default 100.
blob_id_like: Optional[string]
Posix regular expression matching against the clob id

Returns
-------
cwms data type. data.json will return the JSON output and data.df will return a dataframe
Parameters:
office_id: Optional[string]
Specifies the office of the blob.
page_sie: Optional[Integer]
How many entries per page returned. Default 100.
blob_id_like: Optional[string]
Posix regular expression matching against the clob id

Returns:
cwms data type. data.json will return the JSON output and data.df will return a dataframe
"""

endpoint = "blobs"
Expand All @@ -66,21 +62,18 @@ def get_blobs(


def store_blobs(data: JSON, fail_if_exists: Optional[bool] = True) -> None:
f"""Create New Blob
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note here, this format string does not work like I thought it would with pydoc. Was hoping to inject a global STORE_DICT example into the various endpoints but they do not render in the intellisense if you use a format string

"""Create New Blob

Parameters
----------
Parameters:
**Note**: The "id" field is automatically cast to uppercase.

Data: JSON dictionary
JSON containing information of Blob to be updated.

{STORE_DICT}
fail_if_exists: Boolean
Create will fail if the provided ID already exists. Default: True

Returns
-------
Returns:
None
"""

Expand All @@ -97,3 +90,59 @@ def store_blobs(data: JSON, fail_if_exists: Optional[bool] = True) -> None:
endpoint = "blobs"
params = {"fail-if-exists": fail_if_exists}
return api.post(endpoint, data, params, api_version=1)


def delete_blob(blob_id: str, office_id: str) -> None:
"""Delete a single BLOB.

Parameters
----------
blob_id: string
Specifies the id of the blob. ALL blob ids are UPPERCASE.
office_id: string
Specifies the office of the blob.

Returns
-------
None
"""

endpoint = f"blobs/{blob_id}"
params = {"office": office_id}
return api.delete(endpoint, params, api_version=1)


def update_blob(data: JSON, fail_if_not_exists: Optional[bool] = True) -> None:
"""Update Existing Blob

Parameters:
**Note**: The "id" field is automatically cast to uppercase.

Data: JSON dictionary
JSON containing information of Blob to be updated.

fail_if_not_exists: Boolean
Update will fail if the provided ID does not already exist. Default: True

Returns:
None
"""

if not data:
raise ValueError(
f"Cannot update a Blob without a JSON data dictionary:\n{STORE_DICT}"
)

if "id" not in data:
raise ValueError(f"Cannot update a Blob without an 'id' field:\n{STORE_DICT}")

# Encode value if it's not already Base64-encoded
if "value" in data and not is_base64(data["value"]):
# Encode to bytes, then Base64, then decode to string for storing
data["value"] = base64.b64encode(data["value"].encode("utf-8")).decode("utf-8")

blob_id = data.get("id", "").upper()

endpoint = f"blobs/{blob_id}"
params = {"fail-if-not-exists": fail_if_not_exists}
return api.patch(endpoint, data, params, api_version=1)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "cwms-python"
repository = "https://github.com/HydrologicEngineeringCenter/cwms-python"

version = "0.8.2"
version = "0.8.3"


packages = [
Expand Down