REST (REpresentational State Transfer) has emerged as the standard architectural design for web services and web APIs in these recent years.
This post will demonstrate how easy it is to create a RESTful web service using Python and the Flask microframework in Python4Delphi with RAD Studio and gets the results.
Prerequisites: Before we begin to work, download and install the latest Python for your platform. Follow the Python4Delphi installation instructions mentioned here. Alternatively, you can check out the easy instructions found in this video Getting started with Python4Delphi.
First, open and run our Python GUI using project Demo1 from Python4Delphi with RAD Studio. Then insert the script into the lower Memo, click the Execute button, and get the result in the upper Memo. You can find the Demo1 source on GitHub. The behind the scene details of how Delphi manages to run your Python code in this amazing Python GUI can be found at this link.
Table of Contents
1. What is REST System?
The REST system is defined by these six characteristics as its design rules:
- Client-Server: There should be a separation between the server that offers a service, and the client that consumes it.
- 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.
- Cacheable: The server must indicate to the client if requests can be cached or not.
- Layered System: Communication between a client and a server should be standardized in such a way that allows intermediaries to respond to requests instead of the end server, without the client having to do anything different.
- Uniform Interface: The method of communication between a client and a server must be uniform.
- 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.
2. Introduction to RESTful Web Service
The REST architecture was originally designed to fit the HTTP protocol that the world wide web uses.
Central to the concept of RESTful web services is the notion of resources. Resources are represented by Uniform Resource Identifier or 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.
The HTTP request methods are typically designed to affect a given resource in standard ways:
HTTP Method | Action | Examples |
---|---|---|
GET | Obtain information about a resource | http://example.com/api/orders (retrieve order list) |
GET | Obtain information about a resource | http://example.com/api/orders/123 (retrieve order #123) |
POST | Create a new resource | http://example.com/api/orders (create a new order, from data provided with the request) |
PUT | Update a resource | http://example.com/api/orders/123 (update order #123, from data provided with the request) |
DELETE | Delete a resource | http://example.com/api/orders/123 (delete order #123) |
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.
3. A Simple Flask API GET Request
Firstly, make sure you have Flask installed. It’s easiest to use Python package manager, pip or easy install:
1 |
pip install flask |
Installation progress in Windows Command Prompt:
Now open up the Demo01 VCL and copy-paste these code:
1 2 3 4 5 6 7 8 9 10 11 12 |
from flask import Flask, json companies = [{"id": 1, "name": "Embarcadero Technologies"}, {"id": 2, "name": "python.org"}] api = Flask(__name__) @api.route('/companies', methods=['GET']) def get_companies(): return json.dumps(companies) if __name__ == '__main__': api.run() |
Output in Python GUI:
Result in browser:
Congratulations! You have integrated what “seems complicated concept” Python REST Server with Delphi, using Python4Delphi.
Check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi.