clear-skies 1.22.31__py3-none-any.whl → 2.0.0__py3-none-any.whl

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

Potentially problematic release.


This version of clear-skies might be problematic. Click here for more details.

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