clear-skies 1.22.10__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 (368) 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.22.10.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 +8 -39
  7. clearskies/authentication/authentication.py +44 -0
  8. clearskies/authentication/authorization.py +14 -8
  9. clearskies/authentication/authorization_pass_through.py +14 -10
  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 +55 -20
  41. clearskies/backends/api_backend.py +1118 -280
  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 +115 -4
  154. clearskies/di/additional_config_auto_import.py +12 -0
  155. clearskies/di/di.py +714 -125
  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 -160
  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 +1874 -193
  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.22.10.dist-info/METADATA +0 -47
  243. clear_skies-1.22.10.dist-info/RECORD +0 -213
  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 -51
  248. clearskies/backends/api_get_only_backend.py +0 -48
  249. clearskies/backends/example_backend.py +0 -43
  250. clearskies/backends/file_backend.py +0 -48
  251. clearskies/backends/json_backend.py +0 -7
  252. clearskies/backends/restful_api_advanced_search_backend.py +0 -103
  253. clearskies/binding_config.py +0 -16
  254. clearskies/column_types/__init__.py +0 -203
  255. clearskies/column_types/audit.py +0 -249
  256. clearskies/column_types/belongs_to.py +0 -271
  257. clearskies/column_types/boolean.py +0 -60
  258. clearskies/column_types/category_tree.py +0 -304
  259. clearskies/column_types/column.py +0 -373
  260. clearskies/column_types/created.py +0 -26
  261. clearskies/column_types/created_by_authorization_data.py +0 -26
  262. clearskies/column_types/created_by_header.py +0 -24
  263. clearskies/column_types/created_by_ip.py +0 -17
  264. clearskies/column_types/created_by_routing_data.py +0 -25
  265. clearskies/column_types/created_by_user_agent.py +0 -17
  266. clearskies/column_types/created_micro.py +0 -26
  267. clearskies/column_types/datetime.py +0 -109
  268. clearskies/column_types/datetime_micro.py +0 -13
  269. clearskies/column_types/email.py +0 -18
  270. clearskies/column_types/float.py +0 -43
  271. clearskies/column_types/has_many.py +0 -179
  272. clearskies/column_types/has_one.py +0 -58
  273. clearskies/column_types/integer.py +0 -41
  274. clearskies/column_types/json.py +0 -25
  275. clearskies/column_types/many_to_many.py +0 -278
  276. clearskies/column_types/many_to_many_with_data.py +0 -162
  277. clearskies/column_types/phone.py +0 -48
  278. clearskies/column_types/select.py +0 -11
  279. clearskies/column_types/string.py +0 -24
  280. clearskies/column_types/timestamp.py +0 -73
  281. clearskies/column_types/updated.py +0 -26
  282. clearskies/column_types/updated_micro.py +0 -26
  283. clearskies/column_types/uuid.py +0 -25
  284. clearskies/columns.py +0 -123
  285. clearskies/condition_parser.py +0 -172
  286. clearskies/contexts/build_context.py +0 -54
  287. clearskies/contexts/convert_to_application.py +0 -190
  288. clearskies/contexts/extract_handler.py +0 -37
  289. clearskies/contexts/test.py +0 -94
  290. clearskies/decorators/__init__.py +0 -39
  291. clearskies/decorators/auth0_jwks.py +0 -22
  292. clearskies/decorators/authorization.py +0 -10
  293. clearskies/decorators/binding_classes.py +0 -9
  294. clearskies/decorators/binding_modules.py +0 -9
  295. clearskies/decorators/bindings.py +0 -9
  296. clearskies/decorators/create.py +0 -10
  297. clearskies/decorators/delete.py +0 -10
  298. clearskies/decorators/docs.py +0 -14
  299. clearskies/decorators/get.py +0 -10
  300. clearskies/decorators/jwks.py +0 -26
  301. clearskies/decorators/merge.py +0 -124
  302. clearskies/decorators/patch.py +0 -10
  303. clearskies/decorators/post.py +0 -10
  304. clearskies/decorators/public.py +0 -11
  305. clearskies/decorators/response_headers.py +0 -10
  306. clearskies/decorators/return_raw_response.py +0 -9
  307. clearskies/decorators/schema.py +0 -10
  308. clearskies/decorators/secret_bearer.py +0 -24
  309. clearskies/decorators/security_headers.py +0 -10
  310. clearskies/di/standard_dependencies.py +0 -151
  311. clearskies/di/test_module/__init__.py +0 -6
  312. clearskies/di/test_module/another_module/__init__.py +0 -2
  313. clearskies/di/test_module/module_class.py +0 -5
  314. clearskies/handlers/__init__.py +0 -41
  315. clearskies/handlers/advanced_search.py +0 -271
  316. clearskies/handlers/base.py +0 -479
  317. clearskies/handlers/callable.py +0 -191
  318. clearskies/handlers/create.py +0 -35
  319. clearskies/handlers/crud_by_method.py +0 -18
  320. clearskies/handlers/database_connector.py +0 -32
  321. clearskies/handlers/delete.py +0 -61
  322. clearskies/handlers/exceptions/__init__.py +0 -5
  323. clearskies/handlers/exceptions/not_found.py +0 -3
  324. clearskies/handlers/get.py +0 -156
  325. clearskies/handlers/health_check.py +0 -59
  326. clearskies/handlers/input_processing.py +0 -79
  327. clearskies/handlers/list.py +0 -530
  328. clearskies/handlers/mygrations.py +0 -82
  329. clearskies/handlers/request_method_routing.py +0 -47
  330. clearskies/handlers/restful_api.py +0 -218
  331. clearskies/handlers/routing.py +0 -62
  332. clearskies/handlers/schema_helper.py +0 -128
  333. clearskies/handlers/simple_routing.py +0 -206
  334. clearskies/handlers/simple_routing_route.py +0 -192
  335. clearskies/handlers/simple_search.py +0 -136
  336. clearskies/handlers/update.py +0 -96
  337. clearskies/handlers/write.py +0 -193
  338. clearskies/input_requirements/__init__.py +0 -78
  339. clearskies/input_requirements/after.py +0 -36
  340. clearskies/input_requirements/before.py +0 -36
  341. clearskies/input_requirements/in_the_future_at_least.py +0 -19
  342. clearskies/input_requirements/in_the_future_at_most.py +0 -19
  343. clearskies/input_requirements/in_the_past_at_least.py +0 -19
  344. clearskies/input_requirements/in_the_past_at_most.py +0 -19
  345. clearskies/input_requirements/maximum_length.py +0 -19
  346. clearskies/input_requirements/maximum_value.py +0 -19
  347. clearskies/input_requirements/minimum_length.py +0 -22
  348. clearskies/input_requirements/minimum_value.py +0 -19
  349. clearskies/input_requirements/requirement.py +0 -25
  350. clearskies/input_requirements/time_delta.py +0 -38
  351. clearskies/input_requirements/unique.py +0 -18
  352. clearskies/mocks/__init__.py +0 -7
  353. clearskies/mocks/input_output.py +0 -124
  354. clearskies/mocks/models.py +0 -142
  355. clearskies/models.py +0 -350
  356. clearskies/security_headers/base.py +0 -12
  357. clearskies/tests/simple_api/models/__init__.py +0 -2
  358. clearskies/tests/simple_api/models/status.py +0 -23
  359. clearskies/tests/simple_api/models/user.py +0 -21
  360. clearskies/tests/simple_api/users_api.py +0 -64
  361. {clear_skies-1.22.10.dist-info → clear_skies-2.0.23.dist-info/licenses}/LICENSE +0 -0
  362. /clearskies/{contexts/bash.py → autodoc/py.typed} +0 -0
  363. /clearskies/{handlers/exceptions → exceptions}/authentication.py +0 -0
  364. /clearskies/{handlers/exceptions → exceptions}/authorization.py +0 -0
  365. /clearskies/{handlers/exceptions → exceptions}/client_error.py +0 -0
  366. /clearskies/{secrets/exceptions → exceptions}/not_found.py +0 -0
  367. /clearskies/{tests/__init__.py → input_outputs/py.typed} +0 -0
  368. /clearskies/{tests/simple_api/__init__.py → py.typed} +0 -0
