cecil 0.0.13__tar.gz → 0.0.15__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of cecil might be problematic. Click here for more details.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: cecil
3
- Version: 0.0.13
3
+ Version: 0.0.15
4
4
  Summary: Python SDK for Cecil Earth
5
5
  License-Expression: MIT
6
6
  License-File: LICENSE.txt
@@ -0,0 +1 @@
1
+ __version__ = "0.0.15"
@@ -1 +1,2 @@
1
1
  from .client import Client
2
+ from .errors import Error
@@ -6,6 +6,13 @@ import snowflake.connector
6
6
  from pydantic import BaseModel
7
7
  from requests import auth
8
8
 
9
+ from .errors import (
10
+ Error,
11
+ _handle_bad_request,
12
+ _handle_not_found,
13
+ _handle_unprocessable_entity,
14
+ )
15
+
9
16
  from .models import (
10
17
  AOI,
11
18
  AOICreate,
@@ -21,7 +28,7 @@ from .models import (
21
28
  )
22
29
 
23
30
  # TODO: find a way to get this version from __about__.py
24
- SDK_VERSION = "0.0.13"
31
+ SDK_VERSION = "0.0.15"
25
32
 
26
33
  # TODO: Documentation (Google style)
27
34
  # TODO: Add HTTP retries
@@ -106,6 +113,7 @@ class Client:
106
113
  res = self._post(
107
114
  url=f"/v0/recover-api-key",
108
115
  model=RecoverAPIKeyRequest(email=email),
116
+ skip_auth=True,
109
117
  )
110
118
 
111
119
  return RecoverAPIKey(**res)
@@ -115,9 +123,10 @@ class Client:
115
123
 
116
124
  return RotateAPIKey(**res)
117
125
 
118
- def _request(self, method: str, url: str, **kwargs) -> Dict:
126
+ def _request(self, method: str, url: str, skip_auth=False, **kwargs) -> Dict:
119
127
 
120
- self._set_auth()
128
+ if skip_auth is False:
129
+ self._set_auth()
121
130
 
122
131
  headers = {"cecil-python-sdk-version": SDK_VERSION}
123
132
 
@@ -133,21 +142,36 @@ class Client:
133
142
  r.raise_for_status()
134
143
  return r.json()
135
144
 
136
- except requests.exceptions.ConnectionError as err:
137
- raise ValueError("Connection error") from err
145
+ except requests.exceptions.ConnectionError:
146
+ raise Error("failed to connect to the Cecil Platform")
138
147
  except requests.exceptions.HTTPError as err:
139
- message = f"Request failed with status code {err.response.status_code}"
140
- if err.response.text != "":
141
- message += f": {err.response.text}"
142
-
143
- raise ValueError(message) from err
148
+ match err.response.status_code:
149
+ case 400:
150
+ _handle_bad_request(err.response)
151
+ case 401:
152
+ raise Error("unauthorised")
153
+ case 404:
154
+ _handle_not_found(err.response)
155
+ case 422:
156
+ _handle_unprocessable_entity(err.response)
157
+ case 500:
158
+ raise Error("internal server error")
159
+ case _:
160
+ raise Error(
161
+ f"request failed with code {err.response.status_code}",
162
+ err.response.text,
163
+ )
144
164
 
145
165
  def _get(self, url: str, **kwargs) -> Dict:
146
166
  return self._request(method="get", url=url, **kwargs)
147
167
 
148
- def _post(self, url: str, model: BaseModel, **kwargs) -> Dict:
168
+ def _post(self, url: str, model: BaseModel, skip_auth=False, **kwargs) -> Dict:
149
169
  return self._request(
150
- method="post", url=url, json=model.model_dump(by_alias=True), **kwargs
170
+ method="post",
171
+ url=url,
172
+ json=model.model_dump(by_alias=True),
173
+ skip_auth=skip_auth,
174
+ **kwargs,
151
175
  )
152
176
 
153
177
  def _set_auth(self) -> None:
@@ -0,0 +1,62 @@
1
+ import json
2
+
3
+
4
+ class Error(Exception):
5
+ def __init__(self, message: str, details=None):
6
+ self.message = message
7
+ self.details = details
8
+
9
+ if self.details is not None:
10
+ super().__init__(f"{self.message} \n{json.dumps(self.details, indent=2)}")
11
+ return
12
+
13
+ super().__init__(self.message)
14
+
15
+
16
+ def _format_json_key(value: str):
17
+ return "".join(["_" + i.lower() if i.isupper() else i for i in value]).lstrip("_")
18
+
19
+
20
+ def _is_json(value: str):
21
+ try:
22
+ json.loads(value)
23
+ return True
24
+ except ValueError:
25
+ return False
26
+
27
+
28
+ def _handle_bad_request(response):
29
+ if not _is_json(response.text):
30
+ raise Error("bad request")
31
+
32
+ details = {}
33
+ for key, value in response.json().items():
34
+ details[_format_json_key(key)] = value
35
+
36
+ raise Error("bad request", details)
37
+
38
+
39
+ def _handle_not_found(response):
40
+ if not _is_json(response.text):
41
+ raise Error("resource not found")
42
+
43
+ details = {}
44
+ for key, value in response.json().items():
45
+ details[_format_json_key(key)] = value
46
+
47
+ raise Error("resource not found", details)
48
+
49
+
50
+ def _handle_unprocessable_entity(response):
51
+ if not _is_json(response.text):
52
+ raise Error(f"failed to process request")
53
+
54
+ res_body = response.json()
55
+ if "params" not in res_body:
56
+ raise Error(f"failed to process request")
57
+
58
+ details = {}
59
+ for key, value in res_body["params"].items():
60
+ details[_format_json_key(key)] = value
61
+
62
+ raise Error(f"failed to process request", details)
cecil-0.0.13/__about__.py DELETED
@@ -1 +0,0 @@
1
- __version__ = "0.0.13"
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes