001package com.box.sdkgen.managers.retentionpolicies;
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.retentionpolicies.RetentionPolicies;
015import com.box.sdkgen.schemas.retentionpolicy.RetentionPolicy;
016import com.box.sdkgen.serialization.json.JsonManager;
017import java.util.Map;
018
019public class RetentionPoliciesManager {
020
021  public Authentication auth;
022
023  public NetworkSession networkSession;
024
025  public RetentionPoliciesManager() {
026    this.networkSession = new NetworkSession();
027  }
028
029  protected RetentionPoliciesManager(Builder builder) {
030    this.auth = builder.auth;
031    this.networkSession = builder.networkSession;
032  }
033
034  /** Retrieves all of the retention policies for an enterprise. */
035  public RetentionPolicies getRetentionPolicies() {
036    return getRetentionPolicies(
037        new GetRetentionPoliciesQueryParams(), new GetRetentionPoliciesHeaders());
038  }
039
040  /**
041   * Retrieves all of the retention policies for an enterprise.
042   *
043   * @param queryParams Query parameters of getRetentionPolicies method
044   */
045  public RetentionPolicies getRetentionPolicies(GetRetentionPoliciesQueryParams queryParams) {
046    return getRetentionPolicies(queryParams, new GetRetentionPoliciesHeaders());
047  }
048
049  /**
050   * Retrieves all of the retention policies for an enterprise.
051   *
052   * @param headers Headers of getRetentionPolicies method
053   */
054  public RetentionPolicies getRetentionPolicies(GetRetentionPoliciesHeaders headers) {
055    return getRetentionPolicies(new GetRetentionPoliciesQueryParams(), headers);
056  }
057
058  /**
059   * Retrieves all of the retention policies for an enterprise.
060   *
061   * @param queryParams Query parameters of getRetentionPolicies method
062   * @param headers Headers of getRetentionPolicies method
063   */
064  public RetentionPolicies getRetentionPolicies(
065      GetRetentionPoliciesQueryParams queryParams, GetRetentionPoliciesHeaders headers) {
066    Map<String, String> queryParamsMap =
067        prepareParams(
068            mapOf(
069                entryOf("policy_name", convertToString(queryParams.getPolicyName())),
070                entryOf("policy_type", convertToString(queryParams.getPolicyType())),
071                entryOf("created_by_user_id", convertToString(queryParams.getCreatedByUserId())),
072                entryOf("fields", convertToString(queryParams.getFields())),
073                entryOf("limit", convertToString(queryParams.getLimit())),
074                entryOf("marker", convertToString(queryParams.getMarker()))));
075    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
076    FetchResponse response =
077        this.networkSession
078            .getNetworkClient()
079            .fetch(
080                new FetchOptions.Builder(
081                        String.join(
082                            "",
083                            this.networkSession.getBaseUrls().getBaseUrl(),
084                            "/2.0/retention_policies"),
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(), RetentionPolicies.class);
093  }
094
095  /**
096   * Creates a retention policy.
097   *
098   * @param requestBody Request body of createRetentionPolicy method
099   */
100  public RetentionPolicy createRetentionPolicy(CreateRetentionPolicyRequestBody requestBody) {
101    return createRetentionPolicy(requestBody, new CreateRetentionPolicyHeaders());
102  }
103
104  /**
105   * Creates a retention policy.
106   *
107   * @param requestBody Request body of createRetentionPolicy method
108   * @param headers Headers of createRetentionPolicy method
109   */
110  public RetentionPolicy createRetentionPolicy(
111      CreateRetentionPolicyRequestBody requestBody, CreateRetentionPolicyHeaders headers) {
112    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
113    FetchResponse response =
114        this.networkSession
115            .getNetworkClient()
116            .fetch(
117                new FetchOptions.Builder(
118                        String.join(
119                            "",
120                            this.networkSession.getBaseUrls().getBaseUrl(),
121                            "/2.0/retention_policies"),
122                        "POST")
123                    .headers(headersMap)
124                    .data(JsonManager.serialize(requestBody))
125                    .contentType("application/json")
126                    .responseFormat(ResponseFormat.JSON)
127                    .auth(this.auth)
128                    .networkSession(this.networkSession)
129                    .build());
130    return JsonManager.deserialize(response.getData(), RetentionPolicy.class);
131  }
132
133  /**
134   * Retrieves a retention policy.
135   *
136   * @param retentionPolicyId The ID of the retention policy. Example: "982312"
137   */
138  public RetentionPolicy getRetentionPolicyById(String retentionPolicyId) {
139    return getRetentionPolicyById(
140        retentionPolicyId,
141        new GetRetentionPolicyByIdQueryParams(),
142        new GetRetentionPolicyByIdHeaders());
143  }
144
145  /**
146   * Retrieves a retention policy.
147   *
148   * @param retentionPolicyId The ID of the retention policy. Example: "982312"
149   * @param queryParams Query parameters of getRetentionPolicyById method
150   */
151  public RetentionPolicy getRetentionPolicyById(
152      String retentionPolicyId, GetRetentionPolicyByIdQueryParams queryParams) {
153    return getRetentionPolicyById(
154        retentionPolicyId, queryParams, new GetRetentionPolicyByIdHeaders());
155  }
156
157  /**
158   * Retrieves a retention policy.
159   *
160   * @param retentionPolicyId The ID of the retention policy. Example: "982312"
161   * @param headers Headers of getRetentionPolicyById method
162   */
163  public RetentionPolicy getRetentionPolicyById(
164      String retentionPolicyId, GetRetentionPolicyByIdHeaders headers) {
165    return getRetentionPolicyById(
166        retentionPolicyId, new GetRetentionPolicyByIdQueryParams(), headers);
167  }
168
169  /**
170   * Retrieves a retention policy.
171   *
172   * @param retentionPolicyId The ID of the retention policy. Example: "982312"
173   * @param queryParams Query parameters of getRetentionPolicyById method
174   * @param headers Headers of getRetentionPolicyById method
175   */
176  public RetentionPolicy getRetentionPolicyById(
177      String retentionPolicyId,
178      GetRetentionPolicyByIdQueryParams queryParams,
179      GetRetentionPolicyByIdHeaders headers) {
180    Map<String, String> queryParamsMap =
181        prepareParams(mapOf(entryOf("fields", convertToString(queryParams.getFields()))));
182    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
183    FetchResponse response =
184        this.networkSession
185            .getNetworkClient()
186            .fetch(
187                new FetchOptions.Builder(
188                        String.join(
189                            "",
190                            this.networkSession.getBaseUrls().getBaseUrl(),
191                            "/2.0/retention_policies/",
192                            convertToString(retentionPolicyId)),
193                        "GET")
194                    .params(queryParamsMap)
195                    .headers(headersMap)
196                    .responseFormat(ResponseFormat.JSON)
197                    .auth(this.auth)
198                    .networkSession(this.networkSession)
199                    .build());
200    return JsonManager.deserialize(response.getData(), RetentionPolicy.class);
201  }
202
203  /**
204   * Updates a retention policy.
205   *
206   * @param retentionPolicyId The ID of the retention policy. Example: "982312"
207   */
208  public RetentionPolicy updateRetentionPolicyById(String retentionPolicyId) {
209    return updateRetentionPolicyById(
210        retentionPolicyId,
211        new UpdateRetentionPolicyByIdRequestBody(),
212        new UpdateRetentionPolicyByIdHeaders());
213  }
214
215  /**
216   * Updates a retention policy.
217   *
218   * @param retentionPolicyId The ID of the retention policy. Example: "982312"
219   * @param requestBody Request body of updateRetentionPolicyById method
220   */
221  public RetentionPolicy updateRetentionPolicyById(
222      String retentionPolicyId, UpdateRetentionPolicyByIdRequestBody requestBody) {
223    return updateRetentionPolicyById(
224        retentionPolicyId, requestBody, new UpdateRetentionPolicyByIdHeaders());
225  }
226
227  /**
228   * Updates a retention policy.
229   *
230   * @param retentionPolicyId The ID of the retention policy. Example: "982312"
231   * @param headers Headers of updateRetentionPolicyById method
232   */
233  public RetentionPolicy updateRetentionPolicyById(
234      String retentionPolicyId, UpdateRetentionPolicyByIdHeaders headers) {
235    return updateRetentionPolicyById(
236        retentionPolicyId, new UpdateRetentionPolicyByIdRequestBody(), headers);
237  }
238
239  /**
240   * Updates a retention policy.
241   *
242   * @param retentionPolicyId The ID of the retention policy. Example: "982312"
243   * @param requestBody Request body of updateRetentionPolicyById method
244   * @param headers Headers of updateRetentionPolicyById method
245   */
246  public RetentionPolicy updateRetentionPolicyById(
247      String retentionPolicyId,
248      UpdateRetentionPolicyByIdRequestBody requestBody,
249      UpdateRetentionPolicyByIdHeaders headers) {
250    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
251    FetchResponse response =
252        this.networkSession
253            .getNetworkClient()
254            .fetch(
255                new FetchOptions.Builder(
256                        String.join(
257                            "",
258                            this.networkSession.getBaseUrls().getBaseUrl(),
259                            "/2.0/retention_policies/",
260                            convertToString(retentionPolicyId)),
261                        "PUT")
262                    .headers(headersMap)
263                    .data(JsonManager.serialize(requestBody))
264                    .contentType("application/json")
265                    .responseFormat(ResponseFormat.JSON)
266                    .auth(this.auth)
267                    .networkSession(this.networkSession)
268                    .build());
269    return JsonManager.deserialize(response.getData(), RetentionPolicy.class);
270  }
271
272  /**
273   * Permanently deletes a retention policy.
274   *
275   * @param retentionPolicyId The ID of the retention policy. Example: "982312"
276   */
277  public void deleteRetentionPolicyById(String retentionPolicyId) {
278    deleteRetentionPolicyById(retentionPolicyId, new DeleteRetentionPolicyByIdHeaders());
279  }
280
281  /**
282   * Permanently deletes a retention policy.
283   *
284   * @param retentionPolicyId The ID of the retention policy. Example: "982312"
285   * @param headers Headers of deleteRetentionPolicyById method
286   */
287  public void deleteRetentionPolicyById(
288      String retentionPolicyId, DeleteRetentionPolicyByIdHeaders headers) {
289    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
290    FetchResponse response =
291        this.networkSession
292            .getNetworkClient()
293            .fetch(
294                new FetchOptions.Builder(
295                        String.join(
296                            "",
297                            this.networkSession.getBaseUrls().getBaseUrl(),
298                            "/2.0/retention_policies/",
299                            convertToString(retentionPolicyId)),
300                        "DELETE")
301                    .headers(headersMap)
302                    .responseFormat(ResponseFormat.NO_CONTENT)
303                    .auth(this.auth)
304                    .networkSession(this.networkSession)
305                    .build());
306  }
307
308  public Authentication getAuth() {
309    return auth;
310  }
311
312  public NetworkSession getNetworkSession() {
313    return networkSession;
314  }
315
316  public static class Builder {
317
318    protected Authentication auth;
319
320    protected NetworkSession networkSession;
321
322    public Builder() {}
323
324    public Builder auth(Authentication auth) {
325      this.auth = auth;
326      return this;
327    }
328
329    public Builder networkSession(NetworkSession networkSession) {
330      this.networkSession = networkSession;
331      return this;
332    }
333
334    public RetentionPoliciesManager build() {
335      if (this.networkSession == null) {
336        this.networkSession = new NetworkSession();
337      }
338      return new RetentionPoliciesManager(this);
339    }
340  }
341}