embed-client 2.0.0.0__py3-none-any.whl → 3.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.
@@ -0,0 +1,353 @@
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, SecurityMode, create_client, create_client_from_config,
15
+ create_client_from_env, detect_security_mode
16
+ )
17
+ from embed_client.async_client import EmbeddingServiceAsyncClient
18
+
19
+
20
+ async def demonstrate_security_mode_detection():
21
+ """Demonstrate automatic security mode detection."""
22
+ print("=== Security Mode Detection Examples ===")
23
+
24
+ # HTTP mode
25
+ mode = detect_security_mode("http://localhost")
26
+ print(f"http://localhost -> {mode}")
27
+
28
+ # HTTP + Token mode
29
+ mode = detect_security_mode("http://localhost", auth_method="api_key")
30
+ print(f"http://localhost + api_key -> {mode}")
31
+
32
+ # HTTPS mode
33
+ mode = detect_security_mode("https://localhost")
34
+ print(f"https://localhost -> {mode}")
35
+
36
+ # HTTPS + Token mode
37
+ mode = detect_security_mode("https://localhost", auth_method="api_key")
38
+ print(f"https://localhost + api_key -> {mode}")
39
+
40
+ # mTLS mode
41
+ mode = detect_security_mode("https://localhost", cert_file="cert.pem", key_file="key.pem")
42
+ print(f"https://localhost + client certs -> {mode}")
43
+
44
+ # mTLS + Roles mode
45
+ mode = detect_security_mode("https://localhost", cert_file="cert.pem", key_file="key.pem", roles=["admin"])
46
+ print(f"https://localhost + client certs + roles -> {mode}")
47
+
48
+ print()
49
+
50
+
51
+ async def demonstrate_automatic_client_creation():
52
+ """Demonstrate automatic client creation with mode detection."""
53
+ print("=== Automatic Client Creation Examples ===")
54
+
55
+ # HTTP client
56
+ client = create_client("http://localhost", 8001)
57
+ print(f"HTTP client created: {client.base_url}:{client.port}")
58
+ print(f" SSL enabled: {client.is_ssl_enabled()}")
59
+ print(f" Authenticated: {client.is_authenticated()}")
60
+ print()
61
+
62
+ # HTTPS client
63
+ client = create_client("https://localhost", 8001)
64
+ print(f"HTTPS client created: {client.base_url}:{client.port}")
65
+ print(f" SSL enabled: {client.is_ssl_enabled()}")
66
+ print(f" Authenticated: {client.is_authenticated()}")
67
+ print()
68
+
69
+ # HTTP + API Key client
70
+ client = create_client("http://localhost", 8001, auth_method="api_key", api_key="test_key")
71
+ print(f"HTTP + API Key client created: {client.base_url}:{client.port}")
72
+ print(f" SSL enabled: {client.is_ssl_enabled()}")
73
+ print(f" Authenticated: {client.is_authenticated()}")
74
+ print(f" Auth method: {client.get_auth_method()}")
75
+ print()
76
+
77
+ # HTTPS + JWT client
78
+ client = create_client(
79
+ "https://localhost", 8001,
80
+ auth_method="jwt",
81
+ jwt_secret="secret",
82
+ jwt_username="user",
83
+ jwt_password="pass"
84
+ )
85
+ print(f"HTTPS + JWT client created: {client.base_url}:{client.port}")
86
+ print(f" SSL enabled: {client.is_ssl_enabled()}")
87
+ print(f" Authenticated: {client.is_authenticated()}")
88
+ print(f" Auth method: {client.get_auth_method()}")
89
+ print()
90
+
91
+ # mTLS client
92
+ client = create_client(
93
+ "https://localhost", 8001,
94
+ cert_file="client_cert.pem",
95
+ key_file="client_key.pem"
96
+ )
97
+ print(f"mTLS client created: {client.base_url}:{client.port}")
98
+ print(f" SSL enabled: {client.is_ssl_enabled()}")
99
+ print(f" mTLS enabled: {client.is_mtls_enabled()}")
100
+ print(f" Authenticated: {client.is_authenticated()}")
101
+ print()
102
+
103
+
104
+ async def demonstrate_specific_client_creation():
105
+ """Demonstrate creating clients for specific security modes."""
106
+ print("=== Specific Client Creation Examples ===")
107
+
108
+ # HTTP client
109
+ client = ClientFactory.create_http_client("http://localhost", 8001)
110
+ print(f"HTTP client: {client.base_url}:{client.port}")
111
+ print()
112
+
113
+ # HTTP + Token client
114
+ client = ClientFactory.create_http_token_client(
115
+ "http://localhost", 8001, "api_key", api_key="test_key"
116
+ )
117
+ print(f"HTTP + Token client: {client.base_url}:{client.port}")
118
+ print(f" Auth method: {client.get_auth_method()}")
119
+ print()
120
+
121
+ # HTTPS client
122
+ client = ClientFactory.create_https_client("https://localhost", 8001)
123
+ print(f"HTTPS client: {client.base_url}:{client.port}")
124
+ print(f" SSL enabled: {client.is_ssl_enabled()}")
125
+ print()
126
+
127
+ # HTTPS + Token client
128
+ client = ClientFactory.create_https_token_client(
129
+ "https://localhost", 8001, "basic", username="user", password="pass"
130
+ )
131
+ print(f"HTTPS + Token client: {client.base_url}:{client.port}")
132
+ print(f" SSL enabled: {client.is_ssl_enabled()}")
133
+ print(f" Auth method: {client.get_auth_method()}")
134
+ print()
135
+
136
+ # mTLS client
137
+ client = ClientFactory.create_mtls_client(
138
+ "https://localhost", "client_cert.pem", "client_key.pem", 8001
139
+ )
140
+ print(f"mTLS client: {client.base_url}:{client.port}")
141
+ print(f" SSL enabled: {client.is_ssl_enabled()}")
142
+ print(f" mTLS enabled: {client.is_mtls_enabled()}")
143
+ print()
144
+
145
+ # mTLS + Roles client
146
+ client = ClientFactory.create_mtls_roles_client(
147
+ "https://localhost", "client_cert.pem", "client_key.pem", 8001,
148
+ roles=["admin", "user"], role_attributes={"department": "IT"}
149
+ )
150
+ print(f"mTLS + Roles 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
+
156
+ async def demonstrate_environment_based_creation():
157
+ """Demonstrate creating clients from environment variables."""
158
+ print("=== Environment-Based Client Creation ===")
159
+
160
+ # Set environment variables for demonstration
161
+ os.environ["EMBED_CLIENT_BASE_URL"] = "https://example.com"
162
+ os.environ["EMBED_CLIENT_PORT"] = "9443"
163
+ os.environ["EMBED_CLIENT_AUTH_METHOD"] = "api_key"
164
+ os.environ["EMBED_CLIENT_API_KEY"] = "env_test_key"
165
+
166
+ try:
167
+ client = create_client_from_env()
168
+ print(f"Client from environment: {client.base_url}:{client.port}")
169
+ print(f" SSL enabled: {client.is_ssl_enabled()}")
170
+ print(f" Authenticated: {client.is_authenticated()}")
171
+ print(f" Auth method: {client.get_auth_method()}")
172
+ print()
173
+ finally:
174
+ # Clean up environment variables
175
+ for key in ["EMBED_CLIENT_BASE_URL", "EMBED_CLIENT_PORT",
176
+ "EMBED_CLIENT_AUTH_METHOD", "EMBED_CLIENT_API_KEY"]:
177
+ os.environ.pop(key, None)
178
+
179
+
180
+ async def demonstrate_config_file_creation():
181
+ """Demonstrate creating clients from configuration files."""
182
+ print("=== Configuration File Client Creation ===")
183
+
184
+ # Create a sample configuration file
185
+ config_content = """
186
+ {
187
+ "server": {
188
+ "host": "https://secure.example.com",
189
+ "port": 9443
190
+ },
191
+ "auth": {
192
+ "method": "api_key",
193
+ "api_keys": {
194
+ "user": "config_test_key"
195
+ }
196
+ },
197
+ "ssl": {
198
+ "enabled": true,
199
+ "verify_mode": "CERT_REQUIRED",
200
+ "check_hostname": true,
201
+ "check_expiry": true
202
+ },
203
+ "client": {
204
+ "timeout": 30.0
205
+ }
206
+ }
207
+ """
208
+
209
+ config_file = "sample_config.json"
210
+ try:
211
+ with open(config_file, "w") as f:
212
+ f.write(config_content)
213
+
214
+ client = create_client_from_config(config_file)
215
+ print(f"Client from config file: {client.base_url}:{client.port}")
216
+ print(f" SSL enabled: {client.is_ssl_enabled()}")
217
+ print(f" Authenticated: {client.is_authenticated()}")
218
+ print(f" Auth method: {client.get_auth_method()}")
219
+ print()
220
+ finally:
221
+ # Clean up config file
222
+ if os.path.exists(config_file):
223
+ os.remove(config_file)
224
+
225
+
226
+ async def demonstrate_ssl_configuration():
227
+ """Demonstrate SSL/TLS configuration options."""
228
+ print("=== SSL/TLS Configuration Examples ===")
229
+
230
+ # HTTPS with custom CA certificate
231
+ client = create_client(
232
+ "https://localhost", 8001,
233
+ ca_cert_file="custom_ca.pem",
234
+ verify_mode="CERT_REQUIRED",
235
+ check_hostname=True
236
+ )
237
+ print(f"HTTPS with custom CA: {client.base_url}:{client.port}")
238
+ print(f" SSL enabled: {client.is_ssl_enabled()}")
239
+ if client.is_ssl_enabled():
240
+ ssl_config = client.get_ssl_config()
241
+ print(f" SSL config: {ssl_config}")
242
+ print()
243
+
244
+ # HTTPS with disabled hostname checking
245
+ client = create_client(
246
+ "https://localhost", 8001,
247
+ verify_mode="CERT_REQUIRED",
248
+ check_hostname=False
249
+ )
250
+ print(f"HTTPS with disabled hostname check: {client.base_url}:{client.port}")
251
+ print(f" SSL enabled: {client.is_ssl_enabled()}")
252
+ if client.is_ssl_enabled():
253
+ ssl_config = client.get_ssl_config()
254
+ print(f" SSL config: {ssl_config}")
255
+ print()
256
+
257
+ # mTLS with custom SSL settings
258
+ client = create_client(
259
+ "https://localhost", 8001,
260
+ cert_file="client_cert.pem",
261
+ key_file="client_key.pem",
262
+ ca_cert_file="ca_cert.pem",
263
+ verify_mode="CERT_REQUIRED",
264
+ check_hostname=True,
265
+ check_expiry=True
266
+ )
267
+ print(f"mTLS with custom SSL settings: {client.base_url}:{client.port}")
268
+ print(f" SSL enabled: {client.is_ssl_enabled()}")
269
+ print(f" mTLS enabled: {client.is_mtls_enabled()}")
270
+ if client.is_ssl_enabled():
271
+ ssl_config = client.get_ssl_config()
272
+ print(f" SSL config: {ssl_config}")
273
+ print()
274
+
275
+
276
+ async def demonstrate_authentication_methods():
277
+ """Demonstrate different authentication methods."""
278
+ print("=== Authentication Methods Examples ===")
279
+
280
+ # API Key authentication
281
+ client = create_client(
282
+ "https://localhost", 8001,
283
+ auth_method="api_key",
284
+ api_key="test_api_key",
285
+ api_key_header="X-API-Key"
286
+ )
287
+ print(f"API Key auth: {client.get_auth_method()}")
288
+ if client.is_authenticated():
289
+ headers = client.get_auth_headers()
290
+ print(f" Auth headers: {headers}")
291
+ print()
292
+
293
+ # JWT authentication
294
+ client = create_client(
295
+ "https://localhost", 8001,
296
+ auth_method="jwt",
297
+ jwt_secret="jwt_secret_key",
298
+ jwt_username="jwt_user",
299
+ jwt_password="jwt_password",
300
+ jwt_expiry=3600
301
+ )
302
+ print(f"JWT auth: {client.get_auth_method()}")
303
+ if client.is_authenticated():
304
+ headers = client.get_auth_headers()
305
+ print(f" Auth headers: {headers}")
306
+ print()
307
+
308
+ # Basic authentication
309
+ client = create_client(
310
+ "https://localhost", 8001,
311
+ auth_method="basic",
312
+ username="basic_user",
313
+ password="basic_password"
314
+ )
315
+ print(f"Basic auth: {client.get_auth_method()}")
316
+ if client.is_authenticated():
317
+ headers = client.get_auth_headers()
318
+ print(f" Auth headers: {headers}")
319
+ print()
320
+
321
+ # Certificate authentication
322
+ client = create_client(
323
+ "https://localhost", 8001,
324
+ auth_method="certificate",
325
+ cert_file="auth_cert.pem",
326
+ key_file="auth_key.pem"
327
+ )
328
+ print(f"Certificate auth: {client.get_auth_method()}")
329
+ if client.is_authenticated():
330
+ headers = client.get_auth_headers()
331
+ print(f" Auth headers: {headers}")
332
+ print()
333
+
334
+
335
+ async def main():
336
+ """Run all demonstration examples."""
337
+ print("Client Factory Examples")
338
+ print("=" * 50)
339
+ print()
340
+
341
+ await demonstrate_security_mode_detection()
342
+ await demonstrate_automatic_client_creation()
343
+ await demonstrate_specific_client_creation()
344
+ await demonstrate_environment_based_creation()
345
+ await demonstrate_config_file_creation()
346
+ await demonstrate_ssl_configuration()
347
+ await demonstrate_authentication_methods()
348
+
349
+ print("All examples completed successfully!")
350
+
351
+
352
+ if __name__ == "__main__":
353
+ asyncio.run(main())