Flask application to get weather information based on city id.
• API Endpoints • How to Run • Example
OpenWeatherMap is an online service, owned by OpenWeather Ltd, that provides global weather data via API, including current weather data, forecasts, nowcasts and historical weather data for any geographical location. The company provides a minute-by-minute hyperlocal precipitation forecast for any location.
This endpoint can be accessed by GET and POST methods and allows the user to get weather forecast information based on a city id from the API. The desired id can be obtained by accessing the website www.openweathermap.org, searching the city name in the search field, selecting the desired city and getting the id in the browser address bar.
Whether using the GET or the POST method, the only parameter to be passed is the city id.
- Parameter:
- id: id of the desired city from the API. For example: "3443207" or "3442546".
Before execution, it is necessary to set up the OpenWeather API key. You need to modify the apikey.json file located in the owapi folder and replace the term change-me with your API key generated in OpenWeather Api Keys.
The application was developed in Flask and runs using the docker tool, which can be installed via this link
- Steps to build the docker image: In the root of directory, type the following commands:
docker build -t owapi .
docker run --name owapi -p 5000:5000 owapi
- Steps to start or stop the container:
docker start owapi
docker stop owapi
After the docker image has been built and run, the service will be available locally in http://localhost:5000/, where the user can see the service welcome page.
To use the weather endpoint, the user can type the following address:
http://localhost:5000/weather?id=3442546
The following result will be presented:
{
"base": "stations",
"clouds": {
"all": 76
},
"cod": 200,
"coord": {
"lat": -34.6,
"lon": -54.55
},
"id": 3442546,
...
...
...
"weather": [
{
"description": "broken clouds",
"icon": "04d",
"id": 803,
"main": "Clouds"
}
],
"wind": {
"deg": 41,
"gust": 8.79,
"speed": 5.05
}
}