Skip to content

API developed in Python-Flask for the backend challenge

License

Notifications You must be signed in to change notification settings

henr1q/work-at-olist

 
 

Repository files navigation

Flask REST API CRUD

This is an API developed in Python for the olist challenge.
Live at: henrique1472.pythonanywhere.com/

Work environment

Operating system: Windows 10
IDE: Pycharm 2021.2.2
Frameworks/libraries: Flask, Flask-SQLAlchemy
Database: SQLite

Installation

  1. Rename the file example.env to .env and set your custom flask SECRET_KEY
  2. Clone or download this repository, go to the main folder and type this commands
pip install -r requirements.txt
flask db create_db
flask db import_authors authors.csv
flask run

Server will run at: http://localhost:5000/

Testing

After the installation, type this command to run all the tests

python -m pytest tests/ -vv

API Documentation

Authors endpoint

Routes

/authors  # Get all authors data. Method: GET

Parameters

page optional Integer Parameter to specify which page to query. default=1
name optional String Parameter to filter author by name.

Requests example in Python code

authors_page_one = requests.get('http://127.0.0.1:5000/authors').json()
authors_page_ten = requests.get('http://127.0.0.1:5000/authors', params={'page': 10}).json()
single_author = requests.get('http://127.0.0.1:5000/authors', params={'name': 'George Owell'}).json()

Response format

  "authors": {"id": "Author Name"}

authors_page_one

 {
  "pages": {
    "current_page": 1,
    "max_authors_per_page": 100,
    "total_pages": 1,
    "total_authors": 10
  },
  "authors": {
    "1": "Luciano Ramalho",
    "2": "Osvaldo Santana Neto",
    "3": "David Beazley",
    "4": "Chetan Giridhar",
    "5": "Brian K. Jones",
    "6": "J.K Rowling",
    "7": "Emil Cioran",
    "8": "George Orwell",
    "9": "Stephen King",
    "10": "John Green"
    }
}

single_author

{"8": "George Orwell"}

CRUD books endpoint

Create

Route

/books  # Method: POST

JSON Payload format

{
 "name": book_name                     # Name of the book
 "edition": book_edition               # Edition number
 "publication_year": book_year         # Publication year of the book;
 "authors": [author_id, author_id]     # List of author ids, same ids of previous imported data
}

POST example in Python code

book_data = {
    "name": 'Animal Farm',                  # Name of the book
    "edition": 452284244,                   # Edition number
    "publication_year": 1945,               # Publication year of the book
    "authors": [8]                          # List of author ids, same ids of previous imported data
}

response = requests.post('http://127.0.0.1:5000/books', json=book_data)
print(response.json())         # {'Success': 'Book added'}

Read

Route

/books  # Method: GET

Parameters

page optional Integer Parameter to specify which page to query. default=1
name optional String Parameter to filter books by name
author optional String Parameter to filter books by author
year optional Integer Parameter to filter books by year
edition optional Integer Parameter to filter books by edition

Requests example in Python code

all_books = requests.get('http://127.0.0.1:5000/books').json()
george_books = requests.get('http://127.0.0.1:5000/books', params={'author': 'George Orwell'}).json()

all_books

{
  "pages": {
    "current_page": 1, 
    "max_books_per_page": 100, 
    "total_pages": 1, 
    "total_books": 3
  }, 
  "books": [
  {
      "id": 1, 
      "name": "Animal Farm", 
      "edition": 452284244, 
      "publication_year": 1945, 
      "authors": "George Orwell"
    }, 
    {
      "id": 2, 
      "name": "Fluent Python", 
      "edition": 11111, 
      "publication_year": 2015, 
      "authors": "Luciano Ramalho"
    }, 
    {
      "id": 3, 
      "name": "The Trouble With Being Born", 
      "edition": 2222, 
      "publication_year": 1973, 
      "authors": "Emil Cioran"
    }
    ]
}

george_books

{
  "books": [
    {
      "id": 1, 
      "name": "Animal Farm", 
      "edition": 452284244, 
      "publication_year": 1945, 
      "authors": "George Orwell"
    }
  ]
}

Update

You can get the book_id in the /books route.

Route

/books/<book_id>  # Method: PUT

Requests example in Python code

Send a Json payload with the fields you want to update.

book_id = 3
new_data = {"edition": 5555}
update_book = requests.put(f'http://127.0.0.1:5000/books/{book_id}', json=new_data).json()
print(update_book)    #  {"Success": "Book edited"}

Delete

You can get the book_id in the /books route.

Route

/books/<book_id>  # Method: DELETE

Requests example in Python code

book_id = 3
delete_book = requests.delete(f'http://127.0.0.1:5000/books/{book_id}').json()
print(delete_book)   ## {"Success": "Book deleted"}

About

API developed in Python-Flask for the backend challenge

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 99.8%
  • Shell 0.2%