001package com.box.sdkgen.managers.users; 002 003import static com.box.sdkgen.internal.utils.UtilsManager.convertToString; 004import static com.box.sdkgen.internal.utils.UtilsManager.entryOf; 005import static com.box.sdkgen.internal.utils.UtilsManager.mapOf; 006import static com.box.sdkgen.internal.utils.UtilsManager.mergeMaps; 007import static com.box.sdkgen.internal.utils.UtilsManager.prepareParams; 008 009import com.box.sdkgen.networking.auth.Authentication; 010import com.box.sdkgen.networking.fetchoptions.FetchOptions; 011import com.box.sdkgen.networking.fetchoptions.ResponseFormat; 012import com.box.sdkgen.networking.fetchresponse.FetchResponse; 013import com.box.sdkgen.networking.network.NetworkSession; 014import com.box.sdkgen.schemas.userfull.UserFull; 015import com.box.sdkgen.schemas.users.Users; 016import com.box.sdkgen.serialization.json.JsonManager; 017import java.util.Map; 018 019public class UsersManager { 020 021 public Authentication auth; 022 023 public NetworkSession networkSession; 024 025 public UsersManager() { 026 this.networkSession = new NetworkSession(); 027 } 028 029 protected UsersManager(Builder builder) { 030 this.auth = builder.auth; 031 this.networkSession = builder.networkSession; 032 } 033 034 /** 035 * Returns a list of all users for the Enterprise along with their `user_id`, `public_name`, and 036 * `login`. 037 * 038 * <p>The application and the authenticated user need to have the permission to look up users in 039 * the entire enterprise. 040 */ 041 public Users getUsers() { 042 return getUsers(new GetUsersQueryParams(), new GetUsersHeaders()); 043 } 044 045 /** 046 * Returns a list of all users for the Enterprise along with their `user_id`, `public_name`, and 047 * `login`. 048 * 049 * <p>The application and the authenticated user need to have the permission to look up users in 050 * the entire enterprise. 051 * 052 * @param queryParams Query parameters of getUsers method 053 */ 054 public Users getUsers(GetUsersQueryParams queryParams) { 055 return getUsers(queryParams, new GetUsersHeaders()); 056 } 057 058 /** 059 * Returns a list of all users for the Enterprise along with their `user_id`, `public_name`, and 060 * `login`. 061 * 062 * <p>The application and the authenticated user need to have the permission to look up users in 063 * the entire enterprise. 064 * 065 * @param headers Headers of getUsers method 066 */ 067 public Users getUsers(GetUsersHeaders headers) { 068 return getUsers(new GetUsersQueryParams(), headers); 069 } 070 071 /** 072 * Returns a list of all users for the Enterprise along with their `user_id`, `public_name`, and 073 * `login`. 074 * 075 * <p>The application and the authenticated user need to have the permission to look up users in 076 * the entire enterprise. 077 * 078 * @param queryParams Query parameters of getUsers method 079 * @param headers Headers of getUsers method 080 */ 081 public Users getUsers(GetUsersQueryParams queryParams, GetUsersHeaders headers) { 082 Map<String, String> queryParamsMap = 083 prepareParams( 084 mapOf( 085 entryOf("filter_term", convertToString(queryParams.getFilterTerm())), 086 entryOf("user_type", convertToString(queryParams.getUserType())), 087 entryOf( 088 "external_app_user_id", convertToString(queryParams.getExternalAppUserId())), 089 entryOf("fields", convertToString(queryParams.getFields())), 090 entryOf("offset", convertToString(queryParams.getOffset())), 091 entryOf("limit", convertToString(queryParams.getLimit())), 092 entryOf("usemarker", convertToString(queryParams.getUsemarker())), 093 entryOf("marker", convertToString(queryParams.getMarker())))); 094 Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders())); 095 FetchResponse response = 096 this.networkSession 097 .getNetworkClient() 098 .fetch( 099 new FetchOptions.Builder( 100 String.join( 101 "", this.networkSession.getBaseUrls().getBaseUrl(), "/2.0/users"), 102 "GET") 103 .params(queryParamsMap) 104 .headers(headersMap) 105 .responseFormat(ResponseFormat.JSON) 106 .auth(this.auth) 107 .networkSession(this.networkSession) 108 .build()); 109 return JsonManager.deserialize(response.getData(), Users.class); 110 } 111 112 /** 113 * Creates a new managed user in an enterprise. This endpoint is only available to users and 114 * applications with the right admin permissions. 115 * 116 * @param requestBody Request body of createUser method 117 */ 118 public UserFull createUser(CreateUserRequestBody requestBody) { 119 return createUser(requestBody, new CreateUserQueryParams(), new CreateUserHeaders()); 120 } 121 122 /** 123 * Creates a new managed user in an enterprise. This endpoint is only available to users and 124 * applications with the right admin permissions. 125 * 126 * @param requestBody Request body of createUser method 127 * @param queryParams Query parameters of createUser method 128 */ 129 public UserFull createUser(CreateUserRequestBody requestBody, CreateUserQueryParams queryParams) { 130 return createUser(requestBody, queryParams, new CreateUserHeaders()); 131 } 132 133 /** 134 * Creates a new managed user in an enterprise. This endpoint is only available to users and 135 * applications with the right admin permissions. 136 * 137 * @param requestBody Request body of createUser method 138 * @param headers Headers of createUser method 139 */ 140 public UserFull createUser(CreateUserRequestBody requestBody, CreateUserHeaders headers) { 141 return createUser(requestBody, new CreateUserQueryParams(), headers); 142 } 143 144 /** 145 * Creates a new managed user in an enterprise. This endpoint is only available to users and 146 * applications with the right admin permissions. 147 * 148 * @param requestBody Request body of createUser method 149 * @param queryParams Query parameters of createUser method 150 * @param headers Headers of createUser method 151 */ 152 public UserFull createUser( 153 CreateUserRequestBody requestBody, 154 CreateUserQueryParams queryParams, 155 CreateUserHeaders headers) { 156 Map<String, String> queryParamsMap = 157 prepareParams(mapOf(entryOf("fields", convertToString(queryParams.getFields())))); 158 Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders())); 159 FetchResponse response = 160 this.networkSession 161 .getNetworkClient() 162 .fetch( 163 new FetchOptions.Builder( 164 String.join( 165 "", this.networkSession.getBaseUrls().getBaseUrl(), "/2.0/users"), 166 "POST") 167 .params(queryParamsMap) 168 .headers(headersMap) 169 .data(JsonManager.serialize(requestBody)) 170 .contentType("application/json") 171 .responseFormat(ResponseFormat.JSON) 172 .auth(this.auth) 173 .networkSession(this.networkSession) 174 .build()); 175 return JsonManager.deserialize(response.getData(), UserFull.class); 176 } 177 178 /** 179 * Retrieves information about the user who is currently authenticated. 180 * 181 * <p>In the case of a client-side authenticated OAuth 2.0 application this will be the user who 182 * authorized the app. 183 * 184 * <p>In the case of a JWT, server-side authenticated application this will be the service account 185 * that belongs to the application by default. 186 * 187 * <p>Use the `As-User` header to change who this API call is made on behalf of. 188 */ 189 public UserFull getUserMe() { 190 return getUserMe(new GetUserMeQueryParams(), new GetUserMeHeaders()); 191 } 192 193 /** 194 * Retrieves information about the user who is currently authenticated. 195 * 196 * <p>In the case of a client-side authenticated OAuth 2.0 application this will be the user who 197 * authorized the app. 198 * 199 * <p>In the case of a JWT, server-side authenticated application this will be the service account 200 * that belongs to the application by default. 201 * 202 * <p>Use the `As-User` header to change who this API call is made on behalf of. 203 * 204 * @param queryParams Query parameters of getUserMe method 205 */ 206 public UserFull getUserMe(GetUserMeQueryParams queryParams) { 207 return getUserMe(queryParams, new GetUserMeHeaders()); 208 } 209 210 /** 211 * Retrieves information about the user who is currently authenticated. 212 * 213 * <p>In the case of a client-side authenticated OAuth 2.0 application this will be the user who 214 * authorized the app. 215 * 216 * <p>In the case of a JWT, server-side authenticated application this will be the service account 217 * that belongs to the application by default. 218 * 219 * <p>Use the `As-User` header to change who this API call is made on behalf of. 220 * 221 * @param headers Headers of getUserMe method 222 */ 223 public UserFull getUserMe(GetUserMeHeaders headers) { 224 return getUserMe(new GetUserMeQueryParams(), headers); 225 } 226 227 /** 228 * Retrieves information about the user who is currently authenticated. 229 * 230 * <p>In the case of a client-side authenticated OAuth 2.0 application this will be the user who 231 * authorized the app. 232 * 233 * <p>In the case of a JWT, server-side authenticated application this will be the service account 234 * that belongs to the application by default. 235 * 236 * <p>Use the `As-User` header to change who this API call is made on behalf of. 237 * 238 * @param queryParams Query parameters of getUserMe method 239 * @param headers Headers of getUserMe method 240 */ 241 public UserFull getUserMe(GetUserMeQueryParams queryParams, GetUserMeHeaders headers) { 242 Map<String, String> queryParamsMap = 243 prepareParams(mapOf(entryOf("fields", convertToString(queryParams.getFields())))); 244 Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders())); 245 FetchResponse response = 246 this.networkSession 247 .getNetworkClient() 248 .fetch( 249 new FetchOptions.Builder( 250 String.join( 251 "", this.networkSession.getBaseUrls().getBaseUrl(), "/2.0/users/me"), 252 "GET") 253 .params(queryParamsMap) 254 .headers(headersMap) 255 .responseFormat(ResponseFormat.JSON) 256 .auth(this.auth) 257 .networkSession(this.networkSession) 258 .build()); 259 return JsonManager.deserialize(response.getData(), UserFull.class); 260 } 261 262 /** 263 * Retrieves information about a user in the enterprise. 264 * 265 * <p>The application and the authenticated user need to have the permission to look up users in 266 * the entire enterprise. 267 * 268 * <p>This endpoint also returns a limited set of information for external users who are 269 * collaborated on content owned by the enterprise for authenticated users with the right scopes. 270 * In this case, disallowed fields will return null instead. 271 * 272 * @param userId The ID of the user. Example: "12345" 273 */ 274 public UserFull getUserById(String userId) { 275 return getUserById(userId, new GetUserByIdQueryParams(), new GetUserByIdHeaders()); 276 } 277 278 /** 279 * Retrieves information about a user in the enterprise. 280 * 281 * <p>The application and the authenticated user need to have the permission to look up users in 282 * the entire enterprise. 283 * 284 * <p>This endpoint also returns a limited set of information for external users who are 285 * collaborated on content owned by the enterprise for authenticated users with the right scopes. 286 * In this case, disallowed fields will return null instead. 287 * 288 * @param userId The ID of the user. Example: "12345" 289 * @param queryParams Query parameters of getUserById method 290 */ 291 public UserFull getUserById(String userId, GetUserByIdQueryParams queryParams) { 292 return getUserById(userId, queryParams, new GetUserByIdHeaders()); 293 } 294 295 /** 296 * Retrieves information about a user in the enterprise. 297 * 298 * <p>The application and the authenticated user need to have the permission to look up users in 299 * the entire enterprise. 300 * 301 * <p>This endpoint also returns a limited set of information for external users who are 302 * collaborated on content owned by the enterprise for authenticated users with the right scopes. 303 * In this case, disallowed fields will return null instead. 304 * 305 * @param userId The ID of the user. Example: "12345" 306 * @param headers Headers of getUserById method 307 */ 308 public UserFull getUserById(String userId, GetUserByIdHeaders headers) { 309 return getUserById(userId, new GetUserByIdQueryParams(), headers); 310 } 311 312 /** 313 * Retrieves information about a user in the enterprise. 314 * 315 * <p>The application and the authenticated user need to have the permission to look up users in 316 * the entire enterprise. 317 * 318 * <p>This endpoint also returns a limited set of information for external users who are 319 * collaborated on content owned by the enterprise for authenticated users with the right scopes. 320 * In this case, disallowed fields will return null instead. 321 * 322 * @param userId The ID of the user. Example: "12345" 323 * @param queryParams Query parameters of getUserById method 324 * @param headers Headers of getUserById method 325 */ 326 public UserFull getUserById( 327 String userId, GetUserByIdQueryParams queryParams, GetUserByIdHeaders headers) { 328 Map<String, String> queryParamsMap = 329 prepareParams(mapOf(entryOf("fields", convertToString(queryParams.getFields())))); 330 Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders())); 331 FetchResponse response = 332 this.networkSession 333 .getNetworkClient() 334 .fetch( 335 new FetchOptions.Builder( 336 String.join( 337 "", 338 this.networkSession.getBaseUrls().getBaseUrl(), 339 "/2.0/users/", 340 convertToString(userId)), 341 "GET") 342 .params(queryParamsMap) 343 .headers(headersMap) 344 .responseFormat(ResponseFormat.JSON) 345 .auth(this.auth) 346 .networkSession(this.networkSession) 347 .build()); 348 return JsonManager.deserialize(response.getData(), UserFull.class); 349 } 350 351 /** 352 * Updates a managed or app user in an enterprise. This endpoint is only available to users and 353 * applications with the right admin permissions. 354 * 355 * @param userId The ID of the user. Example: "12345" 356 */ 357 public UserFull updateUserById(String userId) { 358 return updateUserById( 359 userId, 360 new UpdateUserByIdRequestBody(), 361 new UpdateUserByIdQueryParams(), 362 new UpdateUserByIdHeaders()); 363 } 364 365 /** 366 * Updates a managed or app user in an enterprise. This endpoint is only available to users and 367 * applications with the right admin permissions. 368 * 369 * @param userId The ID of the user. Example: "12345" 370 * @param requestBody Request body of updateUserById method 371 */ 372 public UserFull updateUserById(String userId, UpdateUserByIdRequestBody requestBody) { 373 return updateUserById( 374 userId, requestBody, new UpdateUserByIdQueryParams(), new UpdateUserByIdHeaders()); 375 } 376 377 /** 378 * Updates a managed or app user in an enterprise. This endpoint is only available to users and 379 * applications with the right admin permissions. 380 * 381 * @param userId The ID of the user. Example: "12345" 382 * @param queryParams Query parameters of updateUserById method 383 */ 384 public UserFull updateUserById(String userId, UpdateUserByIdQueryParams queryParams) { 385 return updateUserById( 386 userId, new UpdateUserByIdRequestBody(), queryParams, new UpdateUserByIdHeaders()); 387 } 388 389 /** 390 * Updates a managed or app user in an enterprise. This endpoint is only available to users and 391 * applications with the right admin permissions. 392 * 393 * @param userId The ID of the user. Example: "12345" 394 * @param requestBody Request body of updateUserById method 395 * @param queryParams Query parameters of updateUserById method 396 */ 397 public UserFull updateUserById( 398 String userId, UpdateUserByIdRequestBody requestBody, UpdateUserByIdQueryParams queryParams) { 399 return updateUserById(userId, requestBody, queryParams, new UpdateUserByIdHeaders()); 400 } 401 402 /** 403 * Updates a managed or app user in an enterprise. This endpoint is only available to users and 404 * applications with the right admin permissions. 405 * 406 * @param userId The ID of the user. Example: "12345" 407 * @param headers Headers of updateUserById method 408 */ 409 public UserFull updateUserById(String userId, UpdateUserByIdHeaders headers) { 410 return updateUserById( 411 userId, new UpdateUserByIdRequestBody(), new UpdateUserByIdQueryParams(), headers); 412 } 413 414 /** 415 * Updates a managed or app user in an enterprise. This endpoint is only available to users and 416 * applications with the right admin permissions. 417 * 418 * @param userId The ID of the user. Example: "12345" 419 * @param requestBody Request body of updateUserById method 420 * @param headers Headers of updateUserById method 421 */ 422 public UserFull updateUserById( 423 String userId, UpdateUserByIdRequestBody requestBody, UpdateUserByIdHeaders headers) { 424 return updateUserById(userId, requestBody, new UpdateUserByIdQueryParams(), headers); 425 } 426 427 /** 428 * Updates a managed or app user in an enterprise. This endpoint is only available to users and 429 * applications with the right admin permissions. 430 * 431 * @param userId The ID of the user. Example: "12345" 432 * @param queryParams Query parameters of updateUserById method 433 * @param headers Headers of updateUserById method 434 */ 435 public UserFull updateUserById( 436 String userId, UpdateUserByIdQueryParams queryParams, UpdateUserByIdHeaders headers) { 437 return updateUserById(userId, new UpdateUserByIdRequestBody(), queryParams, headers); 438 } 439 440 /** 441 * Updates a managed or app user in an enterprise. This endpoint is only available to users and 442 * applications with the right admin permissions. 443 * 444 * @param userId The ID of the user. Example: "12345" 445 * @param requestBody Request body of updateUserById method 446 * @param queryParams Query parameters of updateUserById method 447 * @param headers Headers of updateUserById method 448 */ 449 public UserFull updateUserById( 450 String userId, 451 UpdateUserByIdRequestBody requestBody, 452 UpdateUserByIdQueryParams queryParams, 453 UpdateUserByIdHeaders headers) { 454 Map<String, String> queryParamsMap = 455 prepareParams(mapOf(entryOf("fields", convertToString(queryParams.getFields())))); 456 Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders())); 457 FetchResponse response = 458 this.networkSession 459 .getNetworkClient() 460 .fetch( 461 new FetchOptions.Builder( 462 String.join( 463 "", 464 this.networkSession.getBaseUrls().getBaseUrl(), 465 "/2.0/users/", 466 convertToString(userId)), 467 "PUT") 468 .params(queryParamsMap) 469 .headers(headersMap) 470 .data(JsonManager.serialize(requestBody)) 471 .contentType("application/json") 472 .responseFormat(ResponseFormat.JSON) 473 .auth(this.auth) 474 .networkSession(this.networkSession) 475 .build()); 476 return JsonManager.deserialize(response.getData(), UserFull.class); 477 } 478 479 /** 480 * Deletes a user. By default, this operation fails if the user still owns any content. To 481 * proceed, move their owned content first, or use the `force` parameter to delete the user and 482 * their files. 483 * 484 * @param userId The ID of the user. Example: "12345" 485 */ 486 public void deleteUserById(String userId) { 487 deleteUserById(userId, new DeleteUserByIdQueryParams(), new DeleteUserByIdHeaders()); 488 } 489 490 /** 491 * Deletes a user. By default, this operation fails if the user still owns any content. To 492 * proceed, move their owned content first, or use the `force` parameter to delete the user and 493 * their files. 494 * 495 * @param userId The ID of the user. Example: "12345" 496 * @param queryParams Query parameters of deleteUserById method 497 */ 498 public void deleteUserById(String userId, DeleteUserByIdQueryParams queryParams) { 499 deleteUserById(userId, queryParams, new DeleteUserByIdHeaders()); 500 } 501 502 /** 503 * Deletes a user. By default, this operation fails if the user still owns any content. To 504 * proceed, move their owned content first, or use the `force` parameter to delete the user and 505 * their files. 506 * 507 * @param userId The ID of the user. Example: "12345" 508 * @param headers Headers of deleteUserById method 509 */ 510 public void deleteUserById(String userId, DeleteUserByIdHeaders headers) { 511 deleteUserById(userId, new DeleteUserByIdQueryParams(), headers); 512 } 513 514 /** 515 * Deletes a user. By default, this operation fails if the user still owns any content. To 516 * proceed, move their owned content first, or use the `force` parameter to delete the user and 517 * their files. 518 * 519 * @param userId The ID of the user. Example: "12345" 520 * @param queryParams Query parameters of deleteUserById method 521 * @param headers Headers of deleteUserById method 522 */ 523 public void deleteUserById( 524 String userId, DeleteUserByIdQueryParams queryParams, DeleteUserByIdHeaders headers) { 525 Map<String, String> queryParamsMap = 526 prepareParams( 527 mapOf( 528 entryOf("notify", convertToString(queryParams.getNotify())), 529 entryOf("force", convertToString(queryParams.getForce())))); 530 Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders())); 531 FetchResponse response = 532 this.networkSession 533 .getNetworkClient() 534 .fetch( 535 new FetchOptions.Builder( 536 String.join( 537 "", 538 this.networkSession.getBaseUrls().getBaseUrl(), 539 "/2.0/users/", 540 convertToString(userId)), 541 "DELETE") 542 .params(queryParamsMap) 543 .headers(headersMap) 544 .responseFormat(ResponseFormat.NO_CONTENT) 545 .auth(this.auth) 546 .networkSession(this.networkSession) 547 .build()); 548 } 549 550 public Authentication getAuth() { 551 return auth; 552 } 553 554 public NetworkSession getNetworkSession() { 555 return networkSession; 556 } 557 558 public static class Builder { 559 560 protected Authentication auth; 561 562 protected NetworkSession networkSession; 563 564 public Builder() {} 565 566 public Builder auth(Authentication auth) { 567 this.auth = auth; 568 return this; 569 } 570 571 public Builder networkSession(NetworkSession networkSession) { 572 this.networkSession = networkSession; 573 return this; 574 } 575 576 public UsersManager build() { 577 if (this.networkSession == null) { 578 this.networkSession = new NetworkSession(); 579 } 580 return new UsersManager(this); 581 } 582 } 583}