001package com.box.sdkgen.managers.tasks;
002
003import static com.box.sdkgen.internal.utils.UtilsManager.convertToString;
004import static com.box.sdkgen.internal.utils.UtilsManager.mapOf;
005import static com.box.sdkgen.internal.utils.UtilsManager.mergeMaps;
006import static com.box.sdkgen.internal.utils.UtilsManager.prepareParams;
007
008import com.box.sdkgen.networking.auth.Authentication;
009import com.box.sdkgen.networking.fetchoptions.FetchOptions;
010import com.box.sdkgen.networking.fetchoptions.ResponseFormat;
011import com.box.sdkgen.networking.fetchresponse.FetchResponse;
012import com.box.sdkgen.networking.network.NetworkSession;
013import com.box.sdkgen.schemas.task.Task;
014import com.box.sdkgen.schemas.tasks.Tasks;
015import com.box.sdkgen.serialization.json.JsonManager;
016import java.util.Map;
017
018public class TasksManager {
019
020  public Authentication auth;
021
022  public NetworkSession networkSession;
023
024  public TasksManager() {
025    this.networkSession = new NetworkSession();
026  }
027
028  protected TasksManager(Builder builder) {
029    this.auth = builder.auth;
030    this.networkSession = builder.networkSession;
031  }
032
033  /**
034   * Retrieves a list of all the tasks for a file. This endpoint does not support pagination.
035   *
036   * @param fileId The unique identifier that represents a file.
037   *     <p>The ID for any file can be determined by visiting a file in the web application and
038   *     copying the ID from the URL. For example, for the URL `https://*.app.box.com/files/123` the
039   *     `file_id` is `123`. Example: "12345"
040   */
041  public Tasks getFileTasks(String fileId) {
042    return getFileTasks(fileId, new GetFileTasksHeaders());
043  }
044
045  /**
046   * Retrieves a list of all the tasks for a file. This endpoint does not support pagination.
047   *
048   * @param fileId The unique identifier that represents a file.
049   *     <p>The ID for any file can be determined by visiting a file in the web application and
050   *     copying the ID from the URL. For example, for the URL `https://*.app.box.com/files/123` the
051   *     `file_id` is `123`. Example: "12345"
052   * @param headers Headers of getFileTasks method
053   */
054  public Tasks getFileTasks(String fileId, GetFileTasksHeaders headers) {
055    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
056    FetchResponse response =
057        this.networkSession
058            .getNetworkClient()
059            .fetch(
060                new FetchOptions.Builder(
061                        String.join(
062                            "",
063                            this.networkSession.getBaseUrls().getBaseUrl(),
064                            "/2.0/files/",
065                            convertToString(fileId),
066                            "/tasks"),
067                        "GET")
068                    .headers(headersMap)
069                    .responseFormat(ResponseFormat.JSON)
070                    .auth(this.auth)
071                    .networkSession(this.networkSession)
072                    .build());
073    return JsonManager.deserialize(response.getData(), Tasks.class);
074  }
075
076  /**
077   * Creates a single task on a file. This task is not assigned to any user and will need to be
078   * assigned separately.
079   *
080   * @param requestBody Request body of createTask method
081   */
082  public Task createTask(CreateTaskRequestBody requestBody) {
083    return createTask(requestBody, new CreateTaskHeaders());
084  }
085
086  /**
087   * Creates a single task on a file. This task is not assigned to any user and will need to be
088   * assigned separately.
089   *
090   * @param requestBody Request body of createTask method
091   * @param headers Headers of createTask method
092   */
093  public Task createTask(CreateTaskRequestBody requestBody, CreateTaskHeaders headers) {
094    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
095    FetchResponse response =
096        this.networkSession
097            .getNetworkClient()
098            .fetch(
099                new FetchOptions.Builder(
100                        String.join(
101                            "", this.networkSession.getBaseUrls().getBaseUrl(), "/2.0/tasks"),
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(), Task.class);
111  }
112
113  /**
114   * Retrieves information about a specific task.
115   *
116   * @param taskId The ID of the task. Example: "12345"
117   */
118  public Task getTaskById(String taskId) {
119    return getTaskById(taskId, new GetTaskByIdHeaders());
120  }
121
122  /**
123   * Retrieves information about a specific task.
124   *
125   * @param taskId The ID of the task. Example: "12345"
126   * @param headers Headers of getTaskById method
127   */
128  public Task getTaskById(String taskId, GetTaskByIdHeaders headers) {
129    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
130    FetchResponse response =
131        this.networkSession
132            .getNetworkClient()
133            .fetch(
134                new FetchOptions.Builder(
135                        String.join(
136                            "",
137                            this.networkSession.getBaseUrls().getBaseUrl(),
138                            "/2.0/tasks/",
139                            convertToString(taskId)),
140                        "GET")
141                    .headers(headersMap)
142                    .responseFormat(ResponseFormat.JSON)
143                    .auth(this.auth)
144                    .networkSession(this.networkSession)
145                    .build());
146    return JsonManager.deserialize(response.getData(), Task.class);
147  }
148
149  /**
150   * Updates a task. This can be used to update a task's configuration, or to update its completion
151   * state.
152   *
153   * @param taskId The ID of the task. Example: "12345"
154   */
155  public Task updateTaskById(String taskId) {
156    return updateTaskById(taskId, new UpdateTaskByIdRequestBody(), new UpdateTaskByIdHeaders());
157  }
158
159  /**
160   * Updates a task. This can be used to update a task's configuration, or to update its completion
161   * state.
162   *
163   * @param taskId The ID of the task. Example: "12345"
164   * @param requestBody Request body of updateTaskById method
165   */
166  public Task updateTaskById(String taskId, UpdateTaskByIdRequestBody requestBody) {
167    return updateTaskById(taskId, requestBody, new UpdateTaskByIdHeaders());
168  }
169
170  /**
171   * Updates a task. This can be used to update a task's configuration, or to update its completion
172   * state.
173   *
174   * @param taskId The ID of the task. Example: "12345"
175   * @param headers Headers of updateTaskById method
176   */
177  public Task updateTaskById(String taskId, UpdateTaskByIdHeaders headers) {
178    return updateTaskById(taskId, new UpdateTaskByIdRequestBody(), headers);
179  }
180
181  /**
182   * Updates a task. This can be used to update a task's configuration, or to update its completion
183   * state.
184   *
185   * @param taskId The ID of the task. Example: "12345"
186   * @param requestBody Request body of updateTaskById method
187   * @param headers Headers of updateTaskById method
188   */
189  public Task updateTaskById(
190      String taskId, UpdateTaskByIdRequestBody requestBody, UpdateTaskByIdHeaders headers) {
191    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
192    FetchResponse response =
193        this.networkSession
194            .getNetworkClient()
195            .fetch(
196                new FetchOptions.Builder(
197                        String.join(
198                            "",
199                            this.networkSession.getBaseUrls().getBaseUrl(),
200                            "/2.0/tasks/",
201                            convertToString(taskId)),
202                        "PUT")
203                    .headers(headersMap)
204                    .data(JsonManager.serialize(requestBody))
205                    .contentType("application/json")
206                    .responseFormat(ResponseFormat.JSON)
207                    .auth(this.auth)
208                    .networkSession(this.networkSession)
209                    .build());
210    return JsonManager.deserialize(response.getData(), Task.class);
211  }
212
213  /**
214   * Removes a task from a file.
215   *
216   * @param taskId The ID of the task. Example: "12345"
217   */
218  public void deleteTaskById(String taskId) {
219    deleteTaskById(taskId, new DeleteTaskByIdHeaders());
220  }
221
222  /**
223   * Removes a task from a file.
224   *
225   * @param taskId The ID of the task. Example: "12345"
226   * @param headers Headers of deleteTaskById method
227   */
228  public void deleteTaskById(String taskId, DeleteTaskByIdHeaders headers) {
229    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
230    FetchResponse response =
231        this.networkSession
232            .getNetworkClient()
233            .fetch(
234                new FetchOptions.Builder(
235                        String.join(
236                            "",
237                            this.networkSession.getBaseUrls().getBaseUrl(),
238                            "/2.0/tasks/",
239                            convertToString(taskId)),
240                        "DELETE")
241                    .headers(headersMap)
242                    .responseFormat(ResponseFormat.NO_CONTENT)
243                    .auth(this.auth)
244                    .networkSession(this.networkSession)
245                    .build());
246  }
247
248  public Authentication getAuth() {
249    return auth;
250  }
251
252  public NetworkSession getNetworkSession() {
253    return networkSession;
254  }
255
256  public static class Builder {
257
258    protected Authentication auth;
259
260    protected NetworkSession networkSession;
261
262    public Builder() {}
263
264    public Builder auth(Authentication auth) {
265      this.auth = auth;
266      return this;
267    }
268
269    public Builder networkSession(NetworkSession networkSession) {
270      this.networkSession = networkSession;
271      return this;
272    }
273
274    public TasksManager build() {
275      if (this.networkSession == null) {
276        this.networkSession = new NetworkSession();
277      }
278      return new TasksManager(this);
279    }
280  }
281}