Skip to content

Commit

Permalink
Move the cookie parsing logic up into the Client
Browse files Browse the repository at this point in the history
  • Loading branch information
monoxgas committed Nov 15, 2024
1 parent 7cd9fa2 commit cd6385e
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions dreadnode_cli/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,20 @@ def __init__(
self,
base_url: str = PLATFORM_BASE_URL,
*,
cookies: httpx.Cookies | None = None,
cookies: dict[str, str] | None = None,
debug: bool = DEBUG,
):
_cookies = httpx.Cookies()
cookie_domain = urlparse(base_url).hostname
if cookie_domain is None:
raise Exception(f"Invalid URL: {base_url}")

if "localhost" == cookie_domain:
cookie_domain = "localhost.local"

for key, value in (cookies or {}).items():
_cookies.set(key, value, domain=cookie_domain)

self._base_url = base_url.rstrip("/")
self._client = httpx.Client(
cookies=cookies,
Expand Down Expand Up @@ -403,18 +414,7 @@ def create_client(*, profile: str | None = None) -> Client:
user_config = UserConfig.read()
config = user_config.get_server_config(profile)

cookie_domain = urlparse(config.url).hostname
if cookie_domain is None:
raise Exception(f"Invalid URL: {config.url}")

if "localhost" == cookie_domain:
cookie_domain = "localhost.local"

cookies = httpx.Cookies()
cookies.set("refresh_token", config.refresh_token, domain=cookie_domain)
cookies.set("access_token", config.access_token, domain=cookie_domain)

client = Client(config.url, cookies=cookies)
client = Client(config.url, cookies={"access_token": config.access_token, "refresh_token": config.refresh_token})

# Pre-emptively check if the token is expired
if Token(config.refresh_token).is_expired():
Expand Down

0 comments on commit cd6385e

Please sign in to comment.