-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathToDoDynamo.py
More file actions
75 lines (66 loc) · 2.07 KB
/
ToDoDynamo.py
File metadata and controls
75 lines (66 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# Import ToDo Class
from ToDoSampleApp import ToDo
import boto3
import json
dynamodb = boto3.client('dynamodb')
class Dynamo:
def __init__(self, table_name, todo_item = None):
self.table_name = table_name
self.todo_item = todo_item
def checkTable(self):
try:
check_table = dynamodb.describe_table(
TableName=self.table_name)
return True
except:
return False
def createTable (self):
try:
table = dynamodb.create_table(
AttributeDefinitions=[
{
'AttributeName': 'Title',
'AttributeType': 'S'
}
],
TableName=self.table_name,
KeySchema=[
{
'AttributeName': 'Title',
'KeyType': 'HASH'
},
],
ProvisionedThroughput={
'ReadCapacityUnits': 10,
'WriteCapacityUnits': 10
}
)
return (table)
except Exception as err:
print (err)
def putItem(self):
print ("Adding following new item to dynamoDB table")
print (self.todo_item.title)
item = {
"Title": self.todo_item.title,
"date": self.todo_item.date,
"time": self.todo_item.time,
"description": self.todo_item.description
}
item = json.dumps(item)
putObject = dynamodb.put_item(
TableName=self.table_name,
Item=json.loads(item),
ReturnValues='UPDATED_NEW',
ReturnConsumedCapacity= 'TOTAL',
ReturnItemCollectionMetrics='SIZE'
)
print("PutItem succeeded:")
return (putObject)
def getItem(table_name, primay_key):
getObject = table.get_item(
Key={
'Title': primay_key
}
)
return getObject