An API (Application Programming Interface) is a simple interface that defines the types of requests (demands/questions, etc.) that can be made, how they are made, and how they are processed.
In our case, we will be building an API that allows us to send a range of GET/ POST/ PUT/ PATCH/DELETE requests (more on this later), to different endpoints, and return or modify data connected to our API.
Python
Flask Web Framework
REST API
Setup:
Mainly we will be working on two endpoint users and locations. Two datasets containing users and location info. You can also use your own data from firebase or SQL or MongoDB.
pip install Flask
pip install flask-restful
pip install pandas
Endpoints:
localhost/users
localhost/locations
Flask Setup:
from flask import Flask
from flask_restful import Resource, Api, reqparse
import pandas as pd
import ast
app = Flask(__name__)
api = Api(app)
GET:
Retrieve the URIs of the member resources of the collection resource in the response body. Retrieve a representation of the data in the response body.
POST:
Create a member resource in the collection resource using the instructions in the request body. The URI of the created member resource is automatically assigned and returned in the response Location header field.
PUT:
Replace all the representations of the member resources of the collection resource with the representation in the request body, or create the collection resource if it does not exist.
PATCH:
Update all the representations of the member resources of the collection resource using the instructions in the request body, or may create the collection resource if it does not exist.
DELETE:
Delete all the representations of the member resources of the collection resource.
Users Data:
Get
Post
Put
Delete
Locations Data:
Get
Post
Patch
Delete
Writing API Methods:
GET
http://127.0.0.1:5000/users
http://127.0.0.1:5000/locations
POST
http://127.0.0.1:5000/users?userId=78945&name=name1&city=bangalore
DELETE
http://127.0.0.1:5000/users?userId=78945
Now, you have created a simple REST API Web Application using Flask. Later you can introduce Database and create a simple Blog Application using REST API.
Commentaires