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()
-
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: ...
-
class
b2sdk.b2http.
ClockSkewHook
[source]¶ Bases:
b2sdk.b2http.HttpCallback
-
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.
-