Improve token flow

This commit is contained in:
grym 2023-02-10 20:59:20 -05:00
parent 3eead1a769
commit fb54971d14
1 changed files with 23 additions and 17 deletions

40
oxo.py
View File

@ -56,26 +56,32 @@ def post_to(
for d in data:
if isinstance(d.get("file"), io.BufferedReader):
res = client.post(base_url, files=d, data=data_dict)
token = res.headers.get("x-token")
remote_url = res.text.strip()
if token:
token_data = TokenData(
token=token,
oxo_url=remote_url,
)
err_console.print(f"To remove post, {token_data.curl_rm}")
err_console.print(
f"To update expiration date, {token_data.curl_expiry}"
)
if token_dir:
fname = Path(d.get("file").name).name
token_dir.joinpath(f"{fname}.token").write_text(
json.dumps(asdict(token_data))
)
else:
res = client.post(base_url, data={**d, **data_dict})
remote_url = res.text.strip()
res.raise_for_status()
remote_url = res.text.strip()
token = res.headers.get("x-token")
if token:
token_data = TokenData(
token=token,
oxo_url=remote_url,
)
err_console.print(f"To remove post, {token_data.curl_rm}")
err_console.print(
f"To update expiration date, {token_data.curl_expiry}"
)
if token_dir:
# todo rewrite when drop 3.7
if d.get("url"):
fname = d.get("url").rpartition("/")[2]
elif d.get("file").name:
fname = Path(d.get("file").name).name
else:
raise ValueError(f"{d}")
token_dir.joinpath(f"{fname}.token").write_text(
json.dumps(asdict(token_data))
)
retval.append(remote_url)
return " ".join(retval)