LocationWeatherAPI - PythonLocationWeatherAPI - Python
API that provide personalized recipe recommendations based on dietary preferences, ingredients, and cooking preferences.LocationWeatherAPI - Python
API that provide personalized recipe recommendations based on dietary preferences, ingredients, a...Overview
LocationWeatherAPIÂ is a simple API built using the Flask framework that allows users to retrieve current weather information for a specific city.
By making a GET request to the /weather
endpoint with the city
parameter, users can obtain the temperature and description of the weather in that city.
The API requires an API key obtained from a weather API provider, which is included in the code before running the API.
It utilizes the requests library to send HTTP requests to the weather API provider's endpoint and fetches the current weather data in JSON format.
The API ensures parameter validation by checking if the city
parameter is provided and returns an appropriate error message if it is missing. It also supports language localization by specifying the 'lang' parameter as 'en' to receive weather information in English.
When the weather data is successfully retrieved, the API constructs a JSON response containing the city, temperature, and weather description. If there are any errors during the retrieval process or if the response status code is not 200, an error message is returned instead.
By following the provided instructions, users can set up and run the Flask weather API on their local machine, allowing them to access weather information by sending HTTP GET requests to the API's endpoint.
Features
- Weather data retrieval: The API allows users to retrieve weather information for a specific city.
- Parameter validation: The API checks if the 'city' parameter is provided and returns an appropriate error message if it's missing.
- API key usage: The API uses an API key to authenticate requests to the weather API provider.
- Language support: The API specifies the 'lang' parameter as 'en' to receive weather information in English.
- JSON response: The API returns weather data in JSON format.
- Error handling: If there's an error while fetching weather data or if the response status code is not 200, the API returns an error message.
Requirements
- Flask: The code requires the Flask framework to run the API.
- requests: The code uses the requests library to send HTTP requests to the weather API.
- API key: You need to obtain an API key from the weather API provider and replace '8555c38230c044a8a40152819231606' with your actual API key.
- Weather API endpoint: The code relies on the 'https://api.weatherapi.com/v1/current.json' endpoint to fetch weather data.
- Route configuration: The code sets up a route at '/weather' for handling weather API requests with the HTTP GET method.
- 'GET' method: The API supports only the GET method for retrieving weather data.
- City parameter: The API expects the 'city' parameter to be provided as a query parameter in the API endpoint URL.
- Temperature and description: The API extracts the current temperature and weather description from the weather API response to include them in the result.
Instructions
- Make sure you have python and flask installed
- Replace
{city_name}
with the name of the city for which you want to retrieve the weather data.
- If the city parameter is missing or there's an error, the API will return an appropriate error message.