Weather API
An API that gets the minimum, maximum, average and median temperature for a given city and period of time.
Introduction
An API that gets the minimum, maximum, average and median temperature for a given city and period of time.
Prerequisites
Before you continue, ensure you have met the following requirements:
- You have installed Python 3.0 or later
- You have installed the latest version of virtualenv Python library.
- You are using a Linux or Mac OS or Windows machine.
Usage
-
Get the source code onto your dev machine workspace:
git clone https://github.com/pmutua/weather-api.git
-
Navigae to the folder:
cd weather-api
-
Create a virtual environment
Linux/MacOS:
python3 -m virtualenv env
Windows
python -m virtualenv env
-
Start the virtual environment
Linux/MacOS
source env/bin/activate
Windows
.\env\Scripts\activate
-
Install dependencies
Linux:
pip install -r requirements/dev.txt
Windows:
pip install -r requirements\dev.txt
-
Set up environment variables Create a file called
.env
in the directory wheremanage.py
is located.The
.env
should have the following variables then add neccessary values:DEBUG=<Add value here> # Boolean value. Use True when running in development. SECRET_KEY=<AddSecretKey> DATABASE_URL=<AddDataBaseUrl> ALLOWED_HOSTS=<AddAllowedHostsHere> WEATHER_API_KEY=<AddWeatherAPIKeyHere> WEATHER_API_BASE_URL=http://api.weatherapi.com/v1
Example*
DEBUG=True SECRET_KEY=b54rrrw432313 DATABASE_URL= # If debug is True you don't need this variable. ALLOWED_HOSTS=localhost WEATHER_API_KEY=846452528 WEATHER_API_BASE_URL=http://api.weatherapi.com/v1
You can get the Weather API key at Weather API
-
Create migrations and apply migrations.
python manage.py makemigrations python manage.py migrate
-
Start the app locally
python manage.py runserver
Using the API
The API currently has one endpoint that allows the user to get the minimum, maximum, average and median temperature for a given city and period of time.
Making a Request
Url format: /api/locations/<add city name here>/?days=<add number of days here>
Example
GET /api/locations/london/?days=1
Parameter | Type | Description |
---|---|---|
city |
string |
Required. city name |
days |
integer |
Required. days |
Response
The endpoint returns a JSON response in the following format:
{
"success": true,
"msg": "success",
"data": {
"maximum": 18.6,
"minimum": 11.0,
"average": 13.593055555555555,
"median": 12.95
},
"status": 200
}
The msg
attribute contains a message commonly used to indicate errors or success that the resource was retrieved.
The success
attribute describes if the request was successful or not.
The status
attribute describes status code.
The data
attribute contains JSON data.
Status Codes
The API returns the following status codes in its API:
Status Code | Description |
---|---|
200 | OK |
400 | BAD REQUEST |