import requests
import json


def vaultLogin(baseUrl, vaultUser, vaultPwd, vaultClientId):
    """
    Make the Vault login request, capture session id for the specified username
    and password

    :param baseUrl: the Vault Base URL
    :param vaultUser: Vault User
    :param vaultPwd: Vault Users password
    :param vaultClientId: client ID to track REST API Calls in logs

    :return: sessionId.
    """
    sessionId = ''
    authUrl = baseUrl + 'auth'
    params = {
        'username': vaultUser,
        'password': vaultPwd,
        'client_id': vaultClientId
    }
    responseStr = requests.post(url=authUrl, params=params)
    response = json.loads(responseStr.content)
    if response['responseStatus'] != 'SUCCESS':
        print('HTTP POST request to Vault\n' +
                    'ERROR while posting to URL "' + authUrl + '": ' +
                    response['errors'][0]['message'])
    else:
        sessionId = response['sessionId']
        print("Logged in using sessionId: " + sessionId)
    return sessionId