Skip to content

Commit 505f6b1

Browse files
committed
porting code
1 parent 17d2ac5 commit 505f6b1

File tree

11 files changed

+1316
-0
lines changed

11 files changed

+1316
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import json
2+
import webbrowser
3+
4+
import requests
5+
from requests.models import HTTPBasicAuth
6+
7+
from lightning.app.utilities.network import LightningClient
8+
from lightning_app.model2cloud.utils import LIGHTNING_CLOUD_URL
9+
10+
11+
def get_user_details():
12+
def _get_user_details():
13+
client = LightningClient()
14+
user_details = client.auth_service_get_user()
15+
return (user_details.username, user_details.api_key)
16+
17+
username, api_key = _get_user_details()
18+
return username, api_key
19+
20+
21+
def get_username_from_api_key(api_key: str):
22+
response = requests.get(
23+
url=f"{LIGHTNING_CLOUD_URL}/v1/auth/user",
24+
auth=HTTPBasicAuth("lightning", api_key),
25+
)
26+
assert response.status_code == 200, (
27+
"API_KEY provided is either invalid or wasn't found in the database."
28+
" Please ensure that you passed the correct API_KEY."
29+
)
30+
return json.loads(response.content)["username"]
31+
32+
33+
def _check_browser_runnable():
34+
try:
35+
webbrowser.get()
36+
return True
37+
except webbrowser.Error:
38+
return False
39+
40+
41+
def authenticate(inp_api_key: str = ""):
42+
if not inp_api_key:
43+
if not _check_browser_runnable():
44+
raise ValueError(
45+
"Couldn't find a runnable browser in the current system/server."
46+
" In order to run the commands on this system, we suggest passing the `api_key`"
47+
" after logging into https://lightning.ai."
48+
)
49+
username, api_key = get_user_details()
50+
return username, api_key
51+
52+
username = get_username_from_api_key(inp_api_key)
53+
return username, inp_api_key

0 commit comments

Comments
 (0)