Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webapi fixes #277

Merged
merged 4 commits into from
Mar 30, 2022
Merged

webapi fixes #277

merged 4 commits into from
Mar 30, 2022

Conversation

momchil-flex
Copy link
Collaborator

Credential folder only created when needed
Maximum attempts in authentication while loops
Authentication only called when needed, and checks if already done

Credential folder only created when needed
Maximum attempts in authentication while loops
Authentication only called when needed, and checks if already done
user for login info and saves to file."""
user for login info and saves to file.

If Config["auth" is already set, returns immediately.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo

@tylerflex
Copy link
Collaborator

So what is the new behavior of this in terms of users? Will it try to log in each web api call?

Also, does this print anything to STDOUT each auth call because that could be annoying eg. if web.run() we will get like 7 outputs? I'm fine with reducing the print output in get_credentials.

Also, can we use log.info instead of print?

@lei-flex
Copy link
Collaborator

lei-flex commented Mar 29, 2022

when credential is verified, we can also set values back to env.
os.environ["TIDY3D_USER"]
os.environ["TIDY3D_PASS"]

also the current solution requested unnecessary calls for auth. It is fine for right now.
the correct way is check the response for each call, if 401 error, then retrieve the credential again for your refrence:

def handle_response(func):
    def wrapper(*args, **kwargs):
        resp = func(*args, **kwargs)
        if resp.status_code == 401 and Config.auth_retry < 1:
            getEmailPasswdAuthKey()
            Config.auth_retry = Config.auth_retry + 1
            resp = func(*args, **kwargs)
        elif resp.status_code != 200:
            print(resp.content)
            resp.raise_for_status()
        try:
            Config.auth_retry = 0
            jsn = resp.json()['data']
        except Exception as e:
            print('Could not json decode response : {0}!'.format(resp.text))
            raise
        return jsn

    return wrapper

@@ -3,24 +3,30 @@
import getpass
import hashlib
import json
import logging
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from ..log import log

if "TIDY3D_USER" in os.environ and (
"TIDY3D_PASS" in os.environ or "TIDY3D_PASS_HASH" in os.environ
):
logging.debug("Using Tidy3D credentials from enviornment")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

log.debug


# if we find something in the credential path
if os.path.exists(credential_path):
print("Using Tidy3D credentials from stored file")
logging.info("Using Tidy3D credentials from stored file")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

log.info

return

except Exception: # pylint:disable=broad-except
print("Error: Failed to log in with environment credentials.")
logging.info("Error: Failed to log in with environment credentials.")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

log.info

@@ -73,10 +86,13 @@ def get_credentials() -> None:
return

except Exception: # pylint:disable=broad-except
print("Error: Failed to log in with saved credentials.")
logging.info("Error: Failed to log in with saved credentials.")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

log.info


# keep trying to log in
while True:
attempts = 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could also just do for attempts in range(MAX_ATTEMPTS): I think (or for _ in range(MAX_ATTEMPTS) if attempts is not used.

return

except Exception: # pylint:disable=broad-except
logging.info("Error: Failed to store credentials.")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

log.info

@@ -88,27 +104,46 @@ def get_credentials() -> None:
break

except Exception: # pylint:disable=broad-except
print("Error: Failed to log in with new username and password.")
logging.info("Error: Failed to log in with new username and password.")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

log.info

return

except Exception: # pylint:disable=broad-except
logging.info("Error: Failed to store credentials.")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

log.info

@momchil-flex momchil-flex merged commit d5ea65d into develop Mar 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants