clear-skies 1.19.22__py3-none-any.whl → 2.0.23__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.
Files changed (362) hide show
  1. clear_skies-2.0.23.dist-info/METADATA +76 -0
  2. clear_skies-2.0.23.dist-info/RECORD +265 -0
  3. {clear_skies-1.19.22.dist-info → clear_skies-2.0.23.dist-info}/WHEEL +1 -1
  4. clearskies/__init__.py +37 -21
  5. clearskies/action.py +7 -0
  6. clearskies/authentication/__init__.py +9 -38
  7. clearskies/authentication/authentication.py +44 -0
  8. clearskies/authentication/authorization.py +14 -8
  9. clearskies/authentication/authorization_pass_through.py +22 -0
  10. clearskies/authentication/jwks.py +135 -58
  11. clearskies/authentication/public.py +3 -26
  12. clearskies/authentication/secret_bearer.py +515 -44
  13. clearskies/autodoc/formats/oai3_json/__init__.py +2 -2
  14. clearskies/autodoc/formats/oai3_json/oai3_json.py +11 -9
  15. clearskies/autodoc/formats/oai3_json/parameter.py +6 -3
  16. clearskies/autodoc/formats/oai3_json/request.py +7 -5
  17. clearskies/autodoc/formats/oai3_json/response.py +7 -4
  18. clearskies/autodoc/formats/oai3_json/schema/object.py +10 -1
  19. clearskies/autodoc/request/__init__.py +2 -0
  20. clearskies/autodoc/request/header.py +4 -6
  21. clearskies/autodoc/request/json_body.py +4 -6
  22. clearskies/autodoc/request/parameter.py +8 -0
  23. clearskies/autodoc/request/request.py +16 -4
  24. clearskies/autodoc/request/url_parameter.py +4 -6
  25. clearskies/autodoc/request/url_path.py +4 -6
  26. clearskies/autodoc/schema/__init__.py +4 -2
  27. clearskies/autodoc/schema/array.py +5 -6
  28. clearskies/autodoc/schema/boolean.py +4 -10
  29. clearskies/autodoc/schema/date.py +0 -3
  30. clearskies/autodoc/schema/datetime.py +1 -4
  31. clearskies/autodoc/schema/double.py +0 -3
  32. clearskies/autodoc/schema/enum.py +4 -2
  33. clearskies/autodoc/schema/integer.py +4 -9
  34. clearskies/autodoc/schema/long.py +0 -3
  35. clearskies/autodoc/schema/number.py +4 -9
  36. clearskies/autodoc/schema/object.py +5 -7
  37. clearskies/autodoc/schema/password.py +0 -3
  38. clearskies/autodoc/schema/schema.py +11 -0
  39. clearskies/autodoc/schema/string.py +4 -10
  40. clearskies/backends/__init__.py +56 -17
  41. clearskies/backends/api_backend.py +1128 -166
  42. clearskies/backends/backend.py +54 -85
  43. clearskies/backends/cursor_backend.py +246 -191
  44. clearskies/backends/memory_backend.py +514 -208
  45. clearskies/backends/secrets_backend.py +68 -31
  46. clearskies/column.py +1221 -0
  47. clearskies/columns/__init__.py +71 -0
  48. clearskies/columns/audit.py +306 -0
  49. clearskies/columns/belongs_to_id.py +478 -0
  50. clearskies/columns/belongs_to_model.py +129 -0
  51. clearskies/columns/belongs_to_self.py +109 -0
  52. clearskies/columns/boolean.py +110 -0
  53. clearskies/columns/category_tree.py +273 -0
  54. clearskies/columns/category_tree_ancestors.py +51 -0
  55. clearskies/columns/category_tree_children.py +126 -0
  56. clearskies/columns/category_tree_descendants.py +48 -0
  57. clearskies/columns/created.py +92 -0
  58. clearskies/columns/created_by_authorization_data.py +114 -0
  59. clearskies/columns/created_by_header.py +103 -0
  60. clearskies/columns/created_by_ip.py +90 -0
  61. clearskies/columns/created_by_routing_data.py +102 -0
  62. clearskies/columns/created_by_user_agent.py +89 -0
  63. clearskies/columns/date.py +232 -0
  64. clearskies/columns/datetime.py +284 -0
  65. clearskies/columns/email.py +78 -0
  66. clearskies/columns/float.py +149 -0
  67. clearskies/columns/has_many.py +529 -0
  68. clearskies/columns/has_many_self.py +62 -0
  69. clearskies/columns/has_one.py +21 -0
  70. clearskies/columns/integer.py +158 -0
  71. clearskies/columns/json.py +126 -0
  72. clearskies/columns/many_to_many_ids.py +335 -0
  73. clearskies/columns/many_to_many_ids_with_data.py +274 -0
  74. clearskies/columns/many_to_many_models.py +156 -0
  75. clearskies/columns/many_to_many_pivots.py +132 -0
  76. clearskies/columns/phone.py +162 -0
  77. clearskies/columns/select.py +95 -0
  78. clearskies/columns/string.py +102 -0
  79. clearskies/columns/timestamp.py +164 -0
  80. clearskies/columns/updated.py +107 -0
  81. clearskies/columns/uuid.py +83 -0
  82. clearskies/configs/README.md +105 -0
  83. clearskies/configs/__init__.py +170 -0
  84. clearskies/configs/actions.py +43 -0
  85. clearskies/configs/any.py +15 -0
  86. clearskies/configs/any_dict.py +24 -0
  87. clearskies/configs/any_dict_or_callable.py +25 -0
  88. clearskies/configs/authentication.py +23 -0
  89. clearskies/configs/authorization.py +23 -0
  90. clearskies/configs/boolean.py +18 -0
  91. clearskies/configs/boolean_or_callable.py +20 -0
  92. clearskies/configs/callable_config.py +20 -0
  93. clearskies/configs/columns.py +34 -0
  94. clearskies/configs/conditions.py +30 -0
  95. clearskies/configs/config.py +26 -0
  96. clearskies/configs/datetime.py +20 -0
  97. clearskies/configs/datetime_or_callable.py +21 -0
  98. clearskies/configs/email.py +10 -0
  99. clearskies/configs/email_list.py +17 -0
  100. clearskies/configs/email_list_or_callable.py +17 -0
  101. clearskies/configs/email_or_email_list_or_callable.py +59 -0
  102. clearskies/configs/endpoint.py +23 -0
  103. clearskies/configs/endpoint_list.py +29 -0
  104. clearskies/configs/float.py +18 -0
  105. clearskies/configs/float_or_callable.py +20 -0
  106. clearskies/configs/headers.py +28 -0
  107. clearskies/configs/integer.py +18 -0
  108. clearskies/configs/integer_or_callable.py +20 -0
  109. clearskies/configs/joins.py +30 -0
  110. clearskies/configs/list_any_dict.py +32 -0
  111. clearskies/configs/list_any_dict_or_callable.py +33 -0
  112. clearskies/configs/model_class.py +35 -0
  113. clearskies/configs/model_column.py +67 -0
  114. clearskies/configs/model_columns.py +58 -0
  115. clearskies/configs/model_destination_name.py +26 -0
  116. clearskies/configs/model_to_id_column.py +45 -0
  117. clearskies/configs/readable_model_column.py +11 -0
  118. clearskies/configs/readable_model_columns.py +11 -0
  119. clearskies/configs/schema.py +23 -0
  120. clearskies/configs/searchable_model_columns.py +11 -0
  121. clearskies/configs/security_headers.py +39 -0
  122. clearskies/configs/select.py +28 -0
  123. clearskies/configs/select_list.py +49 -0
  124. clearskies/configs/string.py +31 -0
  125. clearskies/configs/string_dict.py +34 -0
  126. clearskies/configs/string_list.py +47 -0
  127. clearskies/configs/string_list_or_callable.py +48 -0
  128. clearskies/configs/string_or_callable.py +18 -0
  129. clearskies/configs/timedelta.py +20 -0
  130. clearskies/configs/timezone.py +20 -0
  131. clearskies/configs/url.py +25 -0
  132. clearskies/configs/validators.py +45 -0
  133. clearskies/configs/writeable_model_column.py +11 -0
  134. clearskies/configs/writeable_model_columns.py +11 -0
  135. clearskies/configurable.py +78 -0
  136. clearskies/contexts/__init__.py +8 -8
  137. clearskies/contexts/cli.py +129 -43
  138. clearskies/contexts/context.py +93 -56
  139. clearskies/contexts/wsgi.py +79 -33
  140. clearskies/contexts/wsgi_ref.py +87 -0
  141. clearskies/cursors/__init__.py +7 -0
  142. clearskies/cursors/cursor.py +166 -0
  143. clearskies/cursors/from_environment/__init__.py +5 -0
  144. clearskies/cursors/from_environment/mysql.py +51 -0
  145. clearskies/cursors/from_environment/postgresql.py +49 -0
  146. clearskies/cursors/from_environment/sqlite.py +35 -0
  147. clearskies/cursors/mysql.py +61 -0
  148. clearskies/cursors/postgresql.py +61 -0
  149. clearskies/cursors/sqlite.py +62 -0
  150. clearskies/decorators.py +33 -0
  151. clearskies/decorators.pyi +10 -0
  152. clearskies/di/__init__.py +11 -7
  153. clearskies/di/additional_config.py +117 -3
  154. clearskies/di/additional_config_auto_import.py +12 -0
  155. clearskies/di/di.py +717 -126
  156. clearskies/di/inject/__init__.py +23 -0
  157. clearskies/di/inject/akeyless_sdk.py +16 -0
  158. clearskies/di/inject/by_class.py +24 -0
  159. clearskies/di/inject/by_name.py +22 -0
  160. clearskies/di/inject/di.py +16 -0
  161. clearskies/di/inject/environment.py +15 -0
  162. clearskies/di/inject/input_output.py +19 -0
  163. clearskies/di/inject/now.py +16 -0
  164. clearskies/di/inject/requests.py +16 -0
  165. clearskies/di/inject/secrets.py +15 -0
  166. clearskies/di/inject/utcnow.py +16 -0
  167. clearskies/di/inject/uuid.py +16 -0
  168. clearskies/di/injectable.py +32 -0
  169. clearskies/di/injectable_properties.py +131 -0
  170. clearskies/end.py +219 -0
  171. clearskies/endpoint.py +1303 -0
  172. clearskies/endpoint_group.py +333 -0
  173. clearskies/endpoints/__init__.py +25 -0
  174. clearskies/endpoints/advanced_search.py +519 -0
  175. clearskies/endpoints/callable.py +382 -0
  176. clearskies/endpoints/create.py +201 -0
  177. clearskies/endpoints/delete.py +133 -0
  178. clearskies/endpoints/get.py +267 -0
  179. clearskies/endpoints/health_check.py +181 -0
  180. clearskies/endpoints/list.py +567 -0
  181. clearskies/endpoints/restful_api.py +417 -0
  182. clearskies/endpoints/schema.py +185 -0
  183. clearskies/endpoints/simple_search.py +279 -0
  184. clearskies/endpoints/update.py +188 -0
  185. clearskies/environment.py +7 -3
  186. clearskies/exceptions/__init__.py +19 -0
  187. clearskies/{handlers/exceptions/input_error.py → exceptions/input_errors.py} +1 -1
  188. clearskies/exceptions/missing_dependency.py +2 -0
  189. clearskies/exceptions/moved_permanently.py +3 -0
  190. clearskies/exceptions/moved_temporarily.py +3 -0
  191. clearskies/functional/__init__.py +2 -2
  192. clearskies/functional/json.py +47 -0
  193. clearskies/functional/routing.py +92 -0
  194. clearskies/functional/string.py +19 -11
  195. clearskies/functional/validations.py +61 -9
  196. clearskies/input_outputs/__init__.py +9 -7
  197. clearskies/input_outputs/cli.py +135 -152
  198. clearskies/input_outputs/exceptions/__init__.py +6 -1
  199. clearskies/input_outputs/headers.py +54 -0
  200. clearskies/input_outputs/input_output.py +77 -123
  201. clearskies/input_outputs/programmatic.py +62 -0
  202. clearskies/input_outputs/wsgi.py +36 -48
  203. clearskies/model.py +1894 -199
  204. clearskies/query/__init__.py +12 -0
  205. clearskies/query/condition.py +228 -0
  206. clearskies/query/join.py +136 -0
  207. clearskies/query/query.py +193 -0
  208. clearskies/query/sort.py +27 -0
  209. clearskies/schema.py +82 -0
  210. clearskies/secrets/__init__.py +4 -31
  211. clearskies/secrets/additional_configs/mysql_connection_dynamic_producer.py +15 -4
  212. clearskies/secrets/additional_configs/mysql_connection_dynamic_producer_via_ssh_cert_bastion.py +11 -5
  213. clearskies/secrets/akeyless.py +421 -155
  214. clearskies/secrets/exceptions/__init__.py +7 -1
  215. clearskies/secrets/exceptions/not_found_error.py +2 -0
  216. clearskies/secrets/exceptions/permissions_error.py +2 -0
  217. clearskies/secrets/secrets.py +12 -11
  218. clearskies/security_header.py +17 -0
  219. clearskies/security_headers/__init__.py +8 -8
  220. clearskies/security_headers/cache_control.py +47 -109
  221. clearskies/security_headers/cors.py +38 -92
  222. clearskies/security_headers/csp.py +76 -150
  223. clearskies/security_headers/hsts.py +14 -15
  224. clearskies/typing.py +11 -0
  225. clearskies/validator.py +36 -0
  226. clearskies/validators/__init__.py +33 -0
  227. clearskies/validators/after_column.py +61 -0
  228. clearskies/validators/before_column.py +15 -0
  229. clearskies/validators/in_the_future.py +29 -0
  230. clearskies/validators/in_the_future_at_least.py +13 -0
  231. clearskies/validators/in_the_future_at_most.py +12 -0
  232. clearskies/validators/in_the_past.py +29 -0
  233. clearskies/validators/in_the_past_at_least.py +12 -0
  234. clearskies/validators/in_the_past_at_most.py +12 -0
  235. clearskies/validators/maximum_length.py +25 -0
  236. clearskies/validators/maximum_value.py +28 -0
  237. clearskies/validators/minimum_length.py +25 -0
  238. clearskies/validators/minimum_value.py +28 -0
  239. clearskies/{input_requirements → validators}/required.py +18 -9
  240. clearskies/validators/timedelta.py +58 -0
  241. clearskies/validators/unique.py +28 -0
  242. clear_skies-1.19.22.dist-info/METADATA +0 -46
  243. clear_skies-1.19.22.dist-info/RECORD +0 -206
  244. clearskies/application.py +0 -29
  245. clearskies/authentication/auth0_jwks.py +0 -118
  246. clearskies/authentication/auth_exception.py +0 -2
  247. clearskies/authentication/jwks_jwcrypto.py +0 -39
  248. clearskies/backends/example_backend.py +0 -43
  249. clearskies/backends/file_backend.py +0 -48
  250. clearskies/backends/json_backend.py +0 -7
  251. clearskies/backends/restful_api_advanced_search_backend.py +0 -138
  252. clearskies/binding_config.py +0 -16
  253. clearskies/column_types/__init__.py +0 -184
  254. clearskies/column_types/audit.py +0 -235
  255. clearskies/column_types/belongs_to.py +0 -250
  256. clearskies/column_types/boolean.py +0 -60
  257. clearskies/column_types/category_tree.py +0 -226
  258. clearskies/column_types/column.py +0 -373
  259. clearskies/column_types/created.py +0 -26
  260. clearskies/column_types/created_by_authorization_data.py +0 -26
  261. clearskies/column_types/created_by_header.py +0 -24
  262. clearskies/column_types/created_by_ip.py +0 -17
  263. clearskies/column_types/created_by_routing_data.py +0 -25
  264. clearskies/column_types/created_by_user_agent.py +0 -17
  265. clearskies/column_types/created_micro.py +0 -26
  266. clearskies/column_types/datetime.py +0 -108
  267. clearskies/column_types/datetime_micro.py +0 -12
  268. clearskies/column_types/email.py +0 -18
  269. clearskies/column_types/float.py +0 -43
  270. clearskies/column_types/has_many.py +0 -139
  271. clearskies/column_types/integer.py +0 -41
  272. clearskies/column_types/json.py +0 -25
  273. clearskies/column_types/many_to_many.py +0 -278
  274. clearskies/column_types/many_to_many_with_data.py +0 -162
  275. clearskies/column_types/select.py +0 -11
  276. clearskies/column_types/string.py +0 -24
  277. clearskies/column_types/updated.py +0 -24
  278. clearskies/column_types/updated_micro.py +0 -24
  279. clearskies/column_types/uuid.py +0 -25
  280. clearskies/columns.py +0 -123
  281. clearskies/condition_parser.py +0 -172
  282. clearskies/contexts/build_context.py +0 -54
  283. clearskies/contexts/convert_to_application.py +0 -190
  284. clearskies/contexts/extract_handler.py +0 -37
  285. clearskies/contexts/test.py +0 -94
  286. clearskies/decorators/__init__.py +0 -39
  287. clearskies/decorators/auth0_jwks.py +0 -22
  288. clearskies/decorators/authorization.py +0 -10
  289. clearskies/decorators/binding_classes.py +0 -9
  290. clearskies/decorators/binding_modules.py +0 -9
  291. clearskies/decorators/bindings.py +0 -9
  292. clearskies/decorators/create.py +0 -10
  293. clearskies/decorators/delete.py +0 -10
  294. clearskies/decorators/docs.py +0 -14
  295. clearskies/decorators/get.py +0 -10
  296. clearskies/decorators/jwks.py +0 -26
  297. clearskies/decorators/merge.py +0 -124
  298. clearskies/decorators/patch.py +0 -10
  299. clearskies/decorators/post.py +0 -10
  300. clearskies/decorators/public.py +0 -11
  301. clearskies/decorators/response_headers.py +0 -10
  302. clearskies/decorators/return_raw_response.py +0 -9
  303. clearskies/decorators/schema.py +0 -10
  304. clearskies/decorators/secret_bearer.py +0 -24
  305. clearskies/decorators/security_headers.py +0 -10
  306. clearskies/di/standard_dependencies.py +0 -140
  307. clearskies/di/test_module/__init__.py +0 -6
  308. clearskies/di/test_module/another_module/__init__.py +0 -2
  309. clearskies/di/test_module/module_class.py +0 -5
  310. clearskies/handlers/__init__.py +0 -41
  311. clearskies/handlers/advanced_search.py +0 -271
  312. clearskies/handlers/base.py +0 -473
  313. clearskies/handlers/callable.py +0 -189
  314. clearskies/handlers/create.py +0 -35
  315. clearskies/handlers/crud_by_method.py +0 -18
  316. clearskies/handlers/database_connector.py +0 -32
  317. clearskies/handlers/delete.py +0 -61
  318. clearskies/handlers/exceptions/__init__.py +0 -5
  319. clearskies/handlers/exceptions/not_found.py +0 -3
  320. clearskies/handlers/get.py +0 -156
  321. clearskies/handlers/health_check.py +0 -59
  322. clearskies/handlers/input_processing.py +0 -79
  323. clearskies/handlers/list.py +0 -530
  324. clearskies/handlers/mygrations.py +0 -82
  325. clearskies/handlers/request_method_routing.py +0 -47
  326. clearskies/handlers/restful_api.py +0 -218
  327. clearskies/handlers/routing.py +0 -62
  328. clearskies/handlers/schema_helper.py +0 -128
  329. clearskies/handlers/simple_routing.py +0 -204
  330. clearskies/handlers/simple_routing_route.py +0 -192
  331. clearskies/handlers/simple_search.py +0 -136
  332. clearskies/handlers/update.py +0 -96
  333. clearskies/handlers/write.py +0 -193
  334. clearskies/input_requirements/__init__.py +0 -68
  335. clearskies/input_requirements/after.py +0 -36
  336. clearskies/input_requirements/before.py +0 -36
  337. clearskies/input_requirements/in_the_future_at_least.py +0 -19
  338. clearskies/input_requirements/in_the_future_at_most.py +0 -19
  339. clearskies/input_requirements/in_the_past_at_least.py +0 -19
  340. clearskies/input_requirements/in_the_past_at_most.py +0 -19
  341. clearskies/input_requirements/maximum_length.py +0 -19
  342. clearskies/input_requirements/minimum_length.py +0 -22
  343. clearskies/input_requirements/requirement.py +0 -25
  344. clearskies/input_requirements/time_delta.py +0 -38
  345. clearskies/input_requirements/unique.py +0 -18
  346. clearskies/mocks/__init__.py +0 -7
  347. clearskies/mocks/input_output.py +0 -124
  348. clearskies/mocks/models.py +0 -142
  349. clearskies/models.py +0 -345
  350. clearskies/security_headers/base.py +0 -12
  351. clearskies/tests/simple_api/models/__init__.py +0 -2
  352. clearskies/tests/simple_api/models/status.py +0 -23
  353. clearskies/tests/simple_api/models/user.py +0 -21
  354. clearskies/tests/simple_api/users_api.py +0 -64
  355. {clear_skies-1.19.22.dist-info → clear_skies-2.0.23.dist-info/licenses}/LICENSE +0 -0
  356. /clearskies/{contexts/bash.py → autodoc/py.typed} +0 -0
  357. /clearskies/{handlers/exceptions → exceptions}/authentication.py +0 -0
  358. /clearskies/{handlers/exceptions → exceptions}/authorization.py +0 -0
  359. /clearskies/{handlers/exceptions → exceptions}/client_error.py +0 -0
  360. /clearskies/{secrets/exceptions → exceptions}/not_found.py +0 -0
  361. /clearskies/{tests/__init__.py → input_outputs/py.typed} +0 -0
  362. /clearskies/{tests/simple_api/__init__.py → py.typed} +0 -0
