Consuming APIs with Python: requests
In today’s interconnected world, APIs (Application Programming Interfaces) serve as the backbone for much of the software we use. For IBM i users, leveraging APIs can unlock a wealth of data and functionality, bridging the gap between traditional applications and modern services. This post will introduce you to the basics of consuming APIs using Python on the IBM i platform, complete with a real example that you can try out today.
Why Python for API Consumption?
Python stands out for its simplicity and readability, making it an ideal choice for interacting with web APIs. With its vast standard library and third-party modules, Python simplifies the process of sending requests to a web server and handling the responses. Furthermore, Python’s popularity in the development community means you’re likely to encounter Python example code in API documentation, making it easier to understand and implement API integrations.
One such third-party module is requests
, which we will use in this tutorial.
Setting Up Your Environment
Once Python is set up, you’ll need to install the requests
library. You can do this by running the following command:
API Examples
For this example, we’ll use the is.gd service to shorten a URL. This demonstrates not only how to consume APIs using Python but also gives us a practical utility that can be used in a variety of applications.
Here’s a Python script that takes a URL as input and uses the is.gd service to generate a shortened version of that URL:
In this script, we construct a GET request to the is.gd API with the original URL passed as a parameter. We specify the format as JSON so that the response is easy to parse. Upon a successful request, the API returns a JSON object from which we extract the shortened URL.
Running the Script on IBM i
Save as shorten_url.py
on the IFS then in a terminal or QSH:
You should see the original URL and its shortened version printed out to your console.
Why This Matters
URL shortening is a common task in modern web development, useful for making links more manageable, tracking click rates, or simply saving space in documents and messages. Demonstrating API consumption with Python through a practical example like URL shortening showcases the immediate utility and flexibility of Python scripting on IBM i systems.
To make our URL shortening script more powerful, we’ll now add the ability for users to specify a custom alias for the shortened URL. Additionally, we’ll implement error handling to address specific API errors, such as when an alias is already in use.
Modifying the Script to Accept Custom Aliases
We’ll use the argparse
module to add an optional argument for the custom alias. The script will also ensure that the alias meets the specified length criteria and will append a random three-digit number if the initial alias request fails due to duplication.
Here’s the updated script:
Running the Enhanced Script
With the new functionality, you can now specify an optional alias when shortening a URL:
If the alias is already taken, the script automatically tries a new alias by appending a random three-digit number to your original alias, ensuring uniqueness.
Why This Matters
Adding the ability to specify custom aliases for shortened URLs offers more personalized and memorable links, useful for branding and marketing efforts. The automatic handling of alias duplication errors by appending a random number ensures the process is smooth and user-friendly, demonstrating the script’s robustness.
This enhancement showcases the practical application of error handling and conditional logic in Python scripts, making them more adaptive and reliable for real-world use.
Enhancing the URL shortening script to write results to a DB2 table adds another layer of utility, allowing for persistent storage and analysis of the shortened URLs and any encountered errors.
Updating the Script for DB2 Integration
To integrate database logging, we’ll need to use the ibm_db
module for DB2 connectivity. Ensure you have ibm_db
installed in your Python environment:
SQL for Creating the DB2 Table
This table structure includes a primary key LOG_ID
, the ORIGINAL_URL
, the SHORTENED_URL
obtained from the API, and an API_ERROR
field to record any errors returned by the API. The REQUESTED
column will automatically record when each log entry is created.
Here’s how you can modify the script to include database logging:
Running the Enhanced Script
With DB2 integration, every execution of the script now logs the request and response details to your DB2 table:
This setup not only facilitates tracking and analyzing the usage of your URL shortening script but also illustrates a practical example of integrating Python scripts with IBM i’s DB2 database for logging and data persistence.