DEV Community

Phylis Jepchumba
Phylis Jepchumba

Posted on

Intoduction to Flask-RESTful API.

Definition of terms

Representational State Transfer (REST)

We can define REST as software architectural style
for web services that provides a standard for data communication
between different kinds of systems.

Or as a standard for exchanging data over the Web for the sake of interoperability between computer systems.

Application Programming Interface (API)

It is a software intermediary that allows two applications to communicate.

image

Web services which conform to the REST architectural
style are called RESTful web services which allow requesting systems to access and manipulate the data using a uniform and predefined set of stateless operations.

Key features of the REST API.

Uniform Interface

RESTful services have a;

  • Uniform interface to access resources across the system.
  • Uniform API interface across the system.
  • A logical URI system with uniform ways to fetch and manipulate data.

Common HTTP Methods used in RESTful services

Verb CRUD Operation
GET Read Fetch a single or multiple resource
POST Create Insert a new resource
PUT Update/Replace Insert new resource or update existing
DELETE Delete Delete a single or multiple resource
PATCH Update/Modify Only update the provided changes to the resource

Representations

A representation is a machine readable explanation defining the current state of a resource.
When designing RESTful services it is essential to identify different resources and determine the relation between them.

REST provides us the ability to use the following formats for representing the resources in the system.

JSON Representation

{
 "ID": "1",
 "Name": "Building REST APIs wiith Flask",
 "Author": "@phy",
 "Publisher": "Apress"
}
Enter fullscreen mode Exit fullscreen mode

XML Representation

<?xml version="1.0" encoding="UTF-8"?>
<Book>
 <ID> 1 </ID>
 <Name> Building REST APIs with Flask   </Name>
 <Author> @phy </Author>
 <Publisher > Apress </ Publisher >
</Book>
Enter fullscreen mode Exit fullscreen mode

JSON is the preferred method for representing the resources to be called by mobile or web clients.
XML can be used to represent more complex resources.

Stateless

This is the most important characteristic of a REST service. A REST HTTP request consists of all the data needed by the server to understand and give back the response. Once a request is served, the server doesn't remember if the request has arrived after a while. So the operation will be a stateless one.

Links Between Resources

A resource is an object with a type, associated data, and relationships to other resources alongside a set of methods that can be executed on it.
The resource in a REST API can contain link to other resources which should drive the process flow.
Example;

{
 "ID": "1",
 "Name": "Building REST APIs wiith Flask",
 "Author": "@phy",
 "Publisher": "Apress",
 "URI" : "https://apress.com/us/book/123456789"
}
Enter fullscreen mode Exit fullscreen mode

Check more features here

Now, After the basic introduction, Let’s get started with a simple example using Flask-restful.

Flask-RESTful is an extension for Flask that adds support for quickly building REST APIs. It is a lightweight abstraction that works with your existing ORM/libraries.

To Set up Flask environment Check my previous article on my first flask application

Installation.
Install Flask-RESTful with pip as follows;

pip install flask-restful
Enter fullscreen mode Exit fullscreen mode

A minimal Flask-RESTful API

from flask import Flask, jsonify
from flask_restful import Resource, Api

app = Flask(__name__)
api = Api(app)

class info(Resource):
    def get(self):
       return jsonify({'name':'Phylis',
                    'address':'Kenya'
                                    })

api.add_resource(info, '/')

if __name__ == '__main__':
    app.run(debug=True)


Enter fullscreen mode Exit fullscreen mode

From the above code we have imported the required libraries Flask, jsonify, Resource and Api. jsonify is used to convert Python dict structures to json format.

We have initialized and flask app. Then, we have assigned the flask app as an api.

app = Flask(__name__)
api = Api(app)
Enter fullscreen mode Exit fullscreen mode

info is a class which acts as an api resource and the function get acts as get request.

class info(Resource):
    def get(self):
       return jsonify({'name':'Phylis',
                    'address':'Kenya'
                                    })
Enter fullscreen mode Exit fullscreen mode

Adding info as a resource with url ‘/’ for the api.

api.add_resource(info, '/')
Enter fullscreen mode Exit fullscreen mode

Output of the code will be;

{"address":"Kenya","name":"Phylis"}
Enter fullscreen mode Exit fullscreen mode

Oldest comments (0)