@@ -1,222 +1,1184 @@
1
- from .backend import Backend
2
- from typing import Any, Callable, Dict, List, Tuple
3
- from ..autodoc.schema import Integer as AutoDocInteger
4
- from .. import model
5
- from ..column_types import JSON, DateTime
6
-
7
-
8
- class ApiBackend(Backend):
9
- url = None
10
- _requests = None
11
- _auth = None
12
- _records = None
13
-
14
- _allowed_configs = [
15
- "select_all",
16
- "wheres",
17
- "sorts",
18
- "limit",
19
- "pagination",
20
- "table_name",
21
- "model_columns",
22
- ]
23
-
24
- _empty_configs = [
25
- "group_by_column",
26
- "selects",
27
- "joins",
28
- ]
29
-
30
- def __init__(self, requests):
31
- self._requests = requests
32
-
33
- def configure(self, url=None, auth=None):
34
- self.url = url
35
- self._auth = auth
36
-
37
- def update(self, id, data, model):
38
- [url, method, json_data, headers] = self._build_update_request(id, data, model)
39
- response = self._execute_request(url, method, json=json_data, headers=headers)
40
- if not response.content:
41
- return {**model.data, **data}
42
- return self._map_update_response(response.json())
43
-
44
- def _build_update_request(self, id, data, model):
45
- return [self.url, "PATCH", data, {}]
46
-
47
- def _map_update_response(self, json):
48
- if not "data" in json:
49
- raise ValueError("Unexpected API response to update request")
50
- return json["data"]
51
-
52
- def create(self, data, model):
53
- [url, method, json_data, headers] = self._build_create_request(data, model)
54
- response = self._execute_request(url, method, json=json_data, headers=headers)
55
- return self._map_create_response(response.json())
56
-
57
- def _build_create_request(self, data, model):
58
- return [self.url, "POST", data, {}]
59
-
60
- def _map_create_response(self, json):
61
- if not "data" in json:
62
- raise ValueError("Unexpected API response to create request")
63
- return json["data"]
64
-
65
- def delete(self, id, model):
66
- [url, method, json_data, headers] = self._build_delete_request(id, model)
67
- response = self._execute_request(url, method, json=json_data, headers=headers)
68
- return self._validate_delete_response(response.json())
69
-
70
- def _build_delete_request(self, id, model):
71
- return [self.url, "DELETE", {model.id_column_name: id}, {}]
72
-
73
- def _validate_delete_response(self, json):
74
- if "status" not in json:
75
- raise ValueError("Unexpected response to delete API request")
76
- return json["status"] == "success"
77
-
78
- def count(self, configuration, model):
79
- configuration = self._check_query_configuration(configuration)
80
- [url, method, json_data, headers] = self._build_count_request(configuration)
81
- response = self._execute_request(url, method, json=json_data, headers=headers)
82
- return self._map_count_response(response.json())
83
-
84
- def _build_count_request(self, configuration):
85
- return [self.url, "GET", {**{"count_only": True}, **self._as_post_data(configuration)}, {}]
86
-
87
- def _map_count_response(self, json):
88
- if not "total_matches" in json:
89
- raise ValueError("Unexpected API response when executing count request")
90
- return json["total_matches"]
91
-
92
- def records(self, configuration, model, next_page_data=None):
93
- configuration = self._check_query_configuration(configuration)
94
- [url, method, json_data, headers] = self._build_records_request(configuration)
95
- response = self._execute_request(url, method, json=json_data, headers=headers)
96
- records = self._map_records_response(response.json())
97
- if type(next_page_data) == dict:
98
- limit = configuration.get("limit", None)
99
- start = configuration.get("pagination", {}).get("start", 0)
100
- if limit and len(records) == limit:
101
- next_page_data["start"] = start + limit
1
+ from __future__ import annotations
2
+
3
+ import urllib.parse
4
+ from typing import TYPE_CHECKING, Any, Callable
5
+
6
+ import requests
7
+
8
+ from clearskies import columns, configs, configurable, decorators
9
+ from clearskies.autodoc.schema import Integer as AutoDocInteger
10
+ from clearskies.autodoc.schema import Schema as AutoDocSchema
11
+ from clearskies.autodoc.schema import String as AutoDocString
12
+ from clearskies.backends.backend import Backend
13
+ from clearskies.di import InjectableProperties, inject
14
+ from clearskies.functional import json as json_functional
15
+ from clearskies.functional import routing, string
16
+
17
+ if TYPE_CHECKING:
18
+ from clearskies import Column, Model
19
+ from clearskies.authentication import Authentication
20
+ from clearskies.query import Query
21
+
22
+
23
+ class ApiBackend(configurable.Configurable, Backend, InjectableProperties):
24
+ """
25
+ Fetch and store data from an API endpoint.
26
+
27
+ The ApiBackend gives developers a way to quickly build SDKs to connect a clearskies applications
28
+ to arbitrary API endpoints. The backend has some built in flexibility to make it easy to connect it to
29
+ **most** APIs, as well as behavioral hooks so that you can override small sections of the logic to accommodate
30
+ APIs that don't work in the expected way. This allows you to interact with APIs using the standard model
31
+ methods, just like every other backend, and also means that you can attach such models to endpoints to
32
+ quickly enable all kinds of pre-defined behaviors.
33
+
34
+ ## Usage
35
+
36
+ Configuring the API backend is pretty easy:
37
+
38
+ 1. Provide the `base_url` to the constructor, or extend it and set it in the `__init__` for the new backend.
39
+ 2. Provide a `clearskies.authentication.Authentication` object, assuming it isn't a public API.
40
+ 3. Match your model class name to the path of the API (or set `model.destination_name()` appropriately)
41
+ 4. Use the resulting model like you would any other model!
42
+
43
+ It's important to understand how the Api Backend will map queries and saves to the API in question. The rules
44
+ are fairly simple:
45
+
46
+ 1. The API backend only supports searching with the equals operator (e.g. `models.where("column=value")`).
47
+ 2. To specify routing parameters, use the `{parameter_name}` or `:parameter_name` syntax in either the url
48
+ or in the destination name of your model. In order to query the model, you then **must** provide a value
49
+ for any routing parameters, using a matching search condition: (e.g.
50
+ `models.where("routing_parameter_name=value")`)
51
+ 3. Any search clauses that don't correspond to routing parameters will be translated into query parameters.
52
+ So, if your destination_name is `https://example.com/:categoy_id/products` and you executed a
53
+ model query: `models.where("category_id=10").where("on_sale=1")` then this would result in fetching
54
+ a URL of `https://example.com/10/products?on_sale=1`
55
+ 4. When you specifically search on the id column for the model, the id will be appended to the end
56
+ of the URL rather than as a query parameter. So, with a destination name of `https://example.com/products`,
57
+ querying for `models.find("id=10")` will result in fetching `https://example.com/products/10`.
58
+ 5. Delete and Update operations will similarly append the id to the URL, and also set the appropriate
59
+ response method (e.g. `DELETE` or `PATCH` by default).
60
+ 6. When processing the response, the backend will attempt to automatically discover the results by looking
61
+ for dictionaries that contain the expected column names (as determined from the model schema and the mapping
62
+ rules).
63
+ 7. The backend will check for a response header called `link` and parse this to find pagination information
64
+ so it can iterate through records.
65
+
66
+ NOTE: The API backend doesn't support joins or group_by clauses. This limitation, as well as the fact that it only
67
+ supports seaching with the equals operator, isn't a limitation in the API backend itself, but simply reflects the behavior
68
+ of most API endoints. If you want to support an API that has more flexibility (for instance, perhaps it allows for more search
69
+ operations than just `=`), then you can extend the appropritae methods, discussed below, to map a model query to an API request.
70
+
71
+ Here's an example of how to use the API Backend to integrate with the Github API:
72
+
73
+ ```python
74
+ import clearskies
75
+
76
+
77
+ class GithubPublicBackend(clearskies.backends.ApiBackend):
78
+ def __init__(
79
+ self,
80
+ # This varies from endpoint to endpoint, so we want to be able to set it for each model
81
+ pagination_parameter_name: str = "since",
82
+ ):
83
+ # these are fixed for all gitlab API parameters, so there's no need to make them setable
84
+ # from the constructor
85
+ self.base_url = "https://api.github.com"
86
+ self.limit_parameter_name = "per_page"
87
+ self.pagination_parameter_name = pagination_parameter_name
88
+ self.finalize_and_validate_configuration()
89
+
90
+
91
+ class UserRepo(clearskies.Model):
92
+ # Corresponding API Docs: https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#list-repositories-for-a-user
93
+ id_column_name = "full_name"
94
+ backend = GithubPublicBackend(pagination_parameter_name="page")
95
+
96
+ @classmethod
97
+ def destination_name(cls) -> str:
98
+ return "users/:login/repos"
99
+
100
+ id = clearskies.columns.Integer()
101
+ full_name = clearskies.columns.String()
102
+ type = clearskies.columns.Select(["all", "owner", "member"])
103
+ url = clearskies.columns.String()
104
+ html_url = clearskies.columns.String()
105
+ created_at = clearskies.columns.Datetime()
106
+ updated_at = clearskies.columns.Datetime()
107
+
108
+ # The API endpoint won't return "login" (e.g. username), so it may not seem like a column, but we need to search by it
109
+ # because it's a URL parameter for this API endpoint. Clearskies uses strict validation and won't let us search by
110
+ # a column that doesn't exist in the model: therefore, we have to add the login column.
111
+ login = clearskies.columns.String(is_searchable=True, is_readable=False)
112
+
113
+ # The API endpoint let's us sort by `created`/`updated`. Note that the names of the columns (based on the data returned
114
+ # by the API endpoint) are `created_at`/`updated_at`. As above, clearskies strictly validates data, so we need columns
115
+ # named created/updated so that we can sort by them. We can set some flags to (hopefully) avoid confusion
116
+ updated = clearskies.columns.Datetime(
117
+ is_searchable=False, is_readable=False, is_writeable=False
118
+ )
119
+ created = clearskies.columns.Datetime(
120
+ is_searchable=False, is_readable=False, is_writeable=False
121
+ )
122
+
123
+
124
+ class User(clearskies.Model):
125
+ # Corresponding API docs: https://docs.github.com/en/rest/users/users?apiVersion=2022-11-28#list-users
126
+
127
+ # github has two columns that are both effecitvely id columns: id and login.
128
+ # We use the login column for id_column_name because that is the column that gets
129
+ # used in the API to fetch an individual record
130
+ id_column_name = "login"
131
+ backend = GithubPublicBackend()
132
+
133
+ id = clearskies.columns.Integer()
134
+ login = clearskies.columns.String()
135
+ gravatar_id = clearskies.columns.String()
136
+ avatar_url = clearskies.columns.String()
137
+ html_url = clearskies.columns.String()
138
+ repos_url = clearskies.columns.String()
139
+
140
+ # We can hook up relationships between models just like we would if we were using an SQL-like
141
+ # database. The whole point of the backend system is that the model queries work regardless of
142
+ # backend, so clearskies can issue API calls to fetch related records just like it would be able
143
+ # to fetch children from a related database table.
144
+ repos = clearskies.columns.HasMany(
145
+ UserRepo,
146
+ foreign_column_name="login",
147
+ readable_child_columns=["id", "full_name", "html_url"],
148
+ )
149
+
150
+
151
+ def fetch_user(users: User, user_repos: UserRepo):
152
+ # If we execute this models query:
153
+ some_repos = (
154
+ user_repos.where("login=cmancone")
155
+ .sort_by("created", "desc")
156
+ .where("type=owner")
157
+ .pagination(page=2)
158
+ .limit(5)
159
+ )
160
+ # the API backend will fetch this url:
161
+ # https://api.github.com/users/cmancone/repos?type=owner&sort=created&direction=desc&per_page=5&page=2
162
+ # and we can use the results like always
163
+ repo_names = [repo.full_name for repo in some_repos]
164
+
165
+ # For the below case, the backend will fetch this url:
166
+ # https://api.github.com/users/cmancone
167
+ # in addition, the readable column names on the callable endpoint includes "repos", which references our has_many
168
+ # column. This means that when converting the user model to JSON, it will also grab a page of repositories for that user.
169
+ # To do that, it will fetch this URL:
170
+ # https://api.github.com/users/cmancone/repos
171
+ return users.find("login=cmancone")
172
+
173
+
174
+ wsgi = clearskies.contexts.WsgiRef(
175
+ clearskies.endpoints.Callable(
176
+ fetch_user,
177
+ model_class=User,
178
+ readable_column_names=["id", "login", "html_url", "repos"],
179
+ ),
180
+ classes=[User, UserRepo],
181
+ )
182
+
183
+ if __name__ == "__main__":
184
+ wsgi()
185
+ ```
186
+
187
+ The following example demonstrates how models using this backend can be used in other clearskies endpoints, just like any
188
+ other model. Note that the following example is re-using the above models and backend, I have just omitted them for the sake
189
+ of brevity:
190
+
191
+ ```python
192
+ wsgi = clearskies.contexts.WsgiRef(
193
+ clearskies.endpoints.List(
194
+ model_class=User,
195
+ readable_column_names=["id", "login", "html_url"],
196
+ sortable_column_names=["id"],
197
+ default_sort_column_name=None,
198
+ default_limit=10,
199
+ ),
200
+ classes=[User],
201
+ )
202
+
203
+ if __name__ == "__main__":
204
+ wsgi()
205
+ ```
206
+
207
+ And if you invoke it:
208
+
209
+ ```bash
210
+ $ curl 'http://localhost:8080' | jq
211
+ {
212
+ "status": "success",
213
+ "error": "",
214
+ "data": [
215
+ {
216
+ "id": 1,
217
+ "login": "mojombo",
218
+ "html_url": "https://github.com/mojombo"
219
+ },
220
+ {
221
+ "id": 2,
222
+ "login": "defunkt",
223
+ "html_url": "https://github.com/defunkt"
224
+ },
225
+ {
226
+ "id": 3,
227
+ "login": "pjhyett",
228
+ "html_url": "https://github.com/pjhyett"
229
+ },
230
+ {
231
+ "id": 4,
232
+ "login": "wycats",
233
+ "html_url": "https://github.com/wycats"
234
+ },
235
+ {
236
+ "id": 5,
237
+ "login": "ezmobius",
238
+ "html_url": "https://github.com/ezmobius"
239
+ },
240
+ {
241
+ "id": 6,
242
+ "login": "ivey",
243
+ "html_url": "https://github.com/ivey"
244
+ },
245
+ {
246
+ "id": 7,
247
+ "login": "evanphx",
248
+ "html_url": "https://github.com/evanphx"
249
+ },
250
+ {
251
+ "id": 17,
252
+ "login": "vanpelt",
253
+ "html_url": "https://github.com/vanpelt"
254
+ },
255
+ {
256
+ "id": 18,
257
+ "login": "wayneeseguin",
258
+ "html_url": "https://github.com/wayneeseguin"
259
+ },
260
+ {
261
+ "id": 19,
262
+ "login": "brynary",
263
+ "html_url": "https://github.com/brynary"
264
+ }
265
+ ],
266
+ "pagination": {
267
+ "number_results": null,
268
+ "limit": 10,
269
+ "next_page": {
270
+ "since": "19"
271
+ }
272
+ },
273
+ "input_errors": {}
274
+ }
275
+ ```
276
+
277
+ In essence, we now have an endpoint that lists results but, instead of pulling its data from a database, it
278
+ makes API calls. It also tracks pagination as expected, so you can use the data in `pagination.next_page` to
279
+ fetch the next set of results, just as you would if this were backed by a database, e.g.:
280
+
281
+ ```bash
282
+ $ curl http://localhost:8080?since=19
283
+ ```
284
+
285
+ ## Mapping from Queries to API calls
286
+
287
+ The process of mapping a model query into an API request involves a few different methods which can be
288
+ overwritten to fully control the process. This is necessary in cases where an API behaves differently
289
+ than expected by the API backend. This table outlines the method involved and how they are used:
290
+
291
+ | Method | Description |
292
+ |----------------------------------|-------------------------------------------------------------------------------------------------------|
293
+ | records_url | Return the absolute URL to fetch, as well as any columns that were used to fill in routing parameters |
294
+ | records_method | Reurn the HTTP request method to use for the API call |
295
+ | conditions_to_request_parameters | Translate the query conditions into URL fragments, query parameters, or JSON body parameters |
296
+ | pagination_to_request_parameters | Translate the pagination data into URL fragments, query parameters, or JSON body parameters |
297
+ | sorts_to_request_parameters | Translate the sort directive(s) into URL fragments, query parameters, or JSON body parameters |
298
+ | map_records_response | Take the response from the API and return a list of dictionaries with the resulting records |
299
+
300
+ In short, the details of the query are stored in a clearskies.query.Query object which is passed around to these
301
+ various methods. They use that information to adjust the URL, add query parameters, or add parameters into the
302
+ JSON body. The API Backend will then execute an API call with those final details, and use the map_record_response
303
+ method to pull the returned records out of the response from the API endpoint.
304
+
305
+ """
306
+
307
+ can_count = False
308
+
309
+ """
310
+ The Base URL for the requests - will be prepended to the destination_name() from the model.
311
+
312
+ Note: this is treated as a 'folder' path: if set, it becomes the URL prefix and is followed with a '/'
313
+ """
314
+ base_url = configs.String(default="")
315
+
316
+ """
317
+ A suffix to append to the end of the URL.
318
+
319
+ Note: this is treated as a 'folder' path: if set, it becomes the URL suffix and is prefixed with a '/'
320
+ """
321
+ url_suffix = configs.String(default="")
322
+
323
+ """
324
+ An instance of clearskies.authentication.Authentication that handles authentication to the API.
325
+
326
+ The following example is a modification of the Github Backends used above that shows how to setup authentication.
327
+ Github, like many APIs, uses an API key attached to the request via the authorization header. The SecretBearer
328
+ authentication class in clearskies is designed for this common use case, and pulls the secret key out of either
329
+ an environment variable or the secret manager (I use the former in this case, because it's hard to have a
330
+ self-contained example with a secret manager). Of course, any authentication method can be attached to your
331
+ API backend - SecretBearer authentication is used here simply because it's a common approach.
332
+
333
+ Note that, when used in conjunction with a secret manager, the API Backend and the SecretBearer class will work
334
+ together to check for a new secret in the event of an authentication failure from the API endpoint (specifically,
335
+ a 401 error). This allows you to automate credential rotation: create a new API key, put it in the secret manager,
336
+ and then revoke the old API key. The next time an API call is made, the SecretBearer will provide the old key from
337
+ it's cache and the request will fail. The API backend will detect this and try the request again, but this time
338
+ will tell the SecretBearer class to refresh it's cache with a fresh copy of the key from the secrets manager.
339
+ Therefore, as long as you put the new key in your secret manager **before** disabling the old key, this second
340
+ request will succeed and the service will continue to operate successfully with only a slight delay in response time
341
+ caused by refreshing the cache.
342
+
343
+ ```python
344
+ import clearskies
345
+
346
+ class GithubBackend(clearskies.backends.ApiBackend):
347
+ def __init__(
348
+ self,
349
+ pagination_parameter_name: str = "page",
350
+ authentication: clearskies.authentication.Authentication | None = None,
351
+ ):
352
+ self.base_url = "https://api.github.com"
353
+ self.limit_parameter_name = "per_page"
354
+ self.pagination_parameter_name = pagination_parameter_name
355
+ self.authentication = clearskies.authentication.SecretBearer(
356
+ environment_key="GITHUB_API_KEY",
357
+ header_prefix="Bearer ", # Because github expects a header of 'Authorization: Bearer API_KEY'
358
+ )
359
+ self.finalize_and_validate_configuration()
360
+
361
+ class Repo(clearskies.Model):
362
+ id_column_name = "login"
363
+ backend = GithubBackend()
364
+
365
+ @classmethod
366
+ def destination_name(cls):
367
+ return "/user/repos"
368
+
369
+ id = clearskies.columns.Integer()
370
+ name = clearskies.columns.String()
371
+ full_name = clearskies.columns.String()
372
+ html_url = clearskies.columns.String()
373
+ visibility = clearskies.columns.Select(["all", "public", "private"])
374
+
375
+ wsgi = clearskies.contexts.WsgiRef(
376
+ clearskies.endpoints.List(
377
+ model_class=Repo,
378
+ readable_column_names=["id", "name", "full_name", "html_url"],
379
+ sortable_column_names=["full_name"],
380
+ default_sort_column_name="full_name",
381
+ default_limit=10,
382
+ where=["visibility=private"],
383
+ ),
384
+ classes=[Repo],
385
+ )
386
+
387
+ if __name__ == "__main__":
388
+ wsgi()
389
+
390
+ ```
391
+ """
392
+ authentication = configs.Authentication(default=None)
393
+
394
+ """
395
+ A dictionary of headers to attach to all outgoing API requests
396
+ """
397
+ headers = configs.StringDict(default={})
398
+
399
+ """
400
+ The casing used in the model (snake_case, camelCase, TitleCase)
401
+
402
+ This is used in conjunction with api_casing to tell the processing layer when you and the API are using
403
+ different casing standards. The API backend will then automatically covnert the casing style of the API
404
+ to match your model. This can be helpful when you have a standard naming convention in your own code which
405
+ some external API doesn't follow, that way you can at least standardize things in your code. In the following
406
+ example, these parameters are used to convert from the snake_casing native to the Github API into the
407
+ TitleCasing used in the model class:
408
+
409
+ ```python
410
+ import clearskies
411
+
412
+ class User(clearskies.Model):
413
+ id_column_name = "login"
414
+ backend = clearskies.backends.ApiBackend(
415
+ base_url="https://api.github.com",
416
+ limit_parameter_name="per_page",
417
+ pagination_parameter_name="since",
418
+ model_casing="TitleCase",
419
+ api_casing="snake_case",
420
+ )
421
+
422
+ Id = clearskies.columns.Integer()
423
+ Login = clearskies.columns.String()
424
+ GravatarId = clearskies.columns.String()
425
+ AvatarUrl = clearskies.columns.String()
426
+ HtmlUrl = clearskies.columns.String()
427
+ ReposUrl = clearskies.columns.String()
428
+
429
+ wsgi = clearskies.contexts.WsgiRef(
430
+ clearskies.endpoints.List(
431
+ model_class=User,
432
+ readable_column_names=["Login", "AvatarUrl", "HtmlUrl", "ReposUrl"],
433
+ sortable_column_names=["Id"],
434
+ default_sort_column_name=None,
435
+ default_limit=2,
436
+ internal_casing="TitleCase",
437
+ external_casing="TitleCase",
438
+ ),
439
+ classes=[User],
440
+ )
441
+
442
+ if __name__ == "__main__":
443
+ wsgi()
444
+ ```
445
+
446
+ and when executed:
447
+
448
+ ```bash
449
+ $ curl http://localhost:8080 | jq
450
+ {
451
+ "Status": "Success",
452
+ "Error": "",
453
+ "Data": [
454
+ {
455
+ "Login": "mojombo",
456
+ "AvatarUrl": "https://avatars.githubusercontent.com/u/1?v=4",
457
+ "HtmlUrl": "https://github.com/mojombo",
458
+ "ReposUrl": "https://api.github.com/users/mojombo/repos"
459
+ },
460
+ {
461
+ "Login": "defunkt",
462
+ "AvatarUrl": "https://avatars.githubusercontent.com/u/2?v=4",
463
+ "HtmlUrl": "https://github.com/defunkt",
464
+ "ReposUrl": "https://api.github.com/users/defunkt/repos"
465
+ }
466
+ ],
467
+ "Pagination": {
468
+ "NumberResults": null,
469
+ "Limit": 2,
470
+ "NextPage": {
471
+ "Since": "2"
472
+ }
473
+ },
474
+ "InputErrors": {}
475
+ }
476
+ ```
477
+ """
478
+ model_casing = configs.Select(["snake_case", "camelCase", "TitleCase"], default="snake_case")
479
+
480
+ """
481
+ The casing used by the API response (snake_case, camelCase, TitleCase)
482
+
483
+ See model_casing for details and usage.
484
+ """
485
+ api_casing = configs.Select(["snake_case", "camelCase", "TitleCase"], default="snake_case")
486
+
487
+ """
488
+ A mapping from the data keys returned by the API to the data keys expected in the model
489
+
490
+ This comes into play when you want your model columns to use different names than what is returned by the
491
+ API itself. Provide a dictionary where the key is the name of a piece of data from the API, and the value
492
+ is the name of the column in the model. The API Backend will use this to match the API data to your model.
493
+ In the example below, `html_url` from the API has been mapped to `profile_url` in the model:
494
+
495
+ ```python
496
+ import clearskies
497
+
498
+ class User(clearskies.Model):
499
+ id_column_name = "login"
500
+ backend = clearskies.backends.ApiBackend(
501
+ base_url="https://api.github.com",
502
+ limit_parameter_name="per_page",
503
+ pagination_parameter_name="since",
504
+ api_to_model_map={"html_url": "profile_url"},
505
+ )
506
+
507
+ id = clearskies.columns.Integer()
508
+ login = clearskies.columns.String()
509
+ profile_url = clearskies.columns.String()
510
+
511
+ wsgi = clearskies.contexts.WsgiRef(
512
+ clearskies.endpoints.List(
513
+ model_class=User,
514
+ readable_column_names=["login", "profile_url"],
515
+ sortable_column_names=["id"],
516
+ default_sort_column_name=None,
517
+ default_limit=2,
518
+ ),
519
+ classes=[User],
520
+ )
521
+
522
+ if __name__ == "__main__":
523
+ wsgi()
524
+ ```
525
+
526
+ And if you invoke it:
527
+
528
+ ```bash
529
+ $ curl http://localhost:8080 | jq
530
+ {
531
+ "status": "success",
532
+ "error": "",
533
+ "data": [
534
+ {
535
+ "login": "mojombo",
536
+ "profile_url": "https://github.com/mojombo"
537
+ },
538
+ {
539
+ "login": "defunkt",
540
+ "profile_url": "https://github.com/defunkt"
541
+ }
542
+ ],
543
+ "pagination": {
544
+ "number_results": null,
545
+ "limit": 2,
546
+ "next_page": {
547
+ "since": "2"
548
+ }
549
+ },
550
+ "input_errors": {}
551
+ }
552
+ ```
553
+ """
554
+ api_to_model_map = configs.AnyDict(default={})
555
+
556
+ """
557
+ The name of the pagination parameter
558
+ """
559
+ pagination_parameter_name = configs.String(default="start")
560
+
561
+ """
562
+ The expected 'type' of the pagination parameter: must be either 'int' or 'str'
563
+
564
+ Note: this is set as a literal string, not as a type.
565
+ """
566
+ pagination_parameter_type = configs.Select(["int", "str"], default="str")
567
+
568
+ """
569
+ The name of the parameter that sets the number of records per page (if empty, setting the page size will not be allowed)
570
+ """
571
+ limit_parameter_name = configs.String(default="limit")
572
+
573
+ """
574
+ The requests instance.
575
+ """
576
+ requests = inject.Requests()
577
+
578
+ """
579
+ The dependency injection container (so we can pass it along to the Authentication object)
580
+ """
581
+ di = inject.Di()
582
+
583
+ _auth_injected = False
584
+ _response_to_model_map: dict[str, str] = None # type: ignore
585
+
586
+ @decorators.parameters_to_properties
587
+ def __init__(
588
+ self,
589
+ base_url: str,
590
+ authentication: Authentication | None = None,
591
+ model_casing: str = "snake_case",
592
+ api_casing: str = "snake_case",
593
+ api_to_model_map: dict[str, str | list[str]] = {},
594
+ pagination_parameter_name: str = "start",
595
+ pagination_parameter_type: str = "str",
596
+ limit_parameter_name: str = "limit",
597
+ ):
598
+ self.finalize_and_validate_configuration()
599
+
600
+ def finalize_url(self, url: str, available_routing_data: dict[str, str], operation: str) -> tuple[str, list[str]]:
601
+ """
602
+ Given a URL, this will append the base URL, fill in any routing data, and also return any used routing parameters.
603
+
604
+ For example, consider a base URL of `/my/api/{record_id}/:other_id` and then this is called as so:
605
+
606
+ ```python
607
+ (url, used_routing_parameters) = api_backend.finalize_url(
608
+ "entries",
609
+ {
610
+ "record_id": "1-2-3-4",
611
+ "other_id": "a-s-d-f",
612
+ "more_things": "qwerty",
613
+ },
614
+ )
615
+ ```
616
+
617
+ The returned url would be `/my/api/1-2-3-4/a-s-d-f/entries`, and used_routing_parameters would be ["record_id", "other_id"].
618
+ The latter is returned so you can understand what parameters were absorbed into the URL. Often, when some piece of data
619
+ becomes a routing parameter, it needs to be ignored in the rest of the request. `used_routing_parameters` helps with that.
620
+ """
621
+ base_url = self.base_url.strip("/") + "/" if self.base_url.strip("/") else ""
622
+ url_suffix = "/" + self.url_suffix.strip("/") if self.url_suffix.strip("/") else ""
623
+ url = base_url + url + url_suffix
624
+ routing_parameters = routing.extract_url_parameter_name_map(url)
625
+ if not routing_parameters:
626
+ return (url, [])
627
+
628
+ parts = url.split("/")
629
+ used_routing_parameters = []
630
+ for parameter_name, index in routing_parameters.items():
631
+ if parameter_name not in available_routing_data:
632
+ a = "an" if operation == "update" else "a"
633
+ raise ValueError(
634
+ f"""Failed to generate URL while building {a} {operation} request! Url {url} hsa a routing parameter named
635
+ {parameter_name} that I couldn't fill in from the request details. When fetching records, this should be
636
+ provided by adding an equals condition to the model, e.g. `model.where("{parameter_name}=some_value")`.
637
+ When creating/updating a record, this should be provided in the save data, e.g.:
638
+ `model.save({{"{parameter_name}": "some_value"}})`
639
+ """
640
+ )
641
+ if available_routing_data[parameter_name].__class__ not in [str, int]:
642
+ parameter_type = available_routing_data[parameter_name].__class__.__name__
643
+ raise ValueError(
644
+ f"I was filling in a routing parameter named {parameter_name} but the value I was given has a type of {parameter_type}. Routing parameters can only be strings or integers."
645
+ )
646
+ parts[index] = available_routing_data[parameter_name]
647
+ used_routing_parameters.append(parameter_name)
648
+ return ("/".join(parts), used_routing_parameters)
649
+
650
+ def finalize_url_from_data(self, url: str, data: dict[str, Any], operation: str) -> tuple[str, list[str]]:
651
+ """
652
+ Create the final URL using a data dictionary to fill in any URL parameters.
653
+
654
+ See finalize_url for more details about the return value
655
+ """
656
+ return self.finalize_url(url, data, operation)
657
+
658
+ def finalize_url_from_query(self, query: Query, operation: str) -> tuple[str, list[str]]:
659
+ """
660
+ Create the URL using a query to fill in any URL parameters.
661
+
662
+ See finalize_url for more details about the return value
663
+ """
664
+ available_routing_data = {}
665
+ for condition in query.conditions:
666
+ if condition.operator != "=":
667
+ continue
668
+ available_routing_data[condition.column_name] = condition.values[0]
669
+ return self.finalize_url(query.model_class.destination_name(), available_routing_data, operation)
670
+
671
+ def create_url(self, data: dict[str, Any], model: Model) -> tuple[str, list[str]]:
672
+ """
673
+ Calculate the URL to use for a create requst. Also, return the list of ay data parameters used to construct the URL.
674
+
675
+ See finalize_url for more details on the return value.
676
+ """
677
+ return self.finalize_url_from_data(model.destination_name(), data, "create")
678
+
679
+ def create_method(self, data: dict[str, Any], model: Model) -> str:
680
+ """Return the request method to use with a create request."""
681
+ return "POST"
682
+
683
+ def records_url(self, query: Query) -> tuple[str, list[str]]:
684
+ """
685
+ Calculate the URL to use for a records request. Also, return the list of any query parameters used to construct the URL.
686
+
687
+ See finalize_url for more details on the return value.
688
+ """
689
+ return self.finalize_url_from_query(query, "records")
690
+
691
+ def records_method(self, query: Query) -> str:
692
+ """Return the request method to use when fetching records from the API."""
693
+ return "GET"
694
+
695
+ def count_url(self, query: Query) -> tuple[str, list[str]]:
696
+ """
697
+ Calculate the URL to use for a request to get a record count.. Also, return the list of any query parameters used to construct the URL.
698
+
699
+ See finalize_url for more details on the return value.
700
+ """
701
+ return self.records_url(query)
702
+
703
+ def count_method(self, query: Query) -> str:
704
+ """Return the request method to use when making a request for a record count."""
705
+ return self.records_method(query)
706
+
707
+ def delete_url(self, id: int | str, model: Model) -> tuple[str, list[str]]:
708
+ """
709
+ Calculate the URL to use for a delete request. Also, return the list of any query parameters used to construct the URL.
710
+
711
+ See finalize_url for more details on the return value.
712
+ """
713
+ model_base_url = model.destination_name().strip("/") + "/" if model.destination_name() else ""
714
+ return self.finalize_url_from_data(f"{model_base_url}{id}", model.get_raw_data(), "delete")
715
+
716
+ def delete_method(self, id: int | str, model: Model) -> str:
717
+ """Return the request method to use when deleting records via the API."""
718
+ return "DELETE"
719
+
720
+ def update_url(self, id: int | str, data: dict[str, Any], model: Model) -> tuple[str, list[str]]:
721
+ """
722
+ Calculate the URL to use for an update request. Also, return the list of any query parameters used to construct the URL.
723
+
724
+ See finalize_url for more details on the return value.
725
+ """
726
+ model_base_url = model.destination_name().strip("/") + "/" if model.destination_name() else ""
727
+ return self.finalize_url_from_data(f"{model_base_url}{id}", {**model.get_raw_data(), **data}, "update")
728
+
729
+ def update_method(self, id: int | str, data: dict[str, Any], model: Model) -> str:
730
+ """Return the request method to use for an update request."""
731
+ return "PATCH"
732
+
733
+ def update(self, id: int | str, data: dict[str, Any], model: Model) -> dict[str, Any]:
734
+ """Update a record."""
735
+ data = {**data}
736
+ (url, used_routing_parameters) = self.update_url(id, data, model)
737
+ request_method = self.update_method(id, data, model)
738
+ for parameter in used_routing_parameters:
739
+ del data[parameter]
740
+
741
+ response = self.execute_request(url, request_method, json=data)
742
+ json_response = response.json() if response.content else {}
743
+ new_record = {**model.get_raw_data(), **data}
744
+ if response.content:
745
+ new_record = {**new_record, **self.map_update_response(response.json(), model)}
746
+ return new_record
747
+
748
+ def map_update_response(self, response_data: dict[str, Any], model: Model) -> dict[str, Any]:
749
+ """
750
+ Take the response from the API endpoint for an update request and figure out where the data lives/return it to build a new model.
751
+
752
+ See self.map_record_response for goals/motiviation
753
+ """
754
+ return self.map_record_response(response_data, model.get_columns(), "update")
755
+
756
+ def create(self, data: dict[str, Any], model: Model) -> dict[str, Any]:
757
+ """Create a record."""
758
+ data = {**data}
759
+ (url, used_routing_parameters) = self.create_url(data, model)
760
+ request_method = self.create_method(data, model)
761
+ for parameter in used_routing_parameters:
762
+ del data[parameter]
763
+
764
+ response = self.execute_request(url, request_method, json=data, headers=self.headers)
765
+ json_response = response.json() if response.content else {}
766
+ if response.content:
767
+ return self.map_create_response(response.json(), model)
768
+ return {}
769
+
770
+ def map_create_response(self, response_data: dict[str, Any], model: Model) -> dict[str, Any]:
771
+ return self.map_record_response(response_data, model.get_columns(), "create")
772
+
773
+ def delete(self, id: int | str, model: Model) -> bool:
774
+ (url, used_routing_parameters) = self.delete_url(id, model)
775
+ request_method = self.delete_method(id, model)
776
+
777
+ response = self.execute_request(url, request_method)
778
+ return True
779
+
780
+ def records(self, query: Query, next_page_data: dict[str, str | int] | None = None) -> list[dict[str, Any]]:
781
+ self.check_query(query)
782
+ (url, method, body, headers) = self.build_records_request(query)
783
+ response = self.execute_request(url, method, json=body, headers=headers)
784
+ records = self.map_records_response(response.json(), query)
785
+ if isinstance(next_page_data, dict):
786
+ self.set_next_page_data_from_response(next_page_data, query, response)
102
787
  return records
103
788
 
104
- def _build_records_request(self, configuration):
105
- return [self.url, "GET", self._as_post_data(configuration), {}]
789
+ def build_records_request(self, query: Query) -> tuple[str, str, dict[str, Any], dict[str, str]]:
790
+ (url, used_routing_parameters) = self.records_url(query)
791
+
792
+ (condition_route_id, condition_url_parameters, condition_body_parameters) = (
793
+ self.conditions_to_request_parameters(query, used_routing_parameters)
794
+ )
795
+ (pagination_url_parameters, pagination_body_parameters) = self.pagination_to_request_parameters(query)
796
+ (sort_url_parameters, sort_body_parameters) = self.sorts_to_request_parameters(query)
797
+
798
+ url_parameters = {
799
+ **condition_url_parameters,
800
+ **pagination_url_parameters,
801
+ **sort_url_parameters,
802
+ }
803
+
804
+ body_parameters = {
805
+ **condition_body_parameters,
806
+ **pagination_body_parameters,
807
+ **sort_body_parameters,
808
+ }
809
+
810
+ if condition_route_id:
811
+ url = url.rstrip("/") + "/" + condition_route_id
812
+ if url_parameters:
813
+ url = url + "?" + urllib.parse.urlencode(url_parameters)
814
+
815
+ return (
816
+ url,
817
+ self.records_method(query),
818
+ body_parameters,
819
+ {},
820
+ )
821
+
822
+ def conditions_to_request_parameters(
823
+ self, query: Query, used_routing_parameters: list[str]
824
+ ) -> tuple[str, dict[str, str], dict[str, Any]]:
825
+ route_id = ""
826
+
827
+ url_parameters = {}
828
+ for condition in query.conditions:
829
+ if condition.column_name in used_routing_parameters:
830
+ continue
831
+ if condition.operator != "=":
832
+ raise ValueError(
833
+ f"I'm not very smart and only know how to search with the equals operator, but I received a condition of {condition.parsed}. If you need to support this, you'll have to extend the ApiBackend and overwrite the build_records_request method."
834
+ )
835
+ if condition.column_name == query.model_class.id_column_name:
836
+ route_id = condition.values[0]
837
+ continue
838
+ url_parameters[condition.column_name] = condition.values[0]
106
839
 
