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)

Bases: object

Connect 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) –

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.

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.

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

head(url=None, *, endpoint=None, **kwargs)

Sends a HEAD request.

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

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 based.

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

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 Enum and its members also have the following methods:

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)
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
bit_length()

Number of bits necessary to represent self in binary.

>>> bin(37)
'0b100101'
>>> (37).bit_length()
6
conjugate()

Returns self, the complex conjugate of any int.

from_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.

is_integer()

Returns True. Exists for duck type compatibility with float.is_integer.

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.

denominator

the denominator of a rational number in lowest terms

imag

the imaginary part of a complex number

numerator

the numerator of a rational number in lowest terms

real

the real part of a complex number

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 to I-Server or None in case of some error

Return type:

Connection | None