Consuming Web APIs with PythonHow to use Python to consume and process data from open APIsKenneth ReillyBlockedUnblockFollowFollowingMay 23https://www.
orgIntroductionIn this article, I will introduce a simple approach to working with APIs on the open web using Python 3, a highly stable and mature language with a clean and easy to read syntax that makes it perfect for prototyping web app services (or any other product) quickly and easily with minimal frustration.
In this example we’ll use the free open API from NASA and utilize a few classes for wrapping simple python methods to download today’s featured Astronomy Picture of the Day along with corresponding photo information.
Diving Right InThe example source for this project is available here.
If you don’t already have Python 3, head over to their website to grab a build for your operating system.
Let’s take a look at the project entry file, main.
py:The main project file main.
pyThis file serves as the main entry point and therefore is kept fairly clean, as to not get into the habit of overloading the very first file in the project with a bunch of stuff.
Here we have the Example class with a couple of methods:static_init() which in turn initializes the API classget_todays_image() which retrieves the Picture of the DayThat does it for this file.
If we wanted to expand on this, we would simply add new methods for wrapping various features within our API class (or possibly other features, such as accepting and parsing command-line options).
The next source file we’ll take check out is src/api.
py:Contents of the API class in src/api.
pyThe file src/api.
py features the API class, which in this example is a simple controller for consuming an endpoint on the NASA open API, as exposed by the method get_image_of_day().
This method begins with the base URL for the API endpoint and attaching the following query string parameters to it:date: specifies which date to retrieve the image for (default is today)hd: whether the API should return an HD resolution image (OK to change)api_key: the API key specified in .
env (or in .
default if not provided)Next, the method Util.
get_json(url) is called to retrieve the JSON data that will be returned by a GET request to the specified url.
This get_json() method is a simple wrapper defined in Util, which we will examine in a moment.
The returned dictionary object contains a url property which is a URL to the actual image file, which is used to download and retrieve the image with another utility class method, download_file(url, path) which we will also check out momentarily.
Finally, the contents of the JSON data returned by the API are also saved to a local file with the image filename plus a .
Next we’ll take a look at the configuration file src/config.
py:The configuration file, src/config.
pyThe file src/config.
py is responsible for loading environment variables defined within .
env (or if not available, in the default file .
default) and returning the local value for NASA_API_KEY to be used by the API.
While there are dependency modules available for this, I wanted to demonstrate just how trivial it really is to pull in a few variables defined within a .
That’s it for the configuration file.
Additional properties could be added for other API endpoints or to accomplish other configuration functions, such as dynamically selecting whether to retrieve an HD image or not depending on the current environment, for example.
The last file in the series is the utility file, src/util.
py:The Utility class defined in src/util.
pyIn this Util class, we have a few methods for performing various functions:today_str(): returns the current date in the format ‘YYYY-MM-DD’get(url): reads and returns the response of a GET request to urldownload_file(url, path): download a file from url and save it to pathwrite_json(data, path): write the string data to a file at pathAs we can see, this file performs most of the nuts-and-bolts lower level stuff like handling network requests and filesystem operations.
This allows the main application logic to be much cleaner and easier to maintain — for example, we can swap out the HTTP request implementation for another library or with some other service altogether and the rest of the program will continue to function normally as if nothing happened, because from its own perspective, the interfaces still line up as expected.
So, modifications at the lower level can be performed safely while keeping the rest of the application structure intact (or simultaneously improving and adding upon that as well).
ConclusionIn this example we looked at how to consume one endpoint from one single API provider (NASA’s picture of the day).
There are many more endpoints available from NASA alone, in addition to numerous other API providers with various features and content ranging from music and video to scientific data.
Picture of the day (with JSON data above)I hope you enjoyed this article.
The source code for this example is available on GitHub and any comments, suggestions, or other ideas are welcome.
Thanks for reading and good luck on your next python project!.