-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
105 lines (78 loc) · 2.23 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
import pprint
from proto import fields
import web
import json
import logging
import urllib
import requests
import io
import os
import os.path
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
import google.auth
urls = (
'/notes(.*)', 'notes',
'/', 'openapispec'
)
app = web.application(urls, globals())
if not firebase_admin._apps:
cred = credentials.ApplicationDefault()
firebase_admin.initialize_app(cred, {
'projectId': cred.project_id,
})
class notes:
db = firestore.client()
# Returns all of the notes
def GET(self, id):
new_result = {}
if len(id) == 0:
forms_ref = self.db.collection('notes')
forms = forms_ref.stream()
new_result = {
"notes": []
}
for form in forms:
new_result["notes"].append(form.to_dict())
else:
doc_ref = self.db.collection('notes').document(id[1:])
doc = doc_ref.get()
new_result = doc.to_dict()
if new_result:
web.header('Content-Type', 'application/json')
return json.dumps(new_result)
else:
return web.notfound("Not found")
# Posts a new note to be stored
def POST(self, name):
data = json.loads(web.data())
logging.info(json.dumps(data))
self.db.collection('notes').document(data["id"]).set(data)
pprint.pprint(data)
web.header('Content-Type', 'application/json')
return json.dumps(data)
# Puts a note to be updated
def PUT(self, name):
data = json.loads(web.data())
logging.info(json.dumps(data))
self.db.collection('notes').document(data["id"]).set(data)
pprint.pprint(data)
web.header('Content-Type', 'application/json')
return json.dumps(data)
# Delets a note
def DELETE(self, id):
if id:
self.db.collection(u'notes').document(id[1:]).delete()
return "200 OK"
# Returns the OpenAPI spec, filled in with the current server
class openapispec:
#Returns the OpenAPI spec, filled in with the current server
def GET(self):
f = open("apispec.yaml", "r")
spec = f.read()
spec = spec.replace("SERVER_URL", web.ctx.home.replace("http://", "https://"))
web.header('Content-Type', 'text/plain;charset=UTF-8')
return spec
if __name__ == "__main__":
app.run()