libcloud.common.base module

class libcloud.common.base.BaseDriver(key, secret=None, secure=True, host=None, port=None, api_version=None, region=None, **kwargs)[source]

Bases: object

Base driver class from which other classes can inherit from.

Parameters:
  • key (str) – API key or username to be used (required)

  • secret (str) – Secret password to be used (required)

  • secure (bool) – Whether to use HTTPS or HTTP. Note: Some providers only support HTTPS, and it is on by default.

  • host (str) – Override hostname used for connections.

  • port (int) – Override port used for connections.

  • api_version (str) – Optional API version. Only used by drivers which support multiple API versions.

  • region (str) – Optional driver region. Only used by drivers which support multiple regions.

Return type:

None

connectionCls

alias of ConnectionKey

class libcloud.common.base.CertificateConnection(cert_file, secure=True, host=None, port=None, url=None, proxy_url=None, timeout=None, backoff=None, retry_delay=None)[source]

Bases: Connection

Base connection class which accepts a single cert_file argument.

Initialize cert_file; set secure to an int based on passed value.

class libcloud.common.base.Connection(secure=True, host=None, port=None, url=None, timeout=None, proxy_url=None, retry_delay=None, backoff=None)[source]

Bases: object

A Base Connection class to derive from.

action = None
add_default_headers(headers)[source]

Adds default headers (such as Authorization, X-Foo-Bar) to the passed headers

Should return a dictionary.

add_default_params(params)[source]

Adds default parameters (such as API key, version, etc.) to the passed params

Should return a dictionary.

allow_insecure = True
backoff = None
cache_busting = False
conn_class

alias of LibcloudConnection

connect(host=None, port=None, base_url=None, **kwargs)[source]

Establish a connection with the API server.

Parameters:
  • host (str) – Optional host to override our default

  • port (int) – Optional port to override our default

Returns:

A connection

connection = None
driver: Type[BaseDriver] = None
encode_data(data)[source]

Encode body data.

Override in a provider’s subclass.

host: str = '127.0.0.1'
morph_action_hook(action)[source]

Here we strip any duplicated leading or trailing slashes to prevent typos and other issues where some APIs don’t correctly handle double slashes.

