コード例 #1
0
ファイル: item_lambdas.py プロジェクト: c-rodwell/lambda
def delete_item(event, context):
    try:
        userId, itemId = json_func.get_body_args(event, {
            'userId': str,
            'itemId': int
        })
        item_table_name = os.environ['ITEM_TABLE']
        client = boto3.client('dynamodb')
        resp = client.delete_item(
            TableName=item_table_name,
            Key={
                'userId': {
                    'S': userId,
                },
                'itemId': {
                    'N': str(itemId)  #should it be entered as string type?
                }
            },
            ReturnValues='ALL_OLD')
        if "Attributes" in resp:  #deleted something
            return {
                "statusCode": 200,
                "body": "deleted item: " + json.dumps(resp['Attributes'])
            }
        else:
            return {
                "statusCode":
                404,
                "body":
                "item with userId = " + userId + ", itemId = " + str(itemId) +
                " does not exist."
            }
    except Exception as err:
        return json_func.errorMessage(err)
コード例 #2
0
ファイル: item_lambdas.py プロジェクト: c-rodwell/lambda
def get_user_items(event, context):
    try:
        [userId] = json_func.get_querystring_args(event, {'userId': str})
        table = itemTableResource()
        resp = table.query(KeyConditionExpression=Key('userId').eq(userId))
        return {
            "statusCode": 200,
            "body": json.dumps(resp, cls=json_func.DecimalEncoder)
        }
    except Exception as err:
        return json_func.errorMessage(err)
コード例 #3
0
ファイル: hello.py プロジェクト: c-rodwell/lambda
def log_info(event, context):
    try:
        client = boto3.client('cognito-idp')
        token = event['headers']['Authorization']
        shorter_token = token[7:]  #chop off the "Bearer "
        response = client.get_user(AccessToken=shorter_token)
        username = response['Username']

        #logging statements
        print('## ENVIRONMENT VARIABLES')
        print(os.environ)
        print('## EVENT')
        print(event)
        print('## USERNAME')
        print(username)
        print('## CONTEXT')
        print(context)
        print('## CONTEXT IDENTITY')
        print(context.identity)
        print('## COGNITO IDENTITY ID')
        print(context.identity.cognito_identity_id)
        print('## COGNITO IDENTITY POOL ID')
        print(context.identity.cognito_identity_pool_id)

        body = {
            "message":
            "here is the request you sent",
            "event":
            event,
            "token":
            token,
            "shorter_token":
            shorter_token,
            "username":
            username,
            "context":
            str(context),
            "context_identity":
            str(context.identity),
            "context_identity_id":
            str(context.identity.cognito_identity_id),
            "context_identity_pool_id":
            str(context.identity.cognito_identity_pool_id)
        }

        response = {"statusCode": 200, "body": json.dumps(body)}
        return response
    except Exception as err:
        return {
            "statusCode": 500,
            "body": json.dumps(json_func.errorMessage(err))
        }
コード例 #4
0
ファイル: user_lambdas.py プロジェクト: c-rodwell/lambda
def get_user(event, context):
    try:
        [userId] = json_func.get_querystring_args(event, {'userId': str})
        userInfo = existingUser(userId)
        if not userInfo:
            raise FileNotFoundError("user " + userId + " does not exist")

        return {
            "statusCode": 200,
            "body": json.dumps(userInfo, cls=json_func.DecimalEncoder)
        }
    except Exception as err:
        return json_func.errorMessage(err)
コード例 #5
0
def add_item(event, context):
    try:
        userId = json_func.get_username(event)
        [value] = json_func.get_body_args(event, {'value': str})
        itemNum = user_lambdas.nextItemNum(userId)
        table = itemTableResource()
        resp = table.put_item(Item={
            "userId": userId,
            "itemId": itemNum,
            "value": value
        })
        return {"statusCode": 201, "body": "added item"}
    except Exception as err:
        return json_func.errorMessage(err)
コード例 #6
0
def get_item(event, context):
    try:
        userId = json_func.get_username(event)
        [itemId] = json_func.get_querystring_args(event, {'itemId': int})
        table = itemTableResource()
        resp = table.get_item(Key={"userId": userId, "itemId": itemId})
        if 'Item' not in resp:
            raise FileNotFoundError("item with itemId = " + str(itemId) +
                                    " does not exist")
        return {
            "statusCode": 200,
            "body": json.dumps(resp['Item'], cls=json_func.DecimalEncoder)
        }
    except Exception as err:
        return json_func.errorMessage(err)
コード例 #7
0
ファイル: user_lambdas.py プロジェクト: c-rodwell/lambda
def create_user(event, context):
    try:
        userId, name = json_func.get_body_args(event, {
            'userId': str,
            'name': str
        })
        if existingUser(userId):
            return {
                "statusCode": 409,
                "body": "user with: id = " + userId + " already exists"
            }
        table = userTableResource()
        resp = table.put_item(Item={"userId": userId, "name": name})
        return {
            "statusCode": 201,
            "body": "created user: id = " + userId + ", name = " + name
        }
    except Exception as err:
        return json_func.errorMessage(err)
コード例 #8
0
ファイル: item_lambdas.py プロジェクト: c-rodwell/lambda
def edit_item_field(event, context):
    try:
        userId, itemId, attrName, attrValue = json_func.get_body_args(
            event, {
                'userId': str,
                'itemId': int,
                'attrName': str,
                'attrValue': str
            })
        if not existingItem(userId, itemId):
            return {
                "statusCode":
                404,
                "body":
                "item with userId = " + userId + ", itemId = " + str(itemId) +
                " does not exist."
            }
        editResp = itemTableResource().update_item(
            Key={
                "userId": userId,
                "itemId": itemId
            },
            ExpressionAttributeNames={
                "#attrName": attrName,
            },
            ExpressionAttributeValues={
                ":attrValue": attrValue,
            },
            UpdateExpression="SET #attrName = :attrValue",
        )

        #TODO check the edit response for success, or return the response
        return {
            "statusCode": 200,
            "body": "set attribute '" + attrName + "' to " + repr(attrValue)
        }
    except Exception as err:
        return json_func.errorMessage(err)