# Module `netapp_ontap.host_connection`

Copyright © 2020 NetApp Inc. All rights reserved.

This module defines a host connection object which is used to communicate with the API host.

## Functions

`def pretty_print_request_response(response: requests.models.Response)`

  Prints the complete request and response in a pretty way.

## Classes

`class HostConnection (host, username: str = None, password: str = None, cert: str = None, key: str = None, verify: bool = True, poll_timeout: int = 30, poll_interval: int = 5, headers: dict = None)`

  The HostConnection allows the client application to store their credentials and reuse them for each operation. There are three ways to use a connection object:

  - The first is to use the connection object as a context manager. Any operations on a resource that are called within the scope of the block will use that connection.
  - The second is to call set_connection() on a resource object. This will then be the connection used for all actions for that object only.
  - The third way is to call netapp_ontap.config.CONNECTION = connection. This connection instance will now be used for all actions on all resource objects (that do not otherwise set their own connection). This reduces the need to pass the connection around the application.

  Connections will be searched for in this order when executing an action.

  Store information needed to contact the API host

  Either username and password must be provided or certificate and key must be provided.

  If verify is set to False, urllib3's InsecureRequestWarnings will also be silenced in the logs.

  ## Args

  `host`
    The API host that the library should talk to
  `username`
    The user identifier known to the host
  `password`
    The secret for the user
  `cert`
    The file path to the users public certificate. The common name in the certificate must match the account name.
  `key`
    A private key in PEM format
  `verify`
    If an SSL connection is made to the host, this parameter controls how the validity of the trust chain of the certificate is handled. See the documentation for the requests library for more information: https://2.python-requests.org/en/master/user/advanced/#ssl-cert-verification
  `poll_timeout`
    Time in seconds to poll on a job. This setting applies to all polling that uses this connection unless overridden as a parameter to poll(). Defaults to 30 seconds.
  `poll_interval`
    Time in seconds to wait between polls on a job. This setting applies to all polling that uses this connection unless overridden as a parameter to poll(). Defaults to 5 seconds.
  `headers`
    Any custom headers to be passed to each request using this connection object.

  ### Static methods

  `def get_host_context() -> typing.Union`

    Get the current host context, if any.

    ## Returns

    A HostConnection object or None if not in a host connection context.

  ### Instance variables

  `basic_auth`

    Pulls the credentials out of the connection object.

    ## Returns

    A tuple of username and password sufficient for passing to the requests library. Returns None if this connection is not configured for basic auth with a username and password.

  `cert_auth`

    Pulls the certificate details out of the connection object.

    ## Returns

    A tuple of cert and key sufficient for passing to the requests library. Returns None if this connection is not configured for cert auth with a cert and key.

  `origin`

    The beginning of any REST endpoint.

    ## Returns

    The origin part of the URL. For example, `<http://1.2.3.4:8080`.>

  `request_headers`

    Retrieves the headers set out of the connection object

    ## Returns

    A dictionary consisting of header names and values for passing to the requests library. Returns None if no headers are configured.

  `session`

    A `requests.Session` object which is used for all API calls.

    This session is reused for each API call made with this connection. Multiple requests may therefore be sent through the same TCP connection assuming the host supports keep-alive.

    ## Returns

    A `requests.Session` object which is used for all API calls.

  ### Methods

  `def with_headers(self, headers: dict) -> typing.Generator`

    Manually set the headers field of the connection object

`class LoggingAdapter (host_connection, *args, **kwargs)`

  A custom HTTPAdapter which logs API calls if logging is enabled

  ### Ancestors

  - requests.adapters.HTTPAdapter
  - requests.adapters.BaseAdapter

  ### Methods

  `def send(self, request, stream=False, timeout=None, verify=True, cert=None, proxies=None) -> requests.models.Response`

    Sends PreparedRequest object. Returns Response object.

    :param request: The :class:`PreparedRequest <PreparedRequest>` being sent. :param stream: (optional) Whether to stream the request content. :param timeout: (optional) How long to wait for the server to send data before giving up, as a float, or a :ref:`(connect timeout, read timeout) <timeouts>` tuple. :type timeout: float or tuple or urllib3 Timeout object :param verify: (optional) Either a boolean, in which case it controls whether we verify the server's TLS certificate, or a string, in which case it must be a path to a CA bundle to use :param cert: (optional) Any user-provided SSL certificate to be trusted. :param proxies: (optional) The proxies dictionary to apply to the request. :rtype: requests.Response