001package com.box.sdkgen.managers.sessiontermination;
002
003import static com.box.sdkgen.internal.utils.UtilsManager.mapOf;
004import static com.box.sdkgen.internal.utils.UtilsManager.mergeMaps;
005import static com.box.sdkgen.internal.utils.UtilsManager.prepareParams;
006
007import com.box.sdkgen.networking.auth.Authentication;
008import com.box.sdkgen.networking.fetchoptions.FetchOptions;
009import com.box.sdkgen.networking.fetchoptions.ResponseFormat;
010import com.box.sdkgen.networking.fetchresponse.FetchResponse;
011import com.box.sdkgen.networking.network.NetworkSession;
012import com.box.sdkgen.schemas.sessionterminationmessage.SessionTerminationMessage;
013import com.box.sdkgen.serialization.json.JsonManager;
014import java.util.Map;
015
016public class SessionTerminationManager {
017
018  public Authentication auth;
019
020  public NetworkSession networkSession;
021
022  public SessionTerminationManager() {
023    this.networkSession = new NetworkSession();
024  }
025
026  protected SessionTerminationManager(Builder builder) {
027    this.auth = builder.auth;
028    this.networkSession = builder.networkSession;
029  }
030
031  /**
032   * Validates the roles and permissions of the user, and creates asynchronous jobs to terminate the
033   * user's sessions. Returns the status for the POST request.
034   *
035   * @param requestBody Request body of terminateUsersSessions method
036   */
037  public SessionTerminationMessage terminateUsersSessions(
038      TerminateUsersSessionsRequestBody requestBody) {
039    return terminateUsersSessions(requestBody, new TerminateUsersSessionsHeaders());
040  }
041
042  /**
043   * Validates the roles and permissions of the user, and creates asynchronous jobs to terminate the
044   * user's sessions. Returns the status for the POST request.
045   *
046   * @param requestBody Request body of terminateUsersSessions method
047   * @param headers Headers of terminateUsersSessions method
048   */
049  public SessionTerminationMessage terminateUsersSessions(
050      TerminateUsersSessionsRequestBody requestBody, TerminateUsersSessionsHeaders headers) {
051    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
052    FetchResponse response =
053        this.networkSession
054            .getNetworkClient()
055            .fetch(
056                new FetchOptions.Builder(
057                        String.join(
058                            "",
059                            this.networkSession.getBaseUrls().getBaseUrl(),
060                            "/2.0/users/terminate_sessions"),
061                        "POST")
062                    .headers(headersMap)
063                    .data(JsonManager.serialize(requestBody))
064                    .contentType("application/json")
065                    .responseFormat(ResponseFormat.JSON)
066                    .auth(this.auth)
067                    .networkSession(this.networkSession)
068                    .build());
069    return JsonManager.deserialize(response.getData(), SessionTerminationMessage.class);
070  }
071
072  /**
073   * Validates the roles and permissions of the group, and creates asynchronous jobs to terminate
074   * the group's sessions. Returns the status for the POST request.
075   *
076   * @param requestBody Request body of terminateGroupsSessions method
077   */
078  public SessionTerminationMessage terminateGroupsSessions(
079      TerminateGroupsSessionsRequestBody requestBody) {
080    return terminateGroupsSessions(requestBody, new TerminateGroupsSessionsHeaders());
081  }
082
083  /**
084   * Validates the roles and permissions of the group, and creates asynchronous jobs to terminate
085   * the group's sessions. Returns the status for the POST request.
086   *
087   * @param requestBody Request body of terminateGroupsSessions method
088   * @param headers Headers of terminateGroupsSessions method
089   */
090  public SessionTerminationMessage terminateGroupsSessions(
091      TerminateGroupsSessionsRequestBody requestBody, TerminateGroupsSessionsHeaders headers) {
092    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
093    FetchResponse response =
094        this.networkSession
095            .getNetworkClient()
096            .fetch(
097                new FetchOptions.Builder(
098                        String.join(
099                            "",
100                            this.networkSession.getBaseUrls().getBaseUrl(),
101                            "/2.0/groups/terminate_sessions"),
102                        "POST")
103                    .headers(headersMap)
104                    .data(JsonManager.serialize(requestBody))
105                    .contentType("application/json")
106                    .responseFormat(ResponseFormat.JSON)
107                    .auth(this.auth)
108                    .networkSession(this.networkSession)
109                    .build());
110    return JsonManager.deserialize(response.getData(), SessionTerminationMessage.class);
111  }
112
113  public Authentication getAuth() {
114    return auth;
115  }
116
117  public NetworkSession getNetworkSession() {
118    return networkSession;
119  }
120
121  public static class Builder {
122
123    protected Authentication auth;
124
125    protected NetworkSession networkSession;
126
127    public Builder() {}
128
129    public Builder auth(Authentication auth) {
130      this.auth = auth;
131      return this;
132    }
133
134    public Builder networkSession(NetworkSession networkSession) {
135      this.networkSession = networkSession;
136      return this;
137    }
138
139    public SessionTerminationManager build() {
140      if (this.networkSession == null) {
141        this.networkSession = new NetworkSession();
142      }
143      return new SessionTerminationManager(this);
144    }
145  }
146}