clear-skies 2.0.4__py3-none-any.whl → 2.0.5__py3-none-any.whl

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 clear-skies might be problematic. Click here for more details.

Files changed (253) hide show
  1. clear_skies-2.0.5.dist-info/METADATA +74 -0
  2. clear_skies-2.0.5.dist-info/RECORD +4 -0
  3. {clear_skies-2.0.4.dist-info → clear_skies-2.0.5.dist-info}/WHEEL +1 -1
  4. clear_skies-2.0.4.dist-info/METADATA +0 -36
  5. clear_skies-2.0.4.dist-info/RECORD +0 -251
  6. clearskies/__init__.py +0 -61
  7. clearskies/action.py +0 -7
  8. clearskies/authentication/__init__.py +0 -15
  9. clearskies/authentication/authentication.py +0 -46
  10. clearskies/authentication/authorization.py +0 -16
  11. clearskies/authentication/authorization_pass_through.py +0 -20
  12. clearskies/authentication/jwks.py +0 -163
  13. clearskies/authentication/public.py +0 -5
  14. clearskies/authentication/secret_bearer.py +0 -553
  15. clearskies/autodoc/__init__.py +0 -8
  16. clearskies/autodoc/formats/__init__.py +0 -5
  17. clearskies/autodoc/formats/oai3_json/__init__.py +0 -7
  18. clearskies/autodoc/formats/oai3_json/oai3_json.py +0 -87
  19. clearskies/autodoc/formats/oai3_json/oai3_schema_resolver.py +0 -15
  20. clearskies/autodoc/formats/oai3_json/parameter.py +0 -35
  21. clearskies/autodoc/formats/oai3_json/request.py +0 -68
  22. clearskies/autodoc/formats/oai3_json/response.py +0 -28
  23. clearskies/autodoc/formats/oai3_json/schema/__init__.py +0 -11
  24. clearskies/autodoc/formats/oai3_json/schema/array.py +0 -9
  25. clearskies/autodoc/formats/oai3_json/schema/default.py +0 -13
  26. clearskies/autodoc/formats/oai3_json/schema/enum.py +0 -7
  27. clearskies/autodoc/formats/oai3_json/schema/object.py +0 -35
  28. clearskies/autodoc/formats/oai3_json/test.json +0 -1985
  29. clearskies/autodoc/py.typed +0 -0
  30. clearskies/autodoc/request/__init__.py +0 -15
  31. clearskies/autodoc/request/header.py +0 -6
  32. clearskies/autodoc/request/json_body.py +0 -6
  33. clearskies/autodoc/request/parameter.py +0 -8
  34. clearskies/autodoc/request/request.py +0 -47
  35. clearskies/autodoc/request/url_parameter.py +0 -6
  36. clearskies/autodoc/request/url_path.py +0 -6
  37. clearskies/autodoc/response/__init__.py +0 -5
  38. clearskies/autodoc/response/response.py +0 -9
  39. clearskies/autodoc/schema/__init__.py +0 -31
  40. clearskies/autodoc/schema/array.py +0 -10
  41. clearskies/autodoc/schema/base64.py +0 -8
  42. clearskies/autodoc/schema/boolean.py +0 -5
  43. clearskies/autodoc/schema/date.py +0 -5
  44. clearskies/autodoc/schema/datetime.py +0 -5
  45. clearskies/autodoc/schema/double.py +0 -5
  46. clearskies/autodoc/schema/enum.py +0 -17
  47. clearskies/autodoc/schema/integer.py +0 -6
  48. clearskies/autodoc/schema/long.py +0 -5
  49. clearskies/autodoc/schema/number.py +0 -6
  50. clearskies/autodoc/schema/object.py +0 -13
  51. clearskies/autodoc/schema/password.py +0 -5
  52. clearskies/autodoc/schema/schema.py +0 -11
  53. clearskies/autodoc/schema/string.py +0 -5
  54. clearskies/backends/__init__.py +0 -65
  55. clearskies/backends/api_backend.py +0 -1178
  56. clearskies/backends/backend.py +0 -136
  57. clearskies/backends/cursor_backend.py +0 -335
  58. clearskies/backends/memory_backend.py +0 -797
  59. clearskies/backends/secrets_backend.py +0 -106
  60. clearskies/column.py +0 -1233
  61. clearskies/columns/__init__.py +0 -71
  62. clearskies/columns/audit.py +0 -206
  63. clearskies/columns/belongs_to_id.py +0 -483
  64. clearskies/columns/belongs_to_model.py +0 -132
  65. clearskies/columns/belongs_to_self.py +0 -105
  66. clearskies/columns/boolean.py +0 -113
  67. clearskies/columns/category_tree.py +0 -275
  68. clearskies/columns/category_tree_ancestors.py +0 -51
  69. clearskies/columns/category_tree_children.py +0 -127
  70. clearskies/columns/category_tree_descendants.py +0 -48
  71. clearskies/columns/created.py +0 -95
  72. clearskies/columns/created_by_authorization_data.py +0 -116
  73. clearskies/columns/created_by_header.py +0 -99
  74. clearskies/columns/created_by_ip.py +0 -92
  75. clearskies/columns/created_by_routing_data.py +0 -97
  76. clearskies/columns/created_by_user_agent.py +0 -92
  77. clearskies/columns/date.py +0 -234
  78. clearskies/columns/datetime.py +0 -282
  79. clearskies/columns/email.py +0 -76
  80. clearskies/columns/float.py +0 -153
  81. clearskies/columns/has_many.py +0 -505
  82. clearskies/columns/has_many_self.py +0 -56
  83. clearskies/columns/has_one.py +0 -14
  84. clearskies/columns/integer.py +0 -160
  85. clearskies/columns/json.py +0 -126
  86. clearskies/columns/many_to_many_ids.py +0 -337
  87. clearskies/columns/many_to_many_ids_with_data.py +0 -274
  88. clearskies/columns/many_to_many_models.py +0 -158
  89. clearskies/columns/many_to_many_pivots.py +0 -134
  90. clearskies/columns/phone.py +0 -159
  91. clearskies/columns/select.py +0 -92
  92. clearskies/columns/string.py +0 -102
  93. clearskies/columns/timestamp.py +0 -164
  94. clearskies/columns/updated.py +0 -110
  95. clearskies/columns/uuid.py +0 -86
  96. clearskies/configs/README.md +0 -105
  97. clearskies/configs/__init__.py +0 -162
  98. clearskies/configs/actions.py +0 -43
  99. clearskies/configs/any.py +0 -13
  100. clearskies/configs/any_dict.py +0 -22
  101. clearskies/configs/any_dict_or_callable.py +0 -23
  102. clearskies/configs/authentication.py +0 -23
  103. clearskies/configs/authorization.py +0 -23
  104. clearskies/configs/boolean.py +0 -16
  105. clearskies/configs/boolean_or_callable.py +0 -18
  106. clearskies/configs/callable_config.py +0 -18
  107. clearskies/configs/columns.py +0 -34
  108. clearskies/configs/conditions.py +0 -30
  109. clearskies/configs/config.py +0 -24
  110. clearskies/configs/datetime.py +0 -18
  111. clearskies/configs/datetime_or_callable.py +0 -19
  112. clearskies/configs/endpoint.py +0 -23
  113. clearskies/configs/endpoint_list.py +0 -28
  114. clearskies/configs/float.py +0 -16
  115. clearskies/configs/float_or_callable.py +0 -18
  116. clearskies/configs/integer.py +0 -16
  117. clearskies/configs/integer_or_callable.py +0 -18
  118. clearskies/configs/joins.py +0 -30
  119. clearskies/configs/list_any_dict.py +0 -30
  120. clearskies/configs/list_any_dict_or_callable.py +0 -31
  121. clearskies/configs/model_class.py +0 -35
  122. clearskies/configs/model_column.py +0 -65
  123. clearskies/configs/model_columns.py +0 -56
  124. clearskies/configs/model_destination_name.py +0 -25
  125. clearskies/configs/model_to_id_column.py +0 -43
  126. clearskies/configs/readable_model_column.py +0 -9
  127. clearskies/configs/readable_model_columns.py +0 -9
  128. clearskies/configs/schema.py +0 -23
  129. clearskies/configs/searchable_model_columns.py +0 -9
  130. clearskies/configs/security_headers.py +0 -39
  131. clearskies/configs/select.py +0 -26
  132. clearskies/configs/select_list.py +0 -47
  133. clearskies/configs/string.py +0 -29
  134. clearskies/configs/string_dict.py +0 -32
  135. clearskies/configs/string_list.py +0 -32
  136. clearskies/configs/string_list_or_callable.py +0 -35
  137. clearskies/configs/string_or_callable.py +0 -18
  138. clearskies/configs/timedelta.py +0 -18
  139. clearskies/configs/timezone.py +0 -18
  140. clearskies/configs/url.py +0 -23
  141. clearskies/configs/validators.py +0 -45
  142. clearskies/configs/writeable_model_column.py +0 -9
  143. clearskies/configs/writeable_model_columns.py +0 -9
  144. clearskies/configurable.py +0 -76
  145. clearskies/contexts/__init__.py +0 -11
  146. clearskies/contexts/cli.py +0 -117
  147. clearskies/contexts/context.py +0 -98
  148. clearskies/contexts/wsgi.py +0 -76
  149. clearskies/contexts/wsgi_ref.py +0 -82
  150. clearskies/decorators.py +0 -33
  151. clearskies/di/__init__.py +0 -14
  152. clearskies/di/additional_config.py +0 -130
  153. clearskies/di/additional_config_auto_import.py +0 -17
  154. clearskies/di/di.py +0 -973
  155. clearskies/di/inject/__init__.py +0 -23
  156. clearskies/di/inject/by_class.py +0 -21
  157. clearskies/di/inject/by_name.py +0 -18
  158. clearskies/di/inject/di.py +0 -13
  159. clearskies/di/inject/environment.py +0 -14
  160. clearskies/di/inject/input_output.py +0 -20
  161. clearskies/di/inject/now.py +0 -13
  162. clearskies/di/inject/requests.py +0 -13
  163. clearskies/di/inject/secrets.py +0 -14
  164. clearskies/di/inject/utcnow.py +0 -13
  165. clearskies/di/inject/uuid.py +0 -15
  166. clearskies/di/injectable.py +0 -29
  167. clearskies/di/injectable_properties.py +0 -131
  168. clearskies/di/test_module/__init__.py +0 -6
  169. clearskies/di/test_module/another_module/__init__.py +0 -2
  170. clearskies/di/test_module/module_class.py +0 -5
  171. clearskies/end.py +0 -183
  172. clearskies/endpoint.py +0 -1314
  173. clearskies/endpoint_group.py +0 -338
  174. clearskies/endpoints/__init__.py +0 -25
  175. clearskies/endpoints/advanced_search.py +0 -526
  176. clearskies/endpoints/callable.py +0 -388
  177. clearskies/endpoints/create.py +0 -205
  178. clearskies/endpoints/delete.py +0 -139
  179. clearskies/endpoints/get.py +0 -271
  180. clearskies/endpoints/health_check.py +0 -183
  181. clearskies/endpoints/list.py +0 -574
  182. clearskies/endpoints/restful_api.py +0 -427
  183. clearskies/endpoints/schema.py +0 -189
  184. clearskies/endpoints/simple_search.py +0 -286
  185. clearskies/endpoints/update.py +0 -193
  186. clearskies/environment.py +0 -104
  187. clearskies/exceptions/__init__.py +0 -19
  188. clearskies/exceptions/authentication.py +0 -2
  189. clearskies/exceptions/authorization.py +0 -2
  190. clearskies/exceptions/client_error.py +0 -2
  191. clearskies/exceptions/input_errors.py +0 -4
  192. clearskies/exceptions/missing_dependency.py +0 -2
  193. clearskies/exceptions/moved_permanently.py +0 -3
  194. clearskies/exceptions/moved_temporarily.py +0 -3
  195. clearskies/exceptions/not_found.py +0 -2
  196. clearskies/functional/__init__.py +0 -7
  197. clearskies/functional/routing.py +0 -92
  198. clearskies/functional/string.py +0 -112
  199. clearskies/functional/validations.py +0 -76
  200. clearskies/input_outputs/__init__.py +0 -13
  201. clearskies/input_outputs/cli.py +0 -171
  202. clearskies/input_outputs/exceptions/__init__.py +0 -2
  203. clearskies/input_outputs/exceptions/cli_input_error.py +0 -2
  204. clearskies/input_outputs/exceptions/cli_not_found.py +0 -2
  205. clearskies/input_outputs/headers.py +0 -45
  206. clearskies/input_outputs/input_output.py +0 -138
  207. clearskies/input_outputs/programmatic.py +0 -69
  208. clearskies/input_outputs/py.typed +0 -0
  209. clearskies/input_outputs/wsgi.py +0 -77
  210. clearskies/model.py +0 -1922
  211. clearskies/py.typed +0 -0
  212. clearskies/query/__init__.py +0 -12
  213. clearskies/query/condition.py +0 -223
  214. clearskies/query/join.py +0 -136
  215. clearskies/query/query.py +0 -196
  216. clearskies/query/sort.py +0 -27
  217. clearskies/schema.py +0 -82
  218. clearskies/secrets/__init__.py +0 -6
  219. clearskies/secrets/additional_configs/__init__.py +0 -32
  220. clearskies/secrets/additional_configs/mysql_connection_dynamic_producer.py +0 -61
  221. clearskies/secrets/additional_configs/mysql_connection_dynamic_producer_via_ssh_cert_bastion.py +0 -160
  222. clearskies/secrets/akeyless.py +0 -182
  223. clearskies/secrets/exceptions/__init__.py +0 -1
  224. clearskies/secrets/exceptions/not_found.py +0 -2
  225. clearskies/secrets/secrets.py +0 -38
  226. clearskies/security_header.py +0 -15
  227. clearskies/security_headers/__init__.py +0 -11
  228. clearskies/security_headers/cache_control.py +0 -67
  229. clearskies/security_headers/cors.py +0 -50
  230. clearskies/security_headers/csp.py +0 -94
  231. clearskies/security_headers/hsts.py +0 -22
  232. clearskies/security_headers/x_content_type_options.py +0 -0
  233. clearskies/security_headers/x_frame_options.py +0 -0
  234. clearskies/test_base.py +0 -8
  235. clearskies/typing.py +0 -11
  236. clearskies/validator.py +0 -37
  237. clearskies/validators/__init__.py +0 -33
  238. clearskies/validators/after_column.py +0 -62
  239. clearskies/validators/before_column.py +0 -13
  240. clearskies/validators/in_the_future.py +0 -32
  241. clearskies/validators/in_the_future_at_least.py +0 -11
  242. clearskies/validators/in_the_future_at_most.py +0 -10
  243. clearskies/validators/in_the_past.py +0 -32
  244. clearskies/validators/in_the_past_at_least.py +0 -10
  245. clearskies/validators/in_the_past_at_most.py +0 -10
  246. clearskies/validators/maximum_length.py +0 -26
  247. clearskies/validators/maximum_value.py +0 -29
  248. clearskies/validators/minimum_length.py +0 -26
  249. clearskies/validators/minimum_value.py +0 -29
  250. clearskies/validators/required.py +0 -34
  251. clearskies/validators/timedelta.py +0 -59
  252. clearskies/validators/unique.py +0 -30
  253. {clear_skies-2.0.4.dist-info → clear_skies-2.0.5.dist-info/licenses}/LICENSE +0 -0
