1. import java.io.*;
  2. import java.net.*;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.concurrent.*;
  7. public class ApiBackup {
  8. private static final int RETRY_INTERVAL = 5000; // Retry interval in milliseconds
  9. private static final int MAX_RETRIES = 3;
  10. private static final String BACKUP_DIR = "api_backups"; // Directory to store backups
  11. public static void main(String[] args) throws InterruptedException, IOException {
  12. // Create backup directory if it doesn't exist
  13. File backupDir = new File(BACKUP_DIR);
  14. if (!backupDir.exists()) {
  15. backupDir.mkdirs();
  16. }
  17. // Example API endpoint configuration
  18. List<Map<String, String>> apiEndpoints = new ArrayList<>();
  19. apiEndpoints.add(Map.of("url", "https://api.example.com/endpoint1", "method", "GET"));
  20. apiEndpoints.add(Map.of("url", "https://api.example.com/endpoint2", "method", "POST"));
  21. apiEndpoints.add(Map.of("url", "https://api.example.com/endpoint3", "method", "GET"));
  22. for (Map<String, String> endpoint : apiEndpoints) {
  23. String url = endpoint.get("url");
  24. String method = endpoint.get("method");
  25. backupApiEndpoint(url, method);
  26. }
  27. }
  28. public static void backupApiEndpoint(String url, String method) throws InterruptedException, IOException {
  29. String filename = url.replace("://", "_").replace("/", "_") + "_" + System.currentTimeMillis() + ".json";
  30. File outputFile = new File(BACKUP_DIR, filename);
  31. for (int retry = 0; retry <= MAX_RETRIES; retry++) {
  32. try {
  33. // Make the API request
  34. String response = makeApiRequest(url, method);
  35. // Write the response to the file
  36. try (FileWriter writer = new FileWriter(outputFile)) {
  37. writer.write(response);
  38. }
  39. System.out.println("Successfully backed up: " + url + " to " + outputFile.getAbsolutePath());
  40. return; // Exit the retry loop on success
  41. } catch (IOException e) {
  42. System.err.println("Backup failed for " + url + " (attempt " + (retry + 1) + "): " + e.getMessage());
  43. if (retry < MAX_RETRIES) {
  44. Thread.sleep(RETRY_INTERVAL); // Wait before retrying
  45. } else {
  46. System.err.println("Backup failed for " + url + " after " + MAX_RETRIES + " retries.");
  47. }
  48. }
  49. }
  50. }
  51. private static String makeApiRequest(String url, String method) throws IOException {
  52. try (URL u = new URL(url);
  53. HttpURLConnection conn = (HttpURLConnection) u.openConnection()) {
  54. conn.setRequestMethod(method);
  55. conn.setReadTimeout(10000); // 10 seconds timeout
  56. conn.setConnectTimeout(5000); // 5 seconds timeout
  57. int responseCode = conn.getResponseCode();
  58. if (responseCode >= 200 && responseCode < 300) {
  59. try (InputStream inputStream = conn.getInputStream();
  60. BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
  61. StringBuilder response = new StringBuilder();
  62. String line;
  63. while ((line = reader.readLine()) != null) {
  64. response.append(line).append("\n");
  65. }
  66. return response.toString();
  67. }
  68. } else {
  69. throw new IOException("API request failed with response code: " + responseCode);
  70. }
  71. }
  72. }
  73. }

Add your comment