-
Notifications
You must be signed in to change notification settings - Fork 8
/
app.py
189 lines (162 loc) · 6.82 KB
/
app.py
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
from flask import Flask
from flask import request
from flask import make_response
import os
import boto3
from boto3.dynamodb.conditions import Attr
import botocore.exceptions
# Variables de entorno
books_table = os.environ["DYNAMODB_TABLE"]
# Inicializando servicios AWS
dynamodb = boto3.resource("dynamodb")
table = dynamodb.Table(books_table)
# Inicializa Flask
app = Flask(__name__)
# Health check
@app.route("/")
def health_check():
return {"status": "todo ok"}
@app.route("/resources/books", methods=("GET", "POST", "PUT"))
def books_resource():
if request.method == "POST":
try:
data = request.form.to_dict()
print(data)
if data["id"] and data["name"]:
try:
put_item_response = table.put_item(
Item=data, ConditionExpression=Attr("id").not_exists()
)
body = {
"message": "Book successfully created with id {}".format(
data["id"]
)
}
response = make_response(body, 201)
response.headers["Content-Type"] = "application/json"
return response
except botocore.exceptions.ClientError:
body = {"error": "The specified id already exists"}
response = make_response(body, 400)
response.headers["Content-Type"] = "application/json"
return response
except Exception as e:
print(str(e))
body = {"error": "There was an error creating the book"}
response = make_response(body, 500)
response.headers["Content-Type"] = "application/json"
return response
except Exception as e:
print(str(e))
body = {"error": "You must provide a valid id and name"}
response = make_response(body, 400)
response.headers["Content-Type"] = "application/json"
return response
if request.method == "GET":
id = request.args.get("id")
author = request.args.get("author")
if id:
try:
get_item_response = table.get_item(Key={"id": id})
try:
body = {"book": get_item_response["Item"]}
response = make_response(body, 200)
response.headers["Content-Type"] = "application/json"
return response
except Exception as e:
print(str(e))
body = {"error": "Book with id {} not found.".format(id)}
response = make_response(body, 404)
response.headers["Content-Type"] = "application/json"
return response
except Exception as e:
print(str(e))
body = {"error": str(e)}
response = make_response(body, 502)
response.headers["Content-Type"] = "application/json"
return response
elif author:
try:
scan_response = table.scan(FilterExpression=Attr("author").eq(author))
body = {"books": scan_response["Items"]}
response = make_response(body, 200)
response.headers["Content-Type"] = "application/json"
return response
except Exception as e:
print(str(e))
body = {"error": str(e)}
response = make_response(body, 502)
response.headers["Content-Type"] = "application/json"
return response
else:
try:
scan_response = table.scan()
body = {"books": scan_response["Items"]}
response = make_response(body, 200)
response.headers["Content-Type"] = "application/json"
return response
except Exception as e:
print(str(e))
body = {"error": str(e)}
response = make_response(body, 502)
response.headers["Content-Type"] = "application/json"
return response
if request.method == "PUT":
data = request.form.to_dict()
try:
if data["id"]:
try:
put_item_response = table.put_item(
Item=data, ConditionExpression=Attr("id").exists()
)
body = {
"message": "Book with id {} was successfully updated".format(
data["id"]
)
}
response = make_response(body, 200)
response.headers["Content-Type"] = "application/json"
return response
except botocore.exceptions.ClientError:
body = {"error": "The specified id does not exist"}
response = make_response(body, 404)
response.headers["Content-Type"] = "application/json"
return response
except Exception as e:
print(str(e))
body = {
"error": "There was an error updating the book. Please try again"
}
response = make_response(body, 500)
response.headers["Content-Type"] = "application/json"
return response
except Exception as e:
print(str(e))
body = {"error": "You must provide a valid id"}
response = make_response(body, 400)
response.headers["Content-Type"] = "application/json"
return response
@app.route("/resources/books/<id>", methods=["DELETE"])
def book_id_resource(id):
if request.method == "DELETE":
try:
delete_item_response = table.delete_item(
Key={"id": id}, ConditionExpression=Attr("id").exists()
)
body = {"message": "Book with id {} was successfully deleted".format(id)}
response = make_response(body, 200)
response.headers["Content-Type"] = "application/json"
return response
except botocore.exceptions.ClientError:
body = {"error": "Book with id {} not found.".format(id)}
response = make_response(body, 404)
response.headers["Content-Type"] = "application/json"
return response
except Exception as e:
print(str(e))
body = {"error": str(e)}
response = make_response(body, 502)
response.headers["Content-Type"] = "application/json"
return response
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0")