|
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 | 8 | from onepasswordconnectsdk.async_client import AsyncClient
|
| 9 | +from onepasswordconnectsdk.config import ClientConfig |
9 | 10 | from onepasswordconnectsdk.serializer import Serializer
|
10 | 11 | from onepasswordconnectsdk.utils import build_headers, is_valid_uuid, PathBuilder, get_timeout
|
11 | 12 | from onepasswordconnectsdk.errors import (
|
|
24 | 25 | class Client:
|
25 | 26 | """Python Client Class"""
|
26 | 27 |
|
27 |
| - def __init__(self, url: str, token: str) -> None: |
28 |
| - """Initialize client""" |
| 28 | + def __init__(self, url: str, token: str, config: Optional[ClientConfig] = None) -> None: |
| 29 | + """Initialize client |
| 30 | + |
| 31 | + Args: |
| 32 | + url (str): The url of the 1Password Connect API |
| 33 | + token (str): The 1Password Service Account token |
| 34 | + config (Optional[ClientConfig]): Optional configuration for httpx client |
| 35 | + """ |
29 | 36 | self.url = url
|
30 | 37 | self.token = token
|
| 38 | + self.config = config |
31 | 39 | self.session = self.create_session(url, token)
|
32 | 40 | self.serializer = Serializer()
|
33 | 41 |
|
34 | 42 | 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 | + headers = self.build_headers(token) |
| 44 | + timeout = get_timeout() |
| 45 | + |
| 46 | + if self.config: |
| 47 | + client_args = self.config.get_client_args(url, headers, timeout) |
| 48 | + return httpx.Client(**client_args) |
| 49 | + |
| 50 | + return httpx.Client(base_url=url, headers=headers, timeout=timeout) |
36 | 51 |
|
37 | 52 | def build_headers(self, token: str) -> Dict[str, str]:
|
38 | 53 | return build_headers(token)
|
@@ -381,19 +396,21 @@ def sanitize_for_serialization(self, obj):
|
381 | 396 | return self.serializer.sanitize_for_serialization(obj)
|
382 | 397 |
|
383 | 398 |
|
384 |
| -def new_client(url: str, token: str, is_async: bool = False) -> Union[AsyncClient, Client]: |
| 399 | +def new_client(url: str, token: str, is_async: bool = False, config: Optional[ClientConfig] = None) -> Union[AsyncClient, Client]: |
385 | 400 | """Builds a new client for interacting with 1Password Connect
|
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 |
390 |
| -
|
| 401 | + |
| 402 | + Args: |
| 403 | + url (str): The url of the 1Password Connect API |
| 404 | + token (str): The 1Password Service Account token |
| 405 | + is_async (bool): Initialize async or sync client |
| 406 | + config (Optional[ClientConfig]): Optional configuration for httpx client |
| 407 | + |
391 | 408 | Returns:
|
392 |
| - Client: The 1Password Connect client |
| 409 | + Union[AsyncClient, Client]: The 1Password Connect client |
393 | 410 | """
|
394 | 411 | if is_async:
|
395 |
| - return AsyncClient(url, token) |
396 |
| - return Client(url, token) |
| 412 | + return AsyncClient(url, token, config) |
| 413 | + return Client(url, token, config) |
397 | 414 |
|
398 | 415 |
|
399 | 416 | def new_client_from_environment(url: str = None) -> Union[AsyncClient, Client]:
|
|
0 commit comments