2024-10-09 16:36:08 +02:00
|
|
|
import axios from "axios";
|
|
|
|
|
2024-10-11 10:18:33 +02:00
|
|
|
// Initialize the API URL for the weather service
|
|
|
|
const apiURL = new URL("http://localhost:5000/interstellar_ai/api/weather");
|
2024-10-09 16:36:08 +02:00
|
|
|
if (typeof window !== 'undefined') {
|
2024-10-11 10:18:33 +02:00
|
|
|
apiURL.hostname = window.location.hostname; // Use current hostname in the browser
|
2024-10-09 16:36:08 +02:00
|
|
|
} else {
|
2024-10-11 10:18:33 +02:00
|
|
|
apiURL.hostname = "localhost"; // Fallback for server-side
|
2024-10-09 16:36:08 +02:00
|
|
|
}
|
|
|
|
|
2024-10-11 10:18:33 +02:00
|
|
|
// Function to get weather data
|
2024-10-09 16:36:08 +02:00
|
|
|
export const getWeather = async (data: object): Promise<string> => {
|
|
|
|
try {
|
2024-10-11 10:18:33 +02:00
|
|
|
// Make a POST request to the weather API with the provided data
|
2024-10-09 16:36:08 +02:00
|
|
|
const response = await axios.post(apiURL.href, data);
|
2024-10-11 10:18:33 +02:00
|
|
|
const status = response.data.status; // Extract the status from the response
|
|
|
|
const success = response.data.response; // Extract the actual weather data from the response
|
|
|
|
|
|
|
|
// Send the status and success response back to the worker
|
2024-10-09 16:36:08 +02:00
|
|
|
postMessage({ status, success });
|
2024-10-11 10:18:33 +02:00
|
|
|
return JSON.stringify(success); // Return the weather data as a JSON string
|
2024-10-09 16:36:08 +02:00
|
|
|
} catch (error) {
|
2024-10-11 10:18:33 +02:00
|
|
|
// Handle any errors that occur during the request
|
2024-10-09 16:36:08 +02:00
|
|
|
postMessage({ status: 500, success: false });
|
2024-10-11 10:18:33 +02:00
|
|
|
console.log(error); // Log the error for debugging
|
|
|
|
return ""; // Return an empty string in case of an error
|
2024-10-09 16:36:08 +02:00
|
|
|
}
|
2024-10-11 10:18:33 +02:00
|
|
|
};
|