b2sdk.b2http – B2 http client wrapper

class b2sdk.b2http.B2Http(requests_module=None, install_clock_skew_hook=True)[source]

Bases: object

A wrapper for the requests module. Provides the operations needed to access B2, and handles retrying when the returned status is 503 Service Unavailable, 429 Too Many Requests etc

The operations supported are:
  • post_json_return_json

  • post_content_return_json

  • get_content

The methods that return JSON either return a Python dict or raise a subclass of B2Error. They can be used like this:

try:
    response_dict = b2_http.post_json_return_json(url, headers, params)
    ...
except B2Error as e:
    ...
TIMEOUT = 130
__init__(requests_module=None, install_clock_skew_hook=True)[source]

Initialize with a reference to the requests module, which makes it easy to mock for testing.

Parameters
  • requests_module – a reference to requests module

  • install_clock_skew_hook (bool) – if True, install a clock skew hook

add_callback(callback)[source]

Adds a callback that inherits from HttpCallback.

Parameters

callback (callable) – a callback to be added to a chain

get_content(url, headers, try_count=5)[source]

Fetches content from a URL.

Use like this:

try:
    with b2_http.get_content(url, headers) as response:
        for byte_data in response.iter_content(chunk_size=1024):
            ...
except B2Error as e:
    ...
The response object is only guarantee to have:
  • headers

  • iter_content()

Parameters
  • url (str) – URL to call

  • headers (dict) – headers to send

  • try_count (int) – a number or retries

Returns

Context manager that returns an object that supports iter_content()

post_content_return_json(url, headers, data, try_count=5, post_params=None)[source]

Use like this:

try:
    response_dict = b2_http.post_content_return_json(url, headers, data)
    ...
except B2Error as e:
    ...
Parameters
  • url (str) – URL to call

  • headers (dict) – headers to send.

  • data – bytes (Python 3) or str (Python 2), or a file-like object, to send

Returns

a dict that is the decoded JSON

Return type

dict

post_json_return_json(url, headers, params, try_count=5)[source]

Use like this:

try:
    response_dict = b2_http.post_json_return_json(url, headers, params)
    ...
except B2Error as e:
    ...
Parameters
  • url (str) – URL to call

  • headers (dict) – headers to send.

  • params (dict) – a dict that will be converted to JSON

Returns

the decoded JSON document

Return type

dict

class b2sdk.b2http.ClockSkewHook[source]

Bases: b2sdk.b2http.HttpCallback

post_request(method, url, headers, response)[source]

Raises an exception if the clock in the server is too different from the clock on the local host.

The Date header contains a string that looks like: “Fri, 16 Dec 2016 20:52:30 GMT”.

Parameters
  • method (str) – one of: ‘POST’, ‘GET’, etc.

  • url (str) – the URL that will be used.

  • headers (dict) – the header sent with the request.

  • response – a response object from the requests library.

class b2sdk.b2http.HttpCallback[source]

Bases: object

A callback object that does nothing. Override pre_request and/or post_request as desired.

post_request(method, url, headers, response)[source]

Called after processing an HTTP request. Should not raise an exception.

Raises an exception if this request should be treated as failing. The exception raised must inherit from B2HttpCallbackPostRequestException.

Parameters
  • method (str) – one of: ‘POST’, ‘GET’, etc.

  • url (str) – the URL that will be used.

  • headers (dict) – the header sent with the request.

  • response – a response object from the requests library.

pre_request(method, url, headers)[source]

Called before processing an HTTP request.

Raises an exception if this request should not be processed. The exception raised must inherit from B2HttpCallbackPreRequestException.

Parameters
  • method (str) – str, One of: ‘POST’, ‘GET’, etc.

  • url (str) – The URL that will be used.

  • headers (dict) – The header sent with the request.

class b2sdk.b2http.ResponseContextManager(response)[source]

Bases: object

Context manager that closes a requests.Response when done.

__init__(response)[source]

Initialize self. See help(type(self)) for accurate signature.

b2sdk.b2http.test_http()[source]

Runs a few tests on error diagnosis.

This test takes a while to run, and is not used in the automated tests during building. Run the test by hand to exercise the code. Be sure to run in both Python 2 and Python 3.