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