clear-skies 2.0.5__py3-none-any.whl → 2.0.7__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.
- {clear_skies-2.0.5.dist-info → clear_skies-2.0.7.dist-info}/METADATA +1 -1
- clear_skies-2.0.7.dist-info/RECORD +251 -0
- clearskies/__init__.py +61 -0
- clearskies/action.py +7 -0
- clearskies/authentication/__init__.py +15 -0
- clearskies/authentication/authentication.py +46 -0
- clearskies/authentication/authorization.py +16 -0
- clearskies/authentication/authorization_pass_through.py +20 -0
- clearskies/authentication/jwks.py +163 -0
- clearskies/authentication/public.py +5 -0
- clearskies/authentication/secret_bearer.py +553 -0
- clearskies/autodoc/__init__.py +8 -0
- clearskies/autodoc/formats/__init__.py +5 -0
- clearskies/autodoc/formats/oai3_json/__init__.py +7 -0
- clearskies/autodoc/formats/oai3_json/oai3_json.py +87 -0
- clearskies/autodoc/formats/oai3_json/oai3_schema_resolver.py +15 -0
- clearskies/autodoc/formats/oai3_json/parameter.py +35 -0
- clearskies/autodoc/formats/oai3_json/request.py +68 -0
- clearskies/autodoc/formats/oai3_json/response.py +28 -0
- clearskies/autodoc/formats/oai3_json/schema/__init__.py +11 -0
- clearskies/autodoc/formats/oai3_json/schema/array.py +9 -0
- clearskies/autodoc/formats/oai3_json/schema/default.py +13 -0
- clearskies/autodoc/formats/oai3_json/schema/enum.py +7 -0
- clearskies/autodoc/formats/oai3_json/schema/object.py +35 -0
- clearskies/autodoc/formats/oai3_json/test.json +1985 -0
- clearskies/autodoc/py.typed +0 -0
- clearskies/autodoc/request/__init__.py +15 -0
- clearskies/autodoc/request/header.py +6 -0
- clearskies/autodoc/request/json_body.py +6 -0
- clearskies/autodoc/request/parameter.py +8 -0
- clearskies/autodoc/request/request.py +47 -0
- clearskies/autodoc/request/url_parameter.py +6 -0
- clearskies/autodoc/request/url_path.py +6 -0
- clearskies/autodoc/response/__init__.py +5 -0
- clearskies/autodoc/response/response.py +9 -0
- clearskies/autodoc/schema/__init__.py +31 -0
- clearskies/autodoc/schema/array.py +10 -0
- clearskies/autodoc/schema/base64.py +8 -0
- clearskies/autodoc/schema/boolean.py +5 -0
- clearskies/autodoc/schema/date.py +5 -0
- clearskies/autodoc/schema/datetime.py +5 -0
- clearskies/autodoc/schema/double.py +5 -0
- clearskies/autodoc/schema/enum.py +17 -0
- clearskies/autodoc/schema/integer.py +6 -0
- clearskies/autodoc/schema/long.py +5 -0
- clearskies/autodoc/schema/number.py +6 -0
- clearskies/autodoc/schema/object.py +13 -0
- clearskies/autodoc/schema/password.py +5 -0
- clearskies/autodoc/schema/schema.py +11 -0
- clearskies/autodoc/schema/string.py +5 -0
- clearskies/backends/__init__.py +65 -0
- clearskies/backends/api_backend.py +1178 -0
- clearskies/backends/backend.py +136 -0
- clearskies/backends/cursor_backend.py +335 -0
- clearskies/backends/memory_backend.py +797 -0
- clearskies/backends/secrets_backend.py +106 -0
- clearskies/column.py +1233 -0
- clearskies/columns/__init__.py +71 -0
- clearskies/columns/audit.py +206 -0
- clearskies/columns/belongs_to_id.py +483 -0
- clearskies/columns/belongs_to_model.py +132 -0
- clearskies/columns/belongs_to_self.py +105 -0
- clearskies/columns/boolean.py +113 -0
- clearskies/columns/category_tree.py +275 -0
- clearskies/columns/category_tree_ancestors.py +51 -0
- clearskies/columns/category_tree_children.py +127 -0
- clearskies/columns/category_tree_descendants.py +48 -0
- clearskies/columns/created.py +95 -0
- clearskies/columns/created_by_authorization_data.py +116 -0
- clearskies/columns/created_by_header.py +99 -0
- clearskies/columns/created_by_ip.py +92 -0
- clearskies/columns/created_by_routing_data.py +97 -0
- clearskies/columns/created_by_user_agent.py +92 -0
- clearskies/columns/date.py +234 -0
- clearskies/columns/datetime.py +282 -0
- clearskies/columns/email.py +76 -0
- clearskies/columns/float.py +153 -0
- clearskies/columns/has_many.py +505 -0
- clearskies/columns/has_many_self.py +56 -0
- clearskies/columns/has_one.py +14 -0
- clearskies/columns/integer.py +160 -0
- clearskies/columns/json.py +128 -0
- clearskies/columns/many_to_many_ids.py +337 -0
- clearskies/columns/many_to_many_ids_with_data.py +274 -0
- clearskies/columns/many_to_many_models.py +158 -0
- clearskies/columns/many_to_many_pivots.py +134 -0
- clearskies/columns/phone.py +159 -0
- clearskies/columns/select.py +92 -0
- clearskies/columns/string.py +102 -0
- clearskies/columns/timestamp.py +164 -0
- clearskies/columns/updated.py +110 -0
- clearskies/columns/uuid.py +86 -0
- clearskies/configs/README.md +105 -0
- clearskies/configs/__init__.py +162 -0
- clearskies/configs/actions.py +43 -0
- clearskies/configs/any.py +13 -0
- clearskies/configs/any_dict.py +22 -0
- clearskies/configs/any_dict_or_callable.py +23 -0
- clearskies/configs/authentication.py +23 -0
- clearskies/configs/authorization.py +23 -0
- clearskies/configs/boolean.py +16 -0
- clearskies/configs/boolean_or_callable.py +18 -0
- clearskies/configs/callable_config.py +18 -0
- clearskies/configs/columns.py +34 -0
- clearskies/configs/conditions.py +30 -0
- clearskies/configs/config.py +24 -0
- clearskies/configs/datetime.py +18 -0
- clearskies/configs/datetime_or_callable.py +19 -0
- clearskies/configs/endpoint.py +23 -0
- clearskies/configs/endpoint_list.py +29 -0
- clearskies/configs/float.py +16 -0
- clearskies/configs/float_or_callable.py +18 -0
- clearskies/configs/integer.py +16 -0
- clearskies/configs/integer_or_callable.py +18 -0
- clearskies/configs/joins.py +30 -0
- clearskies/configs/list_any_dict.py +30 -0
- clearskies/configs/list_any_dict_or_callable.py +31 -0
- clearskies/configs/model_class.py +35 -0
- clearskies/configs/model_column.py +65 -0
- clearskies/configs/model_columns.py +56 -0
- clearskies/configs/model_destination_name.py +25 -0
- clearskies/configs/model_to_id_column.py +43 -0
- clearskies/configs/readable_model_column.py +9 -0
- clearskies/configs/readable_model_columns.py +9 -0
- clearskies/configs/schema.py +23 -0
- clearskies/configs/searchable_model_columns.py +9 -0
- clearskies/configs/security_headers.py +39 -0
- clearskies/configs/select.py +26 -0
- clearskies/configs/select_list.py +47 -0
- clearskies/configs/string.py +29 -0
- clearskies/configs/string_dict.py +32 -0
- clearskies/configs/string_list.py +32 -0
- clearskies/configs/string_list_or_callable.py +35 -0
- clearskies/configs/string_or_callable.py +18 -0
- clearskies/configs/timedelta.py +18 -0
- clearskies/configs/timezone.py +18 -0
- clearskies/configs/url.py +23 -0
- clearskies/configs/validators.py +45 -0
- clearskies/configs/writeable_model_column.py +9 -0
- clearskies/configs/writeable_model_columns.py +9 -0
- clearskies/configurable.py +76 -0
- clearskies/contexts/__init__.py +11 -0
- clearskies/contexts/cli.py +117 -0
- clearskies/contexts/context.py +98 -0
- clearskies/contexts/wsgi.py +76 -0
- clearskies/contexts/wsgi_ref.py +82 -0
- clearskies/decorators.py +33 -0
- clearskies/di/__init__.py +15 -0
- clearskies/di/additional_config.py +130 -0
- clearskies/di/additional_config_auto_import.py +17 -0
- clearskies/di/di.py +973 -0
- clearskies/di/inject/__init__.py +23 -0
- clearskies/di/inject/by_class.py +21 -0
- clearskies/di/inject/by_name.py +18 -0
- clearskies/di/inject/di.py +13 -0
- clearskies/di/inject/environment.py +14 -0
- clearskies/di/inject/input_output.py +20 -0
- clearskies/di/inject/now.py +13 -0
- clearskies/di/inject/requests.py +13 -0
- clearskies/di/inject/secrets.py +14 -0
- clearskies/di/inject/utcnow.py +13 -0
- clearskies/di/inject/uuid.py +15 -0
- clearskies/di/injectable.py +29 -0
- clearskies/di/injectable_properties.py +131 -0
- clearskies/di/test_module/__init__.py +6 -0
- clearskies/di/test_module/another_module/__init__.py +2 -0
- clearskies/di/test_module/module_class.py +5 -0
- clearskies/end.py +183 -0
- clearskies/endpoint.py +1314 -0
- clearskies/endpoint_group.py +336 -0
- clearskies/endpoints/__init__.py +25 -0
- clearskies/endpoints/advanced_search.py +526 -0
- clearskies/endpoints/callable.py +388 -0
- clearskies/endpoints/create.py +205 -0
- clearskies/endpoints/delete.py +139 -0
- clearskies/endpoints/get.py +271 -0
- clearskies/endpoints/health_check.py +183 -0
- clearskies/endpoints/list.py +574 -0
- clearskies/endpoints/restful_api.py +427 -0
- clearskies/endpoints/schema.py +189 -0
- clearskies/endpoints/simple_search.py +286 -0
- clearskies/endpoints/update.py +193 -0
- clearskies/environment.py +104 -0
- clearskies/exceptions/__init__.py +19 -0
- clearskies/exceptions/authentication.py +2 -0
- clearskies/exceptions/authorization.py +2 -0
- clearskies/exceptions/client_error.py +2 -0
- clearskies/exceptions/input_errors.py +4 -0
- clearskies/exceptions/missing_dependency.py +2 -0
- clearskies/exceptions/moved_permanently.py +3 -0
- clearskies/exceptions/moved_temporarily.py +3 -0
- clearskies/exceptions/not_found.py +2 -0
- clearskies/functional/__init__.py +7 -0
- clearskies/functional/routing.py +92 -0
- clearskies/functional/string.py +112 -0
- clearskies/functional/validations.py +76 -0
- clearskies/input_outputs/__init__.py +13 -0
- clearskies/input_outputs/cli.py +171 -0
- clearskies/input_outputs/exceptions/__init__.py +2 -0
- clearskies/input_outputs/exceptions/cli_input_error.py +2 -0
- clearskies/input_outputs/exceptions/cli_not_found.py +2 -0
- clearskies/input_outputs/headers.py +45 -0
- clearskies/input_outputs/input_output.py +138 -0
- clearskies/input_outputs/programmatic.py +69 -0
- clearskies/input_outputs/py.typed +0 -0
- clearskies/input_outputs/wsgi.py +77 -0
- clearskies/model.py +1922 -0
- clearskies/py.typed +0 -0
- clearskies/query/__init__.py +12 -0
- clearskies/query/condition.py +223 -0
- clearskies/query/join.py +136 -0
- clearskies/query/query.py +196 -0
- clearskies/query/sort.py +27 -0
- clearskies/schema.py +82 -0
- clearskies/secrets/__init__.py +6 -0
- clearskies/secrets/additional_configs/__init__.py +32 -0
- clearskies/secrets/additional_configs/mysql_connection_dynamic_producer.py +61 -0
- clearskies/secrets/additional_configs/mysql_connection_dynamic_producer_via_ssh_cert_bastion.py +160 -0
- clearskies/secrets/akeyless.py +182 -0
- clearskies/secrets/exceptions/__init__.py +1 -0
- clearskies/secrets/exceptions/not_found.py +2 -0
- clearskies/secrets/secrets.py +38 -0
- clearskies/security_header.py +15 -0
- clearskies/security_headers/__init__.py +11 -0
- clearskies/security_headers/cache_control.py +67 -0
- clearskies/security_headers/cors.py +50 -0
- clearskies/security_headers/csp.py +94 -0
- clearskies/security_headers/hsts.py +22 -0
- clearskies/security_headers/x_content_type_options.py +0 -0
- clearskies/security_headers/x_frame_options.py +0 -0
- clearskies/test_base.py +8 -0
- clearskies/typing.py +11 -0
- clearskies/validator.py +37 -0
- clearskies/validators/__init__.py +33 -0
- clearskies/validators/after_column.py +62 -0
- clearskies/validators/before_column.py +13 -0
- clearskies/validators/in_the_future.py +32 -0
- clearskies/validators/in_the_future_at_least.py +11 -0
- clearskies/validators/in_the_future_at_most.py +10 -0
- clearskies/validators/in_the_past.py +32 -0
- clearskies/validators/in_the_past_at_least.py +10 -0
- clearskies/validators/in_the_past_at_most.py +10 -0
- clearskies/validators/maximum_length.py +26 -0
- clearskies/validators/maximum_value.py +29 -0
- clearskies/validators/minimum_length.py +26 -0
- clearskies/validators/minimum_value.py +29 -0
- clearskies/validators/required.py +34 -0
- clearskies/validators/timedelta.py +59 -0
- clearskies/validators/unique.py +30 -0
- clear_skies-2.0.5.dist-info/RECORD +0 -4
- {clear_skies-2.0.5.dist-info → clear_skies-2.0.7.dist-info}/WHEEL +0 -0
- {clear_skies-2.0.5.dist-info → clear_skies-2.0.7.dist-info}/licenses/LICENSE +0 -0
|
@@ -0,0 +1,286 @@
|
|
|
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
|
|
@@ -0,0 +1,193 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
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]
|
|
@@ -0,0 +1,19 @@
|
|
|
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
|
+
]
|