An Rest API to perform weather requests with the OpenWeatherMap API, developed with the objective of fix the need raised by the initial challenge of the selection process for back-end developer of DevGrid.
To run this project in a simple way, it's necessary that you have Docker in the version >= 19 and Docker Compose in the version >= 1.25 installed on your machine.
- First, clone the remote application repository on your local machine:
$ git clone https://github.com/rarygc/weather-buddy-api.git
- Then, access the application's base directory:
$ cd weather-buddy-api
- Finally, being in the directory, just build and up a container with the application in Flask:
$ docker-compose up --build
Ready! The Rest API is available on your local machine on the URL http://localhost:5000/
As requested by the initial challenge, the application consists of a set of microservices for to perform weather requests with the OpenWeatherMap API in JSON format.
The application provides a service interface mapped below:
It's a small initial interface that returns a message of greetings. If you can see it "Hello, DevGrid!", it means that the application is fully operational.
GET http://localhost:5000/
Get the main page greeting
-
Parameters
This endpoint has no input parameters.
-
Response
This is the result for
http://localhost:5000
:{ "greeting": "Hello, DevGrid!" }
It's the main interface of the application. It's responsible for providing the Weather management functionality. From this interface, we can request the weather in cities.
GET http://localhost:5000/weather/{city_name}
get a city weather
-
Parameters
This endpoint has no input parameters.
-
Responses
This is the result for
http://localhost:5000/weather/Florianópolis
:{ "city": "Florianópolis", "temp": 26.02, "weather": "Scattered Clouds" }
Once requested, this city will be available for 5 minutes in cache.
GET http://localhost:5000/weather
get a list of weathers in cache
-
Parameters
key description type required max
Define the number of cities to be request. This parameter must be a positive integer between 1 and 5. Ex.: 4
. If not informed, the default value will be5
integer no -
Responses
This is the result for
http://localhost:5000/weather
withmax = 4
:[ { "city": "São Paulo", "temp": 19.66, "weather": "Light Rain" }, { "city": "Porto Alegre", "temp": 24.28, "weather": "Clear Sky" }, { "city": "Belo Horizonte", "temp": 21, "weather": "Clear Sky" }, { "city": "Florianópolis", "temp": 25.4, "weather": "Scattered Clouds" } ]
To configure your development environment, it's necessary that you have Pipenv installed on your machine.
- First, clone the remote application repository on your local machine:
$ git clone https://github.com/rarygc/weather-buddy-api.git
- Them, access the application's base directory:
$ cd weather-buddy-api
- Now, create a
.env
environment configuration file in the base directory with the following content:
# Flask App settings
FLASK_APP=api
FLASK_ENV=development
DEBUG=True
# OpenWeatherMap API settings
OPENWEATHERMAP_TOKEN=3f62164f8ce1f8cb5ae8e2d02918babb
Note: In the
.env
file there are configuration variables for the flask application and authentication in the OpenWeatherMap API. TheOPENWEATHERMAP_TOKEN
variable is receiving a default value that is an access token generated exclusively for this public application. But, feel free to generate your own access token at the OpenWeatherMap Official Website and use it, if desired.
Assuming that the previous step was properly performed, to run the application in development mode, just follow the guidelines below:
- First, install required packages with Pipenv:
$ pipenv install
- Finally, run the application with Flask:
$ pipenv run flask run
Note: All commands related to a python environment, such as
pip freeze
orpython [file]
, are available, using thepipenv run
command as a prefix to run any command within the environment.
Assuming that the "Settings environment" step was properly executed, to run the application tests in development mode, just follow the guidelines below:
- First, install development required packages:
$ pipenv install --dev
Note: This command installs all packages installed in the previous example, also including development packages listed in
[dev-packages]
in thePipfile
.
- Finally, run the application tests with Pytest:
$ pipenv run pytest