107
- def _map_records_response(self, json):
108
- if not "data" in json:
109
- raise ValueError("Unexpected response from records request")
110
- return json["data"]
840
+ return (route_id, url_parameters, {})
841
+
842
+ def pagination_to_request_parameters(self, query: Query) -> tuple[dict[str, str], dict[str, Any]]:
843
+ url_parameters = {}
844
+ if query.limit:
845
+ if not self.limit_parameter_name:
846
+ raise ValueError(
847
+ "The records query attempted to change the limit (the number of results per page) but the backend does not support it. If it actually does support this, then set an appropriate value for backend.limit_parameter_name"
848
+ )
849
+ url_parameters[self.limit_parameter_name] = str(query.limit)
850
+
851
+ if query.pagination.get(self.pagination_parameter_name):
852
+ url_parameters[self.pagination_parameter_name] = str(query.pagination.get(self.pagination_parameter_name))
853
+
854
+ return (url_parameters, {})
855
+
856
+ def sorts_to_request_parameters(self, query: Query) -> tuple[dict[str, str], dict[str, Any]]:
857
+ if not query.sorts:
858
+ return ({}, {})
859
+
860
+ if len(query.sorts) > 1:
861
+ raise ValueError(
862
+ "I received a query with two sort directives, but I can only handle one. Sorry! If you need o support two sort directions, you'll have to extend the ApiBackend and overwrite the build_records_request method."
863
+ )
111
864
 
