Skip to content

Commit 15d0ca2

Browse files
authored
Add delete/patch endpoints for blob (#220)
* Add delete blob * add patch blob * Correct type check to value check * Correct pydoc formatting and test the update blob method * Bump patch version in pyproject.toml
1 parent 1769a73 commit 15d0ca2

File tree

2 files changed

+77
-28
lines changed

2 files changed

+77
-28
lines changed

cwms/catalog/blobs.py

Lines changed: 76 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"office-id": "SWT",
1010
"id": "MYFILE_OR_BLOB_ID.TXT",
1111
"description": "Your description here",
12-
"media-type-id": "application/octet-stream",
12+
"media-type-id": "text/plain",
1313
"value": "STRING of content or BASE64_ENCODED_STRING"
1414
}
1515
"""
@@ -19,16 +19,14 @@ def get_blob(blob_id: str, office_id: str) -> str:
1919
"""Get a single BLOB (Binary Large Object).
2020
2121
Parameters
22-
----------
23-
blob_id: string
24-
Specifies the id of the blob. ALL blob ids are UPPERCASE.
25-
office_id: string
26-
Specifies the office of the blob.
22+
blob_id: string
23+
Specifies the id of the blob. ALL blob ids are UPPERCASE.
24+
office_id: string
25+
Specifies the office of the blob.
2726
2827
29-
Returns
30-
-------
31-
str: the value returned based on the content-type it was stored with as a string
28+
Returns
29+
str: the value returned based on the content-type it was stored with as a string
3230
"""
3331

3432
endpoint = f"blobs/{blob_id}"
@@ -44,18 +42,16 @@ def get_blobs(
4442
) -> Data:
4543
"""Get a subset of Blobs
4644
47-
Parameters
48-
----------
49-
office_id: Optional[string]
50-
Specifies the office of the blob.
51-
page_sie: Optional[Integer]
52-
How many entries per page returned. Default 100.
53-
blob_id_like: Optional[string]
54-
Posix regular expression matching against the clob id
55-
56-
Returns
57-
-------
58-
cwms data type. data.json will return the JSON output and data.df will return a dataframe
45+
Parameters:
46+
office_id: Optional[string]
47+
Specifies the office of the blob.
48+
page_sie: Optional[Integer]
49+
How many entries per page returned. Default 100.
50+
blob_id_like: Optional[string]
51+
Posix regular expression matching against the clob id
52+
53+
Returns:
54+
cwms data type. data.json will return the JSON output and data.df will return a dataframe
5955
"""
6056

6157
endpoint = "blobs"
@@ -66,21 +62,18 @@ def get_blobs(
6662

6763

6864
def store_blobs(data: JSON, fail_if_exists: Optional[bool] = True) -> None:
69-
f"""Create New Blob
65+
"""Create New Blob
7066
71-
Parameters
72-
----------
67+
Parameters:
7368
**Note**: The "id" field is automatically cast to uppercase.
7469
7570
Data: JSON dictionary
7671
JSON containing information of Blob to be updated.
7772
78-
{STORE_DICT}
7973
fail_if_exists: Boolean
8074
Create will fail if the provided ID already exists. Default: True
8175
82-
Returns
83-
-------
76+
Returns:
8477
None
8578
"""
8679

@@ -97,3 +90,59 @@ def store_blobs(data: JSON, fail_if_exists: Optional[bool] = True) -> None:
9790
endpoint = "blobs"
9891
params = {"fail-if-exists": fail_if_exists}
9992
return api.post(endpoint, data, params, api_version=1)
93+
94+
95+
def delete_blob(blob_id: str, office_id: str) -> None:
96+
"""Delete a single BLOB.
97+
98+
Parameters
99+
----------
100+
blob_id: string
101+
Specifies the id of the blob. ALL blob ids are UPPERCASE.
102+
office_id: string
103+
Specifies the office of the blob.
104+
105+
Returns
106+
-------
107+
None
108+
"""
109+
110+
endpoint = f"blobs/{blob_id}"
111+
params = {"office": office_id}
112+
return api.delete(endpoint, params, api_version=1)
113+
114+
115+
def update_blob(data: JSON, fail_if_not_exists: Optional[bool] = True) -> None:
116+
"""Update Existing Blob
117+
118+
Parameters:
119+
**Note**: The "id" field is automatically cast to uppercase.
120+
121+
Data: JSON dictionary
122+
JSON containing information of Blob to be updated.
123+
124+
fail_if_not_exists: Boolean
125+
Update will fail if the provided ID does not already exist. Default: True
126+
127+
Returns:
128+
None
129+
"""
130+
131+
if not data:
132+
raise ValueError(
133+
f"Cannot update a Blob without a JSON data dictionary:\n{STORE_DICT}"
134+
)
135+
136+
if "id" not in data:
137+
raise ValueError(f"Cannot update a Blob without an 'id' field:\n{STORE_DICT}")
138+
139+
# Encode value if it's not already Base64-encoded
140+
if "value" in data and not is_base64(data["value"]):
141+
# Encode to bytes, then Base64, then decode to string for storing
142+
data["value"] = base64.b64encode(data["value"].encode("utf-8")).decode("utf-8")
143+
144+
blob_id = data.get("id", "").upper()
145+
146+
endpoint = f"blobs/{blob_id}"
147+
params = {"fail-if-not-exists": fail_if_not_exists}
148+
return api.patch(endpoint, data, params, api_version=1)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "cwms-python"
33
repository = "https://github.com/HydrologicEngineeringCenter/cwms-python"
44

5-
version = "0.8.2"
5+
version = "0.8.3"
66

77

88
packages = [

0 commit comments

Comments
 (0)