Last active
February 22, 2020 10:39
-
-
Save siakon89/80f0470effa80960446cb160d079ca80 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from chalice import Chalice | |
import boto3 | |
from boto3.dynamodb.conditions import Key | |
app = Chalice(app_name='blog-demo') | |
dynamodb = boto3.resource("dynamodb") | |
table = dynamodb.Table('blog-demo-1') | |
@app.route('/', methods=['GET']) | |
def index(): | |
response = table.scan() | |
data = response.get('Items', None) | |
return {'data': data} | |
@app.route('/item/{id}', methods=['GET']) | |
def item_get(id): | |
response = table.query( | |
KeyConditionExpression=Key("id").eq(id) | |
) | |
data = response.get('Items', None) | |
return {'data': data} | |
@app.route('/item', methods=['POST']) | |
def item_set(): | |
data = app.current_request.json_body | |
try: | |
table.put_item(Item={ | |
"id": data['id'], | |
"text": data['text'] | |
}) | |
return {'message': 'ok', 'status': 201, "id": data['id']} | |
except Exception as e: | |
return {'message': str(e)} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment