11import getpass
22import os
3- from typing import Optional , Dict
3+
4+ import requests
45
56from . import MAPILLARY_API_VERSION , api_v3 , api_v4 , config
67from .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 )
0 commit comments