The LoggingNetwork network layer is a great tool for debugging your Python SDK applications, or just quickly making requests to the Box Content API.


In [1]:
from boxsdk import Client, OAuth2
from boxsdk.network.logging_network import LoggingNetwork

In [2]:
import logging
root_logger = logging.getLogger()
for handler in root_logger.handlers:
    root_logger.removeHandler(handler)

In [3]:
# Create a new instance of the logging network to be used for auth and API requests
logging_network = LoggingNetwork()
# Create an auth object that uses a developer token
DEV_TOKEN = 'g3T46nWfprIRhuHyjfD5LBldwkFEG1RP'
auth = OAuth2(client_id=None, client_secret=None, access_token=DEV_TOKEN)
# Create an API client that uses the logging network layer
client = Client(auth, network_layer=logging_network)

In [4]:
# Make an API call to get the current user
me = client.user('me').get()


GET https://api.box.com/2.0/users/me {'headers': {u'Authorization': u'Bearer g3T46nWfprIRhuHyjfD5LBldwkFEG1RP'},
 'params': None}
{"type":"user","id":"202476009","name":"Jeffrey Meadows","login":"jmeadows@box.com","created_at":"2013-09-09T14:35:35-07:00","modified_at":"2015-12-07T21:49:14-08:00","language":"en","timezone":"America\/Los_Angeles","space_amount":1.0e+15,"space_used":28861619684,"max_upload_size":16106127360,"status":"active","job_title":"","phone":"","address":"","avatar_url":"https:\/\/cloud.app.box.com\/api\/avatar\/large\/202476009"}

In [6]:
from boxsdk.exception import BoxAPIException
# Make an API call to get a nonexistent file
try:
    four_oh_four = client.file('404').get()
except BoxAPIException:
    pass


GET https://api.box.com/2.0/files/404 {'headers': {u'Authorization': u'Bearer g3T46nWfprIRhuHyjfD5LBldwkFEG1RP'},
 'params': None}
404
{'Content-Length': '234', 'Content-Encoding': 'gzip', 'Age': '0', 'Vary': 'Accept-Encoding', 'Server': 'ATS', 'Connection': 'keep-alive', 'Cache-Control': 'no-cache, no-store', 'Date': 'Tue, 08 Dec 2015 19:18:34 GMT', 'Content-Type': 'application/json'}
'{"type":"error","status":404,"code":"not_found","context_info":{"errors":[{"reason":"invalid_parameter","name":"item","message":"Invalid value \'f_404\'. \'item\' with value \'f_404\' not found"}]},"help_url":"http:\\/\\/developers.box.com\\/docs\\/#errors","message":"Not Found","request_id":"80397196256672d0a379ae"}'

Requests are logged in blue; successful responses in green, error responses in red.