001package com.box.sdkgen.managers.filerequests;
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.filerequest.FileRequest;
015import com.box.sdkgen.schemas.filerequestcopyrequest.FileRequestCopyRequest;
016import com.box.sdkgen.schemas.filerequestupdaterequest.FileRequestUpdateRequest;
017import com.box.sdkgen.serialization.json.JsonManager;
018import java.util.Map;
019
020public class FileRequestsManager {
021
022  public Authentication auth;
023
024  public NetworkSession networkSession;
025
026  public FileRequestsManager() {
027    this.networkSession = new NetworkSession();
028  }
029
030  protected FileRequestsManager(Builder builder) {
031    this.auth = builder.auth;
032    this.networkSession = builder.networkSession;
033  }
034
035  /**
036   * Retrieves the information about a file request.
037   *
038   * @param fileRequestId The unique identifier that represent a file request.
039   *     <p>The ID for any file request can be determined by visiting a file request builder in the
040   *     web application and copying the ID from the URL. For example, for the URL
041   *     `https://*.app.box.com/filerequest/123` the `file_request_id` is `123`. Example: "123"
042   */
043  public FileRequest getFileRequestById(String fileRequestId) {
044    return getFileRequestById(fileRequestId, new GetFileRequestByIdHeaders());
045  }
046
047  /**
048   * Retrieves the information about a file request.
049   *
050   * @param fileRequestId The unique identifier that represent a file request.
051   *     <p>The ID for any file request can be determined by visiting a file request builder in the
052   *     web application and copying the ID from the URL. For example, for the URL
053   *     `https://*.app.box.com/filerequest/123` the `file_request_id` is `123`. Example: "123"
054   * @param headers Headers of getFileRequestById method
055   */
056  public FileRequest getFileRequestById(String fileRequestId, GetFileRequestByIdHeaders headers) {
057    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
058    FetchResponse response =
059        this.networkSession
060            .getNetworkClient()
061            .fetch(
062                new FetchOptions.Builder(
063                        String.join(
064                            "",
065                            this.networkSession.getBaseUrls().getBaseUrl(),
066                            "/2.0/file_requests/",
067                            convertToString(fileRequestId)),
068                        "GET")
069                    .headers(headersMap)
070                    .responseFormat(ResponseFormat.JSON)
071                    .auth(this.auth)
072                    .networkSession(this.networkSession)
073                    .build());
074    return JsonManager.deserialize(response.getData(), FileRequest.class);
075  }
076
077  /**
078   * Updates a file request. This can be used to activate or deactivate a file request.
079   *
080   * @param fileRequestId The unique identifier that represent a file request.
081   *     <p>The ID for any file request can be determined by visiting a file request builder in the
082   *     web application and copying the ID from the URL. For example, for the URL
083   *     `https://*.app.box.com/filerequest/123` the `file_request_id` is `123`. Example: "123"
084   * @param requestBody Request body of updateFileRequestById method
085   */
086  public FileRequest updateFileRequestById(
087      String fileRequestId, FileRequestUpdateRequest requestBody) {
088    return updateFileRequestById(fileRequestId, requestBody, new UpdateFileRequestByIdHeaders());
089  }
090
091  /**
092   * Updates a file request. This can be used to activate or deactivate a file request.
093   *
094   * @param fileRequestId The unique identifier that represent a file request.
095   *     <p>The ID for any file request can be determined by visiting a file request builder in the
096   *     web application and copying the ID from the URL. For example, for the URL
097   *     `https://*.app.box.com/filerequest/123` the `file_request_id` is `123`. Example: "123"
098   * @param requestBody Request body of updateFileRequestById method
099   * @param headers Headers of updateFileRequestById method
100   */
101  public FileRequest updateFileRequestById(
102      String fileRequestId,
103      FileRequestUpdateRequest requestBody,
104      UpdateFileRequestByIdHeaders headers) {
105    Map<String, String> headersMap =
106        prepareParams(
107            mergeMaps(
108                mapOf(entryOf("if-match", convertToString(headers.getIfMatch()))),
109                headers.getExtraHeaders()));
110    FetchResponse response =
111        this.networkSession
112            .getNetworkClient()
113            .fetch(
114                new FetchOptions.Builder(
115                        String.join(
116                            "",
117                            this.networkSession.getBaseUrls().getBaseUrl(),
118                            "/2.0/file_requests/",
119                            convertToString(fileRequestId)),
120                        "PUT")
121                    .headers(headersMap)
122                    .data(JsonManager.serialize(requestBody))
123                    .contentType("application/json")
124                    .responseFormat(ResponseFormat.JSON)
125                    .auth(this.auth)
126                    .networkSession(this.networkSession)
127                    .build());
128    return JsonManager.deserialize(response.getData(), FileRequest.class);
129  }
130
131  /**
132   * Deletes a file request permanently.
133   *
134   * @param fileRequestId The unique identifier that represent a file request.
135   *     <p>The ID for any file request can be determined by visiting a file request builder in the
136   *     web application and copying the ID from the URL. For example, for the URL
137   *     `https://*.app.box.com/filerequest/123` the `file_request_id` is `123`. Example: "123"
138   */
139  public void deleteFileRequestById(String fileRequestId) {
140    deleteFileRequestById(fileRequestId, new DeleteFileRequestByIdHeaders());
141  }
142
143  /**
144   * Deletes a file request permanently.
145   *
146   * @param fileRequestId The unique identifier that represent a file request.
147   *     <p>The ID for any file request can be determined by visiting a file request builder in the
148   *     web application and copying the ID from the URL. For example, for the URL
149   *     `https://*.app.box.com/filerequest/123` the `file_request_id` is `123`. Example: "123"
150   * @param headers Headers of deleteFileRequestById method
151   */
152  public void deleteFileRequestById(String fileRequestId, DeleteFileRequestByIdHeaders headers) {
153    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
154    FetchResponse response =
155        this.networkSession
156            .getNetworkClient()
157            .fetch(
158                new FetchOptions.Builder(
159                        String.join(
160                            "",
161                            this.networkSession.getBaseUrls().getBaseUrl(),
162                            "/2.0/file_requests/",
163                            convertToString(fileRequestId)),
164                        "DELETE")
165                    .headers(headersMap)
166                    .responseFormat(ResponseFormat.NO_CONTENT)
167                    .auth(this.auth)
168                    .networkSession(this.networkSession)
169                    .build());
170  }
171
172  /**
173   * Copies an existing file request that is already present on one folder, and applies it to
174   * another folder.
175   *
176   * @param fileRequestId The unique identifier that represent a file request.
177   *     <p>The ID for any file request can be determined by visiting a file request builder in the
178   *     web application and copying the ID from the URL. For example, for the URL
179   *     `https://*.app.box.com/filerequest/123` the `file_request_id` is `123`. Example: "123"
180   * @param requestBody Request body of createFileRequestCopy method
181   */
182  public FileRequest createFileRequestCopy(
183      String fileRequestId, FileRequestCopyRequest requestBody) {
184    return createFileRequestCopy(fileRequestId, requestBody, new CreateFileRequestCopyHeaders());
185  }
186
187  /**
188   * Copies an existing file request that is already present on one folder, and applies it to
189   * another folder.
190   *
191   * @param fileRequestId The unique identifier that represent a file request.
192   *     <p>The ID for any file request can be determined by visiting a file request builder in the
193   *     web application and copying the ID from the URL. For example, for the URL
194   *     `https://*.app.box.com/filerequest/123` the `file_request_id` is `123`. Example: "123"
195   * @param requestBody Request body of createFileRequestCopy method
196   * @param headers Headers of createFileRequestCopy method
197   */
198  public FileRequest createFileRequestCopy(
199      String fileRequestId,
200      FileRequestCopyRequest requestBody,
201      CreateFileRequestCopyHeaders headers) {
202    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
203    FetchResponse response =
204        this.networkSession
205            .getNetworkClient()
206            .fetch(
207                new FetchOptions.Builder(
208                        String.join(
209                            "",
210                            this.networkSession.getBaseUrls().getBaseUrl(),
211                            "/2.0/file_requests/",
212                            convertToString(fileRequestId),
213                            "/copy"),
214                        "POST")
215                    .headers(headersMap)
216                    .data(JsonManager.serialize(requestBody))
217                    .contentType("application/json")
218                    .responseFormat(ResponseFormat.JSON)
219                    .auth(this.auth)
220                    .networkSession(this.networkSession)
221                    .build());
222    return JsonManager.deserialize(response.getData(), FileRequest.class);
223  }
224
225  public Authentication getAuth() {
226    return auth;
227  }
228
229  public NetworkSession getNetworkSession() {
230    return networkSession;
231  }
232
233  public static class Builder {
234
235    protected Authentication auth;
236
237    protected NetworkSession networkSession;
238
239    public Builder() {}
240
241    public Builder auth(Authentication auth) {
242      this.auth = auth;
243      return this;
244    }
245
246    public Builder networkSession(NetworkSession networkSession) {
247      this.networkSession = networkSession;
248      return this;
249    }
250
251    public FileRequestsManager build() {
252      if (this.networkSession == null) {
253        this.networkSession = new NetworkSession();
254      }
255      return new FileRequestsManager(this);
256    }
257  }
258}