Delphi Easily Create A Flexible Python REST Server In A Delphi Windows GUI App

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009
Easily Create A Flexible Python REST Server In A Delphi Windows GUI App
February 9, 2021 By Muhammad Azizul

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 Для просмотра ссылки Войди или Зарегистрируйся for your platform. Follow the Для просмотра ссылки Войди или Зарегистрируйся installation instructions mentioned Для просмотра ссылки Войди или Зарегистрируйся. Alternatively, you can check out the easy instructions found in this video Для просмотра ссылки Войди или Зарегистрируйся.

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 Для просмотра ссылки Войди или Зарегистрируйся. The behind the scene details of how Delphi manages to run your Python code in this amazing Python GUI can be found at this Для просмотра ссылки Войди или Зарегистрируйся.
1612890936446.png
Open Demo01.dproj

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 MethodActionExamples
GETObtain information about a resourceДля просмотра ссылки Войди или Зарегистрируйся(retrieve order list)
GETObtain information about a resourceДля просмотра ссылки Войди или Зарегистрируйся(retrieve order #123)
POSTCreate a new resourceДля просмотра ссылки Войди или Зарегистрируйся(create a new order, from data provided with the request)
PUTUpdate a resourceДля просмотра ссылки Войди или Зарегистрируйся(update order #123, from data provided with the request)
DELETEDelete a resourceДля просмотра ссылки Войди или Зарегистрируйся(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:

Код:
pip install flask
Installation progress in Windows Command Prompt:
1612891002735.png
pip install flask in Windows Command Prompt.

Now open up the Demo01 VCL and copy-paste these code:
Python:
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:
1612891063161.png
Result in browser:
1612891072772.png
Congratulations! You have integrated what “seems complicated concept” Python REST Server with Delphi, using Python4Delphi.

Для просмотра ссылки Войди или Зарегистрируйся