start of refactor

Signed-off-by: Jess Frazelle <github@jessfraz.com>
This commit is contained in:
Jess Frazelle
2022-02-27 18:39:04 -08:00
parent e7aaaab78d
commit 2a3cec9aac
10 changed files with 817 additions and 72 deletions

View File

@ -7,8 +7,9 @@ import attr
@attr.s(auto_attribs=True)
class Client:
"""A class for keeping track of data related to the API"""
"""A Client which has been authenticated for use on secured endpoints of the KittyCAD API."""
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)
@ -17,7 +18,7 @@ class Client:
def get_headers(self) -> Dict[str, str]:
"""Get headers to be used in all endpoints"""
return {**self.headers}
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"""
@ -39,17 +40,7 @@ class Client:
@attr.s(auto_attribs=True)
class AuthenticatedClient(Client):
"""A Client which has been authenticated for use on secured endpoints"""
token: str = attr.ib(kw_only=True)
def get_headers(self) -> Dict[str, str]:
"""Get headers to be used in authenticated endpoints"""
return {"Authorization": f"Bearer {self.token}", **self.headers}
@attr.s(auto_attribs=True)
class AuthenticatedClientFromEnv(Client):
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."""
token: str = attr.ib(default=os.getenv('KITTYCAD_API_TOKEN'))