mstrio > connection¶
- class mstrio.connection.Connection(base_url, username=None, password=None, project=None, project_id=None, project_name=None, login_mode=None, ssl_verify=True, certificate_path=None, proxies=None, identity_token=None, api_token=None, application_id=None, working_set=None, max_search=None, verbose=True, request_timeout=None, request_retry_on_timeout_count=None, locale=None)¶
Bases:
objectConnect to and interact with the Strategy One environment.
Creates a connection object which is used in subsequent requests and manages the user’s connection with the Strategy One REST and Intelligence Servers. The connection is automatically renewed, or reconnected if server’s session associated with the connection expires due to inactivity.
Examples
>>> from mstrio import connection >>> >>> # connect to the environment and chosen project >>> conn = connection.Connection( >>> base_url="https://demo.microstrategy.com/MicroStrategyLibrary", >>> username="username", >>> password="password", >>> project_name="MicroStrategy Tutorial" >>> ) >>> # disconnect >>> conn.close()
- Parameters:
base_url (str)
username (str | None)
password (str | None)
project (Project | str | None)
project_id (str | None)
project_name (str | None)
login_mode (int | LoginMode | None)
ssl_verify (bool)
certificate_path (str | None)
proxies (dict | None)
identity_token (str | None)
api_token (str | None)
application_id (str | Application | None)
working_set (int | None)
max_search (int | None)
verbose (bool)
request_timeout (int | float | None)
request_retry_on_timeout_count (int | None)
locale (Locale | dict | str | None)
- base_url¶
URL of the Strategy One REST API server.
- username¶
Username.
- project_id¶
Id of the connected Strategy One Project.
- project_name¶
Name of the connected Strategy One Project.
- login_mode¶
Authentication mode. Standard = 1 (default), LDAP = 16 or API Token = 4096.
- ssl_verify¶
If True (default), verifies the server’s SSL certificates with each request.
- user_id¶
Id of the authenticated user
- user_full_name¶
Full name of the authenticated user
- user_initials¶
Initials of the authenticated user
- iserver_version¶
Version of the I-Server
- web_version¶
Version of the Web Server
- token¶
authentication token
- working_set¶
Number of report/document instances that are kept in memory on the server before the oldest instance is replaced.
- max_search¶
Maximum number of concurrent searches.
- timeout¶
time after the server’s session expires, in seconds
- request_timeout¶
time (in seconds) after which every request aborts waiting for response and raises requests.Timeout. If not provided explicitly, defaults to mstrio.config.default_request_timeout parameter value.
- deployment_type¶
Type of the connected Strategy One deployment
- classmethod is_run_in_workstation()¶
Check if the script is run in Workstation.
- Returns:
- True if the script is run in Workstation, False otherwise.
It may return None when gathering this info failed.
- Return type:
bool
- __init__(base_url, username=None, password=None, project=None, project_id=None, project_name=None, login_mode=None, ssl_verify=True, certificate_path=None, proxies=None, identity_token=None, api_token=None, application_id=None, working_set=None, max_search=None, verbose=True, request_timeout=None, request_retry_on_timeout_count=None, locale=None)¶
Establish a connection with Strategy One REST API.
You can establish connection by either providing set of values (username, password, login_mode), just identity_token or just api_token.
When api_token is provided and login_mode is set to 4096 or omitted, the connection is established using API Token and all other authentication parameters are ignored.
Note
Selecting a project in mstrio-py’s Connection is equivalent with connecting to a Project: it will create a project-level I-Server connection session.
Note
When project cannot be established or is not provided, it is reset to None.
- Parameters:
base_url (str) – URL of the Strategy One REST API server. Typically of the form: “https://<mstr_env>.com/MicroStrategyLibrary/api”
username (str, optional) – Username
password (str, optional) – Password
project ('Project' | str, optional) – Project object or ID or name for the project to be selected.
project_name (str, optional) – Name of the project you intend to connect to (case-sensitive). Provide either Project ID or Project Name.
project_id (str, optional) – Id of the project you intend to connect to (case-sensitive). Provide either Project ID or Project Name.
login_mode (int | LoginMode, optional) – Specifies the authentication mode to use. Supported authentication modes are: Standard (1) (default), LDAP (16), or API Token (4096). Defaults to 1 unless api_token is provided, then 4096.
ssl_verify (bool, optional) – If True (default), verifies the server’s SSL certificates with each request
certificate_path (str, optional) – Path to SSL certificate file, if None and ssl_verify is True then the certificate will be looked for in current working directory
proxies (dict, optional) – Dictionary mapping protocol or protocol and host to the URL of the proxy (e.g. {‘http’: ‘foo.bar:3128’, ‘http://host.name’: ‘foo.bar:4012’})
identity_token (str, optional) – Identity token for delegated session. Used for connection initialized by GUI.
api_token (str, optional) – API token for authenticating using login_mode 4096. For other login modes it is ignored.
application_id (str, Application, optional) – Log in using the login mode configured in a custom application. If it’s not provided, the server login modes are used.
working_set (int, optional) – Number of report/document instances that are kept in memory on the server before the oldest instance is replaced. Minimum value is 3, if None, will be set to 10 by default.
max_search (int, optional) – Maximum number of concurrent searches. If None, will be set to 3 by default.
verbose (bool, optional) – True by default. Controls the amount of feedback from the I-Server logged by mstrio-py (logger level INFO if True, WARNING if False).
request_timeout (int | float, optional) – Time (in seconds) after which every request aborts waiting for response and raises requests.Timeout. Defaults to None, meaning no timeout. If set to None, fallbacks to mstrio.config.default_request_timeout value, if set.
request_retry_on_timeout_count (int, optional) – Number of times to retry a request in case of a timeout. If None or less than 1, no retries will be attempted.
locale (Locale | dict | str, optional) – Locale settings for the connection. It can be provided as a Locale object, a dictionary with locale parameters or a string with locale name (ex. “en_us”). In the last case, the provided locale will be applied to all locale parameters with default timezone. If not provided, defaults to keeping timezone as UTC and all locale parameters as “en_us”.
- close()¶
Closes a connection with Strategy One REST API.
- connect()¶
Authenticates the user and creates a new connection with the Intelligence Server.
If an active connection is detected, the session is renewed.
- Return type:
None
- delegate()¶
Delegates identity token to get authentication token and connect to Strategy One Intelligence Server.
- delete(url=None, *, endpoint=None, **kwargs)¶
Sends a DELETE request.
- disconnect()¶
Closes a connection with Strategy One REST API.
- get(url=None, *, endpoint=None, **kwargs)¶
Sends a GET request.
- get_api_token()¶
Create new API token using existing authentication token.
- Return type:
str
- get_identity_token()¶
Create new identity token using existing authentication token.
- Return type:
str
- has_project_selected()¶
Check if a project is currently selected in the connection.
- Return type:
bool
- head(url=None, *, endpoint=None, **kwargs)¶
Sends a HEAD request.
- open()¶
Authenticates the user and creates a new connection with the Intelligence Server.
If an active connection is detected, the session is renewed.
- Return type:
None
- patch(url=None, *, endpoint=None, **kwargs)¶
Sends a PATCH request.
- post(url=None, *, endpoint=None, **kwargs)¶
Sends a POST request.
- put(url=None, *, endpoint=None, **kwargs)¶
Sends a PUT request.
- renew()¶
Authenticates the user and creates a new connection with the Intelligence Server.
If an active connection is detected, the session is renewed.
- Return type:
None
- select_project(project=None, project_id=None, project_name=None)¶
Select project for the given connection.
Note
Selecting a project in mstrio-py’s Connection is equivalent with connecting to a Project: it will create a project-level I-Server connection session.
- Parameters:
project (Project | str, optional) – Project object or ID or name specifying the project. May be used instead of project_id or project_name.
project_id (str, optional) – Project ID
project_name (str, optional) – Project name
- Raises:
ValueError – if project with given id or name does not exist or cannot find a unique project with given name.
- Return type:
None
- set_request_retry_on_timeout_count(count)¶
Set the number of times to retry a request in case of a timeout.
- Parameters:
count (int | None) – Number of times to retry a request in case of a timeout. If None or less than 1, no retries will be attempted.
- Return type:
None
- set_request_timeout(timeout)¶
Set the request timeout time for this connection’s requests.
- Parameters:
timeout (int | float | None) – Time (in seconds) after which every request made by this connection will be aborted if not completed. If set to None, fallbacks to mstrio.config.default_request_timeout value, if set.
- Return type:
None
- status()¶
Checks if the session is still alive.
- Raises:
HTTPError if I-Server behaves unexpectedly –
- Return type:
bool
- temporary_project_change(project=None, project_id=None, project_name=None)¶
Context manager for temporary changing the selected project.
Note
This just changes the project assigned to this Connection instance temporarily. Designed to be called using with statement. See Examples below for usage.
- Parameters:
project (Project | str, optional) – Project object or ID or name specifying the project. May be used instead of project_id or project_name.
project_id (str, optional) – Project ID
project_name (str, optional) – Project name
Examples
>>> from mstrio import Connection >>> >>> # initially we select project "MicroStrategy Tutorial" >>> conn = Connection(..., project="MicroStrategy Tutorial") >>> # ... some actions on the `conn` ... >>> >>> with conn.temporary_project_change(project=None): >>> # ... inside this block no project is selected ... >>> >>> # ... outside the `with` block again ... >>> # ... "MicroStrategy Tutorial" is selected ...
- validate_identity_token()¶
Validate the identity token.
- Return type:
bool
- property deployment_type: str | None¶
- property environment¶
- property headers¶
- property iserver_version: str | None¶
- property project_name: str | None¶
- property request_retry_on_timeout_count: int | None¶
- property request_timeout: int | float | None¶
- property token: str¶
- property user_full_name: str¶
- property user_id: str¶
- property user_initials: str¶
- property web_version: str | None¶
- class mstrio.connection.Locale(metadata_locale=None, warehouse_data_locale=None, display_locale=None, messages_locale=None, number_locale=None, time_zone=None)¶
Bases:
DictableLocale settings for the connection.
Submitted as parameters during logging in. If attribute is not provided or set as None, defaults to “en_us” with timezone “UTC”.
Use Locale.all(“<locale-string>”) to set all locale attributes to the same value.
- Parameters:
metadata_locale (str | None)
warehouse_data_locale (str | None)
display_locale (str | None)
messages_locale (str | None)
number_locale (str | None)
time_zone (str | None)
- classmethod all(locale_string, /, time_zone=None)¶
Helper method to set all locale attributes to the same value.
- Parameters:
locale_string (str | None) – String to set all locale attributes to.
time_zone (str | None, optional) – Time zone to set for the connection. If not provided or set as None, defaults to “UTC”.
- Returns:
Locale object with all locale attributes set to the same value.
- classmethod bulk_from_dict(source_list, connection=None, to_snake_case=True, with_missing_value=False)¶
Creates multiple objects from a list of dictionaries. For each dictionary provided the keys in camel case are changed to object’s attribute names (by default in snake case) and dict values are composed to their proper data types such as Enums, list of Enums etc. as specified in the object’s _FROM_DICT_MAP.
- Parameters:
cls (T) – Class (type) of the objects that should be created.
source_list (List[Dict[str, Any]]) – A list of dictionaries from which the objects will be constructed.
connection (Connection, optional) – A MSTR Connection object. Defaults to None.
to_snake_case (bool, optional) – Set to True if attribute names should be converted from camel case to snake case. Defaults to True.
with_missing_value (
bool) – (bool, optional): If True, class attributes possible to fetch and missing in source will be set as MissingValue objects.
- Returns:
A list of objects of type T.
- Return type:
T
- classmethod from_dict(source, connection=None, to_snake_case=True, with_missing_value=False)¶
Creates an object from a dictionary. The dictionary’s keys in camel case are changed to object’s attribute names (by default in snake case) and dict values are composed to their proper data types such as Enums, list of Enums etc. as specified in _FROM_DICT_MAP.
- Parameters:
cls (T) – Class (type) of an object that should be created.
source (Dict[str, Any]) – A dictionary from which an object will be constructed.
connection (Connection, optional) – A MSTR Connection object. Defaults to None.
to_snake_case (bool, optional) – Set to True if attribute names should be converted from camel case to snake case. Defaults to True.
with_missing_value (
bool) – (bool, optional): If True, class attributes possible to fetch and missing in source will be set as MissingValue objects.
- Returns:
An object of type T.
- Return type:
T
- to_dict(camel_case=True, whitelist_keys=None, skip_private_keys=False)¶
Converts an object to a dictionary excluding object’s private properties. When converting the object to a dictionary, the object’s attributes become the dictionary’s keys and are in camel case by default Attribute values stored as objects are automatically converted to non-/ primitive data structures.
- Parameters:
camel_case (bool, optional) – Set to True if attribute names should be converted from snake case to camel case. Defaults to True.
whitelist_keys (list[str], optional) – List of keys to include in the resulting dictionary. If None, all keys (except hidden ones) are included. Defaults to None.
skip_private_keys (bool, optional) – If True, skips private keys in final dict representation.
- Returns:
- A dictionary representation of object’s attributes and values.
By default, the dictionary keys are in camel case.
- Return type:
dict
-
display_locale:
str|None= None¶
-
messages_locale:
str|None= None¶
-
metadata_locale:
str|None= None¶
-
number_locale:
str|None= None¶
-
time_zone:
str|None= None¶
-
warehouse_data_locale:
str|None= None¶
- enum mstrio.connection.LoginMode(value)¶
Bases:
IntEnum- Member Type:
int
Valid values are as follows:
- STANDARD = <LoginMode.STANDARD: 1>¶
- ANONYMOUS = <LoginMode.ANONYMOUS: 8>¶
- LDAP = <LoginMode.LDAP: 16>¶
- API_TOKEN = <LoginMode.API_TOKEN: 4096>¶
The
Enumand its members also have the following methods:- conjugate()¶
Returns self, the complex conjugate of any int.
- bit_length()¶
Number of bits necessary to represent self in binary.
>>> bin(37) '0b100101' >>> (37).bit_length() 6
- bit_count()¶
Number of ones in the binary representation of the absolute value of self.
Also known as the population count.
>>> bin(13) '0b1101' >>> (13).bit_count() 3
- to_bytes(length=1, byteorder='big', *, signed=False)¶
Return an array of bytes representing an integer.
- length
Length of bytes object to use. An OverflowError is raised if the integer is not representable with the given number of bytes. Default is length 1.
- byteorder
The byte order used to represent the integer. If byteorder is ‘big’, the most significant byte is at the beginning of the byte array. If byteorder is ‘little’, the most significant byte is at the end of the byte array. To request the native byte order of the host system, use sys.byteorder as the byte order value. Default is to use ‘big’.
- signed
Determines whether two’s complement is used to represent the integer. If signed is False and a negative integer is given, an OverflowError is raised.
- classmethod from_bytes(bytes, byteorder='big', *, signed=False)¶
Return the integer represented by the given array of bytes.
- bytes
Holds the array of bytes to convert. The argument must either support the buffer protocol or be an iterable object producing bytes. Bytes and bytearray are examples of built-in objects that support the buffer protocol.
- byteorder
The byte order used to represent the integer. If byteorder is ‘big’, the most significant byte is at the beginning of the byte array. If byteorder is ‘little’, the most significant byte is at the end of the byte array. To request the native byte order of the host system, use sys.byteorder as the byte order value. Default is to use ‘big’.
- signed
Indicates whether two’s complement is used to represent the integer.
- as_integer_ratio()¶
Return a pair of integers, whose ratio is equal to the original int.
The ratio is in lowest terms and has a positive denominator.
>>> (10).as_integer_ratio() (10, 1) >>> (-10).as_integer_ratio() (-10, 1) >>> (0).as_integer_ratio() (0, 1)
- is_integer()¶
Returns True. Exists for duck type compatibility with float.is_integer.
- real¶
the real part of a complex number
- imag¶
the imaginary part of a complex number
- numerator¶
the numerator of a rational number in lowest terms
- denominator¶
the denominator of a rational number in lowest terms
- mstrio.connection.get_connection(workstation_data, project=None, project_id=None, project_name=None, ssl_verify=False, verbose=True, request_timeout=None, request_retry_on_timeout_count=None)¶
Connect to environment without providing user’s credentials.
It is possible to provide project, project_id or project_name to select project. When all are None, project selection is cleared. Project can be also selected later by calling method select_project on Connection object.
Note
ssl_verify is set to False by default just for the get_connection function as it is designed for usage inside Workstation. When ssl_verify is set to False, warning about missing certificate verification (InsecureRequestWarning) is disabled.
- Parameters:
workstation_data (object) – object which is stored in a ‘workstationData’ variable within Workstation
project (Project, str, optional) – Project object or ID or name of the project to be selected
project_id (str, optional) – ID of project to be selected
project_name (str, optional) – Name of project to be selected
ssl_verify (bool, optional) – If False (default), does not verify the server’s SSL certificates
verbose (bool, optional) – True by default. Controls the amount of feedback from the I-Server logged by mstrio-py (logger level INFO if True, WARNING if False).
request_timeout (int | float, optional) – Time (in seconds) after which every request aborts waiting for response and raises requests.Timeout. Defaults to None, meaning no timeout. If set to None, fallbacks to mstrio.config.default_request_timeout value, if set.
request_retry_on_timeout_count (int, optional) – Number of times to retry a request in case of a timeout. If None or less than 2, no retries will be attempted.
- Returns:
Connection|None– connection to I-Server or None in case of some error- Return type:
Connection | None