Skip to content

Commit 3debb62

Browse files
authored
Create regulationAPI.js
1 parent 3d47e77 commit 3debb62

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

src/space/regulationAPI.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// src/space/regulationAPI.js - Ultra Advanced Regulation API Module
2+
3+
const axios = require('axios');
4+
const NodeCache = require('node-cache'); // In-memory caching
5+
const { parseRegulationData } = require('./dataParser'); // Import data parser
6+
const { logError, logInfo } = require('./logger'); // Import logger for error handling
7+
8+
class RegulationAPI {
9+
constructor(apiEndpoints, cacheTTL = 3600) {
10+
this.apiEndpoints = apiEndpoints; // Array of API endpoints
11+
this.cache = new NodeCache({ stdTTL: cacheTTL }); // Cache with default TTL
12+
}
13+
14+
async fetchRegulations() {
15+
const regulationPromises = this.apiEndpoints.map(endpoint => this.fetchFromAPI(endpoint));
16+
try {
17+
const results = await Promise.all(regulationPromises);
18+
return results.flat(); // Flatten the array of results
19+
} catch (error) {
20+
logError('Error fetching regulations:', error);
21+
throw new Error('Failed to fetch regulations');
22+
}
23+
}
24+
25+
async fetchFromAPI(endpoint) {
26+
// Check cache first
27+
const cachedData = this.cache.get(endpoint);
28+
if (cachedData) {
29+
logInfo(`Returning cached data for ${endpoint}`);
30+
return cachedData;
31+
}
32+
33+
try {
34+
const response = await axios.get(endpoint);
35+
const parsedData = parseRegulationData(response.data); // Parse the data
36+
this.cache.set(endpoint, parsedData); // Cache the parsed data
37+
return parsedData;
38+
} catch (error) {
39+
logError(`Error fetching from ${endpoint}:`, error);
40+
throw new Error(`Failed to fetch from ${endpoint}`);
41+
}
42+
}
43+
44+
async getRegulationById(id) {
45+
const regulations = await this.fetchRegulations();
46+
const regulation = regulations.find(reg => reg.id === id);
47+
if (!regulation) {
48+
throw new Error('Regulation not found');
49+
}
50+
return regulation;
51+
}
52+
53+
// Method to refresh cache for a specific endpoint
54+
async refreshCache(endpoint) {
55+
try {
56+
const response = await axios.get(endpoint);
57+
const parsedData = parseRegulationData(response.data);
58+
this.cache.set(endpoint, parsedData);
59+
logInfo(`Cache refreshed for ${endpoint}`);
60+
} catch (error) {
61+
logError(`Error refreshing cache for ${endpoint}:`, error);
62+
}
63+
}
64+
}
65+
66+
// Example usage
67+
const regulationAPI = new RegulationAPI([
68+
'https://api.example.com/regulations',
69+
'https://api.anotherexample.com/regulations'
70+
]);
71+
72+
(async () => {
73+
try {
74+
const regulations = await regulationAPI.fetchRegulations();
75+
console.log('Fetched Regulations:', regulations);
76+
} catch (error) {
77+
console.error('Error:', error.message);
78+
}
79+
})();
80+
81+
module.exports = RegulationAPI;

0 commit comments

Comments
 (0)