Table of Contents
What is REST Architecture?
REST stands for REpresentational State Transfer. REST is a web standards-based architecture and uses HTTP Protocol. It revolves around a resource where every component is a resource and a resource is accessed by a common interface using HTTP standard methods. REST was first introduced by Roy Fielding in 2000.
The REST architectural style emphasizes the scalability of interactions between components, uniform interfaces, independent deployment of components, and the creation of a layered architecture to facilitate caching components to reduce user-perceived latency, enforce security, and encapsulate legacy systems (Fielding, 2000).
REST has been employed throughout the software industry and is a widely accepted set of guidelines for creating stateless, reliable web services. Any web service that obeys the REST constraints is informally described as RESTful. You may use REST API to build Python applications using Python Development Tools.
What are the Architectural Properties of REST?
According to Fielding, 2000, the constraints of the REST architectural style affect the following architectural properties:
- performance in component interactions, which can be the dominant factor in user-perceived performance and network efficiency;
- scalability allowing the support of large numbers of components and interactions among components;
- the simplicity of a uniform interface;
- modifiability of components to meet changing needs (even while the application is running);
- visibility of communication between components by service agents;
- portability of components by moving program code with the data;
- reliability in the resistance to failure at the system level in the presence of failures within components, connectors, or data.
How can We Interact with REST APIs using Python’s Requests Library?
In this tutorial, we will use the Requests library to send HTTP requests to each APIs.
Requests is a simple, yet elegant HTTP library. Requests allow you to execute standard HTTP requests. Using this library, you can pass parameters to requests, add headers, receive and process responses, and execute authenticated requests.
Requests is one of the most downloaded Python packages today, pulling in around 14M downloads/week—according to GitHub, Requests is currently depended upon by 500,000+ repositories. Knowing these facts, you may certainly put your trust in this credible library.
Read another article, which specifically talks about the Request library:
And visit this article, if you looking for Requests and other libraries for Web Scraping purposes:
Delphi adds Powerful GUI Features and Functionalities to Python
In this tutorial, we’ll build Windows Apps with extensive support from apilayer’s REST services, and develop GUI for them using Python4Delphi (P4D).
P4D empowers Python users with Delphi’s award-winning VCL functionalities for Windows which enables us to build native Windows apps 5x faster. This integration enables us to create a modern GUI with Windows 10 looks and responsive controls for our Python applications. Python4Delphi also comes with an extensive range of demos, use cases, and tutorials.
We’re going to cover the following REST services…
How to build Python GUI for coinlayer, currencylayer, ipapi, numverify, and vatlayer REST services
All of them would be integrated with Python4Delphi to create Windows Apps with extensive data and capabilities.
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 the Getting Started With Python4Delphi video by Jim McKeeth.
Time to get started!
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.
How to build a Python GUI for the CoinLayer API?
The coinlayer API is capable of delivering accurate exchange rate data for 385+ cryptocurrencies in real-time. Crypto data is gathered from some of the largest cryptocurrency exchanges, requested using HTTP GET, and returned in straightforward JSON format. Thanks to a refined fallback algorithm availability, consistency, and reliability of crypto data returned by the coinlayer API are at the highest level.
The following is a code example for interacting with coinlayer API (run this inside the lower Memo of Python4Delphi Demo01 GUI):
1 2 3 4 5 6 7 |
import requests r = requests.get("http://api.coinlayer.com/api/live?access_key=YOUR_ACCESS_KEY") responses = r.json() print(responses['rates']) |
Here is the result in Python GUI
You can specifically monitoring Bitcoin vs USD using the following code example:
1 2 3 4 5 6 7 |
import requests r = requests.get("http://api.coinlayer.com/api/live?access_key=YOUR_ACCESS_KEY") responses = r.json() print(responses['rates']['BTC']) |
Here is the result in Python GUI
How to build a Python GUI for the CurrencyLayer API?
currencylayer provides a simple REST API with real-time and historical exchange rates for 168 world currencies, delivering currency pairs in universally usable JSON format – compatible with any of your applications.
Spot exchange rate data is retrieved from several major forex data providers in real-time, validated, processed, and delivered hourly every 10 minutes, or even within the 60-second market window.
Providing the most representative forex market value available (“midpoint” value) for every API request, the currencylayer API powers currency converters, mobile applications, financial software components, and back-office systems all around the world.
The following is a code example for interacting with mailboxlayer API (run this inside the lower Memo of Python4Delphi Demo01 GUI):
1 2 3 4 5 6 7 |
import requests r = requests.get("http://api.currencylayer.com/live?access_key=YOUR_ACCESS_KEY") responses = r.json() print(responses) |
Here is the currencylayer example in the Python GUI:
You can easily compare USD with other currencies using the following code example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import requests r = requests.get("http://api.currencylayer.com/live?access_key=YOUR_ACCESS_KEY") responses = r.json() # USD to Bitcoin print(responses['quotes']['USDBTC']) # USD to British Pound Sterling print(responses['quotes']['USDGBP']) # USD to Australian Dollar print(responses['quotes']['USDAUD']) # USD to Chinese Yuan print(responses['quotes']['USDCNY']) # USD to Japanese Yen print(responses['quotes']['USDJPY']) |
Here is the currencylayer example in the Python GUI:
How to build a Python GUI for the ipapi API?
ipapi provides an easy-to-use API interface allowing customers to look at various pieces of information IPv4 and IPv6 addresses are associated with. For each IP address processed, the API returns a total of 47 unique data points, such as location data, connection data, ISP information, time zone, currency, and security assessment data.
The following is a code example for interacting with ipapi API (run this inside the lower Memo of Python4Delphi Demo01 GUI):
1 2 3 4 5 6 7 |
import requests r = requests.get("http://api.ipapi.com/36.80.244.42?access_key=YOUR_ACCESS_KEY") responses = r.json() print(responses) |
ipapi + Python4Delphi Results
You also can print out each parameter individually, or choose any parameter you like, like this example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import requests r = requests.get("http://api.ipapi.com/36.80.244.42?access_key=YOUR_ACCESS_KEY") responses = r.json() print(responses['ip']) print(responses['type']) print(responses['continent_code']) print(responses['continent_name']) print(responses['country_code']) print(responses['country_name']) print(responses['region_code']) print(responses['region_name']) print(responses['city']) print(responses['zip']) print(responses['latitude']) print(responses['longitude']) print(responses['location']) |
ipapi + Python4Delphi Results
How to build a Python GUI for the NumVerify API?
numverify offers a full-featured yet simple RESTful JSON API for national and international phone number validation and information lookup for a total of 232 countries around the world.
Requested numbers are processed in real-time, cross-checked with the latest international numbering plan databases, and returned in handy JSON format enriched with a useful carrier, geographical location, and line type data.
Integrating the numverify API into your application will enable you to verify the validity of phone numbers at the point of entry, protecting you from fraud and increasing good leads.
The following is a code example for interacting with numverify API (run this inside the lower Memo of Python4Delphi Demo01 GUI):
1 2 3 4 5 6 7 8 9 10 11 12 |
import requests params = { 'access_key': 'YOUR_ACCESS_KEY', 'number': '14158586273' } api_result = requests.get('http://apilayer.net/api/validate?', params) api_response = api_result.json() print(api_response) |
numverify Simple Examples:
How to build a Python GUI for the VatLayer API?
vatlayer is a simple JSON-based REST API enabling you to validate VAT numbers, retrieve all or single EU VAT rates based on IP address or country code, convert prices in compliance with EU VAT rates and types, and more.
API results are requested using a transparent and easy-to-understand URL structure and delivered in handy JSON format, making for the highest possible level of compatibility with any of your applications, systems, programming languages and frameworks.
The following is a code example for interacting with vatlayer API (run this inside the lower Memo of Python4Delphi Demo01 GUI):
1 2 3 4 5 6 7 8 9 10 11 12 |
import requests params = { 'access_key': 'YOUR_ACCESS_KEY', 'vat_number': 'LU26375245' } api_result = requests.get('http://apilayer.net/api/validate?', params) api_response = api_result.json() print(api_response) |
Here is the vatlayer demo Result in the Python GUI:
Are you ready to build awesome things with these powerful APIs and Python4Delphi?
Want to know some more? Then check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi, and
Download RAD Studio to build more powerful Python GUI Windows Apps 5x Faster with Less Code.