forked from React-Group/interstellar_ai
weather comments
This commit is contained in:
parent
8a94beae31
commit
162e84cb7e
1 changed files with 16 additions and 10 deletions
|
@ -1,23 +1,29 @@
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
|
|
||||||
const apiURL = new URL("http://localhost:5000/interstellar_ai/api/weather")
|
// Initialize the API URL for the weather service
|
||||||
|
const apiURL = new URL("http://localhost:5000/interstellar_ai/api/weather");
|
||||||
if (typeof window !== 'undefined') {
|
if (typeof window !== 'undefined') {
|
||||||
apiURL.hostname = window.location.hostname;
|
apiURL.hostname = window.location.hostname; // Use current hostname in the browser
|
||||||
} else {
|
} else {
|
||||||
apiURL.hostname = "localhost"
|
apiURL.hostname = "localhost"; // Fallback for server-side
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Function to get weather data
|
||||||
export const getWeather = async (data: object): Promise<string> => {
|
export const getWeather = async (data: object): Promise<string> => {
|
||||||
try {
|
try {
|
||||||
|
// Make a POST request to the weather API with the provided data
|
||||||
const response = await axios.post(apiURL.href, data);
|
const response = await axios.post(apiURL.href, data);
|
||||||
const status = response.data.status;
|
const status = response.data.status; // Extract the status from the response
|
||||||
const success = response.data.response;
|
const success = response.data.response; // Extract the actual weather data from the response
|
||||||
|
|
||||||
|
// Send the status and success response back to the worker
|
||||||
postMessage({ status, success });
|
postMessage({ status, success });
|
||||||
console.log(JSON.stringify(success))
|
console.log(JSON.stringify(success)); // Log the successful response for debugging
|
||||||
return JSON.stringify(success);
|
return JSON.stringify(success); // Return the weather data as a JSON string
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
// Handle any errors that occur during the request
|
||||||
postMessage({ status: 500, success: false });
|
postMessage({ status: 500, success: false });
|
||||||
console.log(error)
|
console.log(error); // Log the error for debugging
|
||||||
return "";
|
return ""; // Return an empty string in case of an error
|
||||||
}
|
}
|
||||||
};
|
};
|
Loading…
Reference in a new issue