Skip to content

Custom Template for Django Rest Framework for ML purpose

Notifications You must be signed in to change notification settings

Y4rd13/DRF-API-ML-Template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation


DRF-API-ML-Template

( Version 1.0.0 )
Redoc · Swagger · Report Bug · Request Feature

Table of Contents
  1. About The Project
  2. Getting Started
  3. Usage of the API
  4. Contributing

About the Project

This project is a Custom Template for DRF Rest-API for ML/DL purpose, with using a custom service for the endpoint. The API is built in Django REST framework, Redis, Celery, MongoDB, Simple-JWT and Docker (docker-compose). Using this template, you can easily create a new API with a custom service with a few steps.

(back to top)

Getting Started

Prerequisites

  • Docker
  • Docker Compose
  • Ubuntu 20.04 LTS or higher

Installation & Deployment

  1. Clone the repository:
    git clone https://github.com/Y4rd13/DRF-API-Template.git
  2. Set the environment variables
  3. Deploy the application using Docker Compose
    docker-compose -f docker-compose up

(back to top)

Usage of the API

Authentication

This API uses authentication through API Key. The security scheme type is API Key and the header parameter name is Authorization. There are two types of tokens: token and token_create, both are required.

(back to top)

Endpoints

POST /api/token

Create a new access token.

Request Body

  • AUTHORIZATIONS: Auth_Token_Bearer_JWT_
  • REQUEST BODY SCHEMA: application/json
{
  "username": "string",
  "password": "string"
}

Responses

  • 201: Success response:
    • username: required, string (Username) non-empty
    • password: required, string (Password) non-empty
  • 400:
    • Error response

(back to top)

POST api/token/refresh

Create a new access token from a refresh token.

Request Body

  • AUTHORIZATIONS: Auth_Token_Bearer_JWT_
  • REQUEST BODY SCHEMA: application/json
{
  "refresh": "string"
}

Responses

  • 201: Success response:
    • refresh: required, string (Refresh) non-empty
    • access: string (Access) non-empty
  • 400:
    • Error response

(back to top)

POST api/user/create

Create a new user account.

Request Body

  • AUTHORIZATIONS: Auth_Token_Bearer_JWT_
  • REQUEST BODY SCHEMA: application/json
{
  "email": "user@example.com", // non-empty
  "username": "string", // [ 1 .. 150 ] characters
  "password": "string", // [ 1 .. 50 ] characters
  "account_type": "string",
  "token": "string" // non-empty
}

Responses

  • 201: Success response:
    {
      "data": "string",
      "code": 0,
      "status": "string"
    }
  • 400: Error response:
    {
      "message": "string",
      "code": 0,
      "status": "string",
      "error": "string"
    }

(back to top)

POST /api/v1/custom/service

Custom service endpoint.

Query Parameters

  • AUTHORIZATIONS: Auth_Token_Bearer_JWT
{
  "my_variable": "string", # required
  "other_variable_1": "JsonField", # not required (allow_blank=True)
  "other_variable_2" "string" # not required (allow_blank=True)
}

CURL

curl --location 'http://0.0.0.0:8000/api/v1/custom/service' \
--header 'Authorization: Bearer {jtw_token}' \
--header 'Content-Type: application/json' \
--data '{
    "my_variable": "string",
    "other_variable_1": {
        "key": "value"
    },
    "other_variable_2": "string"
}'

Python

import requests
import json

url = "http://0.0.0.0:8000/api/v1/custom/service"

payload = json.dumps({
   "my_variable": "string",
    "other_variable_1": {
        "key": "value"
    },
    "other_variable_2": "string"
})
headers = {
  'Authorization': 'Bearer {jtw_token}',
  'Content-Type': 'application/json'
}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)

JavaScript - Fetch

var myHeaders = new Headers();
myHeaders.append("Authorization", "Bearer {jtw_token}");
myHeaders.append("Content-Type", "application/json");

var raw = JSON.stringify({
  custom_variable: "string",
  other_variable_1: {
    key: "value",
  },
  other_variable_2: "string",
});

var requestOptions = {
  method: "POST",
  headers: myHeaders,
  body: raw,
  redirect: "follow",
};

fetch("http://0.0.0.0:8000/api/v1/custom/service", requestOptions)
  .then((response) => response.text())
  .then((result) => console.log(result))
  .catch((error) => console.log("error", error));

Responses

  • 200: Success response:
    {
      "data": "string",
      "code": 0,
      "status": "string"
    }
  • 400: Error response:
    {
      "message": "string",
      "code": 0,
      "status": "string",
      "error": "string"
    }

    (back to top)

Contributing

For future contributors, please see follow the next steps:

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request
  6. Wait for the Pull Request to be reviewed and merged

Note: Don't forget to update your local main branch before PR to merge.

(back to top)