def call_getRecommendation()

in static/Reliability/300_Health_Checks_and_Dependencies/Code/Python/server_basic.py [0:0]


def call_getRecommendation(region, user_id):

    # It would be more efficient to create the clients once on init
    # But in the lab we change permissions on the EC2 instance
    # and this way we are sure to pick up the new credentials
    session = boto3.Session()

    # Setup client for DDB -- we will use this to mock a service dependency
    ddb_client = session.client('dynamodb', region)

    # Setup client for SSM -- we use this for parameters used as a 
    # enable/disable switch in the lab
    ssm_client = session.client('ssm', region_name=region)

    # Configure if mocked recomendation service is enabled or if it should simulate
    # disabled (unreachable)
    value = ssm_client.get_parameter(Name='RecommendationServiceEnabled')
    dependency_enabled = value['Parameter']['Value'] == "true"
    table_name = "RecommendationService" if dependency_enabled else "dependencyShouldFail"

    # Call the RecommendationService 
    # (actually just a simply lookup in a DynamoDB table, which is acting as a mock for the RecommendationService)
    response = ddb_client.get_item(
        TableName=table_name,
        Key={
            'ServiceAPI': {
                'S': 'getRecommendation',
            },
            'UserID': {
                'N': user_id,
            }
        }
    )

    return response