112
- def _execute_request(self, url, method, json=None, headers=None, is_retry=False):
865
+ return (
866
+ {"sort": query.sorts[0].column_name, "direction": query.sorts[0].direction.lower()},
867
+ {},
868
+ )
869
+
870
+ def map_records_response(
871
+ self, response_data: Any, query: Query, query_data: dict[str, Any] | None = None
872
+ ) -> list[dict[str, Any]]:
873
+ """Take the response from an API endpoint that returns a list of records and find the actual list of records."""
874
+ columns = query.model_class.get_columns()
875
+ # turn all of our conditions into record data and inject these into the results. We do this to keep around
876
+ # any query parameters. This is especially important for any URL parameters, wihch aren't always returned in
877
+ # the data, but which we are likely to need again if we go to update/delete the record.
878
+ if query_data is None:
879
+ query_data = {}
880
+ for condition in query.conditions:
881
+ if condition.operator != "=":
882
+ continue
883
+ query_data[condition.column_name] = condition.values[0]
884
+
885
+ # if our response is actually a list, then presumably the problem is solved. If the response is a list
886
+ # and the individual items aren't model results though... well, then I'm very confused
887
+ if isinstance(response_data, list):
888
+ if not response_data:
889
+ return []
890
+ if not self.check_dict_and_map_to_model(response_data[0], columns, query_data):
891
+ raise ValueError(
892
+ f"The response from a records request returned a list, but the records in the list didn't look anything like the model class. Please check your model class and mapping settings in the API Backend. If those are correct, then you'll have to override the map_records_response method, because the API you are interacting with is returning data in an unexpected way that I can't automatically figure out."
893
+ )
894
+ return [self.check_dict_and_map_to_model(record, columns, query_data) for record in response_data] # type: ignore
895
+
896
+ if not isinstance(response_data, dict):
897
+ raise ValueError(
898
+ f"The response from a records request returned a variable of type {response_data.__class__.__name__}, which is just confusing. To do automatic introspection, I need a list or a dictionary. I'm afraid you'll have to extend the API backend and override the map_record_response method to deal with this."
899
+ )
900
+
901
+ # a records request may only return a single record, so before we fail, let's check for that
902
+ record = self.check_dict_and_map_to_model(response_data, columns, query_data)
903
+ if record is not None:
904
+ return [record]
905
+
906
+ for key, value in response_data.items():
907
+ if not isinstance(value, list):
908
+ continue
909
+ return self.map_records_response(value, query, query_data)
910
+
911
+ raise ValueError(
912
+ "The response from a records request returned a dictionary, but none of the items in the dictionary was a list, so I don't know where to find the records. I only ever check one level deep in dictionaries. I'm afraid you'll have to extend the API backend and override the map_records_response method to deal with this."
913
+ )
914
+
915
+ def map_record_response(
916
+ self, response_data: dict[str, Any], columns: dict[str, Column], operation: str
917
+ ) -> dict[str, Any]:
918
+ """
919
+ Take the response from an API endpoint that returns a single record (typically update and create requests) and return the data for a new model.
920
+
921
+ The goal of this method is to try to use the model schema to automatically understand the response from the
922
+ the API endpoint. The goal is for the backend to work out-of-the-box with most APIs. In general, it works
923
+ by iterating over the response, looking for a dictionary with keys that match the expected model columns.
924
+
925
+ Occassionally the automatic introspection may not be able to make sense of the response from an API
926
+ endoint. If this happens, you have to make a new API backend, override the map_record_response method
927
+ to manage the mapping yourself, and then attach this new backend to your models.
928
+ """
929
+ an = "a" if operation == "create" else "an"
930
+ if not isinstance(response_data, dict):
931
+ raise ValueError(
932
+ f"The response from {an} {operation} request returned a variable of type {response_data.__class__.__name__}, which is just confusing. To do automatic introspection, I need a dictionary. I'm afraid you'll have to build your own API backend and override the map_record_response method to deal with this."
933
+ )
934
+
935
+ response = self.check_dict_and_map_to_model(response_data, columns)
936
+ if response is None:
937
+ raise ValueError(
938
+ f"I was not able to automatically interpret the response from {an} {operation} request. This could be a sign of a response that is structured in a very unusual way, or may be a sign that the casing settings and/or columns on your model to properly reflect the API response. For the former, you will hvae to build your own API backend and override the map_record_response to deal with this."
939
+ )
940
+
941
+ return response
942
+
943
+ def check_dict_and_map_to_model(
944
+ self,
945
+ response_data: dict[str, Any],
946
+ columns: dict[str, Column],
947
+ query_data: dict[str, Any] = {},
948
+ ) -> dict[str, Any] | None:
949
+ """
950
+ Check a dictionary in the response to decide if it contains the data for a record.
951
+
952
+ If not, it will search the keys for something that looks like a record.
953
+ """
954
+ # first let's get a coherent map of expected-key-names in the response to model names
955
+ response_to_model_map = self.build_response_to_model_map(columns)
956
+
957
+ # and now we can see if that appears to be what we have
958
+ response_keys = set(response_data.keys())
959
+ map_keys = set(response_to_model_map.keys())
960
+ matching = response_keys.intersection(map_keys)
961
+
962
+ # we may need to be smarter about whether or not we think we found a match, but for now let's
963
+ # ignore that possibility. If any columns match between the keys in our response dictionary and
964
+ # the keys that we are expecting to find data in, then just assume that we have found a record.
965
+ mapped = {response_to_model_map[key]: response_data[key] for key in matching}
966
+
967
+ for api_key, column_name in self.api_to_model_map.items():
968
+ if not "." in api_key:
969
+ continue
970
+ try:
971
+ value = json_functional.get_nested_attribute(response_data, api_key)
972
+ except KeyError:
973
+ # If the nested attribute is not found, just continue to the next key
974
+ continue
975
+ if value is None:
976
+ continue
977
+ if isinstance(column_name, list):
978
+ for column in column_name:
979
+ mapped[column] = value
980
+ else:
981
+ mapped[column_name] = value
982
+ # finally, move over anything not mentioned in the map
983
+ for key in response_keys.difference(map_keys):
984
+ mapped[string.swap_casing(key, self.api_casing, self.model_casing)] = response_data[key]
985
+
986
+ # if nothing matches then clearly this isn't what we're looking for: repeat on all the children
987
+ if not mapped:
988
+ for key, value in response_data.items():
989
+ if not isinstance(value, dict):
990
+ continue
991
+ remapped = self.check_dict_and_map_to_model(value, columns)
992
+ if remapped:
993
+ return {**query_data, **remapped}
994
+
995
+ # no match anywhere :(
996
+ return None
997
+
998
+ return {**query_data, **mapped}
999
+
1000
+ def build_response_to_model_map(self, columns: dict[str, Column]) -> dict[str, str]:
1001
+ if self._response_to_model_map is not None:
1002
+ return self._response_to_model_map
1003
+
1004
+ self._response_to_model_map = {}
1005
+ for column_name in columns:
1006
+ self._response_to_model_map[string.swap_casing(column_name, self.model_casing, self.api_casing)] = (
1007
+ column_name
1008
+ )
1009
+ self._response_to_model_map = {**self._response_to_model_map, **self.api_to_model_map}
1010
+
1011
+ return self._response_to_model_map
1012
+
1013
+ def set_next_page_data_from_response(
1014
+ self,
1015
+ next_page_data: dict[str, Any],
1016
+ query: Query,
1017
+ response: requests.Response, # type: ignore
1018
+ ) -> None:
1019
+ """
1020
+ Update the next_page_data dictionary with the appropriate data needed to fetch the next page of records.
1021
+
1022
+ This method has a very important job, which is to inform clearskies about how to make another API call to fetch the next
1023
+ page of records. The way this happens is by updating the `next_page_data` dictionary in place with whatever pagination
1024
+ information is necessary. Note that this relies on next_page_data being passed by reference, hence the need to update
1025
+ it in place. That means that you can do this:
1026
+
1027
+ ```python
1028
+ next_page_data["some_key"] = "some_value"
1029
+ ```
1030
+
1031
+ but if you do this:
1032
+
1033
+ ```python
1034
+ next_page_data = {"some_key": "some_value"}
1035
+ ```
1036
+
1037
+ Then things simply won't work.
1038
+ """
1039
+ # Different APIs generally have completely different ways of communicating pagination data, but one somewhat common
1040
+ # approach is to use a link header, so let's support that in the base class.
1041
+ if "link" not in response.headers:
1042
+ return
1043
+ next_link = [rel for rel in response.headers["link"].split(",") if 'rel="next"' in rel]
1044
+ if not next_link:
1045
+ return
1046
+ parsed_next_link = urllib.parse.urlparse(next_link[0].split(";")[0].strip(" <>"))
1047
+ query_parameters = urllib.parse.parse_qs(parsed_next_link.query)
1048
+ if self.pagination_parameter_name not in query_parameters:
1049
+ raise ValueError(
1050
+ f"Configuration error with {self.__class__.__name__}! I am configured to expect a pagination key of '{self.pagination_parameter_name}. However, when I was parsing the next link from a response to get the next pagination details, I could not find the designated pagination key. This likely means that backend.pagination_parameter_name is set to the wrong value. The link in question was "
1051
+ + parsed_next_link.geturl()
1052
+ )
1053
+ next_page_data[self.pagination_parameter_name] = query_parameters[self.pagination_parameter_name][0]
1054
+
1055
+ def count(self, query: Query) -> int:
1056
+ raise NotImplementedError(
1057
+ f"The {self.__class__.__name__} backend does not support count operations, so you can't use the `len` or `bool` function for any models using it."
1058
+ )
1059
+
1060
+ def execute_request(
1061
+ self,
1062
+ url: str,
1063
+ method: str,
1064
+ json: dict[str, Any] | None = None,
1065
+ headers: dict[str, str] | None = None,
1066
+ is_retry=False,
1067
+ ) -> requests.models.Response: # type: ignore
1068
+ """
1069
+ Execute the actual API request and returns the response object.
1070
+
1071
+ We don't directly call the requests library to support retries in the event of failed authentication. The goal
1072
+ is to support short-lived credentials, and our authentication classes denote if they support this feature. If
1073
+ they do, and the requests fails, then we'll ask the authentication method to refresh its credentials and we
1074
+ will retry the request.
1075
+ """
113
1076
  if json is None:
114
1077
  json = {}
115
1078
  if headers is None:
116
1079
  headers = {}
117
1080
 
118
- headers = {**headers, **self._auth.headers(retry_auth=is_retry)}
1081
+ if self.authentication:
1082
+ if not self._auth_injected:
1083
+ self._auth_injected = True
1084
+ if hasattr(self.authentication, "injectable_properties"):
1085
+ self.authentication.injectable_properties(self.di)
1086
+ if is_retry:
1087
+ self.authentication.clear_credential_cache()
119
1088
  # the requests library seems to build a slightly different request if you specify the json parameter,
120
1089
  # even if it is null, and this causes trouble for some picky servers
121
1090
  if not json:
122
- response = self._requests.request(
1091
+ response = self.requests.request(
123
1092
  method,
124
1093
  url,
125
1094
  headers=headers,
1095
+ auth=self.authentication if self.authentication else None,
126
1096
  )
127
1097
  else:
128
- response = self._requests.request(
1098
+ response = self.requests.request(
129
1099
  method,
130
1100
  url,
131
1101
  headers=headers,
132
1102
  json=json,
1103
+ auth=self.authentication if self.authentication else None,
133
1104
  )
134
1105
 
135
1106
  if not response.ok:
136
- if self._auth.has_dynamic_credentials and not is_retry:
137
- return self._execute_request(url, method, json=json, headers=headers, is_retry=True)
1107
+ if not is_retry and response.status_code == 401:
1108
+ return self.execute_request(url, method, json=json, headers=headers, is_retry=True)
138
1109
  if not response.ok:
139
- raise ValueError(f"Failed request. Status code: {response.status_code}, message: {response.content}")
1110
+ raise ValueError(
1111
+ f"Failed request. Status code: {response.status_code}, message: "
1112
+ + response.content.decode("utf-8")
1113
+ )
140
1114
 
141
1115
  return response
142
1116
 
143
- def _check_query_configuration(self, configuration):
144
- for key in configuration.keys():
145
- if key not in self._allowed_configs and configuration[key]:
146
- raise KeyError(f"ApiBackend does not support config '{key}'. You may be using the wrong backend")
147
-
148
- for key in self._allowed_configs:
149
- if not key in configuration:
150
- configuration[key] = [] if key[-1] == "s" else ""
151
- return configuration
152
-
153
- def _as_post_data(self, configuration):
154
- data = {
155
- "where": list(map(lambda where: self._where_for_post(where), configuration["wheres"])),
156
- "sort": configuration["sorts"],
157
- "start": configuration["pagination"].get("start", 0),
158
- "limit": configuration["limit"],
159
- }
160
- return {key: value for (key, value) in data.items() if value}
1117
+ def check_query(self, query: Query) -> None:
1118
+ for key in ["joins", "group_by", "selects"]:
1119
+ if getattr(query, key):
1120
+ raise ValueError(f"{self.__class__.__name__} does not support queries with {key}")
161
1121
 
162
- def _where_for_post(self, where):
163
- return {
164
- "column": where["column"],
165
- "operator": where["operator"],
166
- "values": where["values"],
167
- }
1122
+ for condition in query.conditions:
1123
+ if condition.operator != "=":
1124
+ raise ValueError(
1125
+ f"{self.__class__.__name__} only supports searching with the '=' operator, but I found a search with the {condition.operator} operator"
1126
+ )
168
1127
 
169
- def validate_pagination_kwargs(self, kwargs: Dict[str, Any], case_mapping: Callable) -> str:
170
- extra_keys = set(kwargs.keys()) - set(self.allowed_pagination_keys())
1128
+ def validate_pagination_data(self, data: dict[str, Any], case_mapping: Callable) -> str:
1129
+ extra_keys = set(data.keys()) - set(self.allowed_pagination_keys())
171
1130
  if len(extra_keys):
172
- key_name = case_mapping("start")
1131
+ key_name = case_mapping(self.pagination_parameter_name)
173
1132
  return "Invalid pagination key(s): '" + "','".join(extra_keys) + f"'. Only '{key_name}' is allowed"
174
- if "start" not in kwargs:
175
- key_name = case_mapping("start")
1133
+ if self.pagination_parameter_name not in data:
1134
+ key_name = case_mapping(self.pagination_parameter_name)
176
1135
  return f"You must specify '{key_name}' when setting pagination"
177
- start = kwargs["start"]
1136
+ value = data[self.pagination_parameter_name]
178
1137
  try:
179
- start = int(start)
1138
+ if self.pagination_parameter_type == "int":
1139
+ converted = int(value)
180
1140
  except:
181
- key_name = case_mapping("start")
1141
+ key_name = case_mapping(self.pagination_parameter_name)
182
1142
  return f"Invalid pagination data: '{key_name}' must be a number"
183
1143
  return ""
184
1144
 
185
- def allowed_pagination_keys(self) -> List[str]:
186
- return ["start"]
1145
+ def allowed_pagination_keys(self) -> list[str]:
1146
+ return [self.pagination_parameter_name]
187
1147
 
188
- def documentation_pagination_next_page_response(self, case_mapping: Callable) -> List[Any]:
189
- return [AutoDocInteger(case_mapping("start"), example=0)]
1148
+ def documentation_pagination_next_page_response(self, case_mapping: Callable) -> list[Any]:
1149
+ if self.pagination_parameter_type == "int":
1150
+ return [AutoDocInteger(case_mapping(self.pagination_parameter_name), example=0)]
1151
+ else:
1152
+ return [AutoDocString(case_mapping(self.pagination_parameter_name), example="")]
190
1153
 
191
- def documentation_pagination_next_page_example(self, case_mapping: Callable) -> Dict[str, Any]:
192
- return {case_mapping("start"): 0}
1154
+ def documentation_pagination_next_page_example(self, case_mapping: Callable) -> dict[str, Any]:
1155
+ return {case_mapping(self.pagination_parameter_name): 0 if self.pagination_parameter_type == "int" else ""}
193
1156
 
194
- def documentation_pagination_parameters(self, case_mapping: Callable) -> List[Tuple[Any]]:
1157
+ def documentation_pagination_parameters(self, case_mapping: Callable) -> list[tuple[AutoDocSchema, str]]:
195
1158
  return [
196
1159
  (
197
- AutoDocInteger(case_mapping("start"), example=0),
198
- "The zero-indexed record number to start listing results from",
1160
+ AutoDocInteger(
1161
+ case_mapping(self.pagination_parameter_name),
1162
+ example=0 if self.pagination_parameter_type == "int" else "",
1163
+ ),
1164
+ "The next record",
199
1165
  )
200
1166
  ]
201
1167
 
202
- def column_from_backend(self, column, value):
203
- """
204
- We have a couple columns we want to override transformations for
205
- """
1168
+ def column_from_backend(self, column: Column, value: Any) -> Any:
1169
+ """We have a couple columns we want to override transformations for."""
206
1170
  # most importantly, there's no need to transform a JSON column in either direction
207
- if isinstance(column, JSON):
1171
+ if isinstance(column, columns.json.Json):
208
1172
  return value
209
1173
  return super().column_from_backend(column, value)
210
1174
 
211
- def column_to_backend(self, column, backend_data):
212
- """
213
- We have a couple columns we want to override transformations for
214
- """
1175
+ def column_to_backend(self, column: Column, backend_data: dict[str, Any]) -> dict[str, Any]:
1176
+ """We have a couple columns we want to override transformations for."""
215
1177
  # most importantly, there's no need to transform a JSON column in either direction
216
- if isinstance(column, JSON):
1178
+ if isinstance(column, columns.json.Json):
217
1179
  return backend_data
218
1180
  # also, APIs tend to have a different format for dates than SQL
219
- if isinstance(column, DateTime):
1181
+ if isinstance(column, columns.datetime.Datetime) and column.name in backend_data:
220
1182
  as_date = (
221
1183
  backend_data[column.name].isoformat()
222
1184
  if type(backend_data[column.name]) != str