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:
- hostnamestr or swat.CAS
Name of the server to connect to or an established swat.CAS session.
- usernamestr, 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.
- passwordstr, optional
Password for authentication. Not required when host is a CAS connection, authinfo is provided, token is provided, or Kerberos is used.
- authinfostr, optional
Path to a .authinfo or .netrc file from which credentials should be pulled.
- protocol{‘http’, ‘https’}
Whether to use HTTP or HTTPS connections. Defaults to https.
- portint, optional
Port number for the connection if a non-standard port is used. Defaults to 80 or 443 depending on protocol.
- verify_sslbool, optional
Whether server-side SSL certificates should be verified. Defaults to true. Ignored for HTTP connections.
- tokenstr, optional
OAuth token to use for authorization.
- client_idstr, optional
Client ID requesting access. Use if connection to Viya should be made using client_credentials method.
- client_secretstr, optional
Client secret for client requesting access. Required if client_id is provided.
- Attributes:
- message_loglogging.Logger
A log to which all REST request and response messages will be sent. Attach a handler using add_logger() to capture these messages.
- filterslist 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.
Methods
add_logger
(handler[, level])Log session requests and responses.
add_stderr_logger
([level])Log session requests and responses to stderr.
as_swat
([server])Create a SWAT connection to a CAS server.
cache_token
(token, path)Write an OAuth2 token to the cache.
close
()Closes all adapters and as such the session
delete
(url, **kwargs)Sends a DELETE request.
get
(url, **kwargs)Sends a GET request.
get_adapter
(url)Returns the appropriate connection adapter for the given URL.
get_redirect_target
(resp)Receives a Response.
head
(url, **kwargs)Sends a HEAD request.
merge_environment_settings
(url, proxies, ...)Check the environment and merge it with some settings.
mount
(prefix, adapter)Registers a connection adapter to a prefix.
options
(url, **kwargs)Sends a OPTIONS request.
patch
(url[, data])Sends a PATCH request.
post
(url, **kwargs)Sends a POST request.
prepare_request
(request)Constructs a
PreparedRequest
for transmission and returns it.put
(url, **kwargs)Sends a PUT request.
read_cached_token
(path)Read any cached access tokens from disk
rebuild_auth
(prepared_request, response)When being redirected we may want to strip authentication from the request to avoid leaking credentials.
rebuild_method
(prepared_request, response)When being redirected we may want to change the method of the request based on certain specs or browser behavior.
rebuild_proxies
(prepared_request, proxies)This method re-evaluates the proxy configuration by considering the environment variables.
request
(method, url[, params, data, ...])Constructs a
Request
, prepares it and sends it.resolve_redirects
(resp, req[, stream, ...])Receives a Response.
send
(request, **kwargs)Send a given PreparedRequest.
should_strip_auth
(old_url, new_url)Decide whether Authorization header should be removed when redirecting
Get version information from the connected SAS Viya environment
- add_logger(handler, level=None)[source]#
Log session requests and responses.
- Parameters:
- handlerlogging.Handler
A Handler instance to use for logging the requests and responses.
- levelint, optional
The logging level to assign to the handler. Ignored if handler’s logging level is already set. Defaults to DEBUG.
- Returns:
- handler
New in version 1.2.0: ..
- add_stderr_logger(level=None)[source]#
Log session requests and responses to stderr.
- Parameters:
- levelint, 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:
- serverstr, optional
The logical name of the CAS server, not the hostname. Defaults to “cas-shared-default”.
- kwargsany
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
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)])
New in version 1.5.4.
- cache_token(token, path)[source]#
Write an OAuth2 token to the cache.
- Parameters:
- tokenOAuth2Token
Token to be cached.
- pathstr
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:
- pathstr
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. ReturnsResponse
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.
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 toFalse
, 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 toFalse
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
- 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.