1. import java.io.IOException;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.Map;
  5. import java.util.HashMap;
  6. public class ApiBatchProcessor {
  7. /**
  8. * Processes a batch of API requests, handling errors and logging them.
  9. * @param apiRequestList A list of API request objects. Each object should have a method to execute the API call.
  10. * @param batchSize The number of requests to process in each batch.
  11. */
  12. public void processBatch(List<ApiRequest> apiRequestList, int batchSize) {
  13. if (apiRequestList == null || apiRequestList.isEmpty()) {
  14. System.out.println("No API requests to process.");
  15. return;
  16. }
  17. int totalRequests = apiRequestList.size();
  18. int numBatches = (int) Math.ceil((double) totalRequests / batchSize);
  19. for (int batchNum = 0; batchNum < numBatches; batchNum++) {
  20. int start = batchNum * batchSize;
  21. int end = Math.min((batchNum + 1) * batchSize, totalRequests);
  22. List<ApiRequest> batch = apiRequestList.subList(start, end);
  23. processBatchInternally(batch);
  24. }
  25. }
  26. private void processBatchInternally(List<ApiRequest> batch) {
  27. for (ApiRequest request : batch) {
  28. try {
  29. Map<String, Object> response = request.execute(); // Execute the API request
  30. //Process response
  31. System.out.println("Request successful: " + request.getMethod() + " - " + request.getUrl());
  32. } catch (Exception e) {
  33. logError("Error processing request: " + request.getMethod() + " - " + request.getUrl(), e.getMessage());
  34. }
  35. }
  36. }
  37. private void logError(String message, Exception e) {
  38. System.err.println("ERROR: " + message);
  39. e.printStackTrace(); // Print the stack trace for detailed debugging
  40. }
  41. /**
  42. * Interface for API request objects.
  43. */
  44. public interface ApiRequest {
  45. String getMethod(); // e.g., "GET", "POST"
  46. String getUrl(); // e.g., "https://example.com/api/data"
  47. Map<String, Object> execute() throws Exception; // Executes the API request and returns the response.
  48. }
  49. public static void main(String[] args) {
  50. //Example Usage
  51. List<ApiRequest> requests = new ArrayList<>();
  52. requests.add(new MyApiRequest("GET", "https://api.example.com/users", new HashMap<>()));
  53. requests.add(new MyApiRequest("POST", "https://api.example.com/products", new HashMap<String, Object>() {{ put("name", "Test Product"); }}));
  54. requests.add(new MyApiRequest("GET", "https://api.example.com/orders", new HashMap<>()));
  55. requests.add(new MyApiRequest("GET", "https://api.example.com/nonexistent", new HashMap<>()));
  56. ApiBatchProcessor processor = new ApiBatchProcessor();
  57. processor.processBatch(requests, 2); // Process in batches of 2
  58. }
  59. static class MyApiRequest implements ApiRequest {
  60. private final String method;
  61. private final String url;
  62. private final Map<String, Object> params;
  63. public MyApiRequest(String method, String url, Map<String, Object> params) {
  64. this.method = method;
  65. this.url = url;
  66. this.params = params;
  67. }
  68. @Override
  69. public String getMethod() {
  70. return method;
  71. }
  72. @Override
  73. public String getUrl() {
  74. return url;
  75. }
  76. @Override
  77. public Map<String, Object> execute() throws Exception {
  78. // Simulate API call and response
  79. if (url.equals("https://api.example.com/nonexistent")) {
  80. throw new Exception("Simulated API error");
  81. }
  82. return new HashMap<>(); // Simulate a successful response
  83. }
  84. }
  85. }

Add your comment