import requests import argparse def get_hostname(): parser = argparse.ArgumentParser( usage=('Call this script with your tenant hostname.\n' 'E.g. python demo_script.py jdoe-configeditor.co.sandbox.socotra.com')) parser.add_argument('hostname', help='The url of your tenant.') args = parser.parse_args() return args.hostname def create_session_and_login(hostname): # Login as a employee of your tenant username = 'alice.lee' password = 'socotra' # Create a session with Socotra session = requests.Session() response = session.post( 'https://api.sandbox.socotra.com/account/authenticate', json={'username': username, 'password': password, 'hostName': hostname}) # Save the authorization token for use in future requests authorization_token = response.json()['authorizationToken'] session.headers.update({'Authorization': authorization_token}) return session def create_policyholder(session): # Create a Policyholder policyholder_values = {'first_name': 'John', 'last_name': 'Smith', 'date_of_birth': '1987-10-03', 'policyholder_id': 'placeholder'} response = session.post( 'https://api.sandbox.socotra.com/policyholder/v2/create', json={'values': policyholder_values}) return response.json() def create_exposure(): # Create an exposure with two perils exposure_values = {'vehicle_type': 'Car', 'make': 'Audi', 'model': 'A4', 'year': '2012', 'vehicle_value': '123', 'license_plate': '123456', } exposure = {'exposureName': 'vehicle', 'fieldValues': exposure_values, 'perils': [{'name': 'bodily_injury'}, {'name': 'third_party_liability'}] } return exposure def create_policy(session, policyholder_information): # Create a policy using the exposure referenced above policyholder_locator = policyholder_information['locator'] policy_values = {'channel': 'Direct'} exposures = [create_exposure()] policy = { "policyholderLocator": policyholder_locator, "productName": 'simple_auto', "fieldValues": policy_values, "exposures": exposures } response = session.post('https://api.sandbox.socotra.com/policy', json=policy) policy_information = response.json() return policy_information def get_and_print_policy(session, policy_information): # Use the policy locator to find the policy in Socotra policy_locator = policy_information['locator'] response = session.get( 'https://api.sandbox.socotra.com/policy/' + policy_locator) found_policy = response.json() policy_id = found_policy['displayId'] number_policy_invoices = len(found_policy['invoices']) print 'Found Policy id: %s' % policy_id print 'Number of invoices on Policy: %s' % number_policy_invoices def main(): hostname = get_hostname() session = create_session_and_login(hostname) policyholder_information = create_policyholder(session) policy_information = create_policy(session, policyholder_information) get_and_print_policy(session, policy_information) if __name__ == '__main__': main()