Skip to content

Commit

Permalink
fixing basic_auth; more error handling in get
Browse files Browse the repository at this point in the history
  • Loading branch information
CamDavidsonPilon committed Jul 3, 2023
1 parent 635927e commit ddb4c67
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pioreactor/mureq.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
def basic_auth(username: str, password: str):
# get(..., headers={ 'Authorization' : f'Basic {basic_auth(username, password)}'})
token = b64encode(f"{username}:{password}".encode("utf-8")).decode("ascii")
return {token}
return token


def request(method, url, *, read_limit=None, **kwargs) -> Response:
Expand Down
25 changes: 21 additions & 4 deletions pioreactor/whoami.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ def _get_latest_experiment_name() -> str:
from pioreactor.config import leader_address

retries = 10
exit_reason = ""

for attempt in range(retries):
try:
result = mureq.get(f"http://{leader_address}/api/experiments/latest")
Expand All @@ -47,16 +49,31 @@ def _get_latest_experiment_name() -> str:
except mureq.HTTPErrorStatus as e:
if e.status_code == 401:
# auth error, something is wrong
exit_reason = "auth"
break
except mureq.HTTPException:
exit_reason = "connection_refused"
except Exception:
# some network error? Keep trying
# some other error? Keep trying
pass
time.sleep(0.5 * attempt)
else:
exit_reason = "timeout"

logger = create_logger("pioreactor", experiment=UNIVERSAL_EXPERIMENT, to_mqtt=False)
logger.warning(
f"Not able to access latest experiment. Check http://{leader_address}/api/experiments/latest "
)

if exit_reason == "auth":
logger.warning(
f"Error in authentication to UI. Check http://{leader_address} and config.ini for api_key."
)
elif exit_reason == "timeout":
logger.warning(
f"Not able to access experiments in UI. Check http://{leader_address}/api/experiments/latest"
)
elif exit_reason == "connection_refused":
logger.warning(
f"Not able to access experiments in UI. Check http://{leader_address} is online and check network."
)
return NO_EXPERIMENT


Expand Down

0 comments on commit ddb4c67

Please sign in to comment.