-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.py
43 lines (37 loc) · 1.45 KB
/
auth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from tendrils import api, utils
from requests.exceptions import HTTPError
import warnings
def test_tendrils() -> bool:
"""
Test Tendrils for API token authorization.
"""
try:
_ = api.get_target(8) # type: ignore
return True
except HTTPError as err:
if "Unauthorized" in err.response.reason:
return False
raise err # This shouldn't happen. So raise it for debugging.
def update_api_token() -> None:
token = input("Tendrils needs your FLOWS API token. Please enter it:")
# We remove spaces from the token to avoid errors.
utils.set_api_token(token.strip().replace(" ", ""), overwrite=True)
print("Token set. If there is a problem, please run `get_brightest` again.")
def test_connection() -> None:
"""
Test connection to FLOWS API, and if faulty, prompt for a new token.
"""
# Test connection to flows:
if not test_tendrils():
# Tendrils >0.3.0 will query first if token is None
# But now we also query if connection fails after that too.
# Useful when token is set to something invalid.
update_api_token()
warnings.warn(
RuntimeWarning(
"Could not connect to flows via Tendrils. "
"Check your API key and that target exists."
"Also try Tendrils config steps from:"
"https://www.github.com/SNFlows/tendrils/#before-you-begin-important"
)
)