mistralai 0.4.2__py3-none-any.whl → 1.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.
Files changed (246) hide show
  1. mistralai/__init__.py +5 -0
  2. mistralai/_hooks/__init__.py +5 -0
  3. mistralai/_hooks/custom_user_agent.py +16 -0
  4. mistralai/_hooks/deprecation_warning.py +26 -0
  5. mistralai/_hooks/registration.py +17 -0
  6. mistralai/_hooks/sdkhooks.py +57 -0
  7. mistralai/_hooks/types.py +76 -0
  8. mistralai/agents.py +434 -0
  9. mistralai/async_client.py +5 -413
  10. mistralai/basesdk.py +253 -0
  11. mistralai/chat.py +470 -0
  12. mistralai/client.py +5 -414
  13. mistralai/embeddings.py +182 -0
  14. mistralai/files.py +600 -84
  15. mistralai/fim.py +438 -0
  16. mistralai/fine_tuning.py +16 -0
  17. mistralai/httpclient.py +78 -0
  18. mistralai/jobs.py +822 -150
  19. mistralai/models/__init__.py +82 -0
  20. mistralai/models/agentscompletionrequest.py +96 -0
  21. mistralai/models/agentscompletionstreamrequest.py +92 -0
  22. mistralai/models/archiveftmodelout.py +19 -0
  23. mistralai/models/assistantmessage.py +53 -0
  24. mistralai/models/chatcompletionchoice.py +22 -0
  25. mistralai/models/chatcompletionrequest.py +109 -0
  26. mistralai/models/chatcompletionresponse.py +27 -0
  27. mistralai/models/chatcompletionstreamrequest.py +107 -0
  28. mistralai/models/checkpointout.py +25 -0
  29. mistralai/models/completionchunk.py +27 -0
  30. mistralai/models/completionevent.py +15 -0
  31. mistralai/models/completionresponsestreamchoice.py +48 -0
  32. mistralai/models/contentchunk.py +17 -0
  33. mistralai/models/delete_model_v1_models_model_id_deleteop.py +18 -0
  34. mistralai/models/deletefileout.py +24 -0
  35. mistralai/models/deletemodelout.py +25 -0
  36. mistralai/models/deltamessage.py +47 -0
  37. mistralai/models/detailedjobout.py +91 -0
  38. mistralai/models/embeddingrequest.py +61 -0
  39. mistralai/models/embeddingresponse.py +24 -0
  40. mistralai/models/embeddingresponsedata.py +19 -0
  41. mistralai/models/eventout.py +50 -0
  42. mistralai/models/files_api_routes_delete_fileop.py +16 -0
  43. mistralai/models/files_api_routes_retrieve_fileop.py +16 -0
  44. mistralai/models/files_api_routes_upload_fileop.py +51 -0
  45. mistralai/models/fileschema.py +71 -0
  46. mistralai/models/fimcompletionrequest.py +94 -0
  47. mistralai/models/fimcompletionresponse.py +27 -0
  48. mistralai/models/fimcompletionstreamrequest.py +92 -0
  49. mistralai/models/finetuneablemodel.py +8 -0
  50. mistralai/models/ftmodelcapabilitiesout.py +21 -0
  51. mistralai/models/ftmodelout.py +65 -0
  52. mistralai/models/function.py +19 -0
  53. mistralai/models/functioncall.py +22 -0
  54. mistralai/models/githubrepositoryin.py +52 -0
  55. mistralai/models/githubrepositoryout.py +52 -0
  56. mistralai/models/httpvalidationerror.py +23 -0
  57. mistralai/models/jobin.py +73 -0
  58. mistralai/models/jobmetadataout.py +54 -0
  59. mistralai/models/jobout.py +107 -0
  60. mistralai/models/jobs_api_routes_fine_tuning_archive_fine_tuned_modelop.py +18 -0
  61. mistralai/models/jobs_api_routes_fine_tuning_cancel_fine_tuning_jobop.py +18 -0
  62. mistralai/models/jobs_api_routes_fine_tuning_create_fine_tuning_jobop.py +15 -0
  63. mistralai/models/jobs_api_routes_fine_tuning_get_fine_tuning_jobop.py +18 -0
  64. mistralai/models/jobs_api_routes_fine_tuning_get_fine_tuning_jobsop.py +81 -0
  65. mistralai/models/jobs_api_routes_fine_tuning_start_fine_tuning_jobop.py +16 -0
  66. mistralai/models/jobs_api_routes_fine_tuning_unarchive_fine_tuned_modelop.py +18 -0
  67. mistralai/models/jobs_api_routes_fine_tuning_update_fine_tuned_modelop.py +21 -0
  68. mistralai/models/jobsout.py +20 -0
  69. mistralai/models/legacyjobmetadataout.py +80 -0
  70. mistralai/models/listfilesout.py +17 -0
  71. mistralai/models/metricout.py +50 -0
  72. mistralai/models/modelcapabilities.py +21 -0
  73. mistralai/models/modelcard.py +66 -0
  74. mistralai/models/modellist.py +18 -0
  75. mistralai/models/responseformat.py +18 -0
  76. mistralai/models/retrieve_model_v1_models_model_id_getop.py +18 -0
  77. mistralai/models/retrievefileout.py +71 -0
  78. mistralai/models/sampletype.py +7 -0
  79. mistralai/models/sdkerror.py +22 -0
  80. mistralai/models/security.py +16 -0
  81. mistralai/models/source.py +7 -0
  82. mistralai/models/systemmessage.py +26 -0
  83. mistralai/models/textchunk.py +17 -0
  84. mistralai/models/tool.py +18 -0
  85. mistralai/models/toolcall.py +20 -0
  86. mistralai/models/toolmessage.py +50 -0
  87. mistralai/models/trainingfile.py +17 -0
  88. mistralai/models/trainingparameters.py +48 -0
  89. mistralai/models/trainingparametersin.py +56 -0
  90. mistralai/models/unarchiveftmodelout.py +19 -0
  91. mistralai/models/updateftmodelin.py +44 -0
  92. mistralai/models/uploadfileout.py +71 -0
  93. mistralai/models/usageinfo.py +18 -0
  94. mistralai/models/usermessage.py +26 -0
  95. mistralai/models/validationerror.py +24 -0
  96. mistralai/models/wandbintegration.py +56 -0
  97. mistralai/models/wandbintegrationout.py +52 -0
  98. mistralai/models_.py +928 -0
  99. mistralai/py.typed +1 -0
  100. mistralai/sdk.py +119 -0
  101. mistralai/sdkconfiguration.py +54 -0
  102. mistralai/types/__init__.py +21 -0
  103. mistralai/types/basemodel.py +39 -0
  104. mistralai/utils/__init__.py +86 -0
  105. mistralai/utils/annotations.py +19 -0
  106. mistralai/utils/enums.py +34 -0
  107. mistralai/utils/eventstreaming.py +178 -0
  108. mistralai/utils/forms.py +207 -0
  109. mistralai/utils/headers.py +136 -0
  110. mistralai/utils/logger.py +16 -0
  111. mistralai/utils/metadata.py +118 -0
  112. mistralai/utils/queryparams.py +203 -0
  113. mistralai/utils/requestbodies.py +66 -0
  114. mistralai/utils/retries.py +216 -0
  115. mistralai/utils/security.py +185 -0
  116. mistralai/utils/serializers.py +181 -0
  117. mistralai/utils/url.py +150 -0
  118. mistralai/utils/values.py +128 -0
  119. {mistralai-0.4.2.dist-info → mistralai-1.0.0.dist-info}/LICENSE +1 -1
  120. mistralai-1.0.0.dist-info/METADATA +695 -0
  121. mistralai-1.0.0.dist-info/RECORD +235 -0
  122. mistralai_azure/__init__.py +5 -0
  123. mistralai_azure/_hooks/__init__.py +5 -0
  124. mistralai_azure/_hooks/custom_user_agent.py +16 -0
  125. mistralai_azure/_hooks/registration.py +15 -0
  126. mistralai_azure/_hooks/sdkhooks.py +57 -0
  127. mistralai_azure/_hooks/types.py +76 -0
  128. mistralai_azure/basesdk.py +253 -0
  129. mistralai_azure/chat.py +470 -0
  130. mistralai_azure/httpclient.py +78 -0
  131. mistralai_azure/models/__init__.py +28 -0
  132. mistralai_azure/models/assistantmessage.py +53 -0
  133. mistralai_azure/models/chatcompletionchoice.py +22 -0
  134. mistralai_azure/models/chatcompletionrequest.py +109 -0
  135. mistralai_azure/models/chatcompletionresponse.py +27 -0
  136. mistralai_azure/models/chatcompletionstreamrequest.py +107 -0
  137. mistralai_azure/models/completionchunk.py +27 -0
  138. mistralai_azure/models/completionevent.py +15 -0
  139. mistralai_azure/models/completionresponsestreamchoice.py +48 -0
  140. mistralai_azure/models/contentchunk.py +17 -0
  141. mistralai_azure/models/deltamessage.py +47 -0
  142. mistralai_azure/models/function.py +19 -0
  143. mistralai_azure/models/functioncall.py +22 -0
  144. mistralai_azure/models/httpvalidationerror.py +23 -0
  145. mistralai_azure/models/responseformat.py +18 -0
  146. mistralai_azure/models/sdkerror.py +22 -0
  147. mistralai_azure/models/security.py +16 -0
  148. mistralai_azure/models/systemmessage.py +26 -0
  149. mistralai_azure/models/textchunk.py +17 -0
  150. mistralai_azure/models/tool.py +18 -0
  151. mistralai_azure/models/toolcall.py +20 -0
  152. mistralai_azure/models/toolmessage.py +50 -0
  153. mistralai_azure/models/usageinfo.py +18 -0
  154. mistralai_azure/models/usermessage.py +26 -0
  155. mistralai_azure/models/validationerror.py +24 -0
  156. mistralai_azure/py.typed +1 -0
  157. mistralai_azure/sdk.py +107 -0
  158. mistralai_azure/sdkconfiguration.py +54 -0
  159. mistralai_azure/types/__init__.py +21 -0
  160. mistralai_azure/types/basemodel.py +39 -0
  161. mistralai_azure/utils/__init__.py +84 -0
  162. mistralai_azure/utils/annotations.py +19 -0
  163. mistralai_azure/utils/enums.py +34 -0
  164. mistralai_azure/utils/eventstreaming.py +178 -0
  165. mistralai_azure/utils/forms.py +207 -0
  166. mistralai_azure/utils/headers.py +136 -0
  167. mistralai_azure/utils/logger.py +16 -0
  168. mistralai_azure/utils/metadata.py +118 -0
  169. mistralai_azure/utils/queryparams.py +203 -0
  170. mistralai_azure/utils/requestbodies.py +66 -0
  171. mistralai_azure/utils/retries.py +216 -0
  172. mistralai_azure/utils/security.py +168 -0
  173. mistralai_azure/utils/serializers.py +181 -0
  174. mistralai_azure/utils/url.py +150 -0
  175. mistralai_azure/utils/values.py +128 -0
  176. mistralai_gcp/__init__.py +5 -0
  177. mistralai_gcp/_hooks/__init__.py +5 -0
  178. mistralai_gcp/_hooks/custom_user_agent.py +16 -0
  179. mistralai_gcp/_hooks/registration.py +15 -0
  180. mistralai_gcp/_hooks/sdkhooks.py +57 -0
  181. mistralai_gcp/_hooks/types.py +76 -0
  182. mistralai_gcp/basesdk.py +253 -0
  183. mistralai_gcp/chat.py +458 -0
  184. mistralai_gcp/fim.py +438 -0
  185. mistralai_gcp/httpclient.py +78 -0
  186. mistralai_gcp/models/__init__.py +31 -0
  187. mistralai_gcp/models/assistantmessage.py +53 -0
  188. mistralai_gcp/models/chatcompletionchoice.py +22 -0
  189. mistralai_gcp/models/chatcompletionrequest.py +105 -0
  190. mistralai_gcp/models/chatcompletionresponse.py +27 -0
  191. mistralai_gcp/models/chatcompletionstreamrequest.py +103 -0
  192. mistralai_gcp/models/completionchunk.py +27 -0
  193. mistralai_gcp/models/completionevent.py +15 -0
  194. mistralai_gcp/models/completionresponsestreamchoice.py +48 -0
  195. mistralai_gcp/models/contentchunk.py +17 -0
  196. mistralai_gcp/models/deltamessage.py +47 -0
  197. mistralai_gcp/models/fimcompletionrequest.py +94 -0
  198. mistralai_gcp/models/fimcompletionresponse.py +27 -0
  199. mistralai_gcp/models/fimcompletionstreamrequest.py +92 -0
  200. mistralai_gcp/models/function.py +19 -0
  201. mistralai_gcp/models/functioncall.py +22 -0
  202. mistralai_gcp/models/httpvalidationerror.py +23 -0
  203. mistralai_gcp/models/responseformat.py +18 -0
  204. mistralai_gcp/models/sdkerror.py +22 -0
  205. mistralai_gcp/models/security.py +16 -0
  206. mistralai_gcp/models/systemmessage.py +26 -0
  207. mistralai_gcp/models/textchunk.py +17 -0
  208. mistralai_gcp/models/tool.py +18 -0
  209. mistralai_gcp/models/toolcall.py +20 -0
  210. mistralai_gcp/models/toolmessage.py +50 -0
  211. mistralai_gcp/models/usageinfo.py +18 -0
  212. mistralai_gcp/models/usermessage.py +26 -0
  213. mistralai_gcp/models/validationerror.py +24 -0
  214. mistralai_gcp/py.typed +1 -0
  215. mistralai_gcp/sdk.py +174 -0
  216. mistralai_gcp/sdkconfiguration.py +54 -0
  217. mistralai_gcp/types/__init__.py +21 -0
  218. mistralai_gcp/types/basemodel.py +39 -0
  219. mistralai_gcp/utils/__init__.py +84 -0
  220. mistralai_gcp/utils/annotations.py +19 -0
  221. mistralai_gcp/utils/enums.py +34 -0
  222. mistralai_gcp/utils/eventstreaming.py +178 -0
  223. mistralai_gcp/utils/forms.py +207 -0
  224. mistralai_gcp/utils/headers.py +136 -0
  225. mistralai_gcp/utils/logger.py +16 -0
  226. mistralai_gcp/utils/metadata.py +118 -0
  227. mistralai_gcp/utils/queryparams.py +203 -0
  228. mistralai_gcp/utils/requestbodies.py +66 -0
  229. mistralai_gcp/utils/retries.py +216 -0
  230. mistralai_gcp/utils/security.py +168 -0
  231. mistralai_gcp/utils/serializers.py +181 -0
  232. mistralai_gcp/utils/url.py +150 -0
  233. mistralai_gcp/utils/values.py +128 -0
  234. py.typed +1 -0
  235. mistralai/client_base.py +0 -211
  236. mistralai/constants.py +0 -5
  237. mistralai/exceptions.py +0 -54
  238. mistralai/models/chat_completion.py +0 -93
  239. mistralai/models/common.py +0 -9
  240. mistralai/models/embeddings.py +0 -19
  241. mistralai/models/files.py +0 -23
  242. mistralai/models/jobs.py +0 -100
  243. mistralai/models/models.py +0 -39
  244. mistralai-0.4.2.dist-info/METADATA +0 -82
  245. mistralai-0.4.2.dist-info/RECORD +0 -20
  246. {mistralai-0.4.2.dist-info → mistralai-1.0.0.dist-info}/WHEEL +0 -0
@@ -0,0 +1,203 @@
1
+ """Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""
2
+
3
+ from typing import (
4
+ Any,
5
+ Dict,
6
+ get_type_hints,
7
+ List,
8
+ Optional,
9
+ )
10
+
11
+ from pydantic import BaseModel
12
+ from pydantic.fields import FieldInfo
13
+
14
+ from .metadata import (
15
+ QueryParamMetadata,
16
+ find_field_metadata,
17
+ )
18
+ from .values import _get_serialized_params, _populate_from_globals, _val_to_string
19
+ from .forms import _populate_form
20
+
21
+
22
+ def get_query_params(
23
+ query_params: Any,
24
+ gbls: Optional[Any] = None,
25
+ ) -> Dict[str, List[str]]:
26
+ params: Dict[str, List[str]] = {}
27
+
28
+ globals_already_populated = _populate_query_params(query_params, gbls, params, [])
29
+ if gbls is not None:
30
+ _populate_query_params(gbls, None, params, globals_already_populated)
31
+
32
+ return params
33
+
34
+
35
+ def _populate_query_params(
36
+ query_params: Any,
37
+ gbls: Any,
38
+ query_param_values: Dict[str, List[str]],
39
+ skip_fields: List[str],
40
+ ) -> List[str]:
41
+ globals_already_populated: List[str] = []
42
+
43
+ if not isinstance(query_params, BaseModel):
44
+ return globals_already_populated
45
+
46
+ param_fields: Dict[str, FieldInfo] = query_params.__class__.model_fields
47
+ param_field_types = get_type_hints(query_params.__class__)
48
+ for name in param_fields:
49
+ if name in skip_fields:
50
+ continue
51
+
52
+ field = param_fields[name]
53
+
54
+ metadata = find_field_metadata(field, QueryParamMetadata)
55
+ if not metadata:
56
+ continue
57
+
58
+ value = getattr(query_params, name) if query_params is not None else None
59
+
60
+ value, global_found = _populate_from_globals(
61
+ name, value, QueryParamMetadata, gbls
62
+ )
63
+ if global_found:
64
+ globals_already_populated.append(name)
65
+
66
+ f_name = field.alias if field.alias is not None else name
67
+ serialization = metadata.serialization
68
+ if serialization is not None:
69
+ serialized_parms = _get_serialized_params(
70
+ metadata, f_name, value, param_field_types[name]
71
+ )
72
+ for key, value in serialized_parms.items():
73
+ if key in query_param_values:
74
+ query_param_values[key].extend(value)
75
+ else:
76
+ query_param_values[key] = [value]
77
+ else:
78
+ style = metadata.style
79
+ if style == "deepObject":
80
+ _populate_deep_object_query_params(f_name, value, query_param_values)
81
+ elif style == "form":
82
+ _populate_delimited_query_params(
83
+ metadata, f_name, value, ",", query_param_values
84
+ )
85
+ elif style == "pipeDelimited":
86
+ _populate_delimited_query_params(
87
+ metadata, f_name, value, "|", query_param_values
88
+ )
89
+ else:
90
+ raise NotImplementedError(
91
+ f"query param style {style} not yet supported"
92
+ )
93
+
94
+ return globals_already_populated
95
+
96
+
97
+ def _populate_deep_object_query_params(
98
+ field_name: str,
99
+ obj: Any,
100
+ params: Dict[str, List[str]],
101
+ ):
102
+ if obj is None:
103
+ return
104
+
105
+ if isinstance(obj, BaseModel):
106
+ _populate_deep_object_query_params_basemodel(field_name, obj, params)
107
+ elif isinstance(obj, Dict):
108
+ _populate_deep_object_query_params_dict(field_name, obj, params)
109
+
110
+
111
+ def _populate_deep_object_query_params_basemodel(
112
+ prior_params_key: str,
113
+ obj: Any,
114
+ params: Dict[str, List[str]],
115
+ ):
116
+ if obj is None:
117
+ return
118
+
119
+ if not isinstance(obj, BaseModel):
120
+ return
121
+
122
+ obj_fields: Dict[str, FieldInfo] = obj.__class__.model_fields
123
+ for name in obj_fields:
124
+ obj_field = obj_fields[name]
125
+
126
+ f_name = obj_field.alias if obj_field.alias is not None else name
127
+
128
+ params_key = f"{prior_params_key}[{f_name}]"
129
+
130
+ obj_param_metadata = find_field_metadata(obj_field, QueryParamMetadata)
131
+ if obj_param_metadata is None:
132
+ continue
133
+
134
+ obj_val = getattr(obj, name)
135
+ if obj_val is None:
136
+ continue
137
+
138
+ if isinstance(obj_val, BaseModel):
139
+ _populate_deep_object_query_params_basemodel(params_key, obj_val, params)
140
+ elif isinstance(obj_val, Dict):
141
+ _populate_deep_object_query_params_dict(params_key, obj_val, params)
142
+ elif isinstance(obj_val, List):
143
+ _populate_deep_object_query_params_list(params_key, obj_val, params)
144
+ else:
145
+ params[params_key] = [_val_to_string(obj_val)]
146
+
147
+
148
+ def _populate_deep_object_query_params_dict(
149
+ prior_params_key: str,
150
+ value: Dict,
151
+ params: Dict[str, List[str]],
152
+ ):
153
+ if value is None:
154
+ return
155
+
156
+ for key, val in value.items():
157
+ if val is None:
158
+ continue
159
+
160
+ params_key = f"{prior_params_key}[{key}]"
161
+
162
+ if isinstance(val, BaseModel):
163
+ _populate_deep_object_query_params_basemodel(params_key, val, params)
164
+ elif isinstance(val, Dict):
165
+ _populate_deep_object_query_params_dict(params_key, val, params)
166
+ elif isinstance(val, List):
167
+ _populate_deep_object_query_params_list(params_key, val, params)
168
+ else:
169
+ params[params_key] = [_val_to_string(val)]
170
+
171
+
172
+ def _populate_deep_object_query_params_list(
173
+ params_key: str,
174
+ value: List,
175
+ params: Dict[str, List[str]],
176
+ ):
177
+ if value is None:
178
+ return
179
+
180
+ for val in value:
181
+ if val is None:
182
+ continue
183
+
184
+ if params.get(params_key) is None:
185
+ params[params_key] = []
186
+
187
+ params[params_key].append(_val_to_string(val))
188
+
189
+
190
+ def _populate_delimited_query_params(
191
+ metadata: QueryParamMetadata,
192
+ field_name: str,
193
+ obj: Any,
194
+ delimiter: str,
195
+ query_param_values: Dict[str, List[str]],
196
+ ):
197
+ _populate_form(
198
+ field_name,
199
+ metadata.explode,
200
+ obj,
201
+ delimiter,
202
+ query_param_values,
203
+ )
@@ -0,0 +1,66 @@
1
+ """Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""
2
+
3
+ import io
4
+ from dataclasses import dataclass
5
+ import re
6
+ from typing import (
7
+ Any,
8
+ Optional,
9
+ )
10
+
11
+ from .forms import serialize_form_data, serialize_multipart_form
12
+
13
+ from .serializers import marshal_json
14
+
15
+ SERIALIZATION_METHOD_TO_CONTENT_TYPE = {
16
+ "json": "application/json",
17
+ "form": "application/x-www-form-urlencoded",
18
+ "multipart": "multipart/form-data",
19
+ "raw": "application/octet-stream",
20
+ "string": "text/plain",
21
+ }
22
+
23
+
24
+ @dataclass
25
+ class SerializedRequestBody:
26
+ media_type: str
27
+ content: Optional[Any] = None
28
+ data: Optional[Any] = None
29
+ files: Optional[Any] = None
30
+
31
+
32
+ def serialize_request_body(
33
+ request_body: Any,
34
+ nullable: bool,
35
+ optional: bool,
36
+ serialization_method: str,
37
+ request_body_type,
38
+ ) -> Optional[SerializedRequestBody]:
39
+ if request_body is None:
40
+ if not nullable and optional:
41
+ return None
42
+
43
+ media_type = SERIALIZATION_METHOD_TO_CONTENT_TYPE[serialization_method]
44
+
45
+ serialized_request_body = SerializedRequestBody(media_type)
46
+
47
+ if re.match(r"(application|text)\/.*?\+*json.*", media_type) is not None:
48
+ serialized_request_body.content = marshal_json(request_body, request_body_type)
49
+ elif re.match(r"multipart\/.*", media_type) is not None:
50
+ (
51
+ serialized_request_body.media_type,
52
+ serialized_request_body.data,
53
+ serialized_request_body.files,
54
+ ) = serialize_multipart_form(media_type, request_body)
55
+ elif re.match(r"application\/x-www-form-urlencoded.*", media_type) is not None:
56
+ serialized_request_body.data = serialize_form_data(request_body)
57
+ elif isinstance(request_body, (bytes, bytearray, io.BytesIO, io.BufferedReader)):
58
+ serialized_request_body.content = request_body
59
+ elif isinstance(request_body, str):
60
+ serialized_request_body.content = request_body
61
+ else:
62
+ raise TypeError(
63
+ f"invalid request body type {type(request_body)} for mediaType {media_type}"
64
+ )
65
+
66
+ return serialized_request_body
@@ -0,0 +1,216 @@
1
+ """Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""
2
+
3
+ import random
4
+ import time
5
+ from typing import List
6
+
7
+ import httpx
8
+
9
+
10
+ class BackoffStrategy:
11
+ initial_interval: int
12
+ max_interval: int
13
+ exponent: float
14
+ max_elapsed_time: int
15
+
16
+ def __init__(
17
+ self,
18
+ initial_interval: int,
19
+ max_interval: int,
20
+ exponent: float,
21
+ max_elapsed_time: int,
22
+ ):
23
+ self.initial_interval = initial_interval
24
+ self.max_interval = max_interval
25
+ self.exponent = exponent
26
+ self.max_elapsed_time = max_elapsed_time
27
+
28
+
29
+ class RetryConfig:
30
+ strategy: str
31
+ backoff: BackoffStrategy
32
+ retry_connection_errors: bool
33
+
34
+ def __init__(
35
+ self, strategy: str, backoff: BackoffStrategy, retry_connection_errors: bool
36
+ ):
37
+ self.strategy = strategy
38
+ self.backoff = backoff
39
+ self.retry_connection_errors = retry_connection_errors
40
+
41
+
42
+ class Retries:
43
+ config: RetryConfig
44
+ status_codes: List[str]
45
+
46
+ def __init__(self, config: RetryConfig, status_codes: List[str]):
47
+ self.config = config
48
+ self.status_codes = status_codes
49
+
50
+
51
+ class TemporaryError(Exception):
52
+ response: httpx.Response
53
+
54
+ def __init__(self, response: httpx.Response):
55
+ self.response = response
56
+
57
+
58
+ class PermanentError(Exception):
59
+ inner: Exception
60
+
61
+ def __init__(self, inner: Exception):
62
+ self.inner = inner
63
+
64
+
65
+ def retry(func, retries: Retries):
66
+ if retries.config.strategy == "backoff":
67
+
68
+ def do_request() -> httpx.Response:
69
+ res: httpx.Response
70
+ try:
71
+ res = func()
72
+
73
+ for code in retries.status_codes:
74
+ if "X" in code.upper():
75
+ code_range = int(code[0])
76
+
77
+ status_major = res.status_code / 100
78
+
79
+ if code_range <= status_major < code_range + 1:
80
+ raise TemporaryError(res)
81
+ else:
82
+ parsed_code = int(code)
83
+
84
+ if res.status_code == parsed_code:
85
+ raise TemporaryError(res)
86
+ except httpx.ConnectError as exception:
87
+ if retries.config.retry_connection_errors:
88
+ raise
89
+
90
+ raise PermanentError(exception) from exception
91
+ except httpx.TimeoutException as exception:
92
+ if retries.config.retry_connection_errors:
93
+ raise
94
+
95
+ raise PermanentError(exception) from exception
96
+ except TemporaryError:
97
+ raise
98
+ except Exception as exception:
99
+ raise PermanentError(exception) from exception
100
+
101
+ return res
102
+
103
+ return retry_with_backoff(
104
+ do_request,
105
+ retries.config.backoff.initial_interval,
106
+ retries.config.backoff.max_interval,
107
+ retries.config.backoff.exponent,
108
+ retries.config.backoff.max_elapsed_time,
109
+ )
110
+
111
+ return func()
112
+
113
+
114
+ async def retry_async(func, retries: Retries):
115
+ if retries.config.strategy == "backoff":
116
+
117
+ async def do_request() -> httpx.Response:
118
+ res: httpx.Response
119
+ try:
120
+ res = await func()
121
+
122
+ for code in retries.status_codes:
123
+ if "X" in code.upper():
124
+ code_range = int(code[0])
125
+
126
+ status_major = res.status_code / 100
127
+
128
+ if code_range <= status_major < code_range + 1:
129
+ raise TemporaryError(res)
130
+ else:
131
+ parsed_code = int(code)
132
+
133
+ if res.status_code == parsed_code:
134
+ raise TemporaryError(res)
135
+ except httpx.ConnectError as exception:
136
+ if retries.config.retry_connection_errors:
137
+ raise
138
+
139
+ raise PermanentError(exception) from exception
140
+ except httpx.TimeoutException as exception:
141
+ if retries.config.retry_connection_errors:
142
+ raise
143
+
144
+ raise PermanentError(exception) from exception
145
+ except TemporaryError:
146
+ raise
147
+ except Exception as exception:
148
+ raise PermanentError(exception) from exception
149
+
150
+ return res
151
+
152
+ return await retry_with_backoff_async(
153
+ do_request,
154
+ retries.config.backoff.initial_interval,
155
+ retries.config.backoff.max_interval,
156
+ retries.config.backoff.exponent,
157
+ retries.config.backoff.max_elapsed_time,
158
+ )
159
+
160
+ return await func()
161
+
162
+
163
+ def retry_with_backoff(
164
+ func,
165
+ initial_interval=500,
166
+ max_interval=60000,
167
+ exponent=1.5,
168
+ max_elapsed_time=3600000,
169
+ ):
170
+ start = round(time.time() * 1000)
171
+ retries = 0
172
+
173
+ while True:
174
+ try:
175
+ return func()
176
+ except PermanentError as exception:
177
+ raise exception.inner
178
+ except Exception as exception: # pylint: disable=broad-exception-caught
179
+ now = round(time.time() * 1000)
180
+ if now - start > max_elapsed_time:
181
+ if isinstance(exception, TemporaryError):
182
+ return exception.response
183
+
184
+ raise
185
+ sleep = (initial_interval / 1000) * exponent**retries + random.uniform(0, 1)
186
+ sleep = min(sleep, max_interval / 1000)
187
+ time.sleep(sleep)
188
+ retries += 1
189
+
190
+
191
+ async def retry_with_backoff_async(
192
+ func,
193
+ initial_interval=500,
194
+ max_interval=60000,
195
+ exponent=1.5,
196
+ max_elapsed_time=3600000,
197
+ ):
198
+ start = round(time.time() * 1000)
199
+ retries = 0
200
+
201
+ while True:
202
+ try:
203
+ return await func()
204
+ except PermanentError as exception:
205
+ raise exception.inner
206
+ except Exception as exception: # pylint: disable=broad-exception-caught
207
+ now = round(time.time() * 1000)
208
+ if now - start > max_elapsed_time:
209
+ if isinstance(exception, TemporaryError):
210
+ return exception.response
211
+
212
+ raise
213
+ sleep = (initial_interval / 1000) * exponent**retries + random.uniform(0, 1)
214
+ sleep = min(sleep, max_interval / 1000)
215
+ time.sleep(sleep)
216
+ retries += 1
@@ -0,0 +1,168 @@
1
+ """Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""
2
+
3
+ import base64
4
+ from typing import (
5
+ Any,
6
+ Dict,
7
+ List,
8
+ Tuple,
9
+ )
10
+ from pydantic import BaseModel
11
+ from pydantic.fields import FieldInfo
12
+
13
+ from .metadata import (
14
+ SecurityMetadata,
15
+ find_field_metadata,
16
+ )
17
+
18
+
19
+
20
+ def get_security(security: Any) -> Tuple[Dict[str, str], Dict[str, List[str]]]:
21
+ headers: Dict[str, str] = {}
22
+ query_params: Dict[str, List[str]] = {}
23
+
24
+ if security is None:
25
+ return headers, query_params
26
+
27
+ if not isinstance(security, BaseModel):
28
+ raise TypeError("security must be a pydantic model")
29
+
30
+ sec_fields: Dict[str, FieldInfo] = security.__class__.model_fields
31
+ for name in sec_fields:
32
+ sec_field = sec_fields[name]
33
+
34
+ value = getattr(security, name)
35
+ if value is None:
36
+ continue
37
+
38
+ metadata = find_field_metadata(sec_field, SecurityMetadata)
39
+ if metadata is None:
40
+ continue
41
+ if metadata.option:
42
+ _parse_security_option(headers, query_params, value)
43
+ return headers, query_params
44
+ if metadata.scheme:
45
+ # Special case for basic auth which could be a flattened model
46
+ if metadata.sub_type == "basic" and not isinstance(value, BaseModel):
47
+ _parse_security_scheme(headers, query_params, metadata, name, security)
48
+ else:
49
+ _parse_security_scheme(headers, query_params, metadata, name, value)
50
+
51
+ return headers, query_params
52
+
53
+
54
+ def _parse_security_option(
55
+ headers: Dict[str, str], query_params: Dict[str, List[str]], option: Any
56
+ ):
57
+ if not isinstance(option, BaseModel):
58
+ raise TypeError("security option must be a pydantic model")
59
+
60
+ opt_fields: Dict[str, FieldInfo] = option.__class__.model_fields
61
+ for name in opt_fields:
62
+ opt_field = opt_fields[name]
63
+
64
+ metadata = find_field_metadata(opt_field, SecurityMetadata)
65
+ if metadata is None or not metadata.scheme:
66
+ continue
67
+ _parse_security_scheme(
68
+ headers, query_params, metadata, name, getattr(option, name)
69
+ )
70
+
71
+
72
+ def _parse_security_scheme(
73
+ headers: Dict[str, str],
74
+ query_params: Dict[str, List[str]],
75
+ scheme_metadata: SecurityMetadata,
76
+ field_name: str,
77
+ scheme: Any,
78
+ ):
79
+ scheme_type = scheme_metadata.scheme_type
80
+ sub_type = scheme_metadata.sub_type
81
+
82
+ if isinstance(scheme, BaseModel):
83
+ if scheme_type == "http" and sub_type == "basic":
84
+ _parse_basic_auth_scheme(headers, scheme)
85
+ return
86
+
87
+ scheme_fields: Dict[str, FieldInfo] = scheme.__class__.model_fields
88
+ for name in scheme_fields:
89
+ scheme_field = scheme_fields[name]
90
+
91
+ metadata = find_field_metadata(scheme_field, SecurityMetadata)
92
+ if metadata is None or metadata.field_name is None:
93
+ continue
94
+
95
+ value = getattr(scheme, name)
96
+
97
+ _parse_security_scheme_value(
98
+ headers, query_params, scheme_metadata, metadata, name, value
99
+ )
100
+ else:
101
+ _parse_security_scheme_value(
102
+ headers, query_params, scheme_metadata, scheme_metadata, field_name, scheme
103
+ )
104
+
105
+
106
+ def _parse_security_scheme_value(
107
+ headers: Dict[str, str],
108
+ query_params: Dict[str, List[str]],
109
+ scheme_metadata: SecurityMetadata,
110
+ security_metadata: SecurityMetadata,
111
+ field_name: str,
112
+ value: Any,
113
+ ):
114
+ scheme_type = scheme_metadata.scheme_type
115
+ sub_type = scheme_metadata.sub_type
116
+
117
+ header_name = security_metadata.get_field_name(field_name)
118
+
119
+ if scheme_type == "apiKey":
120
+ if sub_type == "header":
121
+ headers[header_name] = value
122
+ elif sub_type == "query":
123
+ query_params[header_name] = [value]
124
+ else:
125
+ raise ValueError("sub type {sub_type} not supported")
126
+ elif scheme_type == "openIdConnect":
127
+ headers[header_name] = _apply_bearer(value)
128
+ elif scheme_type == "oauth2":
129
+ if sub_type != "client_credentials":
130
+ headers[header_name] = _apply_bearer(value)
131
+ elif scheme_type == "http":
132
+ if sub_type == "bearer":
133
+ headers[header_name] = _apply_bearer(value)
134
+ else:
135
+ raise ValueError("sub type {sub_type} not supported")
136
+ else:
137
+ raise ValueError("scheme type {scheme_type} not supported")
138
+
139
+
140
+ def _apply_bearer(token: str) -> str:
141
+ return token.lower().startswith("bearer ") and token or f"Bearer {token}"
142
+
143
+
144
+ def _parse_basic_auth_scheme(headers: Dict[str, str], scheme: Any):
145
+ username = ""
146
+ password = ""
147
+
148
+ if not isinstance(scheme, BaseModel):
149
+ raise TypeError("basic auth scheme must be a pydantic model")
150
+
151
+ scheme_fields: Dict[str, FieldInfo] = scheme.__class__.model_fields
152
+ for name in scheme_fields:
153
+ scheme_field = scheme_fields[name]
154
+
155
+ metadata = find_field_metadata(scheme_field, SecurityMetadata)
156
+ if metadata is None or metadata.field_name is None:
157
+ continue
158
+
159
+ field_name = metadata.field_name
160
+ value = getattr(scheme, name)
161
+
162
+ if field_name == "username":
163
+ username = value
164
+ if field_name == "password":
165
+ password = value
166
+
167
+ data = f"{username}:{password}".encode()
168
+ headers["Authorization"] = f"Basic {base64.b64encode(data).decode()}"