b2sdk.b2http – thin http client wrapper

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

A context manager that closes a requests.Response when done.

class b2sdk.b2http.HttpCallback[source]

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

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

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

class b2sdk.b2http.ClockSkewHook[source]
post_request(method, url, headers, response)[source]

Raise 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.B2Http(api_config: b2sdk.api_config.B2HttpApiConfig = <b2sdk.api_config.B2HttpApiConfig object>)[source]

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 = 900
add_callback(callback)[source]

Add a callback that inherits from HttpCallback.

Parameters

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

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) – a 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) – a 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

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) – a 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()

head_content(url: str, headers: Dict[str, Any], try_count: int = 5)Dict[str, Any][source]

Does a HEAD instead of a GET for the URL. The response’s content is limited to the headers.

Use like this:

try:
    response_dict = b2_http.head_content(url, headers)
    ...
except B2Error as e:
    ...
The response object is only guaranteed to have:
  • headers

Parameters
  • url (str) – a URL to call

  • headers (dict) – headers to send

  • try_count (int) – a number or retries

Returns

the decoded response

Return type

dict

b2sdk.b2http.test_http()[source]

Run 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.