001package com.box.sdk;
002
003import com.box.sdk.http.HttpHeaders;
004import com.box.sdk.http.HttpMethod;
005import com.eclipsesource.json.Json;
006import com.eclipsesource.json.JsonArray;
007import com.eclipsesource.json.JsonObject;
008import com.eclipsesource.json.JsonValue;
009import java.util.ArrayList;
010import java.util.HashMap;
011import java.util.Iterator;
012import java.util.List;
013import java.util.Map;
014
015/**
016 * @deprecated As of 2.39.0, BatchAPI Request will no longer be supported.
017 * <p>
018 * Used to make a bunch of HTTP Requests as a batch. Currently the number of requests that can be batched at once
019 * is capped at <b>20</b> by the API layer. Also there are certain requests which <b>cannot</b> be performed
020 * by Batch API. Check API documentation for more information.
021 *
022 * <p>The request itself is a BoxJSONRequest but extends it to provide additional functionality for aggregating
023 * a bunch of requests and responding with multiple responses as if the requests were called individually</p>
024 */
025@Deprecated
026public class BatchAPIRequest extends BoxJSONRequest {
027    /**
028     * Batch URL Template.
029     */
030    public static final URLTemplate BATCH_URL_TEMPLATE = new URLTemplate("batch");
031    private final BoxAPIConnection api;
032
033    /**
034     * @param api an API connection for authenticating the request.
035     * @deprecated As of 2.39.0, BatchAPI Request will no longer be supported.
036     * <p>
037     * Constructs an authenticated BatchRequest using a provided BoxAPIConnection.
038     */
039    @Deprecated
040    public BatchAPIRequest(BoxAPIConnection api) {
041        super(api, BATCH_URL_TEMPLATE.build(api.getBaseURL()), HttpMethod.GET);
042        this.api = api;
043    }
044
045    /**
046     * @param requests list of api requests that has to be executed in batch.
047     * @return list of BoxAPIResponses
048     * @deprecated As of 2.39.0, BatchAPI Request will no longer be supported.
049     * <p>
050     * Execute a set of API calls as batch request.
051     */
052    @Deprecated
053    public List<BoxAPIResponse> execute(List<BoxAPIRequest> requests) {
054        this.prepareRequest(requests);
055        BoxJSONResponse batchResponse = (BoxJSONResponse) send();
056        return this.parseResponse(batchResponse);
057    }
058
059    /**
060     * @param requests list of api requests that has to be executed in batch.
061     * @deprecated As of 2.39.0, BatchAPI Request will no longer be supported.
062     * <p>
063     * Prepare a batch api request using list of individual reuests.
064     */
065    @Deprecated
066    protected void prepareRequest(List<BoxAPIRequest> requests) {
067        JsonObject body = new JsonObject();
068        JsonArray requestsJSONArray = new JsonArray();
069        for (BoxAPIRequest request : requests) {
070            JsonObject batchRequest = new JsonObject();
071            batchRequest.add("method", request.getMethod());
072            batchRequest.add("relative_url", request.getUrl().toString().substring(this.api.getBaseURL().length() - 1));
073            //If the actual request has a JSON body then add it to vatch request
074            if (request instanceof BoxJSONRequest) {
075                BoxJSONRequest jsonRequest = (BoxJSONRequest) request;
076                batchRequest.add("body", jsonRequest.getBodyAsJsonValue());
077            }
078            //Add any headers that are in the request, except Authorization
079            if (request.getHeaders() != null) {
080                JsonObject batchRequestHeaders = new JsonObject();
081                for (RequestHeader header : request.getHeaders()) {
082                    if (header.getKey() != null && !header.getKey().isEmpty()
083                        && !HttpHeaders.AUTHORIZATION.equals(header.getKey())) {
084                        batchRequestHeaders.add(header.getKey(), header.getValue());
085                    }
086                }
087                batchRequest.add("headers", batchRequestHeaders);
088            }
089
090            //Add the request to array
091            requestsJSONArray.add(batchRequest);
092        }
093        //Add the requests array to body
094        body.add("requests", requestsJSONArray);
095        super.setBody(body);
096    }
097
098    /**
099     * @param batchResponse response of a batch api request
100     * @return list of BoxAPIResponses
101     * @deprecated As of 2.39.0, BatchAPI Request will no longer be supported.
102     * <p>
103     * Parses btch api response to create a list of BoxAPIResponse objects.
104     */
105    @Deprecated
106    protected List<BoxAPIResponse> parseResponse(BoxJSONResponse batchResponse) {
107        JsonObject responseJSON = Json.parse(batchResponse.getJSON()).asObject();
108        List<BoxAPIResponse> responses = new ArrayList<>();
109        Iterator<JsonValue> responseIterator = responseJSON.get("responses").asArray().iterator();
110        while (responseIterator.hasNext()) {
111            JsonObject jsonResponse = responseIterator.next().asObject();
112            BoxAPIResponse response;
113
114            //Gather headers
115            Map<String, String> responseHeaders = new HashMap<>();
116
117            if (jsonResponse.get("headers") != null) {
118                JsonObject batchResponseHeadersObject = jsonResponse.get("headers").asObject();
119                for (JsonObject.Member member : batchResponseHeadersObject) {
120                    String headerName = member.getName();
121                    String headerValue = member.getValue().asString();
122                    responseHeaders.put(headerName, headerValue);
123                }
124            }
125
126            // Construct a BoxAPIResponse when response is null, or a BoxJSONResponse when there's a response
127            // (not anticipating any other response as per current APIs.
128            // Ideally we should do it based on response header)
129            if (jsonResponse.get("response") == null || jsonResponse.get("response").isNull()) {
130                response =
131                    new BoxAPIResponse(jsonResponse.get("status").asInt(), responseHeaders);
132            } else {
133                response =
134                    new BoxJSONResponse(jsonResponse.get("status").asInt(), responseHeaders,
135                        jsonResponse.get("response").asObject());
136            }
137            responses.add(response);
138        }
139        return responses;
140    }
141}