DelphiLearn PythonPythonPython GUIRAD Studio

Easily Integrate A Flexible Python REST Client In Python Windows GUI App Built In Delphi

blogbanner3 14

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.

0 rundemo1 3322988
Open Demo01.dproj

 

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:

Installation progress in Windows Command Prompt:

1 1 9260837
pip install request 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:

Output:

1 2 6016299
OK Status Code (200) from Google.

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:

Output in our VCL:

1 3 9193334
OK Status Code (200) from Open Notify.

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):

Output of print command in our VCL:

1 4 2597477
Open Notify API Response to Estimate for when the ISS will Fly over a Specified Point.

Let’s try another interesting example, do you want to know how many people in Space right now?

Output in our VCL:

1 5 7779875
Open Notify API Response to Tell You How Many People in Space Right Now, and Who They Are.

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.

Related posts
CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And OpenCV Library?

CodeIDEProjectsPythonWindows

Unlock the Power of Python for Deep Learning with Radial Basis Function Networks (RBFNs)

CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And NumPy Library?

CodeIDEProjectsPythonWindows

Unlock the Power of Python for Deep Learning with Generative Adversarial Networks (GANs) - The Engine behind DALL-E

Leave a Reply

Your email address will not be published. Required fields are marked *