This example contains a Source window with a Python connector that calls a JSON API to read Star Wars data and publish that data into the project every five seconds.
NOTE: Use this example with SAS Event Stream Processing 2023.12 and later.
For more information about how to install and use example projects, see Using the Examples.
Data is obtained by calling a JSON API. No input files are required for this example.
The Python code in the connector reads information about Star Wars species. The connector makes a call to https://starwars-databank-server.vercel.app/api/v1/species/?page=1 to get the first 10 species, yielding 10 events. The next
field in the received data provides information about the URL that the connector needs to call to get more events. After three seconds the connector makes a call to https://starwars-databank-server.vercel.app/api/v1/species/?page=2. This continues until the connector reaches https://starwars-databank-server.vercel.app/api/v1/species/?page=9, which contains the last results. The connector stops at page 9 because the next
field does not contain another link.
The interval
property of the connector is set to 3
so that the connector waits for three seconds between API calls. This means that the server will not be overloaded and data is streamed in rather than the entire data set being published at once. Reading data in this way is common when an API is used to fetch a large data set. In a real-life scenario, you might use a business API that also requires authentication.
The following table explains the Python code in the connector. To view the Python code in context, see the model.xml file.
Step | Python Code Section |
---|---|
Imports the esp, json, and urllib Python modules. The esp module enables the example to write log messages to the ESP server log. urllib is a Python module for fetching URLs. |
|
Specifies the JSON API that the connector reads and prepares for the first page of data to be read. |
|
Logs a message to indicate that an API call is made, makes an HTTP request to the API, decodes the response by using UTF-8, and parses the response as JSON. |
|
Creates a list of species events by using data from specified fields (name and description). |
|
Fetches the next page of results until there are no more pages to fetch. |
|
Returns results as SAS Event Stream Processing events. |
|
The following figure shows the result of running the project in test mode in SAS Event Stream Processing Studio:
The actual events that appear in test mode depend on the events that are available in the JSON feed at the time when you run the project.
You could adjust this example to call a business API that requires authentication.
For more information, see SAS Help Center: Using the Python Connector.