Skip to content

taiyeoguns/drf-api

Repository files navigation

DRF-API

Fake data API.

Built with Python and Django Rest Framework.

Requirements

Installation

Clone Project

git clone https://github.com/taiyeoguns/drf-api.git

Install Requirements

With a virtualenv already set-up, install the requirements with pip:

make install

Add details in .env file

Create .env file from example file and maintain necessary details in it e.g. secret key etc

cp .env.example .env

For 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 secret key

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.

Run migrations

Create tables in the database:

make db-migrate

Seed database

To populate database with sample data, run:

make seed

num parameter specifies how many items to enter into the tables e.g.

python manage.py seed --num 15

Start the server

Start the Django web server by running:

make run

Open a browser and navigate to http://localhost:8000/api

Documentation

Documentation using OpenAPI is available at http://localhost:8000/api/docs

Tests

In command prompt, run:

make test

Authentication

To 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-000000000000

In 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.

Run application with Docker

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-run

Wait till setup is complete and all containers are started.

Thereafter, application should be available at http://localhost:8000

About

Fake data API built with Python and Django Rest Framework

Resources

Stars

Watchers

Forks

Releases

Packages

Used by

Contributors

Languages