-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
16a6173
commit 1bfd63b
Showing
6 changed files
with
126 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,9 +1,12 @@ | ||
""" imports interfaces for interacting with server """ | ||
import sys | ||
|
||
from .cli.migrate import migrate | ||
from .webapi import run, upload, get_info, start, monitor, delete, download, load, estimate_cost | ||
from .webapi import get_tasks, delete_old, download_json, download_log, load_simulation, real_cost | ||
from .container import Job, Batch, BatchData | ||
from .auth import get_credentials | ||
from .cli import tidy3d_cli | ||
from .asynchronous import run_async | ||
|
||
migrate() |
This file contains 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 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,6 @@ | ||
"""Constants for the CLI.""" | ||
from os.path import expanduser | ||
|
||
TIDY3D_DIR = f"{expanduser('~')}/.tidy3d" | ||
CONFIG_FILE = TIDY3D_DIR + "/config" | ||
CREDENTIAL_FILE = TIDY3D_DIR + "/auth.json" |
This file contains 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,71 @@ | ||
import json | ||
import os | ||
|
||
import click | ||
import requests | ||
import toml | ||
|
||
from tidy3d.web.cli.constants import CONFIG_FILE, CREDENTIAL_FILE, TIDY3D_DIR | ||
from tidy3d.web.config import DEFAULT_CONFIG | ||
|
||
|
||
# disable pylint for this file | ||
# pylint: disable-all | ||
|
||
def migrate() -> bool: | ||
"""Click command to migrate the credential to api key.""" | ||
if os.path.exists(CREDENTIAL_FILE): | ||
with open(CREDENTIAL_FILE, "r", encoding="utf-8") as fp: | ||
auth_json = json.load(fp) | ||
email = auth_json["email"] | ||
password = auth_json["password"] | ||
if email and password: | ||
is_migrate = click.prompt( | ||
"auth.json found which doesn't support anymore, do you want to migrate to api key?", | ||
type=bool, | ||
default=True, | ||
) | ||
if is_migrate: | ||
headers = {"Application": "TIDY3D"} | ||
resp = requests.get( | ||
f"{DEFAULT_CONFIG.auth_api_endpoint}/auth", | ||
headers=headers, | ||
auth=(email, password), | ||
) | ||
if resp.status_code != 200: | ||
click.echo(f"Migrate to api key failed: {resp.text}") | ||
return False | ||
else: | ||
# click.echo(json.dumps(resp.json(), indent=4)) | ||
access_token = resp.json()["data"]["auth"]["accessToken"] | ||
headers["Authorization"] = f"Bearer {access_token}" | ||
resp = requests.get( | ||
f"{DEFAULT_CONFIG.web_api_endpoint}/apikey", headers=headers | ||
) | ||
if resp.status_code != 200: | ||
click.echo(f"Migrate to api key failed: {resp.text}") | ||
return False | ||
else: | ||
click.echo(json.dumps(resp.json(), indent=4)) | ||
apikey = resp.json()["data"] | ||
if not apikey: | ||
resp = requests.post( | ||
f"{DEFAULT_CONFIG.web_api_endpoint}/apikey", headers=headers | ||
) | ||
if resp.status_code != 200: | ||
click.echo(f"Migrate to api key failed: {resp.text}") | ||
return False | ||
else: | ||
apikey = resp.json()["data"] | ||
if not os.path.exists(TIDY3D_DIR): | ||
os.mkdir(TIDY3D_DIR) | ||
with open(CONFIG_FILE, "w+", encoding="utf-8") as config_file: | ||
toml_config = toml.loads(config_file.read()) | ||
toml_config.update({"apikey": apikey}) | ||
config_file.write(toml.dumps(toml_config)) | ||
|
||
# rename auth.json to auth.json.bak | ||
os.rename(CREDENTIAL_FILE, CREDENTIAL_FILE + ".bak") | ||
return True | ||
else: | ||
click.echo("You can migrate to api key by running 'tidy3d migrate' command.") |
This file contains 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