@@ -1,264 +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
- import re
7
-
8
-
9
- class ApiBackend(Backend):
10
- url = None
11
- _requests = None
12
- _auth = None
13
- _records = None
14
-
15
- _allowed_configs = [
16
- "select_all",
17
- "wheres",
18
- "sorts",
19
- "limit",
20
- "pagination",
21
- "table_name",
22
- "model_columns",
23
- ]
24
-
25
- _empty_configs = [
26
- "group_by_column",
27
- "selects",
28
- "joins",
29
- ]
30
-
31
- def __init__(self, requests):
32
- self._requests = requests
33
-
34
- def configure(self, url=None, auth=None):
35
- self.url = url
36
- self._auth = auth
37
-
38
- def records_url(self, configuration: Dict[str, Any]) -> str:
39
- return self.url
40
-
41
- def count_url(self, configuration: Dict[str, Any]) -> str:
42
- return self.records_url(configuration)
43
-
44
- def delete_url(self, id: str, model: model.Model) -> str:
45
- return self.url
46
-
47
- def update_url(self, id: str, model: model.Model) -> str:
48
- return self.url
49
-
50
- def create_url(self, data: Dict[str, Any], model: model.Model) -> str:
51
- return self.url
52
-
53
- def records_method(self, configuration: Dict[str, Any]) -> str:
54
- return "GET"
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)
55
393
 
56
- def count_method(self, configuration: Dict[str, Any]) -> str:
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."""
57
693
  return "GET"
58
694
 
59
- def delete_method(self, id: str, model: model.Model) -> str:
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."""
60
718
  return "DELETE"
61
719
 
62
- def update_method(self, id: str, model: model.Model) -> str:
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."""
63
731
  return "PATCH"
64
732
 
65
- def create_method(self, data: Dict[str, Any], model: model.Model) -> str:
66
- return "POST"
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]
67
740
 
68
- def update(self, id, data, model):
69
- [url, method, json_data, headers] = self._build_update_request(id, data, model)
70
- response = self._execute_request(url, method, json=json_data, headers=headers)
71
- if not response.content:
72
- return {**model.data, **data}
73
- return self._map_update_response(response.json())
74
-
75
- def _build_update_request(self, id, data, model):
76
- (url, data) = self._finalize_url_and_data(self.update_url(id, model), data)
77
- return [url, self.update_method(id, model), data, {}]
78
-
79
- def _map_update_response(self, json):
80
- if not "data" in json:
81
- raise ValueError("Unexpected API response to update request")
82
- return json["data"]
83
-
84
- def create(self, data, model):
85
- [url, method, json_data, headers] = self._build_create_request(data, model)
86
- response = self._execute_request(url, method, json=json_data, headers=headers)
87
- return self._map_create_response(response.json())
88
-
89
- def _build_create_request(self, data, model):
90
- (url, data) = self._finalize_url_and_data(self.create_url(data, model), data)
91
- return [url, self.create_method(data, model), data, {}]
92
-
93
- def _map_create_response(self, json):
94
- if not "data" in json:
95
- raise ValueError("Unexpected API response to create request")
96
- return json["data"]
97
-
98
- def delete(self, id, model):
99
- [url, method, json_data, headers] = self._build_delete_request(id, model)
100
- response = self._execute_request(url, method, json=json_data, headers=headers)
101
- return self._validate_delete_response(response.json())
102
-
103
- def _build_delete_request(self, id, model):
104
- data = model.data
105
- (url, data) = self._finalize_url_and_data(self.delete_url(id, model), data)
106
- return [url, self.delete_method(id, model), {model.id_column_name: id}, {}]
107
-
108
- def _validate_delete_response(self, json):
109
- if "status" not in json:
110
- raise ValueError("Unexpected response to delete API request")
111
- return json["status"] == "success"
112
-
113
- def count(self, configuration, model):
114
- configuration = self._check_query_configuration(configuration)
115
- [url, method, json_data, headers] = self._build_count_request(configuration)
116
- response = self._execute_request(url, method, json=json_data, headers=headers)
117
- return self._map_count_response(response.json())
118
-
119
- def _build_count_request(self, configuration):
120
- (url, configuration) = self._finalize_url_and_configuration(self.count_url(configuration), configuration)
121
- return [
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)
787
+ return records
788
+
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 (
122
816
  url,
123
- self.count_method(configuration),
124
- {**{"count_only": True}, **self._as_post_data(configuration)},
817
+ self.records_method(query),
818
+ body_parameters,
125
819
  {},
126
- ]
820
+ )
127
821
 
128
- def _map_count_response(self, json):
129
- if not "total_matches" in json:
130
- raise ValueError("Unexpected API response when executing count request")
131
- return json["total_matches"]
132
-
133
- def records(self, configuration, model, next_page_data=None):
134
- configuration = self._check_query_configuration(configuration)
135
- [url, method, json_data, headers] = self._build_records_request(configuration)
136
- response = self._execute_request(url, method, json=json_data, headers=headers)
137
- records = self._map_records_response(response.json())
138
- if type(next_page_data) == dict:
139
- limit = configuration.get("limit", None)
140
- start = configuration.get("pagination", {}).get("start", 0)
141
- if limit and len(records) == limit:
142
- next_page_data["start"] = start + limit
143
- return records
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]
839
+
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
+ )
864
+
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]
144
985
 
145
- def _build_records_request(self, configuration):
146
- (url, configuration) = self._finalize_url_and_configuration(self.records_url(configuration), configuration)
147
- return [url, self.records_method(configuration), self._as_post_data(configuration), {}]
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}
148
994
 
149
- def _map_records_response(self, json):
150
- if not "data" in json:
151
- raise ValueError("Unexpected response from records request")
152
- return json["data"]
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]
153
1054
 
154
- def _execute_request(self, url, method, json=None, headers=None, is_retry=False):
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
+ """
155
1076
  if json is None:
