001package com.box.sdkgen.managers.signrequests;
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.signrequest.SignRequest;
015import com.box.sdkgen.schemas.signrequestcancelrequest.SignRequestCancelRequest;
016import com.box.sdkgen.schemas.signrequestcreaterequest.SignRequestCreateRequest;
017import com.box.sdkgen.schemas.signrequests.SignRequests;
018import com.box.sdkgen.serialization.json.JsonManager;
019import java.util.Map;
020
021public class SignRequestsManager {
022
023  public Authentication auth;
024
025  public NetworkSession networkSession;
026
027  public SignRequestsManager() {
028    this.networkSession = new NetworkSession();
029  }
030
031  protected SignRequestsManager(Builder builder) {
032    this.auth = builder.auth;
033    this.networkSession = builder.networkSession;
034  }
035
036  /**
037   * Cancels a sign request.
038   *
039   * @param signRequestId The ID of the signature request. Example: "33243242"
040   */
041  public SignRequest cancelSignRequest(String signRequestId) {
042    return cancelSignRequest(signRequestId, null, new CancelSignRequestHeaders());
043  }
044
045  /**
046   * Cancels a sign request.
047   *
048   * @param signRequestId The ID of the signature request. Example: "33243242"
049   * @param requestBody Request body of cancelSignRequest method
050   */
051  public SignRequest cancelSignRequest(String signRequestId, SignRequestCancelRequest requestBody) {
052    return cancelSignRequest(signRequestId, requestBody, new CancelSignRequestHeaders());
053  }
054
055  /**
056   * Cancels a sign request.
057   *
058   * @param signRequestId The ID of the signature request. Example: "33243242"
059   * @param headers Headers of cancelSignRequest method
060   */
061  public SignRequest cancelSignRequest(String signRequestId, CancelSignRequestHeaders headers) {
062    return cancelSignRequest(signRequestId, null, headers);
063  }
064
065  /**
066   * Cancels a sign request.
067   *
068   * @param signRequestId The ID of the signature request. Example: "33243242"
069   * @param requestBody Request body of cancelSignRequest method
070   * @param headers Headers of cancelSignRequest method
071   */
072  public SignRequest cancelSignRequest(
073      String signRequestId,
074      SignRequestCancelRequest requestBody,
075      CancelSignRequestHeaders headers) {
076    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
077    FetchResponse response =
078        this.networkSession
079            .getNetworkClient()
080            .fetch(
081                new FetchOptions.Builder(
082                        String.join(
083                            "",
084                            this.networkSession.getBaseUrls().getBaseUrl(),
085                            "/2.0/sign_requests/",
086                            convertToString(signRequestId),
087                            "/cancel"),
088                        "POST")
089                    .headers(headersMap)
090                    .data((!(requestBody == null) ? JsonManager.serialize(requestBody) : null))
091                    .contentType("application/json")
092                    .responseFormat(ResponseFormat.JSON)
093                    .auth(this.auth)
094                    .networkSession(this.networkSession)
095                    .build());
096    return JsonManager.deserialize(response.getData(), SignRequest.class);
097  }
098
099  /**
100   * Resends a signature request email to all outstanding signers.
101   *
102   * @param signRequestId The ID of the signature request. Example: "33243242"
103   */
104  public void resendSignRequest(String signRequestId) {
105    resendSignRequest(signRequestId, new ResendSignRequestHeaders());
106  }
107
108  /**
109   * Resends a signature request email to all outstanding signers.
110   *
111   * @param signRequestId The ID of the signature request. Example: "33243242"
112   * @param headers Headers of resendSignRequest method
113   */
114  public void resendSignRequest(String signRequestId, ResendSignRequestHeaders headers) {
115    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
116    FetchResponse response =
117        this.networkSession
118            .getNetworkClient()
119            .fetch(
120                new FetchOptions.Builder(
121                        String.join(
122                            "",
123                            this.networkSession.getBaseUrls().getBaseUrl(),
124                            "/2.0/sign_requests/",
125                            convertToString(signRequestId),
126                            "/resend"),
127                        "POST")
128                    .headers(headersMap)
129                    .responseFormat(ResponseFormat.NO_CONTENT)
130                    .auth(this.auth)
131                    .networkSession(this.networkSession)
132                    .build());
133  }
134
135  /**
136   * Gets a sign request by ID.
137   *
138   * @param signRequestId The ID of the signature request. Example: "33243242"
139   */
140  public SignRequest getSignRequestById(String signRequestId) {
141    return getSignRequestById(signRequestId, new GetSignRequestByIdHeaders());
142  }
143
144  /**
145   * Gets a sign request by ID.
146   *
147   * @param signRequestId The ID of the signature request. Example: "33243242"
148   * @param headers Headers of getSignRequestById method
149   */
150  public SignRequest getSignRequestById(String signRequestId, GetSignRequestByIdHeaders headers) {
151    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
152    FetchResponse response =
153        this.networkSession
154            .getNetworkClient()
155            .fetch(
156                new FetchOptions.Builder(
157                        String.join(
158                            "",
159                            this.networkSession.getBaseUrls().getBaseUrl(),
160                            "/2.0/sign_requests/",
161                            convertToString(signRequestId)),
162                        "GET")
163                    .headers(headersMap)
164                    .responseFormat(ResponseFormat.JSON)
165                    .auth(this.auth)
166                    .networkSession(this.networkSession)
167                    .build());
168    return JsonManager.deserialize(response.getData(), SignRequest.class);
169  }
170
171  /**
172   * Gets signature requests created by a user. If the `sign_files` and/or `parent_folder` are
173   * deleted, the signature request will not return in the list.
174   */
175  public SignRequests getSignRequests() {
176    return getSignRequests(new GetSignRequestsQueryParams(), new GetSignRequestsHeaders());
177  }
178
179  /**
180   * Gets signature requests created by a user. If the `sign_files` and/or `parent_folder` are
181   * deleted, the signature request will not return in the list.
182   *
183   * @param queryParams Query parameters of getSignRequests method
184   */
185  public SignRequests getSignRequests(GetSignRequestsQueryParams queryParams) {
186    return getSignRequests(queryParams, new GetSignRequestsHeaders());
187  }
188
189  /**
190   * Gets signature requests created by a user. If the `sign_files` and/or `parent_folder` are
191   * deleted, the signature request will not return in the list.
192   *
193   * @param headers Headers of getSignRequests method
194   */
195  public SignRequests getSignRequests(GetSignRequestsHeaders headers) {
196    return getSignRequests(new GetSignRequestsQueryParams(), headers);
197  }
198
199  /**
200   * Gets signature requests created by a user. If the `sign_files` and/or `parent_folder` are
201   * deleted, the signature request will not return in the list.
202   *
203   * @param queryParams Query parameters of getSignRequests method
204   * @param headers Headers of getSignRequests method
205   */
206  public SignRequests getSignRequests(
207      GetSignRequestsQueryParams queryParams, GetSignRequestsHeaders headers) {
208    Map<String, String> queryParamsMap =
209        prepareParams(
210            mapOf(
211                entryOf("marker", convertToString(queryParams.getMarker())),
212                entryOf("limit", convertToString(queryParams.getLimit())),
213                entryOf("senders", convertToString(queryParams.getSenders())),
214                entryOf("shared_requests", convertToString(queryParams.getSharedRequests()))));
215    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
216    FetchResponse response =
217        this.networkSession
218            .getNetworkClient()
219            .fetch(
220                new FetchOptions.Builder(
221                        String.join(
222                            "",
223                            this.networkSession.getBaseUrls().getBaseUrl(),
224                            "/2.0/sign_requests"),
225                        "GET")
226                    .params(queryParamsMap)
227                    .headers(headersMap)
228                    .responseFormat(ResponseFormat.JSON)
229                    .auth(this.auth)
230                    .networkSession(this.networkSession)
231                    .build());
232    return JsonManager.deserialize(response.getData(), SignRequests.class);
233  }
234
235  /**
236   * Creates a signature request. This involves preparing a document for signing and sending the
237   * signature request to signers.
238   *
239   * @param requestBody Request body of createSignRequest method
240   */
241  public SignRequest createSignRequest(SignRequestCreateRequest requestBody) {
242    return createSignRequest(requestBody, new CreateSignRequestHeaders());
243  }
244
245  /**
246   * Creates a signature request. This involves preparing a document for signing and sending the
247   * signature request to signers.
248   *
249   * @param requestBody Request body of createSignRequest method
250   * @param headers Headers of createSignRequest method
251   */
252  public SignRequest createSignRequest(
253      SignRequestCreateRequest requestBody, CreateSignRequestHeaders headers) {
254    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
255    FetchResponse response =
256        this.networkSession
257            .getNetworkClient()
258            .fetch(
259                new FetchOptions.Builder(
260                        String.join(
261                            "",
262                            this.networkSession.getBaseUrls().getBaseUrl(),
263                            "/2.0/sign_requests"),
264                        "POST")
265                    .headers(headersMap)
266                    .data(JsonManager.serialize(requestBody))
267                    .contentType("application/json")
268                    .responseFormat(ResponseFormat.JSON)
269                    .auth(this.auth)
270                    .networkSession(this.networkSession)
271                    .build());
272    return JsonManager.deserialize(response.getData(), SignRequest.class);
273  }
274
275  public Authentication getAuth() {
276    return auth;
277  }
278
279  public NetworkSession getNetworkSession() {
280    return networkSession;
281  }
282
283  public static class Builder {
284
285    protected Authentication auth;
286
287    protected NetworkSession networkSession;
288
289    public Builder() {}
290
291    public Builder auth(Authentication auth) {
292      this.auth = auth;
293      return this;
294    }
295
296    public Builder networkSession(NetworkSession networkSession) {
297      this.networkSession = networkSession;
298      return this;
299    }
300
301    public SignRequestsManager build() {
302      if (this.networkSession == null) {
303        this.networkSession = new NetworkSession();
304      }
305      return new SignRequestsManager(this);
306    }
307  }
308}