@@ -1,286 +0,0 @@
1
- from __future__ import annotations
2
-
3
- import inspect
4
- from collections import OrderedDict
5
- from typing import TYPE_CHECKING, Any, Callable
6
-
7
- import clearskies.configs
8
- import clearskies.exceptions
9
- from clearskies import authentication, autodoc, typing
10
- from clearskies.endpoints.list import List
11
- from clearskies.functional import string
12
- from clearskies.input_outputs import InputOutput
13
-
14
- if TYPE_CHECKING:
15
- from clearskies import Column, Schema, SecurityHeader
16
- from clearskies.model import Model
17
-
18
-
19
- class SimpleSearch(List):
20
- """
21
- Create an endpoint that supports searching by exact values via url/JSON parameters.
22
-
23
- This acts exactly like the list endpoint but additionally grants the client the ability to search records
24
- via URL parameters or JSON POST body parameters. You just have to specify which columns are searchable.
25
-
26
- In the following example we tell the `SimpleSearch` endpoint that we want it to return records from the
27
- `Student` model, return `id`, `name`, and `grade` in the results, and allow the user to search by
28
- `name` and `grade`. We also seed the memory backend with data so the endpoint has something to return:
29
-
30
- ```python
31
- import clearskies
32
-
33
-
34
- class Student(clearskies.Model):
35
- backend = clearskies.backends.MemoryBackend()
36
- id_column_name = "id"
37
-
38
- id = clearskies.columns.Uuid()
39
- name = clearskies.columns.String()
40
- grade = clearskies.columns.Integer()
41
-
42
-
43
- wsgi = clearskies.contexts.WsgiRef(
44
- clearskies.endpoints.SimpleSearch(
45
- Student,
46
- readable_column_names=["id", "name", "grade"],
47
- sortable_column_names=["name", "grade"],
48
- searchable_column_names=["name", "grade"],
49
- default_sort_column_name="name",
50
- ),
51
- bindings={
52
- "memory_backend_default_data": [
53
- {
54
- "model_class": Student,
55
- "records": [
56
- {"id": "1-2-3-4", "name": "Bob", "grade": 5},
57
- {"id": "1-2-3-5", "name": "Jane", "grade": 3},
58
- {"id": "1-2-3-6", "name": "Greg", "grade": 3},
59
- {"id": "1-2-3-7", "name": "Bob", "grade": 2},
60
- ],
61
- },
62
- ],
63
- },
64
- )
65
- wsgi()
66
- ```
67
-
68
- Here is the basic operation of the endpoint itself, without any search parameters, in which case it behaves
69
- identically to the list endpoint:
70
-
71
- ```bash
72
- $ curl 'http://localhost:8080' | jq
73
- {
74
- "status": "success",
75
- "error": "",
76
- "data": [
77
- {
78
- "id": "1-2-3-4",
79
- "name": "Bob",
80
- "grade": 5
81
- },
82
- {
83
- "id": "1-2-3-7",
84
- "name": "Bob",
85
- "grade": 2
86
- },
87
- {
88
- "id": "1-2-3-6",
89
- "name": "Greg",
90
- "grade": 3
91
- },
92
- {
93
- "id": "1-2-3-5",
94
- "name": "Jane",
95
- "grade": 3
96
- }
97
- ],
98
- "pagination": {},
99
- "input_errors": {}
100
- }
101
- ```
102
-
103
- We can then search on name via the `name` URL parameter:
104
-
105
- ```bash
106
- $ curl 'http://localhost:8080?name=Bob' | jq
107
- {
108
- "status": "success",
109
- "error": "",
110
- "data": [
111
- {
112
- "id": "1-2-3-4",
113
- "name": "Bob",
114
- "grade": 5
115
- },
116
- {
117
- "id": "1-2-3-7",
118
- "name": "Bob",
119
- "grade": 2
120
- }
121
- ],
122
- "pagination": {},
123
- "input_errors": {}
124
- }
125
- ```
126
-
127
- and multiple search terms are allowed:
128
-
129
- ```bash
130
- $ curl 'http://localhost:8080?name=Bob&grade=2' | jq
131
- {
132
- "status": "success",
133
- "error": "",
134
- "data": [
135
- {
136
- "id": "1-2-3-7",
137
- "name": "Bob",
138
- "grade": 2
139
- }
140
- ],
141
- "pagination": {},
142
- "input_errors": {}
143
- }
144
- ```
145
-
146
- Pagination and sorting work just like with the list endpoint:
147
-
148
- ```bash
149
- $ curl 'http://localhost:8080?sort=grade&direction=desc&limit=2' | jq
150
- {
151
- "status": "success",
152
- "error": "",
153
- "data": [
154
- {
155
- "id": "1-2-3-4",
156
- "name": "Bob",
157
- "grade": 5
158
- },
159
- {
160
- "id": "1-2-3-5",
161
- "name": "Jane",
162
- "grade": 3
163
- }
164
- ],
165
- "pagination": {
166
- "number_results": 4,
167
- "limit": 2,
168
- "next_page": {
169
- "start": 2
170
- }
171
- },
172
- "input_errors": {}
173
- }
174
-
175
- $ curl 'http://localhost:8080?sort=grade&direction=desc&limit=2&start=2' | jq
176
- {
177
- "status": "success",
178
- "error": "",
179
- "data": [
180
- {
181
- "id": "1-2-3-6",
182
- "name": "Greg",
183
- "grade": 3
184
- },
185
- {
186
- "id": "1-2-3-7",
187
- "name": "Bob",
188
- "grade": 2
189
- }
190
- ],
191
- "pagination": {},
192
- "input_errors": {}
193
- }
194
- ```
195
- """
196
-
197
- @clearskies.decorators.parameters_to_properties
198
- def __init__(
199
- self,
200
- model_class: type[Model],
201
- readable_column_names: list[str],
202
- sortable_column_names: list[str],
203
- searchable_column_names: list[str],
204
- default_sort_column_name: str,
205
- default_sort_direction: str = "ASC",
206
- default_limit: int = 50,
207
- maximum_limit: int = 200,
208
- where: typing.condition | list[typing.condition] = [],
209
- joins: typing.join | list[typing.join] = [],
210
- url: str = "",
211
- request_methods: list[str] = ["GET", "POST", "QUERY"],
212
- response_headers: list[str | Callable[..., list[str]]] = [],
213
- output_map: Callable[..., dict[str, Any]] | None = None,
214
- output_schema: Schema | None = None,
215
- column_overrides: dict[str, Column] = {},
216
- internal_casing: str = "snake_case",
217
- external_casing: str = "snake_case",
218
- security_headers: list[SecurityHeader] = [],
219
- description: str = "",
220
- authentication: authentication.Authentication = authentication.Public(),
221
- authorization: authentication.Authorization = authentication.Authorization(),
222
- ):
223
- self.request_methods = request_methods
224
-
225
- # we need to call the parent but don't have to pass along any of our kwargs. They are all optional in our parent, and our parent class
226
- # just stores them in parameters, which we have already done. However, the parent does do some extra initialization stuff that we need,
227
- # which is why we have to call the parent.
228
- super().__init__(model_class, readable_column_names, sortable_column_names, default_sort_column_name)
229
-
230
- def check_search_in_request_data(self, request_data: dict[str, Any], query_parameters: dict[str, Any]) -> None:
231
- for input_source_label, input_data in [("request body", request_data), ("URL data", query_parameters)]:
232
- for column_name, value in input_data.items():
233
- if column_name in self.allowed_request_keys and column_name not in self.searchable_column_names:
234
- continue
235
- if column_name not in self.searchable_column_names:
236
- raise clearskies.exceptions.ClientError(
237
- f"Invalid request parameter found in {input_source_label}: '{column_name}'"
238
- )
239
- [relationship_column_name, final_column_name] = self.unpack_column_name_with_relationship(column_name)
240
- column_to_check = relationship_column_name if relationship_column_name else final_column_name
241
- value_error = self.searchable_columns[column_to_check].check_search_value(
242
- value, relationship_reference=final_column_name
243
- )
244
- if value_error:
245
- raise clearskies.exceptions.InputErrors({column_name: value_error})
246
-
247
- def configure_model_from_request_data(
248
- self,
249
- model: Model,
250
- request_data: dict[str, Any],
251
- query_parameters: dict[str, Any],
252
- pagination_data: dict[str, Any],
253
- ) -> Model:
254
- model = super().configure_model_from_request_data(
255
- model,
256
- request_data,
257
- query_parameters,
258
- pagination_data,
259
- )
260
-
261
- for input_source in [request_data, query_parameters]:
262
- for column_name, value in input_source.items():
263
- if column_name not in self.searchable_column_names:
264
- continue
265
-
266
- model = self.add_join(column_name, model)
267
- [relationship_column_name, column_name] = self.unpack_column_name_with_relationship(column_name)
268
- if relationship_column_name:
269
- self.columns[relationship_column_name].add_search(model, value, relationship_reference=column_name)
270
- else:
271
- model = self.columns[column_name].add_search(model, value, operator="=")
272
-
273
- return model
274
-
275
- def documentation_url_search_parameters(self) -> list[autodoc.request.Parameter]:
276
- docs = []
277
- for column in self.searchable_columns.values():
278
- column_doc = column.documentation()[0]
279
- column_doc.name = self.auto_case_internal_column_name(column_doc.name)
280
- docs.append(
281
- autodoc.request.URLParameter(
282
- column_doc,
283
- description=f"Search by {column_doc.name} (via exact match)",
284
- )
285
- )
286
- return docs # type: ignore
@@ -1,193 +0,0 @@
1
- from __future__ import annotations
2
-
3
- import inspect
4
- from collections import OrderedDict
5
- from typing import TYPE_CHECKING, Any, Callable, Type
6
-
7
- import clearskies.configs
8
- import clearskies.exceptions
9
- from clearskies import authentication, autodoc, typing
10
- from clearskies.endpoints.get import Get
11
- from clearskies.functional import routing, string
12
- from clearskies.input_outputs import InputOutput
13
-
14
- if TYPE_CHECKING:
15
- from clearskies import SecurityHeader
16
- from clearskies.model import Column, Model, Schema
17
-
18
-
19
- class Update(Get):
20
- """
21
- An endpoint to update a record.
22
-
23
- This endpoint handles update operations. As with the `Get` endpoint, it will lookup the record by taking
24
- the record id (or any other unique column you specify) out of the URL and then will fetch that record
25
- using the model class. Then, it will use the model and list of writeable column names to validate the
26
- incoming user input. The default request method is `PATCH`. If everything checks out, it will then
27
- update the record.
28
-
29
- ```python
30
- import clearskies
31
-
32
-
33
- class User(clearskies.Model):
34
- id_column_name = "id"
35
- backend = clearskies.backends.MemoryBackend()
36
- id = clearskies.columns.Uuid()
37
- name = clearskies.columns.String()
38
- username = clearskies.columns.String(validators=[clearskies.validators.Required()])
39
-
40
-
41
- wsgi = clearskies.contexts.WsgiRef(
42
- clearskies.endpoints.Update(
43
- model_class=User,
44
- url="/{id}",
45
- readable_column_names=["id", "name", "username"],
46
- writeable_column_names=["name", "username"],
47
- ),
48
- bindings={
49
- "memory_backend_default_data": [
50
- {
51
- "model_class": User,
52
- "records": [
53
- {"id": "1-2-3-4", "name": "Bob Brown", "username": "bobbrown"},
54
- {"id": "1-2-3-5", "name": "Jane Doe", "username": "janedoe"},
55
- {"id": "1-2-3-6", "name": "Greg", "username": "greg"},
56
- ],
57
- },
58
- ],
59
- },
60
- )
61
- wsgi()
62
- ```
63
-
64
- And when invoked:
65
-
66
- ```bash
67
- $ curl 'http://localhost:8080/1-2-3-4' -X PATCH -d '{"name": "Bobby Brown", "username": "bobbybrown"}' | jq
68
- {
69
- "status": "success",
70
- "error": "",
71
- "data": {
72
- "id": "1-2-3-4",
73
- "name": "Bobby Brown",
74
- "username": "bobbybrown"
75
- },
76
- "pagination": {},
77
- "input_errors": {}
78
- }
79
-
80
- $ curl 'http://localhost:8080/1-2-3-5' -X PATCH -d '{"name": 12345, "username": ""}' | jq
81
- {
82
- "status": "input_errors",
83
- "error": "",
84
- "data": [],
85
- "pagination": {},
86
- "input_errors": {
87
- "name": "value should be a string",
88
- "username": "'username' is required."
89
- }
90
- }
91
- """
92
-
93
- @clearskies.decorators.parameters_to_properties
94
- def __init__(
95
- self,
96
- model_class: type[Model],
97
- url: str,
98
- writeable_column_names: list[str],
99
- readable_column_names: list[str],
100
- record_lookup_column_name: str | None = None,
101
- input_validation_callable: Callable | None = None,
102
- request_methods: list[str] = ["PATCH"],
103
- response_headers: list[str | Callable[..., list[str]]] = [],
104
- output_map: Callable[..., dict[str, Any]] | None = None,
105
- output_schema: Schema | None = None,
106
- column_overrides: dict[str, Column] = {},
107
- internal_casing: str = "snake_case",
108
- external_casing: str = "snake_case",
109
- security_headers: list[SecurityHeader] = [],
110
- description: str = "",
111
- where: typing.condition | list[typing.condition] = [],
112
- joins: typing.join | list[typing.join] = [],
113
- authentication: authentication.Authentication = authentication.Public(),
114
- authorization: authentication.Authorization = authentication.Authorization(),
115
- ):
116
- # see comment in clearskies.endpoints.Create.__init__
117
- self.request_methods = request_methods
118
-
119
- # we need to call the parent but don't have to pass along any of our kwargs. They are all optional in our parent, and our parent class
120
- # just stores them in parameters, which we have already done. However, the parent does do some extra initialization stuff that we need,
121
- # which is why we have to call the parent.
122
- super().__init__(model_class, url, readable_column_names)
123
-
124
- def handle(self, input_output: InputOutput) -> Any:
125
- request_data = self.get_request_data(input_output)
126
- if not request_data and input_output.has_body():
127
- raise clearskies.exceptions.ClientError("Request body was not valid JSON")
128
- model = self.fetch_model(input_output)
129
- self.validate_input_against_schema(request_data, input_output, model)
130
- model.save(request_data)
131
- return self.success(input_output, self.model_as_json(model, input_output))
132
-
133
- def documentation(self) -> list[autodoc.request.Request]:
134
- output_schema = self.model_class
135
- nice_model = string.camel_case_to_words(output_schema.__name__)
136
-
137
- schema_model_name = string.camel_case_to_snake_case(output_schema.__name__)
138
- output_data_schema = self.documentation_data_schema(output_schema, self.readable_column_names)
139
- output_autodoc = (
140
- autodoc.schema.Object(
141
- self.auto_case_internal_column_name("data"), children=output_data_schema, model_name=schema_model_name
142
- ),
143
- )
144
-
145
- authentication = self.authentication
146
- # Many swagger UIs will only allow one response per status code, and we use the same status code (200)
147
- # for both a success response and an input error response. This could be fixed by changing the status
148
- # code for input error responses, but there's not actually a great HTTP status code for that, so :shrug:
149
- # standard_error_responses = [self.documentation_input_error_response()]
150
- standard_error_responses = []
151
- if not getattr(authentication, "is_public", False):
152
- standard_error_responses.append(self.documentation_access_denied_response())
153
- if getattr(authentication, "can_authorize", False):
154
- standard_error_responses.append(self.documentation_unauthorized_response())
155
-
156
- return [
157
- autodoc.request.Request(
158
- self.description,
159
- [
160
- self.documentation_success_response(
161
- output_autodoc, # type: ignore
162
- description=self.description,
163
- ),
164
- *standard_error_responses,
165
- self.documentation_generic_error_response(),
166
- ],
167
- relative_path=self.url,
168
- request_methods=self.request_methods,
169
- parameters=[
170
- *self.documentation_request_parameters(),
171
- *self.documentation_url_parameters(),
172
- ],
173
- root_properties={
174
- "security": self.documentation_request_security(),
175
- },
176
- ),
177
- ]
178
-
179
- def documentation_request_parameters(self) -> list[autodoc.request.Parameter]:
180
- return [
181
- *self.standard_json_request_parameters(self.model_class),
182
- ]
183
-
184
- def documentation_models(self) -> dict[str, autodoc.schema.Schema]:
185
- output_schema = self.output_schema if self.output_schema else self.model_class
186
- schema_model_name = string.camel_case_to_snake_case(output_schema.__name__)
187
-
188
- return {
189
- schema_model_name: autodoc.schema.Object(
190
- self.auto_case_internal_column_name("data"),
191
- children=self.documentation_data_schema(output_schema, self.readable_column_names),
192
- ),
193
- }
clearskies/environment.py DELETED
@@ -1,104 +0,0 @@
1
- import os.path
2
- from typing import Any
3
-
4
-
5
- class Environment:
6
- """
7
- This loads up the environment configuration for the application.
8
-
9
- It looks in 3 possible places: first, it looks in os.environ. Next, it tries to load up the .env file.
10
- Therefore, the application root directory should be passed in, at will look for a .env file there.
11
- It should contain lines like NAME=value. Finally, if there is a value of `secret://path/to/secret`,
12
- it will use the secret service to look up the secret value.
13
-
14
- It is a very basic parser. Empty lines and lines starting with a # will be ignored. Otherwise everything
15
- is assumed to be a string.
16
- """
17
-
18
- _env_file_config: dict[str, Any] = None # type: ignore
19
- _resolved_values: dict[str, Any] = {}
20
- os_environ: dict[str, Any] = {}
21
-
22
- def __init__(self, env_file_path, os_environ, secrets):
23
- self._env_file_path = env_file_path
24
- self.os_environ = os_environ
25
- self.secrets = secrets
26
- self._resolved_values = {}
27
-
28
- def get(self, name, silent=False) -> Any:
29
- self._load_env_file()
30
- if name in self.os_environ:
31
- return self.resolve_value(self.os_environ[name])
32
- if name in self._env_file_config:
33
- return self.resolve_value(self._env_file_config[name])
34
-
35
- if not silent:
36
- raise KeyError(f"Could not find environment config '{name}' in environment or .env file")
37
- return None
38
-
39
- def _load_env_file(self):
40
- if self._env_file_config is not None:
41
- return
42
-
43
- self._env_file_config = {}
44
- if not os.path.isfile(self._env_file_path):
45
- return
46
-
47
- with open(self._env_file_path, "r") as env_file:
48
- line_number = 0
49
- for line in env_file.readlines():
50
- line_number += 1
51
- (key, value) = self._parse_env_line(line, line_number)
52
- if key is None:
53
- continue
54
-
55
- self._env_file_config[key] = value
56
-
57
- def _parse_env_line(self, line, line_number):
58
- line = line.strip()
59
- if not line:
60
- return (None, None)
61
- if line[0] == "#":
62
- return (None, None)
63
- if not "=" in line:
64
- raise ValueError(f"Parse error in environment line #{line_number}: should be 'key=value'")
65
-
66
- equal_index = line.index("=")
67
- key = line[:equal_index].strip()
68
- value = line[equal_index + 1 :].strip()
69
- lc_value = value.lower()
70
- if lc_value == "true":
71
- return (key, True)
72
- if lc_value == "false":
73
- return (key, False)
74
- if lc_value[0] == '"' and lc_value[-1] == '"':
75
- return (key, value.strip('"'))
76
- if lc_value[0] == "'" and lc_value[-1] == "'":
77
- return (key, value.strip("'"))
78
- try:
79
- as_int = int(value)
80
- return (key, as_int)
81
- except:
82
- pass
83
- try:
84
- as_float = float(value)
85
- return (key, as_float)
86
- except:
87
- pass
88
- return (key, value)
89
-
90
- def resolve_value(self, value):
91
- if type(value) != str or value[:9] != "secret://":
92
- return value
93
-
94
- secret_path = value[9:]
95
- if secret_path[0] != "/":
96
- secret_path = f"/{secret_path}"
97
- if secret_path not in self._resolved_values:
98
- if not self.secrets:
99
- raise ValueError(
100
- "References to the secret engine were found in the environment, "
101
- + "but a secret engine was not provided"
102
- )
103
- self._resolved_values[secret_path] = self.secrets.get(secret_path)
104
- return self._resolved_values[secret_path]
@@ -1,19 +0,0 @@
1
- from clearskies.exceptions.authentication import Authentication
2
- from clearskies.exceptions.authorization import Authorization
3
- from clearskies.exceptions.client_error import ClientError
4
- from clearskies.exceptions.input_errors import InputErrors
5
- from clearskies.exceptions.missing_dependency import MissingDependency
6
- from clearskies.exceptions.moved_permanently import MovedPermanently
7
- from clearskies.exceptions.moved_temporarily import MovedTemporarily
8
- from clearskies.exceptions.not_found import NotFound
9
-
10
- __all__ = [
11
- "Authentication",
12
- "Authorization",
13
- "ClientError",
14
- "InputErrors",
15
- "MissingDependency",
16
- "MovedPermanently",
17
- "MovedTemporarily",
18
- "NotFound",
19
- ]
@@ -1,2 +0,0 @@
1
- class Authentication(Exception):
2
- pass
@@ -1,2 +0,0 @@
1
- class Authorization(Exception):
2
- pass
@@ -1,2 +0,0 @@
1
- class ClientError(Exception):
2
- pass
@@ -1,4 +0,0 @@
1
- class InputErrors(Exception):
2
- def __init__(self, errors):
3
- super().__init__(self, "Input Error")
4
- self.errors = errors
@@ -1,2 +0,0 @@
1
- class MissingDependency(Exception):
2
- pass
@@ -1,3 +0,0 @@
1
- class MovedPermanently(Exception):
2
- def __init__(self, location):
3
- super().__init__(self, location)
@@ -1,3 +0,0 @@
1
- class MovedTemporarily(Exception):
2
- def __init__(self, location):
3
- super().__init__(self, location)
@@ -1,2 +0,0 @@
1
- class NotFound(Exception):
2
- pass
@@ -1,7 +0,0 @@
1
- from . import routing, string, validations
2
-
3
- __all__ = [
4
- "routing",
5
- "string",
6
- "validations",
7
- ]