Getting Started with RESTful API in Python 3.x

1

Topics –

  1. What is REST?
  2. Rest Conventions.
  3. Implementation.

What is REST?

These are the properties that should be followed by a REST api, along with REST conventions given below.
1. Client-Server: There should be a separation between the server that offers a service, and the client that consumes it.
2. Stateless: Each request from a client must contain all the information required by the server to carry out the request. In other words, the server cannot store information provided by the client in one request and use it in another request.
3. Cacheable: The server must indicate to the client if requests can be cached or not.
4. Layered System: Communication between a client and a server should be standardised in such a way that allows intermediaries to respond to requests instead of the end server, without the client having to do anything different.
5. Uniform Interface: The method of communication between a client and a server must be uniform.
6. Code on demand: Servers can provide executable code or scripts for clients to execute in their context. This constraint is the only one that is optional.

Central to the concept of RESTful web services is the notion of resources. Resources are represented by URIs. The clients send requests to these URIs using the methods defined by the HTTP protocol, and possibly as a result of that the state of the affected resource changes.

REST Conventions

The HTTP request methods are typically designed to affect a given resource in standard ways:

Screenshot 2019-02-17 at 11.51.40 AM

The REST design does not require a specific format for the data provided with the requests. In general data is provided in the request body as a JSON blob, or sometimes as arguments in the query string portion of the URL.

Implementing RESTful Services in Python and Flask

All the softwares needed –

  • python 3.x
  • flask package
  • virtualenv package
  • cygwin (if you use windows)

The code and syntax shown will work on any Linux/Mac OS and also on Windows with a few changes in syntax.

Let’s first create a virtual environment in a shell. To know more about virtual environments check this link. Open Terminal/Bash shell on any Linux/Mac OS and type in the following –

$ mkdir testapp
$ cd testapp
$ virtualenv venv
New python executable in venv/bin/python
Installing setuptools............................ done
Installing pip.........................done
$ source venv/bin/activate

Now when we have made a virtual environment for flask to work in, let’s install the required packages in this virtual environment.

(venv)$ pip install requests[security]
(venv)$ pip install flask

What we have actually done is installed Flask and Requests package for our app in this virtual environment. Now that we have everything installed and set up, let’s code a simple web application, which we will put in new file named ‘blog.py’. Make this file in the same folder as the one in you Terminal / Cmd.

Screenshot 2019-02-17 at 2.35.33 PM

What we have done is that we have imported Flask and made an object app of Flask class. This app object (line 3) is actually a Flask request handler object which handles the different HTTP requests made to this application. We have provided a route ‘ / ‘ which tells the application that if a web browser sends a request to ‘ / ‘ then run the function hello(). If the method is not mentioned in the application route, then is it assumed to be GET method by default. It simply returns a string “Hello World” which gets printed in HTML format in the web browser.
To run this file, run the following command from the same shell.

$ python blog.py

This starts the application on the localhost (127.0.0.1) with following properties

Running on http://127.0.0.1:5000/ (Press Ctrl+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 137-257-718

Now your application has been started. Type in http://localhost:5000/ or http://127.0.0.1:5000/ in your web browser to see this tiny application in force..!

As you can see, when we typed in http://localhost:5000/, it sent a GET request to the application with the url as ‘ / ‘. Our application returned the string “Hello World” on it. Cool no? We have made our first RESTful application.!!

Now let’s create a dynamic URLS for different GET requests. Update the following code in blog.py so that it looks like this.

 

Screenshot 2019-02-17 at 2.35.19 PM

Now try the following url in your web browser – http://localhost:5000/. It outputs a HTML page with “Hello No One” in it (The default value when you don’t pass in any argument). But if we pass in the argument “name” as – http://localhost:5000/?name=EazyDevelop, it greets us!!

Now, when we did not pass “name” as an argument, the function request.args.get(‘name’) actually took the default value which we had passed to it as ‘No One’. This is how you start developing RESTful API on Flask using Python3.x

In the next part we will talk about different HTTP methods – GET, POST, PUT, DELETE and how to convert the data to JSON format to use the API from any platform.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Powered by WordPress.com.

Up ↑

%d bloggers like this: