You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/api/client.rst
+36Lines changed: 36 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -202,6 +202,42 @@ If you ever need to figure out what exact API call the client is making, you can
202
202
print(client.transport.print_reproduceable(call))
203
203
204
204
205
+
Dealing with KeyError Exceptions
206
+
--------------------------------
207
+
208
+
One of the pain points in dealing with the SoftLayer API can be handling issues where you expected a property to be returned, but none was.
209
+
210
+
The hostname property of a `SoftLayer_Billing_Item <https://sldn.softlayer.com/reference/datatypes/SoftLayer_Billing_Item/#hostname>`_ is a good example of this.
211
+
212
+
For example.
213
+
214
+
::
215
+
216
+
# Uses default username and apikey from ~/.softlayer
217
+
client = SoftLayer.create_client_from_env()
218
+
# iter_call returns a python generator, and only makes another API call when the loop runs out of items.
219
+
result = client.iter_call('Account', 'getAllBillingItems', iter=True, mask="mask[id,hostName]")
220
+
print("Id, hostname")
221
+
for item in result:
222
+
# will throw a KeyError: 'hostName' exception on certain billing items that do not have a hostName
Otherwise, this SDK provides a util function to do something similar. Each additional argument passed into `utils.lookup` will go one level deeper into the nested dictionary to find the item requested, returning `None` if a KeyError shows up.
0 commit comments