mirror of
https://github.com/platformio/platformio-core.git
synced 2025-07-30 10:07:14 +02:00
Handle error when internet is offline. Resolve # 3503 (#3505)
* Handle error when internet is offline. * Fix * minor fix
This commit is contained in:
@ -21,6 +21,7 @@ import requests.adapters
|
|||||||
from requests.packages.urllib3.util.retry import Retry # pylint:disable=import-error
|
from requests.packages.urllib3.util.retry import Retry # pylint:disable=import-error
|
||||||
|
|
||||||
from platformio import __pioaccount_api__, app
|
from platformio import __pioaccount_api__, app
|
||||||
|
from platformio.exception import InternetIsOffline
|
||||||
from platformio.commands.account import exception
|
from platformio.commands.account import exception
|
||||||
|
|
||||||
|
|
||||||
@ -75,11 +76,11 @@ class AccountClient(object):
|
|||||||
app.get_state_item("account", {}).get("email", "")
|
app.get_state_item("account", {}).get("email", "")
|
||||||
)
|
)
|
||||||
|
|
||||||
response = self._session.post(
|
result = self.send_request(
|
||||||
|
"post",
|
||||||
self.api_base_url + "/v1/login",
|
self.api_base_url + "/v1/login",
|
||||||
data={"username": username, "password": password},
|
data={"username": username, "password": password},
|
||||||
)
|
)
|
||||||
result = self.raise_error_from_response(response)
|
|
||||||
app.set_state_item("account", result)
|
app.set_state_item("account", result)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@ -93,40 +94,35 @@ class AccountClient(object):
|
|||||||
app.get_state_item("account", {}).get("email", "")
|
app.get_state_item("account", {}).get("email", "")
|
||||||
)
|
)
|
||||||
|
|
||||||
response = self._session.post(
|
result = self.send_request(
|
||||||
|
"post",
|
||||||
self.api_base_url + "/v1/login/code",
|
self.api_base_url + "/v1/login/code",
|
||||||
data={"client_id": client_id, "code": code, "redirect_uri": redirect_uri},
|
data={"client_id": client_id, "code": code, "redirect_uri": redirect_uri},
|
||||||
)
|
)
|
||||||
result = self.raise_error_from_response(response)
|
|
||||||
app.set_state_item("account", result)
|
app.set_state_item("account", result)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def logout(self):
|
def logout(self):
|
||||||
try:
|
refresh_token = self.get_refresh_token()
|
||||||
refresh_token = self.get_refresh_token()
|
|
||||||
except: # pylint:disable=bare-except
|
|
||||||
raise exception.AccountNotAuthorized()
|
|
||||||
self.delete_local_session()
|
self.delete_local_session()
|
||||||
response = requests.post(
|
|
||||||
self.api_base_url + "/v1/logout", data={"refresh_token": refresh_token},
|
|
||||||
)
|
|
||||||
try:
|
try:
|
||||||
self.raise_error_from_response(response)
|
self.send_request(
|
||||||
|
"post",
|
||||||
|
self.api_base_url + "/v1/logout",
|
||||||
|
data={"refresh_token": refresh_token},
|
||||||
|
)
|
||||||
except exception.AccountError:
|
except exception.AccountError:
|
||||||
pass
|
pass
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def change_password(self, old_password, new_password):
|
def change_password(self, old_password, new_password):
|
||||||
try:
|
token = self.fetch_authentication_token()
|
||||||
token = self.fetch_authentication_token()
|
self.send_request(
|
||||||
except: # pylint:disable=bare-except
|
"post",
|
||||||
raise exception.AccountNotAuthorized()
|
|
||||||
response = self._session.post(
|
|
||||||
self.api_base_url + "/v1/password",
|
self.api_base_url + "/v1/password",
|
||||||
headers={"Authorization": "Bearer %s" % token},
|
headers={"Authorization": "Bearer %s" % token},
|
||||||
data={"old_password": old_password, "new_password": new_password},
|
data={"old_password": old_password, "new_password": new_password},
|
||||||
)
|
)
|
||||||
self.raise_error_from_response(response)
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def registration(
|
def registration(
|
||||||
@ -141,7 +137,8 @@ class AccountClient(object):
|
|||||||
app.get_state_item("account", {}).get("email", "")
|
app.get_state_item("account", {}).get("email", "")
|
||||||
)
|
)
|
||||||
|
|
||||||
response = self._session.post(
|
return self.send_request(
|
||||||
|
"post",
|
||||||
self.api_base_url + "/v1/registration",
|
self.api_base_url + "/v1/registration",
|
||||||
data={
|
data={
|
||||||
"username": username,
|
"username": username,
|
||||||
@ -151,50 +148,41 @@ class AccountClient(object):
|
|||||||
"lastname": lastname,
|
"lastname": lastname,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
return self.raise_error_from_response(response)
|
|
||||||
|
|
||||||
def auth_token(self, password, regenerate):
|
def auth_token(self, password, regenerate):
|
||||||
try:
|
token = self.fetch_authentication_token()
|
||||||
token = self.fetch_authentication_token()
|
result = self.send_request(
|
||||||
except: # pylint:disable=bare-except
|
"post",
|
||||||
raise exception.AccountNotAuthorized()
|
|
||||||
response = self._session.post(
|
|
||||||
self.api_base_url + "/v1/token",
|
self.api_base_url + "/v1/token",
|
||||||
headers={"Authorization": "Bearer %s" % token},
|
headers={"Authorization": "Bearer %s" % token},
|
||||||
data={"password": password, "regenerate": 1 if regenerate else 0},
|
data={"password": password, "regenerate": 1 if regenerate else 0},
|
||||||
)
|
)
|
||||||
return self.raise_error_from_response(response).get("auth_token")
|
return result.get("auth_token")
|
||||||
|
|
||||||
def forgot_password(self, username):
|
def forgot_password(self, username):
|
||||||
response = self._session.post(
|
return self.send_request(
|
||||||
self.api_base_url + "/v1/forgot", data={"username": username},
|
"post", self.api_base_url + "/v1/forgot", data={"username": username},
|
||||||
)
|
)
|
||||||
return self.raise_error_from_response(response).get("auth_token")
|
|
||||||
|
|
||||||
def get_profile(self):
|
def get_profile(self):
|
||||||
try:
|
token = self.fetch_authentication_token()
|
||||||
token = self.fetch_authentication_token()
|
return self.send_request(
|
||||||
except: # pylint:disable=bare-except
|
"get",
|
||||||
raise exception.AccountNotAuthorized()
|
|
||||||
response = self._session.get(
|
|
||||||
self.api_base_url + "/v1/profile",
|
self.api_base_url + "/v1/profile",
|
||||||
headers={"Authorization": "Bearer %s" % token},
|
headers={"Authorization": "Bearer %s" % token},
|
||||||
)
|
)
|
||||||
return self.raise_error_from_response(response)
|
|
||||||
|
|
||||||
def update_profile(self, profile, current_password):
|
def update_profile(self, profile, current_password):
|
||||||
try:
|
token = self.fetch_authentication_token()
|
||||||
token = self.fetch_authentication_token()
|
|
||||||
except: # pylint:disable=bare-except
|
|
||||||
raise exception.AccountNotAuthorized()
|
|
||||||
profile["current_password"] = current_password
|
profile["current_password"] = current_password
|
||||||
response = self._session.put(
|
self.delete_local_state("summary")
|
||||||
|
response = self.send_request(
|
||||||
|
"put",
|
||||||
self.api_base_url + "/v1/profile",
|
self.api_base_url + "/v1/profile",
|
||||||
headers={"Authorization": "Bearer %s" % token},
|
headers={"Authorization": "Bearer %s" % token},
|
||||||
data=profile,
|
data=profile,
|
||||||
)
|
)
|
||||||
self.delete_local_state("summary")
|
return response
|
||||||
return self.raise_error_from_response(response)
|
|
||||||
|
|
||||||
def get_account_info(self, offline):
|
def get_account_info(self, offline):
|
||||||
account = app.get_state_item("account")
|
account = app.get_state_item("account")
|
||||||
@ -212,15 +200,12 @@ class AccountClient(object):
|
|||||||
"username": account.get("username"),
|
"username": account.get("username"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
try:
|
token = self.fetch_authentication_token()
|
||||||
token = self.fetch_authentication_token()
|
result = self.send_request(
|
||||||
except: # pylint:disable=bare-except
|
"get",
|
||||||
raise exception.AccountNotAuthorized()
|
|
||||||
response = self._session.get(
|
|
||||||
self.api_base_url + "/v1/summary",
|
self.api_base_url + "/v1/summary",
|
||||||
headers={"Authorization": "Bearer %s" % token},
|
headers={"Authorization": "Bearer %s" % token},
|
||||||
)
|
)
|
||||||
result = self.raise_error_from_response(response)
|
|
||||||
account["summary"] = dict(
|
account["summary"] = dict(
|
||||||
profile=result.get("profile"),
|
profile=result.get("profile"),
|
||||||
packages=result.get("packages"),
|
packages=result.get("packages"),
|
||||||
@ -239,18 +224,29 @@ class AccountClient(object):
|
|||||||
if auth.get("access_token_expire") > time.time():
|
if auth.get("access_token_expire") > time.time():
|
||||||
return auth.get("access_token")
|
return auth.get("access_token")
|
||||||
if auth.get("refresh_token"):
|
if auth.get("refresh_token"):
|
||||||
response = self._session.post(
|
|
||||||
self.api_base_url + "/v1/login",
|
|
||||||
headers={"Authorization": "Bearer %s" % auth.get("refresh_token")},
|
|
||||||
)
|
|
||||||
try:
|
try:
|
||||||
result = self.raise_error_from_response(response)
|
result = self.send_request(
|
||||||
|
"post",
|
||||||
|
self.api_base_url + "/v1/login",
|
||||||
|
headers={
|
||||||
|
"Authorization": "Bearer %s" % auth.get("refresh_token")
|
||||||
|
},
|
||||||
|
)
|
||||||
app.set_state_item("account", result)
|
app.set_state_item("account", result)
|
||||||
return result.get("auth").get("access_token")
|
return result.get("auth").get("access_token")
|
||||||
except exception.AccountError:
|
except exception.AccountError:
|
||||||
self.delete_local_session()
|
self.delete_local_session()
|
||||||
raise exception.AccountNotAuthorized()
|
raise exception.AccountNotAuthorized()
|
||||||
|
|
||||||
|
def send_request(self, method, url, headers=None, data=None):
|
||||||
|
try:
|
||||||
|
response = getattr(self._session, method)(
|
||||||
|
url, headers=headers or {}, data=data or {}
|
||||||
|
)
|
||||||
|
except requests.exceptions.ConnectionError:
|
||||||
|
raise InternetIsOffline()
|
||||||
|
return self.raise_error_from_response(response)
|
||||||
|
|
||||||
def raise_error_from_response(self, response, expected_codes=(200, 201, 202)):
|
def raise_error_from_response(self, response, expected_codes=(200, 201, 202)):
|
||||||
if response.status_code in expected_codes:
|
if response.status_code in expected_codes:
|
||||||
try:
|
try:
|
||||||
|
@ -232,8 +232,8 @@ class InternetIsOffline(UserSideException):
|
|||||||
|
|
||||||
MESSAGE = (
|
MESSAGE = (
|
||||||
"You are not connected to the Internet.\n"
|
"You are not connected to the Internet.\n"
|
||||||
"If you build a project first time, we need Internet connection "
|
"PlatformIO needs the Internet connection to"
|
||||||
"to install all dependencies and toolchains."
|
" download dependent packages or to work with PIO Account."
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user