This repository contains the example code for a contact API project, using SQLAlchemy, PyTest, Celery with Flask.
- Model a Contact with username, email, first name and surname.
- Create a Restful API that returns a list of all contacts.
- Returns a contact by username.
- Saves a Contact.
- Updates a Contact
- Deletes a Contact
- Allow a contact to have multiple email addresses. Adjust GET, POST, PUT, DEL methods to the new sub entity.
- Extend the GET to also accept the email address for contact retrieval.
- Implement a celery task to create a random contact with two email addresses every 15 seconds. Any older entries then 1 min should be cleaned up and deleted.
- Clone this repository.
- Create a virtualenv and activate.
- Install requirement packages.
- Make sure
redis-server
running on background. - Set
FLASK_ENV
environment variable asdevelopment
. (export FLASK_ENV=development
) - Open a second terminal and start celery:
celery worker -A celery_worker.celery --loglevel=info
. - Open a third terminal and start celery-beat:
celery -A celery_worker:celery beat --loglevel=INFO
. - Start the Flask application on your original terminal window:
flask run
. - Go to
http://localhost:5000/api/contacts/
and enjoy!
You can run the tests with
pytest tests
command.
curl -X POST \
http://localhost:5000/api/contacts/ \
-H 'Content-Type: application/json' \
-d '{
"username": "admin",
"first_name": "Armin",
"last_name": "Ronacher",
"emails": [
{"email": "arminronacher@mail.com"}
]
}'
curl -X PATCH \
http://localhost:5000/api/contacts/admin/ \
-H 'Content-Type: application/json' \
-d '{
"emails": [
{"email": "admin@mail.com"}
]
}'
curl -X GET http://localhost:5000/api/contacts/admin/