Skip to content

Commit fb8ac0e

Browse files
authored
improve login error handling (#404)
1 parent a0716c0 commit fb8ac0e

File tree

6 files changed

+57
-66
lines changed

6 files changed

+57
-66
lines changed

mapillary_tools/api_v3.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import os
22
import requests
33

4-
from .error import print_error
5-
64
API_ENDPOINT = os.getenv("API_PROXY_HOST", "https://a.mapillary.com")
75
MAPILLARY_WEB_CLIENT_ID = os.getenv(
86
"MAPILLARY_WEB_CLIENT_ID", "MkJKbDA0bnZuZlcxeTJHTmFqN3g1dzo1YTM0NjRkM2EyZGU5MzBh"
@@ -38,8 +36,6 @@ def get_upload_token(mail, pwd):
3836
params={"client_id": MAPILLARY_WEB_CLIENT_ID},
3937
json=payload,
4038
)
41-
if resp.status_code == 401:
42-
return None
4339
resp.raise_for_status()
4440
return resp.json().get("token")
4541

@@ -52,6 +48,5 @@ def get_user_key(user_name):
5248
resp.raise_for_status()
5349
resp = resp.json()
5450
if not resp or "key" not in resp[0]:
55-
print_error(f"Error, user name {user_name} does not exist...")
56-
return None
51+
raise RuntimeError(f"Error, user name {user_name} does not exist...")
5752
return resp[0]["key"]

mapillary_tools/edit_config.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,5 @@ def edit_config(
100100
else:
101101
# fill in the items and save
102102
user_items = login.prompt_user_for_user_items(user_name)
103-
if not user_items:
104-
raise RuntimeError(
105-
f"Authentication failed for username {user_name}, please try again."
106-
)
107103

108104
config.update_config(config_file, user_name, user_items)

mapillary_tools/login.py

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,54 @@
11
import getpass
22
import os
3-
from typing import Optional, Dict
3+
4+
import requests
45

56
from . import MAPILLARY_API_VERSION, api_v3, api_v4, config
67
from .config import GLOBAL_CONFIG_FILEPATH
8+
from .error import print_error
9+
10+
11+
class HTTPError(Exception):
12+
pass
13+
714

15+
def wrap_http_exception(ex: requests.HTTPError):
16+
resp = ex.response
17+
lines = [
18+
f"{ex.request.method} {resp.url}",
19+
f"> HTTP Status: {ex.response.status_code}",
20+
f"{ex.response.text}",
21+
]
22+
return HTTPError("\n".join(lines))
823

9-
def prompt_user_for_user_items(user_name: str) -> Optional[dict]:
10-
print(f"Enter user credentials for user {user_name}:")
11-
user_email = input("Enter email: ")
12-
user_password = getpass.getpass("Enter user password: ")
1324

14-
if MAPILLARY_API_VERSION == "v3":
15-
user_key = api_v3.get_user_key(user_name)
16-
if not user_key:
17-
return None
18-
upload_token = api_v3.get_upload_token(user_email, user_password)
19-
else:
20-
assert MAPILLARY_API_VERSION == "v4"
21-
data = api_v4.get_upload_token(user_email, user_password)
22-
upload_token = data.get("access_token")
23-
user_key = data.get("user_id")
25+
def prompt_user_for_user_items(user_name: str) -> dict:
26+
print(f"Sign in for user {user_name}")
27+
user_email = input("Enter your Mapillary user email: ")
28+
user_password = getpass.getpass("Enter Mapillary user password: ")
2429

25-
if not upload_token:
26-
return None
30+
try:
31+
if MAPILLARY_API_VERSION == "v3":
32+
user_key = api_v3.get_user_key(user_name)
33+
upload_token = api_v3.get_upload_token(user_email, user_password)
34+
else:
35+
assert MAPILLARY_API_VERSION == "v4"
36+
data = api_v4.get_upload_token(user_email, user_password)
37+
upload_token = data.get("access_token")
38+
user_key = data.get("user_id")
39+
except requests.HTTPError as ex:
40+
if 400 <= ex.response.status_code < 500:
41+
resp = ex.response.json()
42+
subcode = resp.get("error", {}).get("error_subcode")
43+
if subcode in [1348028, 1348092, 3404005]:
44+
title = resp.get("error", {}).get("error_user_title")
45+
message = resp.get("error", {}).get("error_user_msg")
46+
print_error(f"{title}: {message}")
47+
return prompt_user_for_user_items(user_name)
48+
else:
49+
raise wrap_http_exception(ex)
50+
else:
51+
raise wrap_http_exception(ex)
2752

2853
return {
2954
"MAPSettingsUsername": user_name,
@@ -32,15 +57,13 @@ def prompt_user_for_user_items(user_name: str) -> Optional[dict]:
3257
}
3358

3459

35-
def authenticate_user(user_name: str) -> Optional[Dict]:
60+
def authenticate_user(user_name: str) -> dict:
3661
if os.path.isfile(GLOBAL_CONFIG_FILEPATH):
3762
global_config_object = config.load_config(GLOBAL_CONFIG_FILEPATH)
3863
if user_name in global_config_object.sections():
3964
return config.load_user(global_config_object, user_name)
4065

4166
user_items = prompt_user_for_user_items(user_name)
42-
if not user_items:
43-
return None
4467

4568
config.create_config(GLOBAL_CONFIG_FILEPATH)
4669
config.update_config(GLOBAL_CONFIG_FILEPATH, user_name, user_items)

mapillary_tools/process_upload_params.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,21 +68,7 @@ def process_upload_params(
6868
sys.exit(1)
6969

7070
if not master_upload:
71-
try:
72-
credentials = login.authenticate_user(user_name)
73-
except:
74-
print_error("Error, user authentication failed for user " + user_name)
75-
processing.create_and_log_process_in_list(
76-
process_file_list, "upload_params_process" "failed", verbose
77-
)
78-
sys.exit(1)
79-
if credentials is None:
80-
print_error("Error, user authentication failed for user " + user_name)
81-
processing.create_and_log_process_in_list(
82-
process_file_list, "upload_params_process" "failed", verbose
83-
)
84-
sys.exit(1)
85-
71+
credentials = login.authenticate_user(user_name)
8672
user_upload_token = credentials["user_upload_token"]
8773
user_key = credentials["MAPSettingsUserKey"]
8874

mapillary_tools/processing.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,27 +1050,21 @@ def user_properties(
10501050
verbose: bool = False,
10511051
) -> Optional[Dict]:
10521052
# basic
1053-
user_properties = login.authenticate_user(user_name)
1054-
if not user_properties:
1055-
print_error("Error, user authentication failed for user " + user_name)
1056-
print(
1057-
"Make sure your user credentials are correct, user authentication is required for images to be uploaded to Mapillary."
1058-
)
1059-
return None
1053+
user_items = login.authenticate_user(user_name)
10601054
# organization validation
10611055
if organization_username or organization_key:
10621056
organization_key = process_organization(
1063-
user_properties, organization_username, organization_key, private
1057+
user_items, organization_username, organization_key, private
10641058
)
1065-
user_properties.update(
1059+
user_items.update(
10661060
{"MAPOrganizationKey": organization_key, "MAPPrivate": private}
10671061
)
10681062

10691063
# remove uneeded credentials
1070-
if "user_upload_token" in user_properties:
1071-
del user_properties["user_upload_token"]
1064+
if "user_upload_token" in user_items:
1065+
del user_items["user_upload_token"]
10721066

1073-
return user_properties
1067+
return user_items
10741068

10751069

10761070
def process_organization(

mapillary_tools/uploader.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from . import upload_api_v3
2020
from . import upload_api_v4
2121
from . import ipc
22-
from .login import authenticate_user
22+
from .login import authenticate_user, wrap_http_exception
2323
from .utils import force_decode
2424
from . import MAPILLARY_API_VERSION
2525

@@ -339,6 +339,7 @@ def get_organization_key(user_key, organization_username, upload_token):
339339
print("Available organization user names for current user are : ")
340340
print(organization_usernames)
341341
sys.exit(1)
342+
342343
return organization_key
343344

344345

@@ -539,8 +540,6 @@ def _read_captured_at(path):
539540
print(f"Found the compressed sequence file {sequence_zip_path}")
540541

541542
credentials = authenticate_user(user_name)
542-
if credentials is None:
543-
raise RuntimeError(f"Unable to find credentials for user {user_name}")
544543

545544
service = upload_api_v4.UploadService(credentials["user_upload_token"])
546545

@@ -550,10 +549,10 @@ def _read_captured_at(path):
550549
upload_resp = service.upload(sequence_uuid, data_size, fp)
551550
try:
552551
upload_resp.raise_for_status()
553-
except requests.RequestException as ex:
552+
except requests.HTTPError as ex:
554553
for path in file_list:
555554
create_upload_log(path, "upload_failed")
556-
raise RuntimeError(f"Upload server error: {ex.response.text}")
555+
raise wrap_http_exception(ex)
557556

558557
upload_resp_json = upload_resp.json()
559558
try:
@@ -567,10 +566,10 @@ def _read_captured_at(path):
567566
finish_resp = service.finish(file_handle)
568567
try:
569568
finish_resp.raise_for_status()
570-
except requests.RequestException as ex:
569+
except requests.HTTPError as ex:
571570
for path in file_list:
572571
create_upload_log(path, "upload_failed")
573-
raise RuntimeError(f"Upload server error: {ex.response.text}")
572+
raise wrap_http_exception(ex)
574573

575574
finish_data = finish_resp.json()
576575
cluster_id = finish_data.get("cluster_id")
@@ -613,8 +612,6 @@ def upload_file_list_manual(
613612
private = first_image.get("private", False)
614613

615614
credentials = authenticate_user(user_name)
616-
if credentials is None:
617-
raise RuntimeError(f"Unable to find credentials for user {user_name}")
618615

619616
upload_options = {
620617
"token": credentials["user_upload_token"],

0 commit comments

Comments
 (0)