embed-client-dev 4.0.78__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 (48) hide show
  1. embed_client/__init__.py +36 -0
  2. embed_client/__main__.py +15 -0
  3. embed_client/_version.py +3 -0
  4. embed_client/adapter_config_factory.py +213 -0
  5. embed_client/adapter_config_normalizer.py +135 -0
  6. embed_client/adapter_generator_bridge.py +166 -0
  7. embed_client/auth.py +189 -0
  8. embed_client/cli.py +483 -0
  9. embed_client/cli_core.py +427 -0
  10. embed_client/client_factory.py +515 -0
  11. embed_client/client_factory_examples.py +375 -0
  12. embed_client/client_factory_helpers.py +59 -0
  13. embed_client/config.py +408 -0
  14. embed_client/config_defaults.py +77 -0
  15. embed_client/config_examples.py +228 -0
  16. embed_client/config_generator.py +652 -0
  17. embed_client/config_generator_cli.py +151 -0
  18. embed_client/config_presets.py +245 -0
  19. embed_client/config_validator.py +246 -0
  20. embed_client/config_validator_cli.py +141 -0
  21. embed_client/constants.py +52 -0
  22. embed_client/demo_cli.py +78 -0
  23. embed_client/demo_security_cli.py +206 -0
  24. embed_client/embed_file_output_validate.py +348 -0
  25. embed_client/embed_file_status_store.py +80 -0
  26. embed_client/embedding_client.py +1929 -0
  27. embed_client/embedding_client_helpers.py +105 -0
  28. embed_client/example_async_usage.py +357 -0
  29. embed_client/example_async_usage_demo.py +282 -0
  30. embed_client/example_async_usage_ru.py +340 -0
  31. embed_client/example_async_usage_ru_demo.py +313 -0
  32. embed_client/exceptions.py +122 -0
  33. embed_client/identifier_types.py +56 -0
  34. embed_client/json_embed_file_preflight.py +128 -0
  35. embed_client/local_json_embed_file.py +73 -0
  36. embed_client/md_embed_file_prep.py +197 -0
  37. embed_client/response_normalizer.py +160 -0
  38. embed_client/response_parsers.py +219 -0
  39. embed_client/security_cli.py +200 -0
  40. embed_client/security_cli_core.py +227 -0
  41. embed_client/ssl_examples.py +278 -0
  42. embed_client/ssl_manager.py +141 -0
  43. embed_client_dev-4.0.78.dist-info/METADATA +155 -0
  44. embed_client_dev-4.0.78.dist-info/RECORD +48 -0
  45. embed_client_dev-4.0.78.dist-info/WHEEL +5 -0
  46. embed_client_dev-4.0.78.dist-info/entry_points.txt +4 -0
  47. embed_client_dev-4.0.78.dist-info/licenses/LICENSE +21 -0
  48. embed_client_dev-4.0.78.dist-info/top_level.txt +1 -0
@@ -0,0 +1,375 @@
1
+ """
2
+ Client Factory Examples
3
+
4
+ Author: Vasiliy Zdanovskiy
5
+ email: vasilyvz@gmail.com
6
+
7
+ This module demonstrates how to use the ClientFactory to create clients
8
+ with different security modes.
9
+ """
10
+
11
+ import asyncio
12
+ import os
13
+ from embed_client.client_factory import (
14
+ ClientFactory,
15
+ create_client,
16
+ create_client_from_config,
17
+ create_client_from_env,
18
+ detect_security_mode,
19
+ )
20
+
21
+
22
+ async def demonstrate_security_mode_detection():
23
+ """Demonstrate automatic security mode detection."""
24
+ print("=== Security Mode Detection Examples ===")
25
+
26
+ # HTTP mode
27
+ mode = detect_security_mode("http://localhost")
28
+ print(f"http://localhost -> {mode}")
29
+
30
+ # HTTP + Token mode
31
+ mode = detect_security_mode("http://localhost", auth_method="api_key")
32
+ print(f"http://localhost + api_key -> {mode}")
33
+
34
+ # HTTPS mode
35
+ mode = detect_security_mode("https://localhost")
36
+ print(f"https://localhost -> {mode}")
37
+
38
+ # HTTPS + Token mode
39
+ mode = detect_security_mode("https://localhost", auth_method="api_key")
40
+ print(f"https://localhost + api_key -> {mode}")
41
+
42
+ # mTLS mode
43
+ mode = detect_security_mode(
44
+ "https://localhost", cert_file="cert.pem", key_file="key.pem"
45
+ )
46
+ print(f"https://localhost + client certs -> {mode}")
47
+
48
+ # mTLS + Roles mode
49
+ mode = detect_security_mode(
50
+ "https://localhost", cert_file="cert.pem", key_file="key.pem", roles=["admin"]
51
+ )
52
+ print(f"https://localhost + client certs + roles -> {mode}")
53
+
54
+ print()
55
+
56
+
57
+ async def demonstrate_automatic_client_creation():
58
+ """Demonstrate automatic client creation with mode detection."""
59
+ print("=== Automatic Client Creation Examples ===")
60
+
61
+ # HTTP client
62
+ client = create_client("http://localhost", 8001)
63
+ print(f"HTTP client created: {client.base_url}:{client.port}")
64
+ print(f" SSL enabled: {client.is_ssl_enabled()}")
65
+ print(f" Authenticated: {client.is_authenticated()}")
66
+ print()
67
+
68
+ # HTTPS client
69
+ client = create_client("https://localhost", 8001)
70
+ print(f"HTTPS client created: {client.base_url}:{client.port}")
71
+ print(f" SSL enabled: {client.is_ssl_enabled()}")
72
+ print(f" Authenticated: {client.is_authenticated()}")
73
+ print()
74
+
75
+ # HTTP + API Key client
76
+ client = create_client(
77
+ "http://localhost", 8001, auth_method="api_key", api_key="test_key"
78
+ )
79
+ print(f"HTTP + API Key client created: {client.base_url}:{client.port}")
80
+ print(f" SSL enabled: {client.is_ssl_enabled()}")
81
+ print(f" Authenticated: {client.is_authenticated()}")
82
+ print(f" Auth method: {client.get_auth_method()}")
83
+ print()
84
+
85
+ # HTTPS + JWT client
86
+ client = create_client(
87
+ "https://localhost",
88
+ 8001,
89
+ auth_method="jwt",
90
+ jwt_secret="secret",
91
+ jwt_username="user",
92
+ jwt_password="pass",
93
+ )
94
+ print(f"HTTPS + JWT client created: {client.base_url}:{client.port}")
95
+ print(f" SSL enabled: {client.is_ssl_enabled()}")
96
+ print(f" Authenticated: {client.is_authenticated()}")
97
+ print(f" Auth method: {client.get_auth_method()}")
98
+ print()
99
+
100
+ # mTLS client
101
+ client = create_client(
102
+ "https://localhost",
103
+ 8001,
104
+ cert_file="client_cert.pem",
105
+ key_file="client_key.pem",
106
+ )
107
+ print(f"mTLS client created: {client.base_url}:{client.port}")
108
+ print(f" SSL enabled: {client.is_ssl_enabled()}")
109
+ print(f" mTLS enabled: {client.is_mtls_enabled()}")
110
+ print(f" Authenticated: {client.is_authenticated()}")
111
+ print()
112
+
113
+
114
+ async def demonstrate_specific_client_creation():
115
+ """Demonstrate creating clients for specific security modes."""
116
+ print("=== Specific Client Creation Examples ===")
117
+
118
+ # HTTP client
119
+ client = ClientFactory.create_http_client("http://localhost", 8001)
120
+ print(f"HTTP client: {client.base_url}:{client.port}")
121
+ print()
122
+
123
+ # HTTP + Token client
124
+ client = ClientFactory.create_http_token_client(
125
+ "http://localhost", 8001, "api_key", api_key="test_key"
126
+ )
127
+ print(f"HTTP + Token client: {client.base_url}:{client.port}")
128
+ print(f" Auth method: {client.get_auth_method()}")
129
+ print()
130
+
131
+ # HTTPS client
132
+ client = ClientFactory.create_https_client("https://localhost", 8001)
133
+ print(f"HTTPS client: {client.base_url}:{client.port}")
134
+ print(f" SSL enabled: {client.is_ssl_enabled()}")
135
+ print()
136
+
137
+ # HTTPS + Token client
138
+ client = ClientFactory.create_https_token_client(
139
+ "https://localhost", 8001, "basic", username="user", password="pass"
140
+ )
141
+ print(f"HTTPS + Token client: {client.base_url}:{client.port}")
142
+ print(f" SSL enabled: {client.is_ssl_enabled()}")
143
+ print(f" Auth method: {client.get_auth_method()}")
144
+ print()
145
+
146
+ # mTLS client
147
+ client = ClientFactory.create_mtls_client(
148
+ "https://localhost", "client_cert.pem", "client_key.pem", 8001
149
+ )
150
+ print(f"mTLS client: {client.base_url}:{client.port}")
151
+ print(f" SSL enabled: {client.is_ssl_enabled()}")
152
+ print(f" mTLS enabled: {client.is_mtls_enabled()}")
153
+ print()
154
+
155
+ # mTLS + Roles client
156
+ client = ClientFactory.create_mtls_roles_client(
157
+ "https://localhost",
158
+ "client_cert.pem",
159
+ "client_key.pem",
160
+ 8001,
161
+ roles=["admin", "user"],
162
+ role_attributes={"department": "IT"},
163
+ )
164
+ print(f"mTLS + Roles client: {client.base_url}:{client.port}")
165
+ print(f" SSL enabled: {client.is_ssl_enabled()}")
166
+ print(f" mTLS enabled: {client.is_mtls_enabled()}")
167
+ print()
168
+
169
+
170
+ async def demonstrate_environment_based_creation():
171
+ """Demonstrate creating clients from environment variables."""
172
+ print("=== Environment-Based Client Creation ===")
173
+
174
+ # Set environment variables for demonstration
175
+ os.environ["EMBED_CLIENT_BASE_URL"] = "https://example.com"
176
+ os.environ["EMBED_CLIENT_PORT"] = "9443"
177
+ os.environ["EMBED_CLIENT_AUTH_METHOD"] = "api_key"
178
+ os.environ["EMBED_CLIENT_API_KEY"] = "env_test_key"
179
+
180
+ try:
181
+ client = create_client_from_env()
182
+ print(f"Client from environment: {client.base_url}:{client.port}")
183
+ print(f" SSL enabled: {client.is_ssl_enabled()}")
184
+ print(f" Authenticated: {client.is_authenticated()}")
185
+ print(f" Auth method: {client.get_auth_method()}")
186
+ print()
187
+ finally:
188
+ # Clean up environment variables
189
+ for key in [
190
+ "EMBED_CLIENT_BASE_URL",
191
+ "EMBED_CLIENT_PORT",
192
+ "EMBED_CLIENT_AUTH_METHOD",
193
+ "EMBED_CLIENT_API_KEY",
194
+ ]:
195
+ os.environ.pop(key, None)
196
+
197
+
198
+ async def demonstrate_config_file_creation():
199
+ """Demonstrate creating clients from configuration files."""
200
+ print("=== Configuration File Client Creation ===")
201
+
202
+ # Create a sample configuration file
203
+ config_content = """
204
+ {
205
+ "server": {
206
+ "host": "https://secure.example.com",
207
+ "port": 9443
208
+ },
209
+ "auth": {
210
+ "method": "api_key",
211
+ "api_keys": {
212
+ "user": "config_test_key"
213
+ }
214
+ },
215
+ "ssl": {
216
+ "enabled": true,
217
+ "verify_mode": "CERT_REQUIRED",
218
+ "check_hostname": true,
219
+ "check_expiry": true
220
+ },
221
+ "client": {
222
+ "timeout": 30.0
223
+ }
224
+ }
225
+ """
226
+
227
+ config_file = "sample_config.json"
228
+ try:
229
+ with open(config_file, "w") as f:
230
+ f.write(config_content)
231
+
232
+ client = create_client_from_config(config_file)
233
+ print(f"Client from config file: {client.base_url}:{client.port}")
234
+ print(f" SSL enabled: {client.is_ssl_enabled()}")
235
+ print(f" Authenticated: {client.is_authenticated()}")
236
+ print(f" Auth method: {client.get_auth_method()}")
237
+ print()
238
+ finally:
239
+ # Clean up config file
240
+ if os.path.exists(config_file):
241
+ os.remove(config_file)
242
+
243
+
244
+ async def demonstrate_ssl_configuration():
245
+ """Demonstrate SSL/TLS configuration options."""
246
+ print("=== SSL/TLS Configuration Examples ===")
247
+
248
+ # HTTPS with custom CA certificate
249
+ client = create_client(
250
+ "https://localhost",
251
+ 8001,
252
+ ca_cert_file="custom_ca.pem",
253
+ verify_mode="CERT_REQUIRED",
254
+ check_hostname=True,
255
+ )
256
+ print(f"HTTPS with custom CA: {client.base_url}:{client.port}")
257
+ print(f" SSL enabled: {client.is_ssl_enabled()}")
258
+ if client.is_ssl_enabled():
259
+ ssl_config = client.get_ssl_config()
260
+ print(f" SSL config: {ssl_config}")
261
+ print()
262
+
263
+ # HTTPS with disabled hostname checking
264
+ client = create_client(
265
+ "https://localhost", 8001, verify_mode="CERT_REQUIRED", check_hostname=False
266
+ )
267
+ print(f"HTTPS with disabled hostname check: {client.base_url}:{client.port}")
268
+ print(f" SSL enabled: {client.is_ssl_enabled()}")
269
+ if client.is_ssl_enabled():
270
+ ssl_config = client.get_ssl_config()
271
+ print(f" SSL config: {ssl_config}")
272
+ print()
273
+
274
+ # mTLS with custom SSL settings
275
+ client = create_client(
276
+ "https://localhost",
277
+ 8001,
278
+ cert_file="client_cert.pem",
279
+ key_file="client_key.pem",
280
+ ca_cert_file="ca_cert.pem",
281
+ verify_mode="CERT_REQUIRED",
282
+ check_hostname=True,
283
+ check_expiry=True,
284
+ )
285
+ print(f"mTLS with custom SSL settings: {client.base_url}:{client.port}")
286
+ print(f" SSL enabled: {client.is_ssl_enabled()}")
287
+ print(f" mTLS enabled: {client.is_mtls_enabled()}")
288
+ if client.is_ssl_enabled():
289
+ ssl_config = client.get_ssl_config()
290
+ print(f" SSL config: {ssl_config}")
291
+ print()
292
+
293
+
294
+ async def demonstrate_authentication_methods():
295
+ """Demonstrate different authentication methods."""
296
+ print("=== Authentication Methods Examples ===")
297
+
298
+ # API Key authentication
299
+ client = create_client(
300
+ "https://localhost",
301
+ 8001,
302
+ auth_method="api_key",
303
+ api_key="test_api_key",
304
+ api_key_header="X-API-Key",
305
+ )
306
+ print(f"API Key auth: {client.get_auth_method()}")
307
+ if client.is_authenticated():
308
+ headers = client.get_auth_headers()
309
+ print(f" Auth headers: {headers}")
310
+ print()
311
+
312
+ # JWT authentication
313
+ client = create_client(
314
+ "https://localhost",
315
+ 8001,
316
+ auth_method="jwt",
317
+ jwt_secret="jwt_secret_key",
318
+ jwt_username="jwt_user",
319
+ jwt_password="jwt_password",
320
+ jwt_expiry=3600,
321
+ )
322
+ print(f"JWT auth: {client.get_auth_method()}")
323
+ if client.is_authenticated():
324
+ headers = client.get_auth_headers()
325
+ print(f" Auth headers: {headers}")
326
+ print()
327
+
328
+ # Basic authentication
329
+ client = create_client(
330
+ "https://localhost",
331
+ 8001,
332
+ auth_method="basic",
333
+ username="basic_user",
334
+ password="basic_password",
335
+ )
336
+ print(f"Basic auth: {client.get_auth_method()}")
337
+ if client.is_authenticated():
338
+ headers = client.get_auth_headers()
339
+ print(f" Auth headers: {headers}")
340
+ print()
341
+
342
+ # Certificate authentication
343
+ client = create_client(
344
+ "https://localhost",
345
+ 8001,
346
+ auth_method="certificate",
347
+ cert_file="auth_cert.pem",
348
+ key_file="auth_key.pem",
349
+ )
350
+ print(f"Certificate auth: {client.get_auth_method()}")
351
+ if client.is_authenticated():
352
+ headers = client.get_auth_headers()
353
+ print(f" Auth headers: {headers}")
354
+ print()
355
+
356
+
357
+ async def main():
358
+ """Run all demonstration examples."""
359
+ print("Client Factory Examples")
360
+ print("=" * 50)
361
+ print()
362
+
363
+ await demonstrate_security_mode_detection()
364
+ await demonstrate_automatic_client_creation()
365
+ await demonstrate_specific_client_creation()
366
+ await demonstrate_environment_based_creation()
367
+ await demonstrate_config_file_creation()
368
+ await demonstrate_ssl_configuration()
369
+ await demonstrate_authentication_methods()
370
+
371
+ print("All examples completed successfully!")
372
+
373
+
374
+ if __name__ == "__main__":
375
+ asyncio.run(main())
@@ -0,0 +1,59 @@
1
+ """
2
+ Helper functions for ClientFactory.
3
+
4
+ Author: Vasiliy Zdanovskiy
5
+ email: vasilyvz@gmail.com
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ from typing import Optional
11
+
12
+ from embed_client.client_factory import ClientFactory, SecurityMode
13
+ from embed_client.embedding_client import EmbeddingClient
14
+
15
+
16
+ def create_client(
17
+ base_url: str,
18
+ port: int = 8001,
19
+ auth_method: Optional[str] = None,
20
+ ssl_enabled: Optional[bool] = None,
21
+ **kwargs,
22
+ ) -> EmbeddingClient:
23
+ """Create a client with automatic security mode detection."""
24
+ return ClientFactory.create_client(
25
+ base_url, port, auth_method, ssl_enabled, **kwargs
26
+ )
27
+
28
+
29
+ def create_client_from_config(config_path: str) -> EmbeddingClient:
30
+ """Create client from configuration file."""
31
+ return ClientFactory.from_config_file(config_path)
32
+
33
+
34
+ def create_client_from_env() -> EmbeddingClient:
35
+ """Create client from environment variables."""
36
+ return ClientFactory.from_environment()
37
+
38
+
39
+ def detect_security_mode(
40
+ base_url: str,
41
+ auth_method: Optional[str] = None,
42
+ ssl_enabled: Optional[bool] = None,
43
+ cert_file: Optional[str] = None,
44
+ key_file: Optional[str] = None,
45
+ **kwargs,
46
+ ) -> str:
47
+ """Detect security mode based on provided parameters."""
48
+ return ClientFactory.detect_security_mode(
49
+ base_url, auth_method, ssl_enabled, cert_file, key_file, **kwargs
50
+ )
51
+
52
+
53
+ __all__ = [
54
+ "create_client",
55
+ "create_client_from_config",
56
+ "create_client_from_env",
57
+ "detect_security_mode",
58
+ "SecurityMode",
59
+ ]