Session#

class sasctl.core.Session(hostname, username=None, password=None, authinfo=None, protocol=None, port=None, verify_ssl=None, token=None, client_id=None, client_secret=None, consul_token=None)[source]#

Establish a connection to a SAS Viya server.

Parameters:
  • hostname (str or swat.cas.connection.CAS) – Name of the server to connect to or an established CAS session.

  • username (str, optional) – Username for authentication. Not required if host is a CAS connection, if Kerberos is used, or if token is provided. If using Kerberos and an explicit username is desired, maybe be a string in ‘user@REALM’ format.

  • password (str, optional) – Password for authentication. Not required when host is a CAS connection, authinfo is provided, token is provided, or Kerberos is used.

  • authinfo (str, optional) – Path to a .authinfo or .netrc file from which credentials should be pulled.

  • protocol (str) – Choose from {'http', 'https'}. Whether to use HTTP or HTTPS connections. Defaults to https.

  • port (int, optional) – Port number for the connection if a non-standard port is used. Defaults to 80 or 443 depending on protocol.

  • verify_ssl (bool, optional) – Whether server-side SSL certificates should be verified. Defaults to true. Ignored for HTTP connections.

  • token (str, optional) – OAuth token to use for authorization.

  • client_id (str, optional) – Client ID requesting access. Use if connection to Viya should be made using “client_credentials” method.

  • client_secret (str, optional) – Client secret for client requesting access. Required if client_id is provided.

Variables:
  • message_log (logging.Logger) – A log to which all REST request and response messages will be sent. Attach a handler using add_logger() to capture these messages.

  • filters (list of Callable) – A collection of functions that will be called with each request and response object prior to logging the messages, allowing any sensitive information to be removed first.

add_logger(handler, level=None)[source]#

Log session requests and responses.

Parameters:
  • handler (logging.Handler) – A Handler instance to use for logging the requests and responses.

  • level (int, optional) – The logging level to assign to the handler. Ignored if handler’s logging level is already set. Defaults to logging.DEBUG.

Returns:

logging.Handler

Added in version 1.2.0.

add_stderr_logger(level=None)[source]#

Log session requests and responses to stderr.

Parameters:

level (int, optional) – The logging level of the handler. Defaults to logging.DEBUG

Returns:

logging.StreamHandler

as_swat(server=None, **kwargs)[source]#

Create a SWAT connection to a CAS server.

Uses the authentication information from the session to establish a CAS connection using SWAT.

Parameters:
  • server (str, optional) – The logical name of the CAS server, not the hostname. Defaults to “cas-shared-default”.

  • **kwargs – Additional arguments to pass to the swat.CAS constructor. Can be used to override this method’s default behavior or customize the CAS session.

Returns:

swat.cas.connection.CAS – An active SWAT connection

Raises:

RuntimeError – If swat package is not available.

Examples

>>> sess = Session('example.sas.com')
>>> with sess.as_swat() as conn:
...    conn.listnodes()
CASResults([('nodelist', Node List
              name        role connected   IP Address
0  example.sas.com  controller       Yes  127.0.0.1)])

Added in version 1.5.4.

cache_token(token, path)[source]#

Write an OAuth2 token to the cache.

Parameters:
  • token (OAuth2Token) – Token to be cached.

  • path (str) – Path to file containing cached tokens.

Returns:

None

delete(url, **kwargs)[source]#

Sends a DELETE request. Returns Response object.

Parameters:
  • url – URL for the new Request object.

  • **kwargs – Optional arguments that request takes.

Return type:

requests.Response

get(url, **kwargs)[source]#

Sends a GET request. Returns Response object.

Parameters:
  • url – URL for the new Request object.

  • **kwargs – Optional arguments that request takes.

Return type:

requests.Response

head(url, **kwargs)[source]#

Sends a HEAD request. Returns Response object.

Parameters:
  • url – URL for the new Request object.

  • **kwargs – Optional arguments that request takes.

Return type:

requests.Response

post(url, **kwargs)[source]#

Sends a POST request. Returns Response object.

Parameters:
  • url – URL for the new Request object.

  • data – (optional) Dictionary, list of tuples, bytes, or file-like object to send in the body of the Request.

  • json – (optional) json to send in the body of the Request.

  • **kwargs – Optional arguments that request takes.

Return type:

requests.Response

put(url, **kwargs)[source]#

Sends a PUT request. Returns Response object.

Parameters:
  • url – URL for the new Request object.

  • data – (optional) Dictionary, list of tuples, bytes, or file-like object to send in the body of the Request.

  • **kwargs – Optional arguments that request takes.

Return type:

requests.Response

read_cached_token(path)[source]#

Read any cached access tokens from disk

Parameters:

path (str) – Path to file containing cached tokens.

Returns:

OAuth2Token or None

request(method, url, params=None, data=None, headers=None, cookies=None, files=None, auth=None, timeout=None, allow_redirects=True, proxies=None, hooks=None, stream=None, verify=None, cert=None, json=None)[source]#

Constructs a Request, prepares it and sends it. Returns Response object.

Parameters:
  • method – method for the new Request object.

  • url – URL for the new Request object.

  • params – (optional) Dictionary or bytes to be sent in the query string for the Request.

  • data – (optional) Dictionary, list of tuples, bytes, or file-like object to send in the body of the Request.

  • json – (optional) json to send in the body of the Request.

  • headers – (optional) Dictionary of HTTP Headers to send with the Request.

  • cookies – (optional) Dict or CookieJar object to send with the Request.

  • files – (optional) Dictionary of 'filename': file-like-objects for multipart encoding upload.

  • auth – (optional) Auth tuple or callable to enable Basic/Digest/Custom HTTP Auth.

  • timeout (float or tuple) – (optional) How long to wait for the server to send data before giving up, as a float, or a (connect timeout, read timeout) tuple.

  • allow_redirects (bool) – (optional) Set to True by default.

  • proxies – (optional) Dictionary mapping protocol or protocol and hostname to the URL of the proxy.

  • hooks – (optional) Dictionary mapping hook name to one event or list of events, event must be callable.

  • stream – (optional) whether to immediately download the response content. Defaults to False.

  • 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. Defaults to True. When set to False, requests will accept any TLS certificate presented by the server, and will ignore hostname mismatches and/or expired certificates, which will make your application vulnerable to man-in-the-middle (MitM) attacks. Setting verify to False may be useful during local development or testing.

  • cert – (optional) if String, path to ssl client cert file (.pem). If Tuple, (‘cert’, ‘key’) pair.

Return type:

requests.Response

send(req, **kwargs)[source]#

Send a given PreparedRequest.

Return type:

requests.Response

version_info()[source]#

Get version information from the connected SAS Viya environment

Returns:

VersionInfo

Notes

The resulting version information is cached and returned on any subsequent calls. This allows repeatedly checking version information without making redundant network calls to the SAS Viya server.