b2sdk._internal.b2http – thin http client wrapper

b2sdk._internal.b2http.random() x in the interval [0, 1).
b2sdk._internal.b2http.setlocale(name)[source]
class b2sdk._internal.b2http.ResponseContextManager(response)[source]

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

class b2sdk._internal.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._internal.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._internal.b2http.B2Http(api_config=<b2sdk._internal.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:
    ...

Please note that the timeout/retry system, including class-level variables, is not a part of the interface and is subject to change.

Parameters:

api_config (B2HttpApiConfig) –

CONNECTION_TIMEOUT = 46
TIMEOUT = 128
TIMEOUT_FOR_COPY = 1200
TIMEOUT_FOR_UPLOAD = 128
TRY_COUNT_DATA = 20
TRY_COUNT_DOWNLOAD = 20
TRY_COUNT_HEAD = 5
TRY_COUNT_OTHER = 5
add_callback(callback)[source]

Add a callback that inherits from HttpCallback.

Parameters:

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

request(method, url, headers, data=None, try_count=20, params=None, *, stream=False, _timeout=None)[source]

Use like this:

try:
    response_dict = b2_http.request('POST', url, headers, data)
    ...
except B2Error as e:
    ...
Parameters:
  • method (Literal['POST', 'GET', 'HEAD']) – uppercase HTTP method name

  • url (str) – a URL to call

  • headers (dict[str, str]) – headers to send.

  • data (UnionType[BytesIO, bytes, None]) – raw bytes or a file-like object to send

  • try_count (int) – a number of retries

  • params (Optional[dict[str, str]]) – a dict that will be converted to query string for GET requests or additional metadata for POST requests

  • stream (bool) – if True, the response will be streamed

  • _timeout (Optional[int]) – a timeout for the request in seconds if not default

Return type:

Response

Returns:

final response

Raises:

B2Error if the request fails

request_content_return_json(method, url, headers, data=None, try_count=20, params=None, *, _timeout=None)[source]

Use like this:

try:
    response_dict = b2_http.request_content_return_json('POST', url, headers, data)
    ...
except B2Error as e:
    ...
Parameters:
Return type:

Union[Dict[str, JSON], List[JSON], str, int, float, bool, None]

Returns:

decoded JSON

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

Use like this:

try:
    response_dict = b2_http.post_content_return_json(url, headers, data)
    ...
except B2Error as e:
    ...
Parameters:
Return type:

Union[Dict[str, JSON], List[JSON], str, int, float, bool, None]

Returns:

a dict that is the decoded JSON

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

  • try_count (int) –

Returns:

the decoded JSON document

Return type:

dict

get_content(url, headers, try_count=20)[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

  • try_count

Returns:

Context manager that returns an object that supports iter_content()

head_content(url, headers, try_count=5)[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[str, Any]) – headers to send

  • try_count (int) – a number or retries

  • url

  • headers

  • try_count

Return type:

Response

Returns:

HTTP response

class b2sdk._internal.b2http.NotDecompressingHTTPAdapter(pool_connections=10, pool_maxsize=10, max_retries=0, pool_block=False)[source]

HTTP adapter that uses b2sdk._internal.requests.NotDecompressingResponse instead of the default requests.Response class.

build_response(req, resp)[source]

Builds a Response object from a urllib3 response. This should not be called from user code, and is only exposed for use when subclassing the HTTPAdapter

Parameters:
  • req – The PreparedRequest used to generate the response.

  • resp – The urllib3 response object.

Return type:

requests.Response