@@ -454,8 +454,13 @@ def encode_action_code_settings(settings):
454454class  UserManager :
455455    """Provides methods for interacting with the Google Identity Toolkit.""" 
456456
457-     def  __init__ (self , client ):
458-         self ._client  =  client 
457+     ID_TOOLKIT_URL  =  'https://identitytoolkit.googleapis.com/v1' 
458+ 
459+     def  __init__ (self , http_client , project_id , tenant_id = None ):
460+         self .http_client  =  http_client 
461+         self .base_url  =  '{0}/projects/{1}' .format (self .ID_TOOLKIT_URL , project_id )
462+         if  tenant_id :
463+             self .base_url  +=  '/tenants/{0}' .format (tenant_id )
459464
460465    def  get_user (self , ** kwargs ):
461466        """Gets the user data corresponding to the provided key.""" 
@@ -471,17 +476,12 @@ def get_user(self, **kwargs):
471476        else :
472477            raise  TypeError ('Unsupported keyword arguments: {0}.' .format (kwargs ))
473478
474-         try :
475-             body , http_resp  =  self ._client .body_and_response (
476-                 'post' , '/accounts:lookup' , json = payload )
477-         except  requests .exceptions .RequestException  as  error :
478-             raise  _auth_utils .handle_auth_backend_error (error )
479-         else :
480-             if  not  body  or  not  body .get ('users' ):
481-                 raise  _auth_utils .UserNotFoundError (
482-                     'No user record found for the provided {0}: {1}.' .format (key_type , key ),
483-                     http_response = http_resp )
484-             return  body ['users' ][0 ]
479+         body , http_resp  =  self ._make_request ('post' , '/accounts:lookup' , json = payload )
480+         if  not  body  or  not  body .get ('users' ):
481+             raise  _auth_utils .UserNotFoundError (
482+                 'No user record found for the provided {0}: {1}.' .format (key_type , key ),
483+                 http_response = http_resp )
484+         return  body ['users' ][0 ]
485485
486486    def  list_users (self , page_token = None , max_results = MAX_LIST_USERS_RESULTS ):
487487        """Retrieves a batch of users.""" 
@@ -498,10 +498,8 @@ def list_users(self, page_token=None, max_results=MAX_LIST_USERS_RESULTS):
498498        payload  =  {'maxResults' : max_results }
499499        if  page_token :
500500            payload ['nextPageToken' ] =  page_token 
501-         try :
502-             return  self ._client .body ('get' , '/accounts:batchGet' , params = payload )
503-         except  requests .exceptions .RequestException  as  error :
504-             raise  _auth_utils .handle_auth_backend_error (error )
501+         body , _  =  self ._make_request ('get' , '/accounts:batchGet' , params = payload )
502+         return  body 
505503
506504    def  create_user (self , uid = None , display_name = None , email = None , phone_number = None ,
507505                    photo_url = None , password = None , disabled = None , email_verified = None ):
@@ -517,15 +515,11 @@ def create_user(self, uid=None, display_name=None, email=None, phone_number=None
517515            'disabled' : bool (disabled ) if  disabled  is  not None  else  None ,
518516        }
519517        payload  =  {k : v  for  k , v  in  payload .items () if  v  is  not None }
520-         try :
521-             body , http_resp  =  self ._client .body_and_response ('post' , '/accounts' , json = payload )
522-         except  requests .exceptions .RequestException  as  error :
523-             raise  _auth_utils .handle_auth_backend_error (error )
524-         else :
525-             if  not  body  or  not  body .get ('localId' ):
526-                 raise  _auth_utils .UnexpectedResponseError (
527-                     'Failed to create new user.' , http_response = http_resp )
528-             return  body .get ('localId' )
518+         body , http_resp  =  self ._make_request ('post' , '/accounts' , json = payload )
519+         if  not  body  or  not  body .get ('localId' ):
520+             raise  _auth_utils .UnexpectedResponseError (
521+                 'Failed to create new user.' , http_response = http_resp )
522+         return  body .get ('localId' )
529523
530524    def  update_user (self , uid , display_name = None , email = None , phone_number = None ,
531525                    photo_url = None , password = None , disabled = None , email_verified = None ,
@@ -568,29 +562,19 @@ def update_user(self, uid, display_name=None, email=None, phone_number=None,
568562            payload ['customAttributes' ] =  _auth_utils .validate_custom_claims (json_claims )
569563
570564        payload  =  {k : v  for  k , v  in  payload .items () if  v  is  not None }
571-         try :
572-             body , http_resp  =  self ._client .body_and_response (
573-                 'post' , '/accounts:update' , json = payload )
574-         except  requests .exceptions .RequestException  as  error :
575-             raise  _auth_utils .handle_auth_backend_error (error )
576-         else :
577-             if  not  body  or  not  body .get ('localId' ):
578-                 raise  _auth_utils .UnexpectedResponseError (
579-                     'Failed to update user: {0}.' .format (uid ), http_response = http_resp )
580-             return  body .get ('localId' )
565+         body , http_resp  =  self ._make_request ('post' , '/accounts:update' , json = payload )
566+         if  not  body  or  not  body .get ('localId' ):
567+             raise  _auth_utils .UnexpectedResponseError (
568+                 'Failed to update user: {0}.' .format (uid ), http_response = http_resp )
569+         return  body .get ('localId' )
581570
582571    def  delete_user (self , uid ):
583572        """Deletes the user identified by the specified user ID.""" 
584573        _auth_utils .validate_uid (uid , required = True )
585-         try :
586-             body , http_resp  =  self ._client .body_and_response (
587-                 'post' , '/accounts:delete' , json = {'localId'  : uid })
588-         except  requests .exceptions .RequestException  as  error :
589-             raise  _auth_utils .handle_auth_backend_error (error )
590-         else :
591-             if  not  body  or  not  body .get ('kind' ):
592-                 raise  _auth_utils .UnexpectedResponseError (
593-                     'Failed to delete user: {0}.' .format (uid ), http_response = http_resp )
574+         body , http_resp  =  self ._make_request ('post' , '/accounts:delete' , json = {'localId'  : uid })
575+         if  not  body  or  not  body .get ('kind' ):
576+             raise  _auth_utils .UnexpectedResponseError (
577+                 'Failed to delete user: {0}.' .format (uid ), http_response = http_resp )
594578
595579    def  import_users (self , users , hash_alg = None ):
596580        """Imports the given list of users to Firebase Auth.""" 
@@ -609,16 +593,11 @@ def import_users(self, users, hash_alg=None):
609593            if  not  isinstance (hash_alg , _user_import .UserImportHash ):
610594                raise  ValueError ('A UserImportHash is required to import users with passwords.' )
611595            payload .update (hash_alg .to_dict ())
612-         try :
613-             body , http_resp  =  self ._client .body_and_response (
614-                 'post' , '/accounts:batchCreate' , json = payload )
615-         except  requests .exceptions .RequestException  as  error :
616-             raise  _auth_utils .handle_auth_backend_error (error )
617-         else :
618-             if  not  isinstance (body , dict ):
619-                 raise  _auth_utils .UnexpectedResponseError (
620-                     'Failed to import users.' , http_response = http_resp )
621-             return  body 
596+         body , http_resp  =  self ._make_request ('post' , '/accounts:batchCreate' , json = payload )
597+         if  not  isinstance (body , dict ):
598+             raise  _auth_utils .UnexpectedResponseError (
599+                 'Failed to import users.' , http_response = http_resp )
600+         return  body 
622601
623602    def  generate_email_action_link (self , action_type , email , action_code_settings = None ):
624603        """Fetches the email action links for types 
@@ -646,16 +625,18 @@ def generate_email_action_link(self, action_type, email, action_code_settings=No
646625        if  action_code_settings :
647626            payload .update (encode_action_code_settings (action_code_settings ))
648627
628+         body , http_resp  =  self ._make_request ('post' , '/accounts:sendOobCode' , json = payload )
629+         if  not  body  or  not  body .get ('oobLink' ):
630+             raise  _auth_utils .UnexpectedResponseError (
631+                 'Failed to generate email action link.' , http_response = http_resp )
632+         return  body .get ('oobLink' )
633+ 
634+     def  _make_request (self , method , path , ** kwargs ):
635+         url  =  '{0}{1}' .format (self .base_url , path )
649636        try :
650-             body , http_resp  =  self ._client .body_and_response (
651-                 'post' , '/accounts:sendOobCode' , json = payload )
637+             return  self .http_client .body_and_response (method , url , ** kwargs )
652638        except  requests .exceptions .RequestException  as  error :
653639            raise  _auth_utils .handle_auth_backend_error (error )
654-         else :
655-             if  not  body  or  not  body .get ('oobLink' ):
656-                 raise  _auth_utils .UnexpectedResponseError (
657-                     'Failed to generate email action link.' , http_response = http_resp )
658-             return  body .get ('oobLink' )
659640
660641
661642class  _UserIterator :
0 commit comments