REST (REpresentational State Transfer) has emerged as the standard architectural design for web services and web APIs in these days.
This article will show you how easy it is to integrate a REST API Clients to RAD Server using Python and run it in the Python GUI by 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.
1. Setup RAD Server
If this is your first time configuring RAD Server, you can follow this tutorial. If you successfully configure the RAD Server, an Information wizard appears with the files created and the data added to the database. Click OK:
In RAD Development Server, you will get the following log:
And you can also test the connection, by clicking to “Open Browser”:
Or “Open Console”:
Congratulations! You are successfully configuring the RAD Server!
2. Access the Server via Python request Module
How do we interact with REST API Clients? In Python 3, we are lucky to have an excellent HTTP library: Kenneth Reitz’
requests. It’s one of the few most powerful projects worth treating as if it is part of the Python’s standard library.
First, let’s install it using easy install:
pip install requests
Installation progress in Windows Command Prompt:
The installation is completed! Let’s test the installation by running this simple GET request to RAD Server using project Demo1 from Python4Delphi with RAD Studio. :
response = requests.get("http://localhost:8080/version")
Request returns а Response, a powerful object for inspecting the results of the request. Using Response, we can examine the headers and contents of the response, get a dictionary with data in JSON format as the response, and also determine how successful our access to the server was by the response code from it.
In our example above, the response code was 200, which means that the request was successful!
In total, there are four main types of actions in Request Methods:
- GET: Retrieve information (works like the search results). This is the most common type of request. Using it, we can get or select the data we are interested in from those that the API is ready to share.
- POST: Adds new data to the server. Using this type of request, we can, for example; add a new item to your inventory or database.
- PUT: Changes existing information. For example, using this type of request, it would be possible to change the color or value of an existing product or data.
- DELETE: Deletes existing information.
Congratulations, you have successfully integrate Python REST API Client to RAD Server in Python GUI for Delphi Windows App! See you in our next tutorials!