Beginner Python Script #38
ChristiaanEmpowerBI
started this conversation in
Show and tell
Replies: 1 comment
-
Here are some videos: https://youtube.com/playlist?list=PL2K8Xb9vcI4XzrBQVfRHNfZ8vvH33wJoe&si=0GxUiCKwnkAwWJba |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I am a BI Developer, not a Software Developer.
I was tasked with figuring out the REST API (I do wish the training and examples on this topic were more detailed). I have limited Python knowledge, and have only ever learnt something from chat GPT or other people's code. I am a bad and slow programmer, but a pretty good MicroStrategy consultant.
Here is my first attempt at writing a Python script to get the Revenue of a Customer out of a derived metric out of a Dossier where the user application needs to change the Customer ID, using Standard Authentication. I am not done, and will update my code if I figure it out in time. Feel free to comment or criticize, that is the only way I learn:
#pip install mstrio.py
#https://github.com/MicroStrategy/mstrio-py
import mstrio
from mstrio.connection import get_connection, Connection
from mstrio.utils.error_handlers import ErrorHandler
import re
import requests
import json
PROJECT_NAME = 'Tutorial Project'
BASE_URL = 'http://my_home_pc:8080/MicroStrategyLibrary/api'
MSTR_USERNAME = 'Username'
MSTR_PASSWORD = 'Password'
PROJECT_ID = 'B19DEDCC11D4E0EFC000EB9495D0F44F'
dossier_id = 'C3D32F6C48EC04BACC94D281C4ABC936'
conn = Connection(
BASE_URL,
MSTR_USERNAME,
MSTR_PASSWORD,
project_id=PROJECT_ID,
)
def login(connection):
return connection.post(
skip_expiration_check=True,
endpoint='/api/auth/login',
data={
'username': connection.username,
'password': connection._Connection__password,
'loginMode': connection.login_mode
},
)
def get_JSESSIONID(input_string):
pattern = r'JSESSIONID=([^;]+)'
match = re.search(pattern, input_string)
if match:
JSESSIONID = match.group(1)
return JSESSIONID
else:
print('No JSESSIONID found.')
def get_dossiers_dossier_id_definition(auth_token, project_id, dossier_id, connection):
api_request = f'/api/dossiers/{dossier_id}/definition'
request_headers={'accept' : 'application/json', 'X-MSTR-AuthToken': auth_token, 'X-MSTR-ProjectID': PROJECT_ID, }
return connection.get(
endpoint=api_request,
headers=request_headers,
)
def convert_bytes_to_JSON(bytes_input):
json_string = bytes_input.decode('utf-8')
data_dictionary = json.loads(json_string)
return data_dictionary
HTTP_response = login(conn) #<class 'requests.models.Response'>
JSESSIONID = get_JSESSIONID(HTTP_response.headers['Set-Cookie'])
print('JSESSIONID:',JSESSIONID)
X_MSTR_AuthToken = HTTP_response.headers['X-MSTR-AuthToken']
print('X-MSTR-AuthToken:', X_MSTR_AuthToken)
HTTP_response = get_dossiers_dossier_id_definition(X_MSTR_AuthToken, PROJECT_ID, dossier_id, conn) #<class 'requests.structures.CaseInsensitiveDict'>
response_JSON = convert_bytes_to_JSON(HTTP_response.content)
formatted_json = json.dumps(response_JSON, indent=2)
print(formatted_json)
conn.close()
Beta Was this translation helpful? Give feedback.
All reactions