001package com.box.sdk; 002 003import java.util.ArrayList; 004import java.util.HashMap; 005import java.util.Iterator; 006import java.util.List; 007import java.util.Map; 008 009import com.box.sdk.http.HttpHeaders; 010import com.box.sdk.http.HttpMethod; 011import com.eclipsesource.json.JsonArray; 012import com.eclipsesource.json.JsonObject; 013import com.eclipsesource.json.JsonValue; 014 015/** 016 * @deprecated As of 2.39.0, BatchAPI Request will no longer be supported. 017 * 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 * @deprecated As of 2.39.0, BatchAPI Request will no longer be supported. 035 * 036 * Constructs an authenticated BatchRequest using a provided BoxAPIConnection. 037 * @param api an API connection for authenticating the request. 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 * @deprecated As of 2.39.0, BatchAPI Request will no longer be supported. 047 * 048 * Execute a set of API calls as batch request. 049 * @param requests list of api requests that has to be executed in batch. 050 * @return list of BoxAPIResponses 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 * @deprecated As of 2.39.0, BatchAPI Request will no longer be supported. 061 * 062 * Prepare a batch api request using list of individual reuests. 063 * @param requests list of api requests that has to be executed in batch. 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 * @deprecated As of 2.39.0, BatchAPI Request will no longer be supported. 100 * 101 * Parses btch api response to create a list of BoxAPIResponse objects. 102 * @param batchResponse response of a batch api request 103 * @return list of BoxAPIResponses 104 */ 105 @Deprecated 106 protected List<BoxAPIResponse> parseResponse(BoxJSONResponse batchResponse) { 107 JsonObject responseJSON = JsonObject.readFrom(batchResponse.getJSON()); 108 List<BoxAPIResponse> responses = new ArrayList<BoxAPIResponse>(); 109 Iterator<JsonValue> responseIterator = responseJSON.get("responses").asArray().iterator(); 110 while (responseIterator.hasNext()) { 111 JsonObject jsonResponse = responseIterator.next().asObject(); 112 BoxAPIResponse response = null; 113 114 //Gather headers 115 Map<String, String> responseHeaders = new HashMap<String, String>(); 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}