import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
public class ApiBatchProcessor {
/**
* Processes a batch of API requests, handling errors and logging them.
* @param apiRequestList A list of API request objects. Each object should have a method to execute the API call.
* @param batchSize The number of requests to process in each batch.
*/
public void processBatch(List<ApiRequest> apiRequestList, int batchSize) {
if (apiRequestList == null || apiRequestList.isEmpty()) {
System.out.println("No API requests to process.");
return;
}
int totalRequests = apiRequestList.size();
int numBatches = (int) Math.ceil((double) totalRequests / batchSize);
for (int batchNum = 0; batchNum < numBatches; batchNum++) {
int start = batchNum * batchSize;
int end = Math.min((batchNum + 1) * batchSize, totalRequests);
List<ApiRequest> batch = apiRequestList.subList(start, end);
processBatchInternally(batch);
}
}
private void processBatchInternally(List<ApiRequest> batch) {
for (ApiRequest request : batch) {
try {
Map<String, Object> response = request.execute(); // Execute the API request
//Process response
System.out.println("Request successful: " + request.getMethod() + " - " + request.getUrl());
} catch (Exception e) {
logError("Error processing request: " + request.getMethod() + " - " + request.getUrl(), e.getMessage());
}
}
}
private void logError(String message, Exception e) {
System.err.println("ERROR: " + message);
e.printStackTrace(); // Print the stack trace for detailed debugging
}
/**
* Interface for API request objects.
*/
public interface ApiRequest {
String getMethod(); // e.g., "GET", "POST"
String getUrl(); // e.g., "https://example.com/api/data"
Map<String, Object> execute() throws Exception; // Executes the API request and returns the response.
}
public static void main(String[] args) {
//Example Usage
List<ApiRequest> requests = new ArrayList<>();
requests.add(new MyApiRequest("GET", "https://api.example.com/users", new HashMap<>()));
requests.add(new MyApiRequest("POST", "https://api.example.com/products", new HashMap<String, Object>() {{ put("name", "Test Product"); }}));
requests.add(new MyApiRequest("GET", "https://api.example.com/orders", new HashMap<>()));
requests.add(new MyApiRequest("GET", "https://api.example.com/nonexistent", new HashMap<>()));
ApiBatchProcessor processor = new ApiBatchProcessor();
processor.processBatch(requests, 2); // Process in batches of 2
}
static class MyApiRequest implements ApiRequest {
private final String method;
private final String url;
private final Map<String, Object> params;
public MyApiRequest(String method, String url, Map<String, Object> params) {
this.method = method;
this.url = url;
this.params = params;
}
@Override
public String getMethod() {
return method;
}
@Override
public String getUrl() {
return url;
}
@Override
public Map<String, Object> execute() throws Exception {
// Simulate API call and response
if (url.equals("https://api.example.com/nonexistent")) {
throw new Exception("Simulated API error");
}
return new HashMap<>(); // Simulate a successful response
}
}
}
Add your comment