Fake data API.
Built with Python and Django Rest Framework.
- Python 3
- Django Rest Framework 3.9.x
- Django 5.1.x
- Postgres database
git clone https://github.com/taiyeoguns/drf-api.gitWith a virtualenv already set-up, install the requirements with pip:
make installCreate .env file from example file and maintain necessary details in it e.g. secret key etc
cp .env.example .envFor deployment to other environments, ensure to maintain the host details in the .env file in the ALLOWED_HOSTS variable. Similar for CSRF_TRUSTED_ORIGINS.
Also set DEBUG=False for environments other than development.
Generate a secret key to be used by the Django application using the command below:
python -c "import string,secrets; uni=string.ascii_letters+string.digits+string.punctuation; print(repr(''.join([secrets.choice(uni) for i in range(48)])))"Copy the generated string and add to the .env file created in previous step.
Create tables in the database:
make db-migrateTo populate database with sample data, run:
make seednum parameter specifies how many items to enter into the tables e.g.
python manage.py seed --num 15Start the Django web server by running:
make runOpen a browser and navigate to http://localhost:8000/api
Documentation using OpenAPI is available at http://localhost:8000/api/docs
In command prompt, run:
make testTo authenticate with the API, appropriate user API token should be passed in the X-API-KEY HTTP header, e.g.
X-API-KEY: 00000000-0000-0000-0000-000000000000In development, if seeding was done, an API token 00000000-0000-0000-0000-000000000000 should exist for use.
Example request:
curl -X 'GET' \
'http://localhost:8000/api/employees/' \
-H 'accept: application/json' \
-H 'X-API-KEY: 00000000-0000-0000-0000-000000000000'In the OpenAPI (Swagger) documentation page, to authenticate, click on the Authorize button before any requests and paste in the token to apply it for all requests needing authentication.
Ensure database details are added to .env file from earlier.
The following environment variables should be set in the .env file even if they do not 'exist', the docker postgres image will use them for setting up the container -
POSTGRES_USER, POSTGRES_PASSWORD, POSTGRES_DB
With Docker and Docker Compose set up, run:
make docker-runWait till setup is complete and all containers are started.
Thereafter, application should be available at http://localhost:8000