GithubHelp home page GithubHelp logo

restapi's Introduction

SIMPLE REST API EXAMPLE - Python - DJANGO - Django Rest Framework

This is a sample of how make a simple rest-api with Python, Django and django-rest-framework Django REST framework is a powerful and flexible toolkit for building Web APIs.

Requirements

  • Python 3.6
  • Django (2.2.1)
  • Django REST Framework

Installation

	pip install django
	pip install djangorestframework

Installation

How this is a example, to make data ready to read, this application contains a SQLite database file in your environment.

Structure

A RESTfull API's, are definide by endpoints. This structure allow end users and applications, access data from our application using the HTTP methods - GET, POST, PUT, DELETE.

We have one single resource, employee, so we will use the following URLS - /employee/ and /employee/<id> for detailed element, or specific method, respectively:

The resource can list, add, update and remove employees

Endpoint HTTP Method Result
employee GET Get a list of all employees
employee/:id GET Get a single employee
employee POST Insert a new employee
employee/:id PUT Update a employee
employee/:id DELETE Delete a employee

Using the API

We can test the API using curl or httpie. Httpie is a user friendly http client that's written in Python.

You can install httpie using pip:

pip install httpie

First, we have to start up Django's development server.

	python manage.py runserver

Calling the below endpoints from another terminal

Endpoint HTTP Method Result
employee GET Get a list of all employees
	http http://127.0.0.1:8000/employee/

we list all employees

{
    "name": "Thales G",
    "email": "[email protected]",
    "department": "Engenharia"
},
{
    "name": "Kleber Alves",
    "email": "[email protected]",
    "department": "Laboratório de Ciência de dados"
},
{
    "name": "Thiago Bueno",
    "email": "[email protected]",
    "department": "Engenharia"
},
{
    "name": "Ariane C",
    "email": "[email protected]",
    "department": "Hospotal das Clinicas"
},
{
    "name": "Leonardo",
    "email": "[email protected]",
    "department": "UFSCAR"
}
Endpoint HTTP Method Result
employee/:id GET Get a single employee
	http http://127.0.0.1:8000/employee/4

We get the employee with id = 4

{
    "name": "Kleber Alves",
    "email": "[email protected]",
    "department": "Laboratório de Ciência de dados"
}
Endpoint HTTP Method Result
employee POST Insert a new employee
http --json -v POST localhost:8000/employee/ name=John [email protected] department=helthcare

We can insert a new employee in our API with post method

See the return below

HTTP/1.1 201 Created
Allow: GET, POST, OPTIONS
Content-Length: 67
Content-Type: application/json
Date: Wed, 05 Jun 2019 12:58:28 GMT
Server: WSGIServer/0.2 CPython/3.5.3
Vary: Accept, Cookie
X-Frame-Options: SAMEORIGIN

{
    "department": "helthcare",
    "email": "[email protected]",
    "name": "John"
}

Endpoint HTTP Method Result
employee PUT Update a existing employee
http --json -v PUT localhost:8000/employee/5 name=Carlos [email protected] department=Finance

We can update a new employee in our API with PUT method

See the return below

HTTP/1.1 200 OK
Allow: PUT, GET, DELETE, OPTIONS
Content-Length: 69
Content-Type: application/json
Date: Wed, 05 Jun 2019 13:05:36 GMT
Server: WSGIServer/0.2 CPython/3.5.3
Vary: Accept, Cookie
X-Frame-Options: SAMEORIGIN

{
    "department": "Finance",
    "email": "[email protected]",
    "name": "Carlos"
}
Endpoint HTTP Method Result
employee DELETE Delete a existing employee
	http --json -v DELETE localhost:8000/employee/5

We can update a new employee in our API with PUT method

See the return below

HTTP/1.1 204 No Content
Allow: PUT, GET, DELETE, OPTIONS
Content-Length: 0
Date: Wed, 05 Jun 2019 13:14:47 GMT
Server: WSGIServer/0.2 CPython/3.5.3
Vary: Accept, Cookie
X-Frame-Options: SAMEORIGIN

Administration page

This application contains too a very simple administration page, when you can see your datas and manage your employees, to do CRUD operations.

The index page will cal the CRUD Administration View.

You can check in URL below

	http://localhost:8000

restapi's People

Contributors

dependabot[bot] avatar thiagojoa avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.