|
2 | 2 | import httpx |
3 | 3 | from httpx import HTTPError, USE_CLIENT_DEFAULT |
4 | 4 | import json |
5 | | -from typing import Dict, List, Union |
| 5 | +from typing import Dict, List, Union, Optional |
6 | 6 | import os |
7 | 7 |
|
| 8 | +from onepasswordconnectsdk.async_client import AsyncClient |
| 9 | +from onepasswordconnectsdk.config import ClientConfig |
8 | 10 | from onepasswordconnectsdk.serializer import Serializer |
9 | 11 | from onepasswordconnectsdk.utils import build_headers, is_valid_uuid, PathBuilder, get_timeout |
10 | 12 | from onepasswordconnectsdk.errors import ( |
|
22 | 24 | class Client: |
23 | 25 | """Python Client Class""" |
24 | 26 |
|
25 | | - def __init__(self, config: 'ClientConfig') -> None: |
26 | | - """Initialize client |
27 | | - |
28 | | - Args: |
29 | | - config: ClientConfig instance containing all client configuration |
30 | | - """ |
31 | | - self.url = config.url |
32 | | - self.token = config.token |
33 | | - self.session = self.create_session(config) |
| 27 | + def __init__(self, url: str, token: str) -> None: |
| 28 | + """Initialize client""" |
| 29 | + self.url = url |
| 30 | + self.token = token |
| 31 | + self.session = self.create_session(url, token) |
34 | 32 | self.serializer = Serializer() |
35 | 33 |
|
36 | | - def create_session(self, config: 'ClientConfig') -> httpx.Client: |
37 | | - """Create an HTTP session using the provided configuration""" |
38 | | - # Update headers in the existing config |
39 | | - headers = config.headers or {} |
40 | | - headers.update(self.build_headers(config.token)) |
41 | | - config.headers = headers |
42 | | - return config |
| 34 | + def create_session(self, url: str, token: str) -> httpx.Client: |
| 35 | + return httpx.Client(base_url=url, headers=self.build_headers(token), timeout=get_timeout()) |
43 | 36 |
|
44 | 37 | def build_headers(self, token: str) -> Dict[str, str]: |
45 | 38 | return build_headers(token) |
@@ -388,20 +381,19 @@ def sanitize_for_serialization(self, obj): |
388 | 381 | return self.serializer.sanitize_for_serialization(obj) |
389 | 382 |
|
390 | 383 |
|
391 | | -def new_client(url: str, token: str, **kwargs) -> Client: |
| 384 | +def new_client(url: str, token: str, is_async: bool = False) -> Union[AsyncClient, Client]: |
392 | 385 | """Builds a new client for interacting with 1Password Connect |
393 | | - |
394 | | - Args: |
395 | | - url: The url of the 1Password Connect API |
396 | | - token: The 1Password Service Account token |
397 | | - **kwargs: Additional httpx.Client configuration options |
| 386 | + Parameters: |
| 387 | + url: The url of the 1Password Connect API |
| 388 | + token: The 1Password Service Account token |
| 389 | + is_async: Initialize async or sync client |
398 | 390 |
|
399 | 391 | Returns: |
400 | | - Client: The 1Password Connect client |
| 392 | + Client: The 1Password Connect client |
401 | 393 | """ |
402 | | - from onepasswordconnectsdk.config import ClientConfig |
403 | | - config = ClientConfig(url=url, token=token, **kwargs) |
404 | | - return Client(config) |
| 394 | + if is_async: |
| 395 | + return AsyncClient(url, token) |
| 396 | + return Client(url, token) |
405 | 397 |
|
406 | 398 |
|
407 | 399 | def new_client_from_environment(url: str = None, **kwargs) -> Client: |
|
0 commit comments