001package com.box.sdkgen.managers.groups;
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.groupfull.GroupFull;
015import com.box.sdkgen.schemas.groups.Groups;
016import com.box.sdkgen.serialization.json.JsonManager;
017import java.util.Map;
018
019public class GroupsManager {
020
021  public Authentication auth;
022
023  public NetworkSession networkSession;
024
025  public GroupsManager() {
026    this.networkSession = new NetworkSession();
027  }
028
029  protected GroupsManager(Builder builder) {
030    this.auth = builder.auth;
031    this.networkSession = builder.networkSession;
032  }
033
034  /**
035   * Retrieves all of the groups for a given enterprise. The user must have admin permissions to
036   * inspect enterprise's groups.
037   */
038  public Groups getGroups() {
039    return getGroups(new GetGroupsQueryParams(), new GetGroupsHeaders());
040  }
041
042  /**
043   * Retrieves all of the groups for a given enterprise. The user must have admin permissions to
044   * inspect enterprise's groups.
045   *
046   * @param queryParams Query parameters of getGroups method
047   */
048  public Groups getGroups(GetGroupsQueryParams queryParams) {
049    return getGroups(queryParams, new GetGroupsHeaders());
050  }
051
052  /**
053   * Retrieves all of the groups for a given enterprise. The user must have admin permissions to
054   * inspect enterprise's groups.
055   *
056   * @param headers Headers of getGroups method
057   */
058  public Groups getGroups(GetGroupsHeaders headers) {
059    return getGroups(new GetGroupsQueryParams(), headers);
060  }
061
062  /**
063   * Retrieves all of the groups for a given enterprise. The user must have admin permissions to
064   * inspect enterprise's groups.
065   *
066   * @param queryParams Query parameters of getGroups method
067   * @param headers Headers of getGroups method
068   */
069  public Groups getGroups(GetGroupsQueryParams queryParams, GetGroupsHeaders headers) {
070    Map<String, String> queryParamsMap =
071        prepareParams(
072            mapOf(
073                entryOf("filter_term", convertToString(queryParams.getFilterTerm())),
074                entryOf("fields", convertToString(queryParams.getFields())),
075                entryOf("limit", convertToString(queryParams.getLimit())),
076                entryOf("offset", convertToString(queryParams.getOffset()))));
077    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
078    FetchResponse response =
079        this.networkSession
080            .getNetworkClient()
081            .fetch(
082                new FetchOptions.Builder(
083                        String.join(
084                            "", this.networkSession.getBaseUrls().getBaseUrl(), "/2.0/groups"),
085                        "GET")
086                    .params(queryParamsMap)
087                    .headers(headersMap)
088                    .responseFormat(ResponseFormat.JSON)
089                    .auth(this.auth)
090                    .networkSession(this.networkSession)
091                    .build());
092    return JsonManager.deserialize(response.getData(), Groups.class);
093  }
094
095  /**
096   * Creates a new group of users in an enterprise. Only users with admin permissions can create new
097   * groups.
098   *
099   * @param requestBody Request body of createGroup method
100   */
101  public GroupFull createGroup(CreateGroupRequestBody requestBody) {
102    return createGroup(requestBody, new CreateGroupQueryParams(), new CreateGroupHeaders());
103  }
104
105  /**
106   * Creates a new group of users in an enterprise. Only users with admin permissions can create new
107   * groups.
108   *
109   * @param requestBody Request body of createGroup method
110   * @param queryParams Query parameters of createGroup method
111   */
112  public GroupFull createGroup(
113      CreateGroupRequestBody requestBody, CreateGroupQueryParams queryParams) {
114    return createGroup(requestBody, queryParams, new CreateGroupHeaders());
115  }
116
117  /**
118   * Creates a new group of users in an enterprise. Only users with admin permissions can create new
119   * groups.
120   *
121   * @param requestBody Request body of createGroup method
122   * @param headers Headers of createGroup method
123   */
124  public GroupFull createGroup(CreateGroupRequestBody requestBody, CreateGroupHeaders headers) {
125    return createGroup(requestBody, new CreateGroupQueryParams(), headers);
126  }
127
128  /**
129   * Creates a new group of users in an enterprise. Only users with admin permissions can create new
130   * groups.
131   *
132   * @param requestBody Request body of createGroup method
133   * @param queryParams Query parameters of createGroup method
134   * @param headers Headers of createGroup method
135   */
136  public GroupFull createGroup(
137      CreateGroupRequestBody requestBody,
138      CreateGroupQueryParams queryParams,
139      CreateGroupHeaders headers) {
140    Map<String, String> queryParamsMap =
141        prepareParams(mapOf(entryOf("fields", convertToString(queryParams.getFields()))));
142    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
143    FetchResponse response =
144        this.networkSession
145            .getNetworkClient()
146            .fetch(
147                new FetchOptions.Builder(
148                        String.join(
149                            "", this.networkSession.getBaseUrls().getBaseUrl(), "/2.0/groups"),
150                        "POST")
151                    .params(queryParamsMap)
152                    .headers(headersMap)
153                    .data(JsonManager.serialize(requestBody))
154                    .contentType("application/json")
155                    .responseFormat(ResponseFormat.JSON)
156                    .auth(this.auth)
157                    .networkSession(this.networkSession)
158                    .build());
159    return JsonManager.deserialize(response.getData(), GroupFull.class);
160  }
161
162  /**
163   * Retrieves information about a group. Only members of this group or users with admin-level
164   * permissions will be able to use this API.
165   *
166   * @param groupId The ID of the group. Example: "57645"
167   */
168  public GroupFull getGroupById(String groupId) {
169    return getGroupById(groupId, new GetGroupByIdQueryParams(), new GetGroupByIdHeaders());
170  }
171
172  /**
173   * Retrieves information about a group. Only members of this group or users with admin-level
174   * permissions will be able to use this API.
175   *
176   * @param groupId The ID of the group. Example: "57645"
177   * @param queryParams Query parameters of getGroupById method
178   */
179  public GroupFull getGroupById(String groupId, GetGroupByIdQueryParams queryParams) {
180    return getGroupById(groupId, queryParams, new GetGroupByIdHeaders());
181  }
182
183  /**
184   * Retrieves information about a group. Only members of this group or users with admin-level
185   * permissions will be able to use this API.
186   *
187   * @param groupId The ID of the group. Example: "57645"
188   * @param headers Headers of getGroupById method
189   */
190  public GroupFull getGroupById(String groupId, GetGroupByIdHeaders headers) {
191    return getGroupById(groupId, new GetGroupByIdQueryParams(), headers);
192  }
193
194  /**
195   * Retrieves information about a group. Only members of this group or users with admin-level
196   * permissions will be able to use this API.
197   *
198   * @param groupId The ID of the group. Example: "57645"
199   * @param queryParams Query parameters of getGroupById method
200   * @param headers Headers of getGroupById method
201   */
202  public GroupFull getGroupById(
203      String groupId, GetGroupByIdQueryParams queryParams, GetGroupByIdHeaders headers) {
204    Map<String, String> queryParamsMap =
205        prepareParams(mapOf(entryOf("fields", convertToString(queryParams.getFields()))));
206    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
207    FetchResponse response =
208        this.networkSession
209            .getNetworkClient()
210            .fetch(
211                new FetchOptions.Builder(
212                        String.join(
213                            "",
214                            this.networkSession.getBaseUrls().getBaseUrl(),
215                            "/2.0/groups/",
216                            convertToString(groupId)),
217                        "GET")
218                    .params(queryParamsMap)
219                    .headers(headersMap)
220                    .responseFormat(ResponseFormat.JSON)
221                    .auth(this.auth)
222                    .networkSession(this.networkSession)
223                    .build());
224    return JsonManager.deserialize(response.getData(), GroupFull.class);
225  }
226
227  /**
228   * Updates a specific group. Only admins of this group or users with admin-level permissions will
229   * be able to use this API.
230   *
231   * @param groupId The ID of the group. Example: "57645"
232   */
233  public GroupFull updateGroupById(String groupId) {
234    return updateGroupById(
235        groupId,
236        new UpdateGroupByIdRequestBody(),
237        new UpdateGroupByIdQueryParams(),
238        new UpdateGroupByIdHeaders());
239  }
240
241  /**
242   * Updates a specific group. Only admins of this group or users with admin-level permissions will
243   * be able to use this API.
244   *
245   * @param groupId The ID of the group. Example: "57645"
246   * @param requestBody Request body of updateGroupById method
247   */
248  public GroupFull updateGroupById(String groupId, UpdateGroupByIdRequestBody requestBody) {
249    return updateGroupById(
250        groupId, requestBody, new UpdateGroupByIdQueryParams(), new UpdateGroupByIdHeaders());
251  }
252
253  /**
254   * Updates a specific group. Only admins of this group or users with admin-level permissions will
255   * be able to use this API.
256   *
257   * @param groupId The ID of the group. Example: "57645"
258   * @param queryParams Query parameters of updateGroupById method
259   */
260  public GroupFull updateGroupById(String groupId, UpdateGroupByIdQueryParams queryParams) {
261    return updateGroupById(
262        groupId, new UpdateGroupByIdRequestBody(), queryParams, new UpdateGroupByIdHeaders());
263  }
264
265  /**
266   * Updates a specific group. Only admins of this group or users with admin-level permissions will
267   * be able to use this API.
268   *
269   * @param groupId The ID of the group. Example: "57645"
270   * @param requestBody Request body of updateGroupById method
271   * @param queryParams Query parameters of updateGroupById method
272   */
273  public GroupFull updateGroupById(
274      String groupId,
275      UpdateGroupByIdRequestBody requestBody,
276      UpdateGroupByIdQueryParams queryParams) {
277    return updateGroupById(groupId, requestBody, queryParams, new UpdateGroupByIdHeaders());
278  }
279
280  /**
281   * Updates a specific group. Only admins of this group or users with admin-level permissions will
282   * be able to use this API.
283   *
284   * @param groupId The ID of the group. Example: "57645"
285   * @param headers Headers of updateGroupById method
286   */
287  public GroupFull updateGroupById(String groupId, UpdateGroupByIdHeaders headers) {
288    return updateGroupById(
289        groupId, new UpdateGroupByIdRequestBody(), new UpdateGroupByIdQueryParams(), headers);
290  }
291
292  /**
293   * Updates a specific group. Only admins of this group or users with admin-level permissions will
294   * be able to use this API.
295   *
296   * @param groupId The ID of the group. Example: "57645"
297   * @param requestBody Request body of updateGroupById method
298   * @param headers Headers of updateGroupById method
299   */
300  public GroupFull updateGroupById(
301      String groupId, UpdateGroupByIdRequestBody requestBody, UpdateGroupByIdHeaders headers) {
302    return updateGroupById(groupId, requestBody, new UpdateGroupByIdQueryParams(), headers);
303  }
304
305  /**
306   * Updates a specific group. Only admins of this group or users with admin-level permissions will
307   * be able to use this API.
308   *
309   * @param groupId The ID of the group. Example: "57645"
310   * @param queryParams Query parameters of updateGroupById method
311   * @param headers Headers of updateGroupById method
312   */
313  public GroupFull updateGroupById(
314      String groupId, UpdateGroupByIdQueryParams queryParams, UpdateGroupByIdHeaders headers) {
315    return updateGroupById(groupId, new UpdateGroupByIdRequestBody(), queryParams, headers);
316  }
317
318  /**
319   * Updates a specific group. Only admins of this group or users with admin-level permissions will
320   * be able to use this API.
321   *
322   * @param groupId The ID of the group. Example: "57645"
323   * @param requestBody Request body of updateGroupById method
324   * @param queryParams Query parameters of updateGroupById method
325   * @param headers Headers of updateGroupById method
326   */
327  public GroupFull updateGroupById(
328      String groupId,
329      UpdateGroupByIdRequestBody requestBody,
330      UpdateGroupByIdQueryParams queryParams,
331      UpdateGroupByIdHeaders headers) {
332    Map<String, String> queryParamsMap =
333        prepareParams(mapOf(entryOf("fields", convertToString(queryParams.getFields()))));
334    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
335    FetchResponse response =
336        this.networkSession
337            .getNetworkClient()
338            .fetch(
339                new FetchOptions.Builder(
340                        String.join(
341                            "",
342                            this.networkSession.getBaseUrls().getBaseUrl(),
343                            "/2.0/groups/",
344                            convertToString(groupId)),
345                        "PUT")
346                    .params(queryParamsMap)
347                    .headers(headersMap)
348                    .data(JsonManager.serialize(requestBody))
349                    .contentType("application/json")
350                    .responseFormat(ResponseFormat.JSON)
351                    .auth(this.auth)
352                    .networkSession(this.networkSession)
353                    .build());
354    return JsonManager.deserialize(response.getData(), GroupFull.class);
355  }
356
357  /**
358   * Permanently deletes a group. Only users with admin-level permissions will be able to use this
359   * API.
360   *
361   * @param groupId The ID of the group. Example: "57645"
362   */
363  public void deleteGroupById(String groupId) {
364    deleteGroupById(groupId, new DeleteGroupByIdHeaders());
365  }
366
367  /**
368   * Permanently deletes a group. Only users with admin-level permissions will be able to use this
369   * API.
370   *
371   * @param groupId The ID of the group. Example: "57645"
372   * @param headers Headers of deleteGroupById method
373   */
374  public void deleteGroupById(String groupId, DeleteGroupByIdHeaders headers) {
375    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
376    FetchResponse response =
377        this.networkSession
378            .getNetworkClient()
379            .fetch(
380                new FetchOptions.Builder(
381                        String.join(
382                            "",
383                            this.networkSession.getBaseUrls().getBaseUrl(),
384                            "/2.0/groups/",
385                            convertToString(groupId)),
386                        "DELETE")
387                    .headers(headersMap)
388                    .responseFormat(ResponseFormat.NO_CONTENT)
389                    .auth(this.auth)
390                    .networkSession(this.networkSession)
391                    .build());
392  }
393
394  public Authentication getAuth() {
395    return auth;
396  }
397
398  public NetworkSession getNetworkSession() {
399    return networkSession;
400  }
401
402  public static class Builder {
403
404    protected Authentication auth;
405
406    protected NetworkSession networkSession;
407
408    public Builder() {}
409
410    public Builder auth(Authentication auth) {
411      this.auth = auth;
412      return this;
413    }
414
415    public Builder networkSession(NetworkSession networkSession) {
416      this.networkSession = networkSession;
417      return this;
418    }
419
420    public GroupsManager build() {
421      if (this.networkSession == null) {
422        this.networkSession = new NetworkSession();
423      }
424      return new GroupsManager(this);
425    }
426  }
427}