Keep in mind that in some situations, “/” is a valid path name so we have a module flag which disables this behavior (https://github.com/apache/libcloud/issues/1529).

port = 443
pre_connect_hook(params, headers)[source]

A hook which is called before connecting to the remote server. This hook can perform a final manipulation on the params, headers and url parameters.

Parameters:
  • params (dict) – Request parameters.

  • headers (dict) – Request headers.

rawResponseCls

alias of RawResponse

request(action, params=None, data=None, headers=None, method='GET', raw=False, stream=False, json=None, retry_failed=None)[source]

Request a given action.

Basically a wrapper around the connection object’s request that does some helpful pre-processing.

Parameters:
  • action (str) – A path. This can include arguments. If included, any extra parameters are appended to the existing ones.

  • params (dict) – Optional mapping of additional parameters to send. If None, leave as an empty dict.

  • data (unicode) – A body of data to send with the request.

  • headers (dict) – Extra headers to add to the request None, leave as an empty dict.

  • method (str) – An HTTP method such as “GET” or “POST”.

  • raw (bool) – True to perform a “raw” request aka only send the headers and use the rawResponseCls class. This is used with storage API when uploading a file.

  • stream (bool) – True to return an iterator in Response.iter_content and allow streaming of the response data (for downloading large files)

  • retry_failed – True if failed requests should be retried. This argument can override module level constant and environment variable value on per-request basis.

Returns:

An Response instance.

Return type:

Response instance

reset_context()[source]
responseCls

alias of Response

retryCls

alias of Retry

retry_delay = None
secure = 1
set_context(context)[source]
set_http_proxy(proxy_url)[source]

Set a HTTP / HTTPS proxy which will be used with this connection.

Parameters:

proxy_url (str) – Proxy URL (e.g. http://<hostname>:<port> without authentication and <scheme>://<username>:<password>@<hostname>:<port> for basic auth authentication information.

timeout: int | float | None = None
user_agent_append(token)[source]

Append a token to a user agent string.

Users of the library should call this to uniquely identify their requests to a provider.

Parameters:

token (str) – Token to add to the user agent.

class libcloud.common.base.ConnectionKey(key, secure=True, host=None, port=None, url=None, timeout=None, proxy_url=None, backoff=None, retry_delay=None)[source]

Bases: Connection

Base connection class which accepts a single key argument.

Initialize user_id and key; set secure to an int based on passed value.

class libcloud.common.base.ConnectionUserAndKey(user_id, key, secure=True, host=None, port=None, url=None, timeout=None, proxy_url=None, backoff=None, retry_delay=None)[source]

Bases: ConnectionKey

Base connection class which accepts a user_id and key argument.

Initialize user_id and key; set secure to an int based on passed value.

user_id: int = None
class libcloud.common.base.HTTPResponse(sock, debuglevel=0, method=None, url=None)[source]

Bases: HTTPResponse

read(amt=None)[source]

Read and return up to n bytes.

If the argument is omitted, None, or negative, reads and returns all data until EOF.

If the argument is positive, and the underlying raw stream is not ‘interactive’, multiple raw reads may be issued to satisfy the byte count (unless EOF is reached first). But for interactive raw streams (as well as sockets and pipes), at most one raw read will be issued, and a short result does not imply that EOF is imminent.

Returns an empty bytes object on EOF.

Returns None if the underlying raw stream was open in non-blocking mode and no data is available at the moment.

class libcloud.common.base.JsonResponse(response, connection)[source]

Bases: Response

A Base JSON Response class to derive from.

Parameters:
  • response (httplib.HTTPResponse) – HTTP response object. (optional)

  • connection (Connection) – Parent connection object.

parse_body()[source]

Parse response body.

Override in a provider’s subclass.

Returns:

Parsed body.

Return type:

str

parse_error()

Parse the error messages.

Override in a provider’s subclass.

Returns:

Parsed error.

Return type:

str

class libcloud.common.base.PollingConnection(secure=True, host=None, port=None, url=None, timeout=None, proxy_url=None, retry_delay=None, backoff=None)[source]

Bases: Connection

Connection class which can also work with the async APIs.

After initial requests, this class periodically polls for jobs status and waits until the job has finished. If job doesn’t finish in timeout seconds, an Exception thrown.

async_request(action, params=None, data=None, headers=None, method='GET', context=None)[source]

Perform an ‘async’ request to the specified path. Keep in mind that this function is blocking and ‘async’ in this case means that the hit URL only returns a job ID which is the periodically polled until the job has completed.

This function works like this:

  • Perform a request to the specified path. Response should contain a ‘job_id’.

  • Returned ‘job_id’ is then used to construct a URL which is used for retrieving job status. Constructed URL is then periodically polled until the response indicates that the job has completed or the timeout of ‘self.timeout’ seconds has been reached.

Parameters:
  • action (str) – A path

  • params (dict) – Optional mapping of additional parameters to send. If None, leave as an empty dict.

  • data (unicode) – A body of data to send with the request.

  • headers (dict) – Extra headers to add to the request None, leave as an empty dict.

  • method (str) – An HTTP method such as “GET” or “POST”.

  • context (dict) – Context dictionary which is passed to the functions which construct initial and poll URL.

Returns:

An Response instance.

Return type:

Response instance

get_poll_request_kwargs(response, context, request_kwargs)[source]

Return keyword arguments which are passed to the request() method when polling for the job status.

Parameters:
  • response (dict) – Response object returned by poll request.

  • request_kwargs – Kwargs previously used to initiate the poll request.

:return dict Keyword arguments

get_request_kwargs(action, params=None, data=None, headers=None, method='GET', context=None)[source]

Arguments which are passed to the initial request() call inside async_request.

has_completed(response)[source]

Return job completion status.

Parameters:

response (HTTPResponse) – Response object returned by poll request.

:return bool True if the job has completed, False otherwise.

poll_interval = 0.5
request_method = 'request'
timeout: int | float | None = 200
class libcloud.common.base.RawResponse(connection, response=None)[source]

Bases: Response

Parameters:

connection (Connection) – Parent connection object.

property body
property reason
property response
success()[source]

Determine if our request was successful.

The meaning of this can be arbitrary; did we receive OK status? Did the node get created? Were we authenticated?

Return type:

bool

Returns:

True or False

class libcloud.common.base.Response(response, connection)[source]

Bases: object

A base Response class to derive from.

Parameters:
  • response (httplib.HTTPResponse) – HTTP response object. (optional)

  • connection (Connection) – Parent connection object.

body = None
connection = None
error = None
headers: dict = {}
object = None
parse_body()[source]

Parse response body.

Override in a provider’s subclass.

Returns:

Parsed body.

Return type:

str

parse_error()[source]

Parse the error messages.

Override in a provider’s subclass.

Returns:

Parsed error.

Return type:

str

parse_zero_length_body = False
status: int = 200
success()[source]

Determine if our request was successful.

The meaning of this can be arbitrary; did we receive OK status? Did the node get created? Were we authenticated?

Return type:

bool

Returns:

True or False

class libcloud.common.base.XmlResponse(response, connection)[source]

Bases: Response

A Base XML Response class to derive from.

Parameters:
  • response (httplib.HTTPResponse) – HTTP response object. (optional)

  • connection (Connection) – Parent connection object.

parse_body()[source]

Parse response body.

Override in a provider’s subclass.

Returns:

Parsed body.

Return type:

str

parse_error()

Parse the error messages.

Override in a provider’s subclass.

Returns:

Parsed error.

Return type:

str