|
| 1 | +from typing import Optional |
| 2 | + |
| 3 | +import cwms.api as api |
| 4 | +from cwms.cwms_types import JSON, Data |
| 5 | + |
| 6 | + |
| 7 | +def get_clob(clob_id: str, office_id: str, clob_id_query: Optional[str] = None) -> Data: |
| 8 | + """Get a single clob. |
| 9 | +
|
| 10 | + Parameters |
| 11 | + ---------- |
| 12 | + clob_id: string |
| 13 | + Specifies the id of the clob |
| 14 | + office_id: string |
| 15 | + Specifies the office of the clob. |
| 16 | + clob_id_query: string |
| 17 | + If this query parameter is provided the id path parameter is ignored and the |
| 18 | + value of the query parameter is used. Note: this query parameter is necessary |
| 19 | + for id's that contain '/' or other special characters. Because of abuse even |
| 20 | + properly escaped '/' in url paths are blocked. When using this query parameter |
| 21 | + a valid path parameter must still be provided for the request to be properly |
| 22 | + routed. If your clob id contains '/' you can't specify the clob-id query |
| 23 | + parameter and also specify the id path parameter because firewall and/or server |
| 24 | + rules will deny the request even though you are specifying this override. "ignored" |
| 25 | + is suggested. |
| 26 | +
|
| 27 | +
|
| 28 | + Returns |
| 29 | + ------- |
| 30 | + cwms data type. data.json will return the JSON output and data.df will return a dataframe |
| 31 | + """ |
| 32 | + |
| 33 | + endpoint = f"clobs/{clob_id}" |
| 34 | + params = { |
| 35 | + "office": office_id, |
| 36 | + "clob-id-query": clob_id_query, |
| 37 | + } |
| 38 | + response = api.get(endpoint, params) |
| 39 | + return Data(response) |
| 40 | + |
| 41 | + |
| 42 | +def get_clobs( |
| 43 | + office_id: Optional[str] = None, |
| 44 | + page_size: Optional[int] = 100, |
| 45 | + include_values: Optional[bool] = False, |
| 46 | + clob_id_like: Optional[str] = None, |
| 47 | +) -> Data: |
| 48 | + """Get a subset of Clobs |
| 49 | +
|
| 50 | + Parameters |
| 51 | + ---------- |
| 52 | + office_id: Optional[string] |
| 53 | + Specifies the office of the clob. |
| 54 | + page_sie: Optional[Integer] |
| 55 | + How many entries per page returned. Default 100. |
| 56 | + include_values: Optional[Boolean] |
| 57 | + Do you want the value associated with this particular clob (default: false) |
| 58 | + clob_id_like: Optional[string] |
| 59 | + Posix regular expression matching against the clob id |
| 60 | +
|
| 61 | + Returns |
| 62 | + ------- |
| 63 | + cwms data type. data.json will return the JSON output and data.df will return a dataframe |
| 64 | + """ |
| 65 | + |
| 66 | + endpoint = "clobs" |
| 67 | + params = { |
| 68 | + "office": office_id, |
| 69 | + "page-size": page_size, |
| 70 | + "include-values": include_values, |
| 71 | + "like": clob_id_like, |
| 72 | + } |
| 73 | + |
| 74 | + response = api.get(endpoint, params) |
| 75 | + return Data(response, selector="clobs") |
| 76 | + |
| 77 | + |
| 78 | +def delete_clob(clob_id: str, office_id: str) -> None: |
| 79 | + """Deletes requested clob |
| 80 | +
|
| 81 | + Parameters |
| 82 | + ---------- |
| 83 | + clob_id: string |
| 84 | + Specifies the id of the clob to be deleted |
| 85 | + office_id: string |
| 86 | + Specifies the office of the clob. |
| 87 | +
|
| 88 | + Returns |
| 89 | + ------- |
| 90 | + None |
| 91 | + """ |
| 92 | + |
| 93 | + endpoint = f"clobs/{clob_id}" |
| 94 | + params = {"office": office_id} |
| 95 | + |
| 96 | + return api.delete(endpoint, params=params, api_version=1) |
| 97 | + |
| 98 | + |
| 99 | +def update_clob(data: JSON, clob_id: str, ignore_nulls: Optional[bool] = True) -> None: |
| 100 | + """Updates clob |
| 101 | +
|
| 102 | + Parameters |
| 103 | + ---------- |
| 104 | + Data: JSON dictionary |
| 105 | + JSON containing information of Clob to be updated |
| 106 | + { |
| 107 | + "office-id": "string", |
| 108 | + "id": "string", |
| 109 | + "description": "string", |
| 110 | + "value": "string" |
| 111 | + } |
| 112 | + clob_id: string |
| 113 | + Specifies the id of the clob to be deleted |
| 114 | + ignore_nulls: Boolean |
| 115 | + If true, null and empty fields in the provided clob will be ignored and the existing value of those fields left in place. Default: true |
| 116 | +
|
| 117 | + Returns |
| 118 | + ------- |
| 119 | + None |
| 120 | + """ |
| 121 | + |
| 122 | + if not isinstance(data, dict): |
| 123 | + raise ValueError("Cannot store a Clob without a JSON data dictionary") |
| 124 | + |
| 125 | + endpoint = f"clobs/{clob_id}" |
| 126 | + params = {"ignore-nulls": ignore_nulls} |
| 127 | + |
| 128 | + return api.patch(endpoint, data, params, api_version=1) |
| 129 | + |
| 130 | + |
| 131 | +def store_clobs(data: JSON, fail_if_exists: Optional[bool] = True) -> None: |
| 132 | + """Create New Clob |
| 133 | +
|
| 134 | + Parameters |
| 135 | + ---------- |
| 136 | + Data: JSON dictionary |
| 137 | + JSON containing information of Clob to be updated |
| 138 | + { |
| 139 | + "office-id": "string", |
| 140 | + "id": "string", |
| 141 | + "description": "string", |
| 142 | + "value": "string" |
| 143 | + } |
| 144 | + fail_if_exists: Boolean |
| 145 | + Create will fail if provided ID already exists. Default: true |
| 146 | +
|
| 147 | + Returns |
| 148 | + ------- |
| 149 | + None |
| 150 | + """ |
| 151 | + |
| 152 | + if not isinstance(data, dict): |
| 153 | + raise ValueError("Cannot store a Clob without a JSON data dictionary") |
| 154 | + |
| 155 | + endpoint = f"clobs" |
| 156 | + params = {"fail-if-exists": fail_if_exists} |
| 157 | + |
| 158 | + return api.post(endpoint, data, params, api_version=1) |
0 commit comments