36
36
import com .google .common .base .Strings ;
37
37
import com .google .common .collect .ImmutableList ;
38
38
import com .google .common .collect .ImmutableMap ;
39
+ import com .google .common .collect .ImmutableSortedSet ;
39
40
import com .google .firebase .FirebaseApp ;
40
41
import com .google .firebase .ImplFirebaseTrampolines ;
41
- import com .google .firebase .auth .UserRecord .CreateRequest ;
42
- import com .google .firebase .auth .UserRecord .UpdateRequest ;
42
+ import com .google .firebase .auth .UserRecord ;
43
43
import com .google .firebase .auth .internal .DownloadAccountResponse ;
44
44
import com .google .firebase .auth .internal .GetAccountInfoResponse ;
45
45
import com .google .firebase .auth .internal .HttpErrorResponse ;
@@ -171,7 +171,7 @@ UserRecord getUserByPhoneNumber(String phoneNumber) throws FirebaseAuthException
171
171
return new UserRecord (response .getUsers ().get (0 ), jsonFactory );
172
172
}
173
173
174
- String createUser (CreateRequest request ) throws FirebaseAuthException {
174
+ String createUser (UserRecord . CreateRequest request ) throws FirebaseAuthException {
175
175
GenericJson response = post (
176
176
"/accounts" , request .getProperties (), GenericJson .class );
177
177
if (response != null ) {
@@ -183,7 +183,8 @@ String createUser(CreateRequest request) throws FirebaseAuthException {
183
183
throw new FirebaseAuthException (INTERNAL_ERROR , "Failed to create new user" );
184
184
}
185
185
186
- void updateUser (UpdateRequest request , JsonFactory jsonFactory ) throws FirebaseAuthException {
186
+ void updateUser (UserRecord .UpdateRequest request , JsonFactory jsonFactory )
187
+ throws FirebaseAuthException {
187
188
GenericJson response = post (
188
189
"/accounts:update" , request .getProperties (jsonFactory ), GenericJson .class );
189
190
if (response == null || !request .getUid ().equals (response .get ("localId" ))) {
@@ -230,20 +231,33 @@ UserImportResult importUsers(UserImportRequest request) throws FirebaseAuthExcep
230
231
231
232
Tenant getTenant (String tenantId ) throws FirebaseAuthException {
232
233
GenericUrl url = new GenericUrl (tenantMgtBaseUrl + "/tenants/" + tenantId );
233
- Tenant response = sendRequest ("GET" , url , null , Tenant .class );
234
- if (Strings .isNullOrEmpty (response .getTenantId ())) {
235
- throw new FirebaseAuthException (TENANT_NOT_FOUND_ERROR , "Failed to get tenant." );
236
- }
237
- return response ;
234
+ return sendRequest ("GET" , url , null , Tenant .class );
235
+ }
236
+
237
+ Tenant createTenant (Tenant .CreateRequest request ) throws FirebaseAuthException {
238
+ GenericUrl url = new GenericUrl (tenantMgtBaseUrl + "/tenants" );
239
+ return sendRequest ("POST" , url , request .getProperties (), Tenant .class );
240
+ }
241
+
242
+ Tenant updateTenant (Tenant .UpdateRequest request ) throws FirebaseAuthException {
243
+ Map <String , Object > properties = request .getProperties ();
244
+ checkArgument (!properties .isEmpty (), "tenant update must have at least one property set" );
245
+ GenericUrl url = new GenericUrl (tenantMgtBaseUrl + "/tenants/" + request .getTenantId ());
246
+ url .put ("updateMask" , generateMask (properties ));
247
+ return sendRequest ("PATCH" , url , properties , Tenant .class );
248
+ }
249
+
250
+ private static String generateMask (Map <String , Object > properties ) {
251
+ // This implementation does not currently handle the case of nested properties. This is fine
252
+ // since we do not currently generate masks for any properties with nested values. When it
253
+ // comes time to implement this, we can check if a property has nested properties by checking
254
+ // if it is an instance of the Map class.
255
+ return String .join ("," , ImmutableSortedSet .copyOf (properties .keySet ()));
238
256
}
239
257
240
258
void deleteTenant (String tenantId ) throws FirebaseAuthException {
241
259
GenericUrl url = new GenericUrl (tenantMgtBaseUrl + "/tenants/" + tenantId );
242
- GenericJson response = sendRequest ("DELETE" , url , null , GenericJson .class );
243
- if (response == null ) {
244
- throw new FirebaseAuthException (TENANT_NOT_FOUND_ERROR ,
245
- "Failed to delete tenant: " + tenantId );
246
- }
260
+ sendRequest ("DELETE" , url , null , GenericJson .class );
247
261
}
248
262
249
263
ListTenantsResponse listTenants (int maxResults , String pageToken )
0 commit comments