Request (internal)#

class qbittorrentapi.request.Request(host='', port=None, username=None, password=None, **kwargs)#

Bases: object

Facilitates HTTP requests to qBittorrent’s Web API.

_get(_name=APINames.EMPTY, _method='', **kwargs)#

Send GET request.

Parameters
  • api_namespace – the namespace for the API endpoint (e.g. APINames or torrents)

  • api_method – the name for the API endpoint (e.g. add)

  • kwargs – see _request()

Returns

Requests Response

_get_data(http_method, params=None, data=None, files=None, **kwargs)#

Determine data, params, and files for the Requests call.

Parameters
  • http_methodget or post

  • params – key value pairs to send with GET calls

  • data – key value pairs to send with POST calls

  • files – dictionary of files to send with request

Returns

final dictionaries of data to send to qBittorrent

_get_headers(headers=None, more_headers=None)#

Determine headers specific to this request. Request headers can be specified explicitly or with the requests kwargs. Headers specified in self._EXTRA_HEADERS are merged in Requests itself.

Parameters
  • headers – headers specified for this specific request

  • more_headers – headers from requests_kwargs config

Returns

final dictionary of headers for this specific request

_get_requests_kwargs(requests_args=None, requests_params=None)#

Determine the requests_kwargs for the call to Requests. The global configuration in self._REQUESTS_ARGS is updated by any arguments provided for a specific call.

Parameters
  • requests_args – default location to expect Requests requests_kwargs

  • requests_params – alternative location to expect Requests requests_kwargs

Returns

final dictionary of Requests requests_kwargs

static _handle_error_responses(data, params, response)#

Raise proper exception if qBittorrent returns Error HTTP Status.

_initialize_context()#

Initialize and/or reset communications context with qBittorrent.

This is necessary on startup or when the auth cookie needs to be replaced…perhaps because it expired, qBittorrent was restarted, significant settings changes, etc.

_initialize_lesser(EXTRA_HEADERS=None, REQUESTS_ARGS=None, VERIFY_WEBUI_CERTIFICATE=True, FORCE_SCHEME_FROM_HOST=False, RAISE_UNIMPLEMENTEDERROR_FOR_UNIMPLEMENTED_API_ENDPOINTS=False, RAISE_NOTIMPLEMENTEDERROR_FOR_UNIMPLEMENTED_API_ENDPOINTS=False, RAISE_ERROR_FOR_UNSUPPORTED_QBITTORRENT_VERSIONS=False, VERBOSE_RESPONSE_LOGGING=False, PRINT_STACK_FOR_EACH_REQUEST=False, SIMPLE_RESPONSES=False, DISABLE_LOGGING_DEBUG_OUTPUT=False, MOCK_WEB_API_VERSION=None)#

Initialize lesser used configuration.

classmethod _list2string(input_list=None, delimiter='|')#

Convert entries in a list to a concatenated string.

Parameters
  • input_list – list to convert

  • delimiter – delimiter for concatenation

Returns

if input is a list, concatenated string…else whatever the input was

_post(_name=APINames.EMPTY, _method='', **kwargs)#

Send POST request.

Parameters
  • api_namespace – the namespace for the API endpoint (e.g. APINames or torrents)

  • api_method – the name for the API endpoint (e.g. add)

  • kwargs – see _request()

Returns

Requests Response

_request(http_method, api_namespace, api_method, requests_args=None, requests_params=None, headers=None, params=None, data=None, files=None, **kwargs)#

Meat and potatoes of sending requests to qBittorrent.

Parameters
  • http_methodget or post

  • api_namespace – the namespace for the API endpoint (e.g. APINames or torrents)

  • api_method – the name for the API endpoint (e.g. add)

  • requests_args – default location for Requests kwargs

  • requests_params – alternative location for Requests kwargs

  • headers – HTTP headers to send with the request

  • params – key/value pairs to send with a GET request

  • data – key/value pairs to send with a POST request

  • files – files to be sent with the request

  • kwargs – arbitrary keyword args to send to qBittorrent with the request

Returns

Requests Response

_request_manager(_retries=1, _retry_backoff_factor=0.3, **kwargs)#

Wrapper to manage request retries and severe exceptions.

This should retry at least once to account for the Web API switching from HTTP to HTTPS. During the second attempt, the URL is rebuilt using HTTP or HTTPS as appropriate.

property _session#

Create or return existing HTTP session.

Returns

Requests Session object

_trigger_session_initialization()#

Effectively resets the HTTP session by removing the reference to it.

During the next request, a new session will be created.

static _trim_known_kwargs(**kwargs)#

Since any extra keyword arguments from the user are automatically included in the request to qBittorrent, this removes any “known” arguments that definitely shouldn’t be sent to qBittorrent. Generally, these are removed in previous processing, but in certain circumstances, they can survive in to request.

Parameters

kwargs – extra keywords arguments to be passed along in request

Returns

sanitized arguments

_verbose_logging(http_method, url, data, params, requests_kwargs, response)#

Log verbose information about request.

Can be useful during development.

class qbittorrentapi.request.URL(client)#

Bases: object

Management for the qBittorrent Web API URL.

build_base_url(headers, requests_kwargs=None)#

Determine the Base URL for the Web API endpoints.

A URL is only actually built here if it’s the first time here or the context was re-initialized. Otherwise, the most recently built URL is used.

If the user doesn’t provide a scheme for the URL, it will try HTTP first and fall back to HTTPS if that doesn’t work. While this is probably backwards, qBittorrent or an intervening proxy can simply redirect to HTTPS and that’ll be respected.

Additionally, if users want to augment the path to the API endpoints, any path provided here will be preserved in the returned Base URL and prefixed to all subsequent API calls.

Parameters
  • headers – HTTP headers for request

  • requests_kwargs – additional params from user for HTTP HEAD request

Returns

base URL as a string for Web API endpoint

build_url(api_namespace, api_method, headers, requests_kwargs)#

Create a fully qualified URL for the API endpoint.

Parameters
  • api_namespace – the namespace for the API endpoint (e.g. torrents)

  • api_method – the specific method for the API endpoint (e.g. info)

  • headers – HTTP headers for request

  • requests_kwargs – kwargs for any calls to Requests

Returns

fully qualified URL string for endpoint

build_url_path(api_namespace, api_method)#

Determine the full URL path for the API endpoint.

Parameters
  • api_namespace – the namespace for the API endpoint (e.g. torrents)

  • api_method – the specific method for the API endpoint (e.g. info)

Returns

entire URL string for API endpoint (e.g. http://localhost:8080/api/v2/torrents/info or http://example.com/qbt/api/v2/torrents/info)

detect_scheme(base_url, default_scheme, alt_scheme, headers, requests_kwargs)#

Determine if the URL endpoint is using HTTP or HTTPS.

Parameters
  • base_url – urllib ParseResult URL object

  • default_scheme – default scheme to use for URL

  • alt_scheme – alternative scheme to use for URL if default doesn’t work

  • headers – HTTP headers for request

  • requests_kwargs – kwargs for calls to Requests

Returns

scheme (ie HTTP or HTTPS)