ogdhruv / weather-assignment

A API for weather data of cities on homepage.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Weather Assignment

A restful api for weather data and representation of city on homepage.

demo

📖 Prerequisite

📀 Running Demo

  • Must have postgreSQL data details same as below for running or change according to your ease :

    base/setting.py line 85

    "NAME": "postgres",
    "USER": "postgres",
    "PASSWORD": "postgres",
    "HOST": "localhost",
    "PORT": 5432, # default port
    

Steps for running the instance of projects without docker :

git clone https://github.com/ogdhruv/weather-assignment.git
cd weather-assignment
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
python manage.py migrate
python manage.py loaddata fixtures/city.json --app weather.city
python manage.py runserver

Steps for API access and CRUD functionality use :

  • Create a user with signup functionality from navigation bar.

  • Go to http://127.0.0.1:8000/token/ to get your access token.

  • Add your detail below where it asks for username and password.

  • Copy access token.

  • Open postman ,create a collection and create a request.

    • if using http://127.0.0.1:8000/api/cities/ endpoint GET and POST request will work.
    • if using http://127.0.0.1:8000/api/cities/<str:city_name>/ endpoint GET,PUT and DELETE request will work.
  • In authorization toolbar choose type as Bearer Token and paste the access token.

  • If on http://127.0.0.1:8000/api/cities/ endpoint add data in body toolbar -> choose raw -> select JSON instead of Text.

    • City Name must start with capital letter.
    • Add data in format given below:
    {
        "city_name": "Leh",
        "city_country": "IN",
        "temp": 22.0,
        "pressure": "1016",
        "humidity": "83",
        "weather": "Clouds",
        "icon": "03n"
    
    }
  • Hit Send.

  • The changes will be shown on homepage of the web application.

API endpoints

http://127.0.0.1:8000/token
http://127.0.0.1:8000/api/ - for all endpoint list
http://127.0.0.1:8000/api/cities/ - for list of cities with their weather data [POST and GET]
http://127.0.0.1:8000/api/cities/<str:city_name>/, - for each city weather data [GET,PUT and DELETE]

Steps for running the instance of projects with docker :

⚠️ Firstly change "HOST": "localhost" in postgresql setting to "HOST": "db".

git clone https://github.com/ogdhruv/weather-assignment.git
cd weather-assignment

(Docker may ask for sudo privilege)

docker compose up -d --build
docker compose exec web python manage.py migrate
docker compose exec web python manage.py loaddata fixtures/city.json --app weather.city
  • If you refresh the Django welcome page at http://127.0.0.1:8000/ it will start to work.

When you're done, don't forget to close down your Docker container :

docker compose down

Features 🌟

  • Use of PostgreSQL.
  • Use of Docker.
  • Use of openweatherAPI for weather data and saving the same in database.
  • Use djangorestframework-simplejwt for Token access to the api.
  • Django-rest-framework for API Development.

Screenshot

  • API View

API List view

Project Structure

📦weather-assignment
┣ 📦base (Project core folder)
┃ ┣ 📜__init__.py
┃ ┣ 📜asgi.py
┃ ┣ 📜settings.py
┃ ┣ 📜urls.py
┃ ┗ 📜wsgi.py
┣ 📦fixtures (Fixtures for predefined data to load in postgres)
┃ ┗ 📜city.json
┣ 📦images (Images for README.md)
┃ ┣ 📜api.png
┃ ┗ 📜main.png
┣ 📦templates (Template folder for all html files)
┃ ┣ 📂weather
┃ ┃ ┣ 📜home.html
┃ ┃ ┣ 📜login.html
┃ ┃ ┗ 📜signup.html
┃ ┣ 📜base.html
┃ ┗ 📜navbar.html
┣ 📦weather (Project's application)
┃ ┣ 📂migrations
┃ ┣ 📜__init__.py
┃ ┣ 📜admin.py
┃ ┣ 📜apps.py
┃ ┣ 📜forms.py
┃ ┣ 📜models.py
┃ ┣ 📜serializers.py
┃ ┣ 📜tests.py
┃ ┣ 📜urls.py
┃ ┣ 📜utilities.py
┃ ┗ 📜views.py
┣ 📜.dockerignore
┣ 📜.gitignore
┣ 📜Dockerfile
┣ 📜README.md
┣ 📜manage.py
┣ 📜requirement.txt
┗ 📜docker-compose.yml

Authors 😁️

ogDhruv

You can find me here at:

Github

Gmail

© ogDhruv

About

A API for weather data of cities on homepage.


Languages

Language:Python 85.0%Language:HTML 13.6%Language:Dockerfile 1.4%