MCPcopy Create free account
hub / github.com/OpenPipe/OpenPipe / Client

Class Client

trainer/src/api_client/client.py:9–132  ·  view source on GitHub ↗

A class for keeping track of data related to the API The following are accepted as keyword arguments and will be used to construct httpx Clients internally: ``base_url``: The base URL for the API, all requests are made to a relative path to this URL ``cookies``: A dictionary o

Source from the content-addressed store, hash-verified

7
8@define
9class Client:
10 """A class for keeping track of data related to the API
11
12 The following are accepted as keyword arguments and will be used to construct httpx Clients internally:
13
14 ``base_url``: The base URL for the API, all requests are made to a relative path to this URL
15
16 ``cookies``: A dictionary of cookies to be sent with every request
17
18 ``headers``: A dictionary of headers to be sent with every request
19
20 ``timeout``: The maximum amount of a time a request can take. API functions will raise
21 httpx.TimeoutException if this is exceeded.
22
23 ``verify_ssl``: Whether or not to verify the SSL certificate of the API server. This should be True in production,
24 but can be set to False for testing purposes.
25
26 ``follow_redirects``: Whether or not to follow redirects. Default value is False.
27
28 ``httpx_args``: A dictionary of additional arguments to be passed to the ``httpx.Client`` and ``httpx.AsyncClient`` constructor.
29
30
31 Attributes:
32 raise_on_unexpected_status: Whether or not to raise an errors.UnexpectedStatus if the API returns a
33 status code that was not documented in the source OpenAPI document. Can also be provided as a keyword
34 argument to the constructor.
35 """
36
37 raise_on_unexpected_status: bool = field(default=False, kw_only=True)
38 _base_url: str
39 _cookies: Dict[str, str] = field(factory=dict, kw_only=True)
40 _headers: Dict[str, str] = field(factory=dict, kw_only=True)
41 _timeout: Optional[httpx.Timeout] = field(default=None, kw_only=True)
42 _verify_ssl: Union[str, bool, ssl.SSLContext] = field(default=True, kw_only=True)
43 _follow_redirects: bool = field(default=False, kw_only=True)
44 _httpx_args: Dict[str, Any] = field(factory=dict, kw_only=True)
45 _client: Optional[httpx.Client] = field(default=None, init=False)
46 _async_client: Optional[httpx.AsyncClient] = field(default=None, init=False)
47
48 def with_headers(self, headers: Dict[str, str]) -> "Client":
49 """Get a new client matching this one with additional headers"""
50 if self._client is not None:
51 self._client.headers.update(headers)
52 if self._async_client is not None:
53 self._async_client.headers.update(headers)
54 return evolve(self, headers={**self._headers, **headers})
55
56 def with_cookies(self, cookies: Dict[str, str]) -> "Client":
57 """Get a new client matching this one with additional cookies"""
58 if self._client is not None:
59 self._client.cookies.update(cookies)
60 if self._async_client is not None:
61 self._async_client.cookies.update(cookies)
62 return evolve(self, cookies={**self._cookies, **cookies})
63
64 def with_timeout(self, timeout: httpx.Timeout) -> "Client":
65 """Get a new client matching this one with a new timeout (in seconds)"""
66 if self._client is not None:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected