-
Notifications
You must be signed in to change notification settings - Fork 10
[EDPEV-2767] Move Code from QuartzBio Client #484
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
|
|
||
| import sys | ||
|
|
||
| from .cli.main import main | ||
|
|
||
| if __name__ == "__main__": | ||
| main(sys.argv[1:]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| from __future__ import absolute_import | ||
|
|
||
| import os | ||
| from typing import Literal, Tuple | ||
|
|
||
| from six.moves.urllib.parse import urlparse | ||
|
|
||
| import logging | ||
|
|
||
| from requests.auth import AuthBase | ||
| import requests | ||
|
|
||
| from solvebio import SolveError | ||
| from solvebio.cli.credentials import get_credentials | ||
|
|
||
| logger = logging.getLogger("solvebio") | ||
|
|
||
|
|
||
| class SolveBioTokenAuth(AuthBase): | ||
| """Custom auth handler for SolveBio API token authentication""" | ||
|
|
||
| def __init__(self, token=None, token_type="Token"): | ||
| self.token = token | ||
| self.token_type = token_type | ||
|
|
||
| def __call__(self, r): | ||
| if self.token: | ||
| r.headers["Authorization"] = "{0} {1}".format(self.token_type, self.token) | ||
| return r | ||
|
|
||
| def __repr__(self): | ||
| if self.token: | ||
| return self.token_type | ||
| else: | ||
| return "Anonymous" | ||
|
|
||
|
|
||
| def authenticate( | ||
| host: str, | ||
| token: str, | ||
| token_type: Literal["Bearer", "Token"], | ||
| *, | ||
| raise_on_missing=True, | ||
| ) -> Tuple[str, SolveBioTokenAuth]: | ||
| """ | ||
| Sets login credentials for SolveBio API authentication. | ||
|
|
||
| :param str host: API host url | ||
| :param str token: API access token or API key | ||
| :param str token_type: API token type. `Bearer` is used for access tokens, while `Token` is used for API Keys. | ||
| :param bool raise_on_missing: Raise an exception if no credentials are available. | ||
| """ | ||
|
|
||
| # Find credentials from environment variables | ||
| if not host: | ||
| host = ( | ||
| os.environ.get("SOLVEBIO_API_HOST", None) | ||
| or os.environ.get("SOLVEBIO_API_HOST", None) | ||
| or os.environ.get("EDP_API_HOST", None) | ||
| ) | ||
|
|
||
| if not token: | ||
| api_key = ( | ||
| os.environ.get("SOLVEBIO_API_KEY", None) | ||
| or os.environ.get("SOLVEBIO_API_KEY", None) | ||
| or os.environ.get("EDP_API_KEY", None) | ||
| ) | ||
|
|
||
| access_token = ( | ||
| os.environ.get("SOLVEBIO_ACCESS_TOKEN", None) | ||
| or os.environ.get("SOLVEBIO_ACCESS_TOKEN", None) | ||
| or os.environ.get("EDP_ACCESS_TOKEN", None) | ||
| ) | ||
|
|
||
| if access_token: | ||
| token = access_token | ||
| token_type = "Bearer" | ||
| elif api_key: | ||
| token = api_key | ||
| token_type = "Token" | ||
|
|
||
| # Find credentials from local credentials file | ||
| if not token: | ||
| if creds := get_credentials(host): | ||
| token_type = creds.token_type | ||
| token = creds.token | ||
|
|
||
| if host is None: | ||
| # this happens when user/ennvars provided no API host for the login command | ||
| # but the credentials file still contains login credentials | ||
| host = creds.api_host | ||
|
|
||
| if not host and raise_on_missing: | ||
| raise SolveError("No SolveBio API host is set") | ||
|
|
||
| host = validate_api_host_url(host) | ||
|
|
||
| # If the domain ends with .solvebio.com, determine if | ||
| # we are being redirected. If so, update the url with the new host | ||
| # and log a warning. | ||
| if host and host.rstrip("/").endswith(".api.solvebio.com"): | ||
| old_host = host.rstrip("/") | ||
| response = requests.head(old_host, allow_redirects=True) | ||
| # Strip the port number from the host for comparison | ||
| new_host = validate_api_host_url(response.url).rstrip("/").replace(":443", "") | ||
|
|
||
| if old_host != new_host: | ||
| logger.warning( | ||
| 'API host redirected from "{}" to "{}", ' | ||
| "please update your local credentials file".format(old_host, new_host) | ||
| ) | ||
| host = new_host | ||
|
|
||
| if token is not None: | ||
| auth = SolveBioTokenAuth(token, token_type) | ||
| else: | ||
| auth = None | ||
|
|
||
| # TODO: warn user if WWW url is provided in edp_login! | ||
|
|
||
| # @TODO: remove references to solvebio.api_host, etc... | ||
|
|
||
| return host, auth | ||
|
|
||
|
|
||
| def validate_api_host_url(url): | ||
| """ | ||
| Validate SolveBio API host url. | ||
|
|
||
| Valid urls must not be empty and | ||
| must contain either HTTP or HTTPS scheme. | ||
| """ | ||
|
|
||
| # Default to https if no scheme is set | ||
| if "://" not in url: | ||
| url = "https://" + url | ||
|
|
||
| parsed = urlparse(url) | ||
| if parsed.scheme not in ["http", "https"]: | ||
| raise SolveError( | ||
| "Invalid API host: %s. " "Missing url scheme (HTTP or HTTPS)." % url | ||
| ) | ||
| elif not parsed.netloc: | ||
| raise SolveError("Invalid API host: %s." % url) | ||
|
|
||
| return parsed.geturl() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.