156
1077
  json = {}
157
1078
  if headers is None:
158
1079
  headers = {}
159
1080
 
160
- 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()
161
1088
  # the requests library seems to build a slightly different request if you specify the json parameter,
162
1089
  # even if it is null, and this causes trouble for some picky servers
163
1090
  if not json:
164
- response = self._requests.request(
1091
+ response = self.requests.request(
165
1092
  method,
166
1093
  url,
167
1094
  headers=headers,
1095
+ auth=self.authentication if self.authentication else None,
168
1096
  )
169
1097
  else:
170
- response = self._requests.request(
1098
+ response = self.requests.request(
171
1099
  method,
172
1100
  url,
173
1101
  headers=headers,
174
1102
  json=json,
1103
+ auth=self.authentication if self.authentication else None,
175
1104
  )
176
1105
 
177
1106
  if not response.ok:
178
- if self._auth.has_dynamic_credentials and not is_retry:
179
- 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)
180
1109
  if not response.ok:
181
- 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
+ )
182
1114
 
183
1115
  return response
184
1116
 
185
- def _check_query_configuration(self, configuration):
186
- for key in configuration.keys():
187
- if key not in self._allowed_configs and configuration[key]:
188
- raise KeyError(f"ApiBackend does not support config '{key}'. You may be using the wrong backend")
189
-
190
- for key in self._allowed_configs:
191
- if not key in configuration:
192
- configuration[key] = [] if key[-1] == "s" else ""
193
- return configuration
194
-
195
- def _as_post_data(self, configuration):
196
- data = {
197
- "where": list(map(lambda where: self._where_for_post(where), configuration["wheres"])),
198
- "sort": configuration["sorts"],
199
- "start": configuration["pagination"].get("start", 0),
200
- "limit": configuration["limit"],
201
- }
202
- 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}")
203
1121
 
204
- def _where_for_post(self, where):
205
- return {
206
- "column": where["column"],
207
- "operator": where["operator"],
208
- "values": where["values"],
209
- }
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
+ )
210
1127
 
211
- def validate_pagination_kwargs(self, kwargs: Dict[str, Any], case_mapping: Callable) -> str:
212
- 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())
213
1130
  if len(extra_keys):
214
- key_name = case_mapping("start")
1131
+ key_name = case_mapping(self.pagination_parameter_name)
215
1132
  return "Invalid pagination key(s): '" + "','".join(extra_keys) + f"'. Only '{key_name}' is allowed"
216
- if "start" not in kwargs:
217
- key_name = case_mapping("start")
1133
+ if self.pagination_parameter_name not in data:
1134
+ key_name = case_mapping(self.pagination_parameter_name)
218
1135
  return f"You must specify '{key_name}' when setting pagination"
219
- start = kwargs["start"]
1136
+ value = data[self.pagination_parameter_name]
220
1137
  try:
221
- start = int(start)
1138
+ if self.pagination_parameter_type == "int":
1139
+ converted = int(value)
222
1140
  except:
223
- key_name = case_mapping("start")
1141
+ key_name = case_mapping(self.pagination_parameter_name)
224
1142
  return f"Invalid pagination data: '{key_name}' must be a number"
225
1143
  return ""
226
1144
 
227
- def allowed_pagination_keys(self) -> List[str]:
228
- return ["start"]
1145
+ def allowed_pagination_keys(self) -> list[str]:
1146
+ return [self.pagination_parameter_name]
229
1147
 
230
- def documentation_pagination_next_page_response(self, case_mapping: Callable) -> List[Any]:
231
- 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="")]
232
1153
 
233
- def documentation_pagination_next_page_example(self, case_mapping: Callable) -> Dict[str, Any]:
234
- 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 ""}
235
1156
 
236
- def documentation_pagination_parameters(self, case_mapping: Callable) -> List[Tuple[Any]]:
1157
+ def documentation_pagination_parameters(self, case_mapping: Callable) -> list[tuple[AutoDocSchema, str]]:
237
1158
  return [
238
1159
  (
239
- AutoDocInteger(case_mapping("start"), example=0),
240
- "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",
241
1165
  )
242
1166
  ]
243
1167
 
244
- def column_from_backend(self, column, value):
245
- """
246
- We have a couple columns we want to override transformations for
247
- """
1168
+ def column_from_backend(self, column: Column, value: Any) -> Any:
1169
+ """We have a couple columns we want to override transformations for."""
248
1170
  # most importantly, there's no need to transform a JSON column in either direction
249
- if isinstance(column, JSON):
1171
+ if isinstance(column, columns.json.Json):
250
1172
  return value
251
1173
  return super().column_from_backend(column, value)
252
1174
 
253
- def column_to_backend(self, column, backend_data):
254
- """
255
- We have a couple columns we want to override transformations for
256
- """
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."""
257
1177
  # most importantly, there's no need to transform a JSON column in either direction
258
- if isinstance(column, JSON):
1178
+ if isinstance(column, columns.json.Json):
259
1179
  return backend_data
260
1180
  # also, APIs tend to have a different format for dates than SQL
261
- if isinstance(column, DateTime):
1181
+ if isinstance(column, columns.datetime.Datetime) and column.name in backend_data:
262
1182
  as_date = (
263
1183
  backend_data[column.name].isoformat()
264
1184
  if type(backend_data[column.name]) != str
@@ -266,85 +1186,3 @@ class ApiBackend(Backend):
266
1186
  )
267
1187
  return {**backend_data, **{column.name: as_date}}
268
1188
  return column.to_backend(backend_data)
269
-
270
- def _finalize_url_and_data(self, url: str, data: Dict[str, Any]) -> Tuple[str, Dict[str, Any]]:
271
- (url, used_columns) = self._finalize_url(url, data, model)
272
- for used_column in used_columns:
273
- del data[used_column]
274
- return (url, data)
275
-
276
- def _finalize_url_and_configuration(self, url: str, configuration: Dict[str, Any]) -> Tuple[str, Dict[str, Any]]:
277
- # we need to convert the wheres in the configuration to a dictionary of key/values, but
278
- # *only* for cases where we have performed an equals search.
279
- filters_by_equals = {}
280
- index_lookup = {}
281
- for index, where in enumerate(configuration["wheres"]):
282
- if where["operator"] != "=":
283
- continue
284
- filters_by_equals[where["column"]] = where["values"][0]
285
- index_lookup[where["column"]] = index
286
-
287
- # always call _finalize_url, even if we don't have any search columns,
288
- # because if there are placeholders in the URL but we don't have any values,
289
- # then we need to throw an exception.
290
- (url, used_columns) = self._finalize_url(url, filters_by_equals, model)
291
- # we need to remove the used entries from the wheres but in doing so we start at the end
292
- # of the array so our indexes stay valid.
293
- to_delete = [index_lookup[used_column] for used_column in used_columns]
294
- to_delete.sort(reverse=True)
295
- for index_to_delete in to_delete:
296
- del configuration["wheres"][index_to_delete]
297
-
298
- return (url, configuration)
299
-
300
- def _finalize_url(self, url: str, data: Dict[str, Any], model: model.Models) -> Tuple[str, List[str]]:
301
- """
302
- This function is what gives support for placeholders in URLs. We support two formats:
303
-
304
- 1. /some/path/{some_field}/blah
305
- 2. /some/path/:some_field/blah
306
-
307
- The url comes from the `my_url` function, which (by default) is just self.url. You can
308
- always extend `my_url` to pull the URL from something else (the `model.table_name()` for instance).
309
-
310
- You would then:
311
-
312
- ```
313
- models.where("some_field=some_value")
314
- ```
315
-
316
- and when the API backend makes the call it will then build the appropriate URL. Naturally,
317
- you'll want to add a corresponding column to your model, otherwise the model will complain
318
- that "some_field is not an allowed column in model class 'BLAH'" (since all search columns
319
- used in a `where` query go through strict input validation).
320
- """
321
- # many Snyk API calls require a resource id in the URL. Let's check if that is the case here,
322
- # and if so, get it out of the query configuration
323
- used_columns = []
324
- resource_references = self._find_resource_references_in_url(url)
325
- for resource_reference in resource_references:
326
- resource_name = resource_reference["name"]
327
- placeholder = resource_reference["placeholder"]
328
- if not data.get(resource_name):
329
- raise ValueError(
330
- 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"
331
- )
332
-
333
- url = url.replace(placeholder, str(data.get(resource_name)))
334
- used_columns.append(resource_name)
335
- return (url, used_columns)
336
-
337
- def _find_resource_references_in_url(self, url: str) -> list[str]:
338
- if not url:
339
- return []
340
- # To help with the regexp matching, it helps if the URL both starts and ends with a "/".
341
- # We don't need to modify the URL at all - we just need it for our matching, so it's fine
342
- # that our changes aren't propogated back to the calling function.
343
- if url[-1] != "/":
344
- url += "/"
345
- if url[0] != "/":
346
- url = f"/{url}"
347
- return [
348
- *[{"name": reference, "placeholder": "{" + reference + "}"} for reference in re.findall(r"{(\w+)}", url)],
349
- *[{"name": reference, "placeholder": f":{reference}"} for reference in re.findall(r"/:([^/]+)/", url)],
350
- ]