* bump Signed-off-by: Jess Frazelle <github@jessfraz.com> * some fixes Signed-off-by: Jess Frazelle <github@jessfraz.com> * YOYO NEW API SPEC! * reformat Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * fixups Signed-off-by: Jess Frazelle <github@jessfraz.com> * for now force true Signed-off-by: Jess Frazelle <github@jessfraz.com> * run the tests on generations Signed-off-by: Jess Frazelle <github@jessfraz.com> * add tests Signed-off-by: Jess Frazelle <github@jessfraz.com> * update Signed-off-by: Jess Frazelle <github@jessfraz.com> * update Signed-off-by: Jess Frazelle <github@jessfraz.com> * update Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * update Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix some types Signed-off-by: Jess Frazelle <github@jessfraz.com> * float to top Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix mypy Signed-off-by: Jess Frazelle <github@jessfraz.com> * more noqa Signed-off-by: Jess Frazelle <github@jessfraz.com> * fixups Signed-off-by: Jess Frazelle <github@jessfraz.com> * ruff pass Signed-off-by: Jess Frazelle <github@jessfraz.com> * add docs Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix Signed-off-by: Jess Frazelle <github@jessfraz.com> * fixes Signed-off-by: Jess Frazelle <github@jessfraz.com> * even less mypy errors Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * add test Signed-off-by: Jess Frazelle <github@jessfraz.com> * fixes Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * fixes Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * fixups Signed-off-by: Jess Frazelle <github@jessfraz.com> * fixes Signed-off-by: Jess Frazelle <github@jessfraz.com> * cleanup Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix Signed-off-by: Jess Frazelle <github@jessfraz.com> * new path Signed-off-by: Jess Frazelle <github@jessfraz.com> * fixes for mypy Signed-off-by: Jess Frazelle <github@jessfraz.com> * skip tests Signed-off-by: Jess Frazelle <github@jessfraz.com> --------- Signed-off-by: Jess Frazelle <github@jessfraz.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
61 lines
2.2 KiB
Python
61 lines
2.2 KiB
Python
import os
|
|
import ssl
|
|
from typing import Dict, Optional, Union
|
|
|
|
import attr
|
|
|
|
|
|
@attr.s(auto_attribs=True)
|
|
class Client:
|
|
"""A Client which has been authenticated for use on secured endpoints of the KittyCAD API.""" # noqa: E501
|
|
|
|
token: str = attr.ib(kw_only=True)
|
|
base_url: str = attr.ib(default="https://api.kittycad.io")
|
|
cookies: Dict[str, str] = attr.ib(factory=dict, kw_only=True)
|
|
headers: Dict[str, str] = attr.ib(factory=dict, kw_only=True)
|
|
timeout: float = attr.ib(50.0, kw_only=True)
|
|
verify_ssl: Union[str, bool, ssl.SSLContext] = attr.ib(True, kw_only=True)
|
|
|
|
def get_headers(self) -> Dict[str, str]:
|
|
"""Get headers to be used in all endpoints"""
|
|
return {"Authorization": f"Bearer {self.token}", **self.headers}
|
|
|
|
def with_headers(self, headers: Dict[str, str]) -> "Client":
|
|
"""Get a new client matching this one with additional headers"""
|
|
return attr.evolve(self, headers={**self.headers, **headers})
|
|
|
|
def get_cookies(self) -> Dict[str, str]:
|
|
return {**self.cookies}
|
|
|
|
def with_cookies(self, cookies: Dict[str, str]) -> "Client":
|
|
"""Get a new client matching this one with additional cookies"""
|
|
return attr.evolve(self, cookies={**self.cookies, **cookies})
|
|
|
|
def get_timeout(self) -> float:
|
|
return self.timeout
|
|
|
|
def with_timeout(self, timeout: float) -> "Client":
|
|
"""Get a new client matching this one with a new timeout (in seconds)"""
|
|
return attr.evolve(self, timeout=timeout)
|
|
|
|
|
|
@attr.s(auto_attribs=True)
|
|
class ClientFromEnv(Client):
|
|
"""A Client which has been authenticated for use on secured endpoints that uses the KITTYCAD_API_TOKEN environment variable for the authentication token.""" # noqa: E501
|
|
|
|
token: str = attr.field()
|
|
|
|
@token.default
|
|
def set_token(self):
|
|
maybe_token: Optional[str] = os.getenv("KITTYCAD_API_TOKEN")
|
|
if maybe_token is None:
|
|
raise ValueError(
|
|
"KITTYCAD_API_TOKEN environment variable must be set to use ClientFromEnv"
|
|
)
|
|
token: str = maybe_token
|
|
return token
|
|
|
|
def get_headers(self) -> Dict[str, str]:
|
|
"""Get headers to be used in authenticated endpoints"""
|
|
return {"Authorization": f"Bearer {self.token}", **self.headers}
|