-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathclient_wrapper.py
More file actions
155 lines (137 loc) · 5.59 KB
/
Copy pathclient_wrapper.py
File metadata and controls
155 lines (137 loc) · 5.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# This file was auto-generated by Fern from our API Definition.
import typing
import httpx
from .http_client import AsyncHttpClient, HttpClient
from .logging import LogConfig, Logger
class BaseClientWrapper:
def __init__(
self,
*,
client_name: typing.Optional[str] = None,
token: typing.Union[str, typing.Callable[[], str]],
headers: typing.Optional[typing.Dict[str, str]] = None,
base_url: str,
timeout: typing.Optional[float] = None,
max_retries: int = 2,
stream_reconnection_enabled: typing.Optional[bool] = None,
max_stream_reconnection_attempts: typing.Optional[int] = None,
logging: typing.Optional[typing.Union[LogConfig, Logger]] = None,
):
self._client_name = client_name
self._token = token
self._headers = headers
self._base_url = base_url
self._timeout = timeout
self._max_retries = max_retries
self._stream_reconnection_enabled = stream_reconnection_enabled
self._max_stream_reconnection_attempts = max_stream_reconnection_attempts
self._logging = logging
def get_headers(self) -> typing.Dict[str, str]:
import platform
headers: typing.Dict[str, str] = {
"User-Agent": "cohere/7.1.1",
"X-Fern-Language": "Python",
"X-Fern-Runtime": f"python/{platform.python_version()}",
"X-Fern-Platform": f"{platform.system().lower()}/{platform.release()}",
"X-Fern-SDK-Name": "cohere",
"X-Fern-SDK-Version": "7.1.1",
**(self.get_custom_headers() or {}),
}
if self._client_name is not None:
headers["X-Client-Name"] = self._client_name
headers["Authorization"] = f"Bearer {self._get_token()}"
return headers
def _get_token(self) -> str:
if isinstance(self._token, str):
return self._token
else:
return self._token()
def get_custom_headers(self) -> typing.Optional[typing.Dict[str, str]]:
return self._headers
def get_base_url(self) -> str:
return self._base_url
def get_timeout(self) -> typing.Optional[float]:
return self._timeout
def get_max_retries(self) -> int:
return self._max_retries
def get_stream_reconnection_enabled(self) -> bool:
return self._stream_reconnection_enabled if self._stream_reconnection_enabled is not None else True
def get_max_stream_reconnection_attempts(self) -> typing.Optional[int]:
return self._max_stream_reconnection_attempts
class SyncClientWrapper(BaseClientWrapper):
def __init__(
self,
*,
client_name: typing.Optional[str] = None,
token: typing.Union[str, typing.Callable[[], str]],
headers: typing.Optional[typing.Dict[str, str]] = None,
base_url: str,
timeout: typing.Optional[float] = None,
max_retries: int = 2,
stream_reconnection_enabled: typing.Optional[bool] = None,
max_stream_reconnection_attempts: typing.Optional[int] = None,
logging: typing.Optional[typing.Union[LogConfig, Logger]] = None,
httpx_client: httpx.Client,
):
super().__init__(
client_name=client_name,
token=token,
headers=headers,
base_url=base_url,
timeout=timeout,
max_retries=max_retries,
stream_reconnection_enabled=stream_reconnection_enabled,
max_stream_reconnection_attempts=max_stream_reconnection_attempts,
logging=logging,
)
self.httpx_client = HttpClient(
httpx_client=httpx_client,
base_headers=self.get_headers,
base_timeout=self.get_timeout,
base_url=self.get_base_url,
base_max_retries=self.get_max_retries(),
logging_config=self._logging,
)
class AsyncClientWrapper(BaseClientWrapper):
def __init__(
self,
*,
client_name: typing.Optional[str] = None,
token: typing.Union[str, typing.Callable[[], str]],
headers: typing.Optional[typing.Dict[str, str]] = None,
base_url: str,
timeout: typing.Optional[float] = None,
max_retries: int = 2,
stream_reconnection_enabled: typing.Optional[bool] = None,
max_stream_reconnection_attempts: typing.Optional[int] = None,
logging: typing.Optional[typing.Union[LogConfig, Logger]] = None,
async_token: typing.Optional[typing.Callable[[], typing.Awaitable[str]]] = None,
httpx_client: httpx.AsyncClient,
):
super().__init__(
client_name=client_name,
token=token,
headers=headers,
base_url=base_url,
timeout=timeout,
max_retries=max_retries,
stream_reconnection_enabled=stream_reconnection_enabled,
max_stream_reconnection_attempts=max_stream_reconnection_attempts,
logging=logging,
)
self._async_token = async_token
self.httpx_client = AsyncHttpClient(
httpx_client=httpx_client,
base_headers=self.get_headers,
base_timeout=self.get_timeout,
base_url=self.get_base_url,
base_max_retries=self.get_max_retries(),
async_base_headers=self.async_get_headers,
logging_config=self._logging,
)
async def async_get_headers(self) -> typing.Dict[str, str]:
headers = self.get_headers()
if self._async_token is not None:
token = await self._async_token()
headers["Authorization"] = f"Bearer {token}"
return headers