|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "The `LoggingNetwork` network layer is a great tool for debugging your Python SDK applications, or just quickly making requests to the Box Content API." |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "code", |
| 12 | + "execution_count": 1, |
| 13 | + "metadata": { |
| 14 | + "collapsed": true |
| 15 | + }, |
| 16 | + "outputs": [], |
| 17 | + "source": [ |
| 18 | + "from boxsdk import Client, OAuth2\n", |
| 19 | + "from boxsdk.network.logging_network import LoggingNetwork" |
| 20 | + ] |
| 21 | + }, |
| 22 | + { |
| 23 | + "cell_type": "code", |
| 24 | + "execution_count": 2, |
| 25 | + "metadata": { |
| 26 | + "collapsed": true |
| 27 | + }, |
| 28 | + "outputs": [], |
| 29 | + "source": [ |
| 30 | + "import logging\n", |
| 31 | + "root_logger = logging.getLogger()\n", |
| 32 | + "for handler in root_logger.handlers:\n", |
| 33 | + " root_logger.removeHandler(handler)" |
| 34 | + ] |
| 35 | + }, |
| 36 | + { |
| 37 | + "cell_type": "code", |
| 38 | + "execution_count": 3, |
| 39 | + "metadata": { |
| 40 | + "collapsed": true |
| 41 | + }, |
| 42 | + "outputs": [], |
| 43 | + "source": [ |
| 44 | + "# Create a new instance of the logging network to be used for auth and API requests\n", |
| 45 | + "logging_network = LoggingNetwork()\n", |
| 46 | + "# Create an auth object that uses a developer token\n", |
| 47 | + "DEV_TOKEN = 'g3T46nWfprIRhuHyjfD5LBldwkFEG1RP'\n", |
| 48 | + "auth = OAuth2(client_id=None, client_secret=None, access_token=DEV_TOKEN)\n", |
| 49 | + "# Create an API client that uses the logging network layer\n", |
| 50 | + "client = Client(auth, network_layer=logging_network)" |
| 51 | + ] |
| 52 | + }, |
| 53 | + { |
| 54 | + "cell_type": "code", |
| 55 | + "execution_count": 4, |
| 56 | + "metadata": { |
| 57 | + "collapsed": false |
| 58 | + }, |
| 59 | + "outputs": [ |
| 60 | + { |
| 61 | + "name": "stdout", |
| 62 | + "output_type": "stream", |
| 63 | + "text": [ |
| 64 | + "\u001b[36mGET https://api.box.com/2.0/users/me {'headers': {u'Authorization': u'Bearer g3T46nWfprIRhuHyjfD5LBldwkFEG1RP'},\n", |
| 65 | + " 'params': None}\u001b[0m\n", |
| 66 | + "\u001b[32m{\"type\":\"user\",\"id\":\"202476009\",\"name\":\"Jeffrey Meadows\",\"login\":\"[email protected]\",\"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\"}\u001b[0m\n" |
| 67 | + ] |
| 68 | + } |
| 69 | + ], |
| 70 | + "source": [ |
| 71 | + "# Make an API call to get the current user\n", |
| 72 | + "me = client.user('me').get()" |
| 73 | + ] |
| 74 | + }, |
| 75 | + { |
| 76 | + "cell_type": "code", |
| 77 | + "execution_count": 6, |
| 78 | + "metadata": { |
| 79 | + "collapsed": false |
| 80 | + }, |
| 81 | + "outputs": [ |
| 82 | + { |
| 83 | + "name": "stdout", |
| 84 | + "output_type": "stream", |
| 85 | + "text": [ |
| 86 | + "\u001b[36mGET https://api.box.com/2.0/files/404 {'headers': {u'Authorization': u'Bearer g3T46nWfprIRhuHyjfD5LBldwkFEG1RP'},\n", |
| 87 | + " 'params': None}\u001b[0m\n", |
| 88 | + "\u001b[31m404\n", |
| 89 | + "{'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'}\n", |
| 90 | + "'{\"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\"}'\n", |
| 91 | + "\u001b[0m\n" |
| 92 | + ] |
| 93 | + } |
| 94 | + ], |
| 95 | + "source": [ |
| 96 | + "from boxsdk.exception import BoxAPIException\n", |
| 97 | + "# Make an API call to get a nonexistent file\n", |
| 98 | + "try:\n", |
| 99 | + " four_oh_four = client.file('404').get()\n", |
| 100 | + "except BoxAPIException:\n", |
| 101 | + " pass" |
| 102 | + ] |
| 103 | + }, |
| 104 | + { |
| 105 | + "cell_type": "markdown", |
| 106 | + "metadata": {}, |
| 107 | + "source": [ |
| 108 | + "**Requests** are logged in blue; **successful responses** in green, **error responses** in red." |
| 109 | + ] |
| 110 | + } |
| 111 | + ], |
| 112 | + "metadata": { |
| 113 | + "kernelspec": { |
| 114 | + "display_name": "Python 2", |
| 115 | + "language": "python", |
| 116 | + "name": "python2" |
| 117 | + }, |
| 118 | + "language_info": { |
| 119 | + "codemirror_mode": { |
| 120 | + "name": "ipython", |
| 121 | + "version": 2 |
| 122 | + }, |
| 123 | + "file_extension": ".py", |
| 124 | + "mimetype": "text/x-python", |
| 125 | + "name": "python", |
| 126 | + "nbconvert_exporter": "python", |
| 127 | + "pygments_lexer": "ipython2", |
| 128 | + "version": "2.7.9" |
| 129 | + } |
| 130 | + }, |
| 131 | + "nbformat": 4, |
| 132 | + "nbformat_minor": 0 |
| 133 | +} |
0 commit comments