b2sdk._internal.b2http
– thin http client wrapper
- b2sdk._internal.b2http.random() x in the interval [0, 1).
- 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.
- class b2sdk._internal.b2http.ClockSkewHook[source]
- 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 nameurl (
str
) – a URL to calldata (
UnionType
[BytesIO
,bytes
,None
]) – raw bytes or a file-like object to sendtry_count (
int
) – a number of retriesparams (
Optional
[dict
[str
,str
]]) – a dict that will be converted to query string for GET requests or additional metadata for POST requestsstream (
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: ...
- 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: ...
- 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()
- 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 defaultrequests.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 theHTTPAdapter
- Parameters:
req – The
PreparedRequest
used to generate the response.resp – The urllib3 response object.
- Return type:
requests.Response