forked from GoogleCloudPlatform/python-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
133 lines (104 loc) · 3.54 KB
/
Copy pathmain.py
File metadata and controls
133 lines (104 loc) · 3.54 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
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
# Copyright 2016 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# [START app]
import logging
from flask import Flask, jsonify, request
import flask_cors
from google.appengine.ext import ndb
import google.auth.transport.requests
import google.oauth2.id_token
import requests_toolbelt.adapters.appengine
# Use the App Engine Requests adapter. This makes sure that Requests uses
# URLFetch.
requests_toolbelt.adapters.appengine.monkeypatch()
HTTP_REQUEST = google.auth.transport.requests.Request()
app = Flask(__name__)
flask_cors.CORS(app)
# [START note]
class Note(ndb.Model):
"""NDB model class for a user's note.
Key is user id from decrypted token.
"""
friendly_id = ndb.StringProperty()
message = ndb.TextProperty()
created = ndb.DateTimeProperty(auto_now_add=True)
# [END note]
# [START query_database]
def query_database(user_id):
"""Fetches all notes associated with user_id.
Notes are ordered them by date created, with most recent note added
first.
"""
ancestor_key = ndb.Key(Note, user_id)
query = Note.query(ancestor=ancestor_key).order(-Note.created)
notes = query.fetch()
note_messages = []
for note in notes:
note_messages.append({
'friendly_id': note.friendly_id,
'message': note.message,
'created': note.created
})
return note_messages
# [END query_database]
# [START list_notes]
@app.route('/notes', methods=['GET'])
def list_notes():
"""Returns a list of notes added by the current Firebase user."""
# Verify Firebase auth.
# [START verify_token]
id_token = request.headers['Authorization'].split(' ').pop()
claims = google.oauth2.id_token.verify_firebase_token(
id_token, HTTP_REQUEST)
if not claims:
return 'Unauthorized', 401
# [END verify_token]
notes = query_database(claims['sub'])
return jsonify(notes)
# [END list_notes]
# [START add_note]
@app.route('/notes', methods=['POST', 'PUT'])
def add_note():
"""
Adds a note to the user's notebook. The request should be in this format:
{
"message": "note message."
}
"""
# Verify Firebase auth.
id_token = request.headers['Authorization'].split(' ').pop()
claims = google.oauth2.id_token.verify_firebase_token(
id_token, HTTP_REQUEST)
if not claims:
return 'Unauthorized', 401
# [START create_entity]
data = request.get_json()
# Populates note properties according to the model,
# with the user ID as the key name.
note = Note(
parent=ndb.Key(Note, claims['sub']),
message=data['message'])
# Some providers do not provide one of these so either can be used.
note.friendly_id = claims.get('name', claims.get('email', 'Unknown'))
# [END create_entity]
# Stores note in database.
note.put()
return 'OK', 200
# [END add_note]
@app.errorhandler(500)
def server_error(e):
# Log the error and stacktrace.
logging.exception('An error occurred during a request.')
return 'An internal error occurred.', 500
# [END app]