Python REST API¶
By using the Python programming language, you can script a client application that interacts with the IBM Spectrum Protect Plus REST API. The examples in this section and throughout this guide use third-party library Requests for HTTP transactions.
Applicable Python releases: The provided examples are based on the following releases: Python series: Python 3.6, 3.7, 3.8, and 3.9.
Getting a session ID using Python¶
Assume that you want to work with an IBM Spectrum Protect Plus virtual appliance with the following configuration:
IPv4 address: 10.0.0.100
Username: Sarah
Password:
MyPassw0rd!
SSL verification: False, ignore warnings.
The following Python libraries and values to access to the REST API.
import json
import requests
spp_ipv4 = '10.0.0.100'
spp_username = 'Sarah'
spp_password = 'MyPassw0rd!'
spp_verify = False # Disable SSL.
# Supress HTTPS warnings when spp_verify is set to False.
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
After the previous script runs, the following Python snippet is used to retrieve a session ID from the target IBM Spectrum Protect Plus application:
spp_session = requests.post('https://' + spp_ipv4 + '/api/endeavour/session',
auth=(spp_username, spp_password),
headers={
'Accept': 'application/json',
'Content-type': 'application/json'
},
verify=spp_verify
)
spp_session = json.loads(spp_session.text) # Convert to JSON
spp_sessionid = spp_session['sessionid']
print(spp_sessionid)
ee88d182812f49c98bbf9c819d69af07
You can use the obtained session ID for subsequent operations. For more information about session IDs, follow the instructions in Creating a session ID.
Sending a request to the REST API by using Python¶
Assume that you append the following Python snippet to the previous script. The snippet sends an HTTP request to the REST API and starts an inventory job for virtualized systems, such as virtual machines hosted on vCenter Server. For more information about this operation, follow the instructions in Running an inventory job for virtualized systems.
_params = {
"action": "start",
"actionname": "start"
}
_data = ""
requests.post('https://' + spp_ipv4 + '/api/endeavour/job/1004,
headers={
'Accept': 'application/json',
'Content-type': 'application/json',
'X-Endeavour-Sessionid': spp_sessionid
},
params=_params, data=_data, verify=spp_verify
)
The structure of this POST command is similar to the command for getting a session ID except for the URI and the session ID in the header.
The IBM Spectrum Protect Plus REST API has a consistent structure. You can use Python scripts that are similar to this example to develop applications that send requests to the REST API. You can enhance those basic structures with many other methods and mechanisms that are available in Python and its third-party libraries.
Decoding URIs with parameters by using Python¶
You can decode URI strings with parameters by using Python snippets.
Assume that you captured the following HTTP request by using Mozilla Firefox:
curl 'https://10.0.0.100/api/hypervisor?from=hlo&pageSize=100&
↪sort=%5B%7B%22property%22:%22name%22,%22direction%22:%22ASC%22%7D%5D&
↪filter=%5B%7B%22property%22:%22type%22,%22value%22:%22vmware%22,
↪%22op%22:%22=%22%7D%5D'
-H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) ...'
...
A Python snippet that is similar to the following example can be used to decode the URI string:
import urllib.parse
uri_original = "https://10.0.0.100/api/hypervisor?" \
+ "from=hlo&" \
+ "pageSize=100&" \
+ "sort=%5B%7B%22property%22:%22name%22,%22direction%22:%22ASC%22%7D%5D&" \
+ "filter=%5B%7B%22property%22:%22type%22,%22value%22:%22vmware%22," \
+ "%22op%22:%22=%22%7D%5D"
uri_decoded = urllib.parse.unquote(uri_original)
print(uri_decoded)
The request prompts a response that is structured as shown:
https://10.0.0.100/api/hypervisor?from=hlo&pageSize=100&
↪sort=[{"property":"name","direction":"ASC"}]&
↪filter=[{"property":"type","value":"vmware","op":"="}]