001package com.box.sdkgen.managers.termsofservices;
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.termsofservice.TermsOfService;
015import com.box.sdkgen.schemas.termsofservices.TermsOfServices;
016import com.box.sdkgen.serialization.json.JsonManager;
017import java.util.Map;
018
019public class TermsOfServicesManager {
020
021  public Authentication auth;
022
023  public NetworkSession networkSession;
024
025  public TermsOfServicesManager() {
026    this.networkSession = new NetworkSession();
027  }
028
029  protected TermsOfServicesManager(Builder builder) {
030    this.auth = builder.auth;
031    this.networkSession = builder.networkSession;
032  }
033
034  /** Returns the current terms of service text and settings for the enterprise. */
035  public TermsOfServices getTermsOfService() {
036    return getTermsOfService(new GetTermsOfServiceQueryParams(), new GetTermsOfServiceHeaders());
037  }
038
039  /**
040   * Returns the current terms of service text and settings for the enterprise.
041   *
042   * @param queryParams Query parameters of getTermsOfService method
043   */
044  public TermsOfServices getTermsOfService(GetTermsOfServiceQueryParams queryParams) {
045    return getTermsOfService(queryParams, new GetTermsOfServiceHeaders());
046  }
047
048  /**
049   * Returns the current terms of service text and settings for the enterprise.
050   *
051   * @param headers Headers of getTermsOfService method
052   */
053  public TermsOfServices getTermsOfService(GetTermsOfServiceHeaders headers) {
054    return getTermsOfService(new GetTermsOfServiceQueryParams(), headers);
055  }
056
057  /**
058   * Returns the current terms of service text and settings for the enterprise.
059   *
060   * @param queryParams Query parameters of getTermsOfService method
061   * @param headers Headers of getTermsOfService method
062   */
063  public TermsOfServices getTermsOfService(
064      GetTermsOfServiceQueryParams queryParams, GetTermsOfServiceHeaders headers) {
065    Map<String, String> queryParamsMap =
066        prepareParams(mapOf(entryOf("tos_type", convertToString(queryParams.getTosType()))));
067    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
068    FetchResponse response =
069        this.networkSession
070            .getNetworkClient()
071            .fetch(
072                new FetchOptions.Builder(
073                        String.join(
074                            "",
075                            this.networkSession.getBaseUrls().getBaseUrl(),
076                            "/2.0/terms_of_services"),
077                        "GET")
078                    .params(queryParamsMap)
079                    .headers(headersMap)
080                    .responseFormat(ResponseFormat.JSON)
081                    .auth(this.auth)
082                    .networkSession(this.networkSession)
083                    .build());
084    return JsonManager.deserialize(response.getData(), TermsOfServices.class);
085  }
086
087  /**
088   * Creates a terms of service for a given enterprise and type of user.
089   *
090   * @param requestBody Request body of createTermsOfService method
091   */
092  public TermsOfService createTermsOfService(CreateTermsOfServiceRequestBody requestBody) {
093    return createTermsOfService(requestBody, new CreateTermsOfServiceHeaders());
094  }
095
096  /**
097   * Creates a terms of service for a given enterprise and type of user.
098   *
099   * @param requestBody Request body of createTermsOfService method
100   * @param headers Headers of createTermsOfService method
101   */
102  public TermsOfService createTermsOfService(
103      CreateTermsOfServiceRequestBody requestBody, CreateTermsOfServiceHeaders headers) {
104    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
105    FetchResponse response =
106        this.networkSession
107            .getNetworkClient()
108            .fetch(
109                new FetchOptions.Builder(
110                        String.join(
111                            "",
112                            this.networkSession.getBaseUrls().getBaseUrl(),
113                            "/2.0/terms_of_services"),
114                        "POST")
115                    .headers(headersMap)
116                    .data(JsonManager.serialize(requestBody))
117                    .contentType("application/json")
118                    .responseFormat(ResponseFormat.JSON)
119                    .auth(this.auth)
120                    .networkSession(this.networkSession)
121                    .build());
122    return JsonManager.deserialize(response.getData(), TermsOfService.class);
123  }
124
125  /**
126   * Fetches a specific terms of service.
127   *
128   * @param termsOfServiceId The ID of the terms of service. Example: "324234"
129   */
130  public TermsOfService getTermsOfServiceById(String termsOfServiceId) {
131    return getTermsOfServiceById(termsOfServiceId, new GetTermsOfServiceByIdHeaders());
132  }
133
134  /**
135   * Fetches a specific terms of service.
136   *
137   * @param termsOfServiceId The ID of the terms of service. Example: "324234"
138   * @param headers Headers of getTermsOfServiceById method
139   */
140  public TermsOfService getTermsOfServiceById(
141      String termsOfServiceId, GetTermsOfServiceByIdHeaders headers) {
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                            "",
150                            this.networkSession.getBaseUrls().getBaseUrl(),
151                            "/2.0/terms_of_services/",
152                            convertToString(termsOfServiceId)),
153                        "GET")
154                    .headers(headersMap)
155                    .responseFormat(ResponseFormat.JSON)
156                    .auth(this.auth)
157                    .networkSession(this.networkSession)
158                    .build());
159    return JsonManager.deserialize(response.getData(), TermsOfService.class);
160  }
161
162  /**
163   * Updates a specific terms of service.
164   *
165   * @param termsOfServiceId The ID of the terms of service. Example: "324234"
166   * @param requestBody Request body of updateTermsOfServiceById method
167   */
168  public TermsOfService updateTermsOfServiceById(
169      String termsOfServiceId, UpdateTermsOfServiceByIdRequestBody requestBody) {
170    return updateTermsOfServiceById(
171        termsOfServiceId, requestBody, new UpdateTermsOfServiceByIdHeaders());
172  }
173
174  /**
175   * Updates a specific terms of service.
176   *
177   * @param termsOfServiceId The ID of the terms of service. Example: "324234"
178   * @param requestBody Request body of updateTermsOfServiceById method
179   * @param headers Headers of updateTermsOfServiceById method
180   */
181  public TermsOfService updateTermsOfServiceById(
182      String termsOfServiceId,
183      UpdateTermsOfServiceByIdRequestBody requestBody,
184      UpdateTermsOfServiceByIdHeaders headers) {
185    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
186    FetchResponse response =
187        this.networkSession
188            .getNetworkClient()
189            .fetch(
190                new FetchOptions.Builder(
191                        String.join(
192                            "",
193                            this.networkSession.getBaseUrls().getBaseUrl(),
194                            "/2.0/terms_of_services/",
195                            convertToString(termsOfServiceId)),
196                        "PUT")
197                    .headers(headersMap)
198                    .data(JsonManager.serialize(requestBody))
199                    .contentType("application/json")
200                    .responseFormat(ResponseFormat.JSON)
201                    .auth(this.auth)
202                    .networkSession(this.networkSession)
203                    .build());
204    return JsonManager.deserialize(response.getData(), TermsOfService.class);
205  }
206
207  public Authentication getAuth() {
208    return auth;
209  }
210
211  public NetworkSession getNetworkSession() {
212    return networkSession;
213  }
214
215  public static class Builder {
216
217    protected Authentication auth;
218
219    protected NetworkSession networkSession;
220
221    public Builder() {}
222
223    public Builder auth(Authentication auth) {
224      this.auth = auth;
225      return this;
226    }
227
228    public Builder networkSession(NetworkSession networkSession) {
229      this.networkSession = networkSession;
230      return this;
231    }
232
233    public TermsOfServicesManager build() {
234      if (this.networkSession == null) {
235        this.networkSession = new NetworkSession();
236      }
237      return new TermsOfServicesManager(this);
238    }
239  }
240}