import json
import os
import boto3

def handler(event, context):

    id = event["pathParameters"]["id"]

    client = boto3.client('dynamodb')

    response = client.get_item(
        TableName = os.environ['TABLE_NAME'],
        Key = {'id': {'S': id} }
    )
    item = response.get("Item", None)

    if (item):
        """ convert ddb json data into a dict """
        data = unmarshal_dynamodb_json(item)
        response = {
            "statusCode": 200,
            "body": json.dumps(data)
        }
    else:
        response = {
            "statusCode": 404
        }

    return response

def unmarshal_dynamodb_json(node):
    data = dict({})
    data['M'] = node
    return _unmarshal_value(data)


def _unmarshal_value(node):
    if type(node) is not dict:
        return node

    for key, value in node.items():
        # S – String - return string
        # N – Number - return int or float (if includes '.')
        # B – Binary - not handled
        # BOOL – Boolean - return Bool
        # NULL – Null - return None
        # M – Map - return a dict
        # L – List - return a list
        # SS – String Set - not handled
        # NN – Number Set - not handled
        # BB – Binary Set - not handled
        key = key.lower()
        if key == 'bool':
            return value
        if key == 'null':
            return None
        if key == 's':
            return value
        if key == 'n':
            if '.' in str(value):
                return float(value)
            return int(value)
        if key in ['m', 'l']:
            if key == 'm':
                data = {}
                for key1, value1 in value.items():
                    if key1.lower() == 'l':
                        data = [_unmarshal_value(n) for n in value1]
                    else:
                        if type(value1) is not dict:
                            return _unmarshal_value(value)
                        data[key1] = _unmarshal_value(value1)
                return data
            data = []
            for item in value:
                data.append(_unmarshal_value(item))
            return data