SoftLayer.API.Client provides a simple method for connecting to and making calls from the SoftLayer XML-RPC API and provides support for many of the SoftLayer API’s features. XML-RPC method calls and client management are handled by Python’s built-in xmlrpc client class.
Making API calls using the SoftLayer.API.Client class is done in the following steps:
- Instantiate a new
SoftLayer.API.Client
object. Provide the name of the service that you wish to query, an optional id number of the object that you wish to instantiate, your SoftLayer API username, and your SoftLayer API key. The client class defaults to connect over the public Internet. EnterAPI_PRIVATE_ENDPOINT
orAPI_PRIVATE_ENDPOINT
to connect to the API over SoftLayer’s private network. The system making API calls must be connected to SoftLayer’s private network (eg. purchased from SoftLayer or connected via VPN) in order to use the private network API endpoints. - Define and add optional headers to the client, such as object masks and result limits.
- Call the API method you wish to call as if it were local to your client object. This class throws exceptions if it’s unable to execute a query, so it’s best to place API method calls in try / except statements for proper error handling.
Once your method is done executed you may continue using the same client if you need to conenct to the same service or define another client object if you wish to work with multiple services at once.
The most up to date version of this library can be found on the SoftLayer github public repositories: http://github.com/softlayer/ . Please post to the SoftLayer forums <http://forums.softlayer.com/> or open a support ticket in the SoftLayer customer portal if you have any questions regarding use of this library.
This library has been tested to work in Python 2.4 – 3.1 on POSIX compliant and Windows operating systems.
A network connection is required for installation, and a valid SoftLayer API username and key are required to call the SoftLayer API. A connection to the SoftLayer private network is required to connect to SoftLayer’s private network API endpopints.
Change directory to your downloaded project root directory and run the following command to install this pagkage:
python ./setup.py install
This installs the SoftLayer.API package into your Python installation’s site-packages
directory. SoftLayer.API contains a Client class that handles API method calls per service.
Add --help
to the end of this command to view installation options.
These examples are written for Python 2.×. Python 3.x users will need to modify print() usage and change how exceptions are caught.
Here’s a simple usage example that retrieves account information by calling the getObject method in the SoftLayer_Account service:
import SoftLayer.API
import pprint
api_username = 'set me'
api_key = 'set me too'
client = SoftLayer.API.Client('SoftLayer_Account', None, api_username, api_key)
try:
account = client.getObject()
pprint.pprint(account)
except Exception, e:
print "Unable to retrieve account information: %", e
For a more complex example we’ll retrieve a support ticket with id 123456 along with the ticket’s updates, the user it’s assigned to, the servers attached to it, and the datacenter those servers are in. We’ll retrieve our extra information using a nested object mask. After we have the ticket we’ll update it with the text ‘Hello!’.
import SoftLayer.API
api_username = 'set me'
api_key = 'set me too'
client = SoftLayer.API.Client('SoftLayer_Ticket', 123456, api_username, api_key)
# Assign an object mask to our API client:
client.set_object_mask({
'updates' : {},
'assignedUser' : {},
'attachedHardware' : {
'datacenter' : {}
},
})
try:
ticket = client.getObject()
except Exception, e:
print "Unable to retrieve ticket record: %", e
# Update the ticket.
update = {
'entry' : 'Hello!',
}
try:
update = client.addUpdate(update)
print "Update ticket 123456. The new update's id is %.", update{'id'}
except Exception, e:
print "Unable to update ticket: %", e
This software is written by the SoftLayer Development Team <[email protected]>.
This software is Copyright © 2010 SoftLayer Technologies, Inc. See the bundled LICENSE.textile file for more information.