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 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.
Table of Contents
1. 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:
1 |
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 google.com in our Python GUI:
1 2 3 4 |
import requests response = requests.get('https://google.com/') print(response) |
Output:
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.
2. Example: GET Requests
Now we’re ready to start using Python Requests to interact with a REST API, make sure you import the Requests library into any scripts you want to use it in, and let’s try another requests:
1 2 3 4 |
import requests response = requests.get("http://api.open-notify.org/astros.json") print(response) |
Output in our VCL:
In this example, we send our GET request to Open Notify. Open Notify is an open source project to provide a simple programming interface for some of NASA’s awesome data.
Let’s try more complex example, send request with query parameters. Queries can be used to filter the data that an API returns, and these are added as query parameters that are appended to the endpoint URL. With Python Requests, this is handled via the params argument, which accepts a dictionary object.
Let’s see what that looks like when we use the Open Notify API to GET an estimate for when the International Space Station (ISS) will fly over a specified point (latitude and longitude):
1 2 3 4 5 6 |
import requests query = {'lat':'55', 'lon':'180'} response = requests.get('http://api.open-notify.org/iss-pass.json', params=query) print(response.json()) |
Output of print command in our VCL:
Let’s try another interesting example, do you want to know how many people in Space right now?
1 2 3 4 |
import requests response = requests.get('http://api.open-notify.org/astros.json?callback') print(response.json()) |
Output in our VCL:
Congratulations, you have successfully integrate Python REST API Client in Python GUI for Delphi Windows App! See you in our next tutorials!
Check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi.