diff options
author | Milo Casagrande <milo.casagrande@linaro.org> | 2014-09-05 16:53:36 +0200 |
---|---|---|
committer | Milo Casagrande <milo.casagrande@linaro.org> | 2014-09-05 16:53:36 +0200 |
commit | e05b2d646ddb36926c769f3f5e44e682ddc6eb1b (patch) | |
tree | 67d92cf0cd2ba4417de638a3c509eab12eaf3fd1 /doc | |
parent | 54925c135df569bb2d8f881c80ee35eaec395361 (diff) |
doc: Add new example.
Change-Id: Ib3cb49950baba34d59e276280ef4c2e6646381a2
Diffstat (limited to 'doc')
-rw-r--r-- | doc/examples.rst | 60 |
1 files changed, 57 insertions, 3 deletions
diff --git a/doc/examples.rst b/doc/examples.rst index fa119cf..704ab41 100644 --- a/doc/examples.rst +++ b/doc/examples.rst @@ -3,9 +3,8 @@ Code Examples ============= -All the following code examples are Python based and rely on the ``requests`` -module. - +All the following code examples are Python based and rely on the +`requests <http://docs.python-requests.org/en/latest/>`_ module. Boot reports examples --------------------- @@ -120,3 +119,58 @@ Boot reports examples if __name__ == "__main__": main() + +Handling compressed data +------------------------ + +If you need to directly handle the compressed data as returned by the server, +you can access it from the response object. + +Keep in mind though that the `requests <http://docs.python-requests.org/en/latest/>`_ +module automatically handles ``gzip`` and ``deflate`` compressions. + +:: + + #!/usr/bin/env python + + """Get all defconfig reports with a specified job ID. + + Explicitly defines the Accept-Encoding header and manually handle the + compressed data. + """ + + import gzip + import requests + + from cStringIO import StringIO + from urlparse import urljoin + + AUTHORIZATION_TOKEN = 'foo' + BACKEND_URL = 'http://api.armcloud.us' + JOB = 'job' + KERNEL = 'kernel' + + + def main(): + headers = { + 'Authorization': AUTHORIZATION_TOKEN, + 'Accept-Encoding': 'gzip' + } + + params = { + 'job_id': JOB + '-' + KERNEL, + } + + url = urljoin(BACKEND_URL, '/defconfig') + response = requests.get(url, params=params, headers=headers, stream=True) + + in_buffer = StringIO(response.raw.data) + json_str = "" + + with gzip.GzipFile(mode='rb', fileobj=in_buffer) as g_data: + json_str = g_data.read() + + print json_str + + if __name__ == "__main__": + main() |