This sample shows the code to make a call to the iMedidata API endpoint.
Note the extracted method generate_signer for creating a MAuth Request signer using either the key text or a reference to a Private Key file.
import requests
from mauth_client.requests_mauth import MAuth
def generate_signer(app_uuid, private_key_file=None, private_key_string=None):
"""
Generate a MAUTH instance
:param str app_uuid: Application UUID
:param str private_key_file: name of the Private Key File
:param str private_key_string: content of the Private Key String
:return:
"""
if not private_key_string:
auth = MAuth(app_uuid, open(private_key_file, "r").read())
else:
auth = MAuth(app_uuid, private_key_string)
return auth
def get_user_details(configuration, user_uuid):
"""
Get the User details from the iMedidata API
:param dict configuration: Configuration Set
:param str user_uuid: UUID for user
"""
mauth_signer = generate_signer(**configuration)
base_url = "https://www.imedidata.com"
api_path = f"api/v2/users/{user_uuid}.json"
full_url = base_url + "/" + api_path
response = requests.get(full_url, auth=mauth_signer)
if response.status_code == 200:
return response.json()
else:
print(f"Looking up {user_uuid} failed with code {response.status_code}")
return {}
In this example we use the MAuth signer to access the underlying Countries API endpoint.
import requests
from mauth_client.requests_mauth import MAuth
def generate_signer(app_uuid, private_key_file=None, private_key_string=None):
"""
Generate a MAUTH instance
:param str app_uuid: Application UUID
:param str private_key_file: name of the Private Key File
:param str private_key_string: content of the Private Key String
:return:
"""
if not private_key_string:
auth = MAuth(app_uuid, open(private_key_file, "r").read())
else:
auth = MAuth(app_uuid, private_key_string)
return auth
def get_countries(configuration):
"""
Get the list of countries from the API GW for a specific API version
:param dict configuration: a configuration dictionary
"""
full_url = "https://api.mdsol.com/countries"
headers = {"Accept": "application/json", "Mcc-Version": "v2019-03-22"}
mauth_signer = generate_signer(**configuration)
session = requests.Session()
session.auth = mauth_signer
session.headers = headers
response = session.get(full_url)
if response.status_code == 200:
return response.json()
else:
print(f"Accessing the countries endpoint failed with error: {response.status_code}")
return {}
In this example we use the lambda authenticator to authenticate incoming requests.
from mauth_client.lambda_authenticator import LambdaAuthenticator
lambda_authenticator = LambdaAuthenticator(method, url, headers, body)
authentic, status_code, message = lambda_authenticator.is_authentic()
app_uuid = lambda_authenticator.get_app_uuid()