001package com.box.sdkgen.managers.workflows;
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.workflows.Workflows;
015import com.box.sdkgen.serialization.json.JsonManager;
016import java.util.Map;
017
018public class WorkflowsManager {
019
020  public Authentication auth;
021
022  public NetworkSession networkSession;
023
024  public WorkflowsManager() {
025    this.networkSession = new NetworkSession();
026  }
027
028  protected WorkflowsManager(Builder builder) {
029    this.auth = builder.auth;
030    this.networkSession = builder.networkSession;
031  }
032
033  /**
034   * Returns list of workflows that act on a given `folder ID`, and have a flow with a trigger type
035   * of `WORKFLOW_MANUAL_START`.
036   *
037   * <p>You application must be authorized to use the `Manage Box Relay` application scope within
038   * the developer console in to use this endpoint.
039   *
040   * @param queryParams Query parameters of getWorkflows method
041   */
042  public Workflows getWorkflows(GetWorkflowsQueryParams queryParams) {
043    return getWorkflows(queryParams, new GetWorkflowsHeaders());
044  }
045
046  /**
047   * Returns list of workflows that act on a given `folder ID`, and have a flow with a trigger type
048   * of `WORKFLOW_MANUAL_START`.
049   *
050   * <p>You application must be authorized to use the `Manage Box Relay` application scope within
051   * the developer console in to use this endpoint.
052   *
053   * @param queryParams Query parameters of getWorkflows method
054   * @param headers Headers of getWorkflows method
055   */
056  public Workflows getWorkflows(GetWorkflowsQueryParams queryParams, GetWorkflowsHeaders headers) {
057    Map<String, String> queryParamsMap =
058        prepareParams(
059            mapOf(
060                entryOf("folder_id", convertToString(queryParams.getFolderId())),
061                entryOf("trigger_type", convertToString(queryParams.getTriggerType())),
062                entryOf("limit", convertToString(queryParams.getLimit())),
063                entryOf("marker", convertToString(queryParams.getMarker()))));
064    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
065    FetchResponse response =
066        this.networkSession
067            .getNetworkClient()
068            .fetch(
069                new FetchOptions.Builder(
070                        String.join(
071                            "", this.networkSession.getBaseUrls().getBaseUrl(), "/2.0/workflows"),
072                        "GET")
073                    .params(queryParamsMap)
074                    .headers(headersMap)
075                    .responseFormat(ResponseFormat.JSON)
076                    .auth(this.auth)
077                    .networkSession(this.networkSession)
078                    .build());
079    return JsonManager.deserialize(response.getData(), Workflows.class);
080  }
081
082  /**
083   * Initiates a flow with a trigger type of `WORKFLOW_MANUAL_START`.
084   *
085   * <p>You application must be authorized to use the `Manage Box Relay` application scope within
086   * the developer console.
087   *
088   * @param workflowId The ID of the workflow. Example: "12345"
089   * @param requestBody Request body of startWorkflow method
090   */
091  public void startWorkflow(String workflowId, StartWorkflowRequestBody requestBody) {
092    startWorkflow(workflowId, requestBody, new StartWorkflowHeaders());
093  }
094
095  /**
096   * Initiates a flow with a trigger type of `WORKFLOW_MANUAL_START`.
097   *
098   * <p>You application must be authorized to use the `Manage Box Relay` application scope within
099   * the developer console.
100   *
101   * @param workflowId The ID of the workflow. Example: "12345"
102   * @param requestBody Request body of startWorkflow method
103   * @param headers Headers of startWorkflow method
104   */
105  public void startWorkflow(
106      String workflowId, StartWorkflowRequestBody requestBody, StartWorkflowHeaders headers) {
107    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
108    FetchResponse response =
109        this.networkSession
110            .getNetworkClient()
111            .fetch(
112                new FetchOptions.Builder(
113                        String.join(
114                            "",
115                            this.networkSession.getBaseUrls().getBaseUrl(),
116                            "/2.0/workflows/",
117                            convertToString(workflowId),
118                            "/start"),
119                        "POST")
120                    .headers(headersMap)
121                    .data(JsonManager.serialize(requestBody))
122                    .contentType("application/json")
123                    .responseFormat(ResponseFormat.NO_CONTENT)
124                    .auth(this.auth)
125                    .networkSession(this.networkSession)
126                    .build());
127  }
128
129  public Authentication getAuth() {
130    return auth;
131  }
132
133  public NetworkSession getNetworkSession() {
134    return networkSession;
135  }
136
137  public static class Builder {
138
139    protected Authentication auth;
140
141    protected NetworkSession networkSession;
142
143    public Builder() {}
144
145    public Builder auth(Authentication auth) {
146      this.auth = auth;
147      return this;
148    }
149
150    public Builder networkSession(NetworkSession networkSession) {
151      this.networkSession = networkSession;
152      return this;
153    }
154
155    public WorkflowsManager build() {
156      if (this.networkSession == null) {
157        this.networkSession = new NetworkSession();
158      }
159      return new WorkflowsManager(this);
160    }
161  }
162}