This is an API developed in Python for the olist challenge.
Live at: henrique1472.pythonanywhere.com/
Operating system: Windows 10
IDE: Pycharm 2021.2.2
Frameworks/libraries: Flask, Flask-SQLAlchemy
Database: SQLite
- Rename the file
example.env
to.env
and set your custom flask SECRET_KEY - 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/
After the installation, type this command to run all the tests
python -m pytest tests/ -vv
/authors # Get all authors data. Method: GET
page
optional Integer Parameter to specify which page to query. default=1
name
optional String Parameter to filter author by name.
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()
"authors": {"id": "Author Name"}
{
"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"
}
}
{"8": "George Orwell"}
/books # Method: POST
{
"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
}
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'}
/books # Method: GET
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
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()
{
"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"
}
]
}
{
"books": [
{
"id": 1,
"name": "Animal Farm",
"edition": 452284244,
"publication_year": 1945,
"authors": "George Orwell"
}
]
}
You can get the book_id in the /books
route.
/books/<book_id> # Method: PUT
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"}
You can get the book_id in the /books
route.
/books/<book_id> # Method: DELETE
book_id = 3
delete_book = requests.delete(f'http://127.0.0.1:5000/books/{book_id}').json()
print(delete_book) ## {"Success": "Book deleted"}