Exemple #1
0
def update(event, context):
    """Update item in the collection."""

    try:

        # Determine if required env var for region is present.
        val.check_region()

        # Determine if required env var for DynamoDB table is present.
        val.check_dynamodb()

        # Check for the url {id}.
        note_id = val.check_id(event)

        # Determine if required property body is present.
        val.check_body(event)

        # Attempt to parse JSON.
        data = val.check_json(event)

        # Determine if required properties are present.
        val.check_props(data)

        # Set up resource and environment. This is where we keep
        # *aaS provider resources away from biz logic.
        region = os.environ["AWS_DEFAULT_REGION"]
        table = os.environ["DYNAMODB_TABLE"]

        # Determine which DynamoDB host we need (local/remote)?
        host = val.check_dynamodb_host()
        conn = boto3.resource("dynamodb", region, endpoint_url=host)
        conn_table = conn.Table(table)

        # Build our model and update.
        note = NoteModel(conn_table)
        item = note.update(note_id, data)

        logger.info("Note updated: {}".format(item))
        return respond(200, item)

    except ex.AwsRegionNotSetException as exc:
        return respond(500, {"error": str(exc)})

    except ex.DynamoDbTableNotSetException as exc:
        return respond(500, {"error": str(exc)})

    except ex.RequestUrlIdNotSetException as exc:
        return respond(400, {"error": str(exc)})

    except ex.RequestBodyNotSetException as exc:
        return respond(400, {"error": str(exc)})

    except ex.RequestBodyNotJsonException as exc:
        return respond(400, {"error": str(exc)})

    except ex.RequiredPropertiesNotSetException as exc:
        return respond(400, {"error": str(exc)})

    except Exception as exc:
        return respond(500, {"error": str(exc)})
Exemple #2
0
def search_by_notebook(event, context):
    """Return the collection of items based on query."""

    try:

        # Determine if required env var for region is present.
        val.check_region()

        # Determine if required env var for DynamoDB table is present.
        val.check_dynamodb()

        # Check for the url {id}.
        notebook = val.check_id(event)

        # Set up resource and environment. This is where we keep
        # *aaS provider resources away from biz logic.
        region = os.environ["AWS_DEFAULT_REGION"]
        table = os.environ["DYNAMODB_TABLE"]

        # Determine which DynamoDB host we need (local/remote)?
        host = val.check_dynamodb_host()
        conn = boto3.resource("dynamodb", region, endpoint_url=host)
        conn_table = conn.Table(table)

        # Build our model and read.
        note = NoteModel(conn_table)
        items = note.search_by_notebook(notebook)

        logger.info("Notes for notebook found: {} [{}]".format(
            notebook, len(items)))
        return respond(200, items)

    except ex.AwsRegionNotSetException as exc:
        return respond(500, {"error": str(exc)})

    except ex.DynamoDbTableNotSetException as exc:
        return respond(500, {"error": str(exc)})

    except ex.RequestUrlIdNotSetException as exc:
        return respond(400, {"error": str(exc)})

    except Exception as exc:
        return respond(500, {"error": str(exc)})
Exemple #3
0
def test_beacon_responds_with_valid_payload_when_valid_data(monkeypatch):
    res = respond(200, {"test": "dictionary"})

    assert "isBase64Encoded" in res and isinstance(res["isBase64Encoded"],
                                                   bool)

    assert "statusCode" in res and isinstance(res["statusCode"], int)
    assert res["statusCode"] == 200

    assert "headers" in res and isinstance(res["headers"], dict)
    assert "Content-Type" in res["headers"] and res["headers"][
        "Content-Type"] == "application/json"

    assert "body" in res and isinstance(res["body"], str)
    assert json.loads(res["body"]) == {"test": "dictionary"}
Exemple #4
0
def test_beacon_responds_with_valid_payload_when_invalid_data(monkeypatch):
    with pytest.raises(TypeError) as exc:
        respond(200, bytes("bad dictionary", "utf-8"))

    assert "is not JSON serializable" in str(exc.value)