REST API for Songs DB with Flask and MongoDB

This service is written for GET or POST informations on a song database with Flask and locally installed MongoDB, it has features like,

  • song_id
  • artist
  • title
  • difficulty
  • level
  • released

Methods which can be used in this REST API are,

  1. GET /songs
    • Getting all the songs as a list in the database with the option of pagination (e.g. /songs/?page=1).
  2. POST /songs
    • Posting the song data to songs collection in the database. Song with same artist and title can only be adding to the collection once.
  3. GET /songs/search/string:message
    • With a parameter, search can be done throughout the whole data by looking at title and artist features with given message parameter.
  4. GET /songs/avg/difficulty/int:level
    • Here by giving level parameter, songs with that level are grouped and average difficulty value is calculated and returned as response. If any level parameter is given in this GET method, then average difficulty of all the songs is calculated.
  5. POST /songs/rating/
    • In this method, any song in the songs collection is rated by passing song_id and rating parameters to this method. Rating must be done by giving value between 1 and 5, unless it gives error.
  6. GET /songs/avg/rating/int:song_id
    • By passing a song id to this method the minimum, maximum and average rating values of that song id are returned as a response.

Installation

There are two options to make installations and run this project.

1 - Installation with Docker

git clone https://github.com/vurbag/songs-db-api.git
cd songs-db-api
docker-compose build
docker-compose up

This is the easy method to start using the API.

It only requires to install Docker and git,

Docker: https://docs.docker.com/install/ git: https://git-scm.com/downloads

After they are installed, follow these steps,

  1. Open Command Line

  2. Go to or create the directory which you want to download the code

  3. Run git clone to download this repository and then run cd songs-db-api to go to the main directory of programme

    git clone https://github.com/vurbag/songs-db-api.git

    cd songs-db-api

  4. Run "docker-compose build", which set python, mongodb and all the requirements for this project at once

  5. And then run "docker-compose up" which starts running flask server

After it starts, you can go to your web browser and type "http://localhost:5000", you should see the following response,

{
  "response": {
      "owner": "Burc Turkoglu",
      "projectName": "REST API For Songs DB",
      "version": "1.0"
  }
}

2 - Installation from scratch

This method requires followings,

Python 3.6 or newer: You can check if python exists by typing python --version to Command Line. If not, you can download it from https://www.python.org/downloads/

pip and virtualenv : By following this guide make sure pip and virtualenv is installed.

git: https://git-scm.com/downloads

MongoDB: https://docs.mongodb.com/manual/installation/#mongodb-community-edition

After the installations are completed, follow these steps,

  1. Open CMD/Terminal

  2. Go to or create the directory which you want to download the code

  3. Run git clone to download this repository and then run cd songs-db-api to go to the main directory of programme

    git clone https://github.com/vurbag/songs-db-api.git

    cd songs-db-api

  4. Create virtualenv to install required modules for this project by python -m virtualenv venv and then activate it by source venv/bin/activate if you are on Linux/macOS or activate it by .\venv\Scripts\activate if you are on Windows.

  5. Now download and install requirements for this project by pip install -r requirements.txt

  6. Finally, you can run Flask server by python app.py

When it starts go to your web browser and type http://localhost:5000, it will give response like,

{
  "response": {
      "owner": "Burc Turkoglu",
      "projectName": "REST API For Songs DB",
      "version": "1.0"
  }
}

Usage

For using this REST API, it is suggested to use REST clients like Postman or Insomnia.

Initally, by running python test/test.py you will add songs.json and songratings.json file to the database by making POST requests. These files also can be added by using clients.

Some example calls,