By Using the Django Rest Framework (check link here: https://www.django-rest-framework.org), I build a service that authenticates a user and displays the temperature of his requested city.
-
Create simple API endpoints for:
- registration
- login
-
Create simple API endpoint that takes the city and return its weather.
- consume the API using Django and return its results in JSON format.
- Use API for weather from weatherstack.com
-
Build a simple html interface that:
- allows the user to login using his credentials and register.
- another interface that allows authenticated users to enter a city and get its weather conditions.
I used Django Rest Framework (check link here: https://www.django-rest-framework.org) for API and even in the interface
-
in your terminal run this command
git clone https://github.com/Nasrallah-Adel/weather.git
-
move to project folder by
cd weather
-
use virtual environment [Conda or virtualenv] and create env for project and activate it
-
install project packages to your env by this command
pip install -r requirements.txt
-
migrate to database sqlite by command :
python manage.py migrate
-
If you need run UnitTest For Apis run this command:
python manage.py test apps/apis
-
run project server by this command
-
python manage.py runserver 127.0.0.1:8000
-
your project will run on
127.0.0.1:8000
-
#project Documentation
#######Note For postman URL : PostMan Collection URL . File json :Postman Json File have three url in Api app
-
POST : 127.0.0.1:8000/apis/register
this register endpoint to create new user-
POST data of user in this format
{ "username": "string", "password": "string" }
-
you will receive Response in this format
{ "id": int, "username": "string", "token": "string" }
-
-
POST : 127.0.0.1:8000/apis/login
this login endpoint to authenticate exist user-
POST data of user in this format
{ "username": "string", "password": "string" }
-
you will receive Response in this format
{ "token": "string" }
you will use this token to can invoke
127.0.0.1:8000/apis/weather/:city
API endpoint
-
-
-
GET 127.0.0.1:8000/apis/weather/{city}/
this WEATHER endpoint to retrieve weather data for specific city-
example
-
Use GET method to call
127.0.0.1:8000/apis/weather/cairo/
but you need add to request headerAuthorization: TOKEN <your token from login or register>
to allow just auth user to call api -
if you write good city name ( cairo ) you will receive this response
{ "location_name": "Cairo", "location_country": "Egypt", "location_region": "Al Qahirah", "location_timezone": "Africa/Cairo", "location_temperature": 16, "location_weather_icons": [ "https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png" ], "location_weather_descriptions": [ "Partly cloudy" ] }
- if you write bad city name you will get error response
{ "error_type": "request_failed", "error_info": "Your API request failed. Please try again or contact support." }
-
-
-
http://127.0.0.1:8000/docs
for swagger documentation for api -
http://127.0.0.1:8000/weather/
interface of weather form to take city name
after enter correct city name (cairo) and submit get weather button you will git data like