24
24
from firebase_admin import _user_import
25
25
26
26
27
- USER_IMPORT_ERROR = 'USER_IMPORT_ERROR'
28
- USER_DOWNLOAD_ERROR = 'LIST_USERS_ERROR'
29
-
30
27
MAX_LIST_USERS_RESULTS = 1000
31
28
MAX_IMPORT_USERS_SIZE = 1000
32
29
@@ -44,15 +41,6 @@ def __init__(self, description):
44
41
DELETE_ATTRIBUTE = Sentinel ('Value used to delete an attribute from a user profile' )
45
42
46
43
47
- class ApiCallError (Exception ):
48
- """Represents an Exception encountered while invoking the Firebase user management API."""
49
-
50
- def __init__ (self , code , message , error = None ):
51
- Exception .__init__ (self , message )
52
- self .code = code
53
- self .detail = error
54
-
55
-
56
44
class UserMetadata (object ):
57
45
"""Contains additional metadata associated with a user account."""
58
46
@@ -510,7 +498,7 @@ def list_users(self, page_token=None, max_results=MAX_LIST_USERS_RESULTS):
510
498
try :
511
499
return self ._client .body ('get' , '/accounts:batchGet' , params = payload )
512
500
except requests .exceptions .RequestException as error :
513
- self . _handle_http_error ( USER_DOWNLOAD_ERROR , 'Failed to download user accounts.' , error )
501
+ raise _auth_utils . handle_auth_backend_error ( error )
514
502
515
503
def create_user (self , uid = None , display_name = None , email = None , phone_number = None ,
516
504
photo_url = None , password = None , disabled = None , email_verified = None ):
@@ -619,13 +607,15 @@ def import_users(self, users, hash_alg=None):
619
607
raise ValueError ('A UserImportHash is required to import users with passwords.' )
620
608
payload .update (hash_alg .to_dict ())
621
609
try :
622
- response = self ._client .body ('post' , '/accounts:batchCreate' , json = payload )
610
+ body , http_resp = self ._client .body_and_response (
611
+ 'post' , '/accounts:batchCreate' , json = payload )
623
612
except requests .exceptions .RequestException as error :
624
- self . _handle_http_error ( USER_IMPORT_ERROR , 'Failed to import users.' , error )
613
+ raise _auth_utils . handle_auth_backend_error ( error )
625
614
else :
626
- if not isinstance (response , dict ):
627
- raise ApiCallError (USER_IMPORT_ERROR , 'Failed to import users.' )
628
- return response
615
+ if not isinstance (body , dict ):
616
+ raise _auth_utils .UnexpectedResponseError (
617
+ 'Failed to import users.' , http_response = http_resp )
618
+ return body
629
619
630
620
def generate_email_action_link (self , action_type , email , action_code_settings = None ):
631
621
"""Fetches the email action links for types
@@ -640,7 +630,7 @@ def generate_email_action_link(self, action_type, email, action_code_settings=No
640
630
link_url: action url to be emailed to the user
641
631
642
632
Raises:
643
- ApiCallError : If an error occurs while generating the link
633
+ FirebaseError : If an error occurs while generating the link
644
634
ValueError: If the provided arguments are invalid
645
635
"""
646
636
payload = {
@@ -663,13 +653,6 @@ def generate_email_action_link(self, action_type, email, action_code_settings=No
663
653
'Failed to generate email action link.' , http_response = http_resp )
664
654
return body .get ('oobLink' )
665
655
666
- def _handle_http_error (self , code , msg , error ):
667
- if error .response is not None :
668
- msg += '\n Server response: {0}' .format (error .response .content .decode ())
669
- else :
670
- msg += '\n Reason: {0}' .format (error )
671
- raise ApiCallError (code , msg , error )
672
-
673
656
674
657
class _UserIterator (object ):
675
658
"""An iterator that allows iterating over user accounts, one at a time.
0 commit comments