|
| 1 | +# Copyright 2016 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import logging |
| 16 | +import os |
| 17 | + |
| 18 | +from flask import Flask, jsonify, request |
| 19 | +import flask_cors |
| 20 | +from google.appengine.ext import ndb |
| 21 | +import google.auth.transport.requests |
| 22 | +import google.oauth2.id_token |
| 23 | +import requests_toolbelt.adapters.appengine |
| 24 | + |
| 25 | +# Use the App Engine Requests adapter. This makes sure that Requests uses |
| 26 | +# URLFetch. |
| 27 | +requests_toolbelt.adapters.appengine.monkeypatch() |
| 28 | +HTTP_REQUEST = google.auth.transport.requests.Request() |
| 29 | + |
| 30 | +app = Flask(__name__) |
| 31 | +flask_cors.CORS(app) |
| 32 | + |
| 33 | + |
| 34 | +class Note(ndb.Model): |
| 35 | + """NDB model class for a user's note. |
| 36 | +
|
| 37 | + Key is user id from decrypted token. |
| 38 | + """ |
| 39 | + |
| 40 | + friendly_id = ndb.StringProperty() |
| 41 | + message = ndb.TextProperty() |
| 42 | + created = ndb.DateTimeProperty(auto_now_add=True) |
| 43 | + |
| 44 | + |
| 45 | +# [START gae_python_query_database] |
| 46 | +# This code is for illustration purposes only. |
| 47 | + |
| 48 | +def query_database(user_id): |
| 49 | + """Fetches all notes associated with user_id. |
| 50 | +
|
| 51 | + Notes are ordered them by date created, with most recent note added |
| 52 | + first. |
| 53 | + """ |
| 54 | + ancestor_key = ndb.Key(Note, user_id) |
| 55 | + query = Note.query(ancestor=ancestor_key).order(-Note.created) |
| 56 | + notes = query.fetch() |
| 57 | + |
| 58 | + note_messages = [] |
| 59 | + |
| 60 | + for note in notes: |
| 61 | + note_messages.append( |
| 62 | + { |
| 63 | + "friendly_id": note.friendly_id, |
| 64 | + "message": note.message, |
| 65 | + "created": note.created, |
| 66 | + } |
| 67 | + ) |
| 68 | + |
| 69 | + return note_messages |
| 70 | + |
| 71 | + |
| 72 | +# [END gae_python_query_database] |
| 73 | + |
| 74 | + |
| 75 | +@app.route("/notes", methods=["GET"]) |
| 76 | +def list_notes(): |
| 77 | + """Returns a list of notes added by the current Firebase user.""" |
| 78 | + |
| 79 | + # Verify Firebase auth. |
| 80 | + # [START gae_python_verify_token] |
| 81 | + # This code is for illustration purposes only. |
| 82 | + |
| 83 | + id_token = request.headers["Authorization"].split(" ").pop() |
| 84 | + claims = google.oauth2.id_token.verify_firebase_token( |
| 85 | + id_token, HTTP_REQUEST, audience=os.environ.get("GOOGLE_CLOUD_PROJECT") |
| 86 | + ) |
| 87 | + if not claims: |
| 88 | + return "Unauthorized", 401 |
| 89 | + # [END gae_python_verify_token] |
| 90 | + |
| 91 | + notes = query_database(claims["sub"]) |
| 92 | + |
| 93 | + return jsonify(notes) |
| 94 | + |
| 95 | + |
| 96 | +@app.route("/notes", methods=["POST", "PUT"]) |
| 97 | +def add_note(): |
| 98 | + """ |
| 99 | + Adds a note to the user's notebook. The request should be in this format: |
| 100 | +
|
| 101 | + { |
| 102 | + "message": "note message." |
| 103 | + } |
| 104 | + """ |
| 105 | + |
| 106 | + # Verify Firebase auth. |
| 107 | + id_token = request.headers["Authorization"].split(" ").pop() |
| 108 | + claims = google.oauth2.id_token.verify_firebase_token( |
| 109 | + id_token, HTTP_REQUEST, audience=os.environ.get("GOOGLE_CLOUD_PROJECT") |
| 110 | + ) |
| 111 | + if not claims: |
| 112 | + return "Unauthorized", 401 |
| 113 | + |
| 114 | + # [START gae_python_create_entity] |
| 115 | + # This code is for illustration purposes only. |
| 116 | + |
| 117 | + data = request.get_json() |
| 118 | + |
| 119 | + # Populates note properties according to the model, |
| 120 | + # with the user ID as the key name. |
| 121 | + note = Note(parent=ndb.Key(Note, claims["sub"]), message=data["message"]) |
| 122 | + |
| 123 | + # Some providers do not provide one of these so either can be used. |
| 124 | + note.friendly_id = claims.get("name", claims.get("email", "Unknown")) |
| 125 | + # [END gae_python_create_entity] |
| 126 | + |
| 127 | + # Stores note in database. |
| 128 | + note.put() |
| 129 | + |
| 130 | + return "OK", 200 |
| 131 | + |
| 132 | + |
| 133 | +@app.errorhandler(500) |
| 134 | +def server_error(e): |
| 135 | + # Log the error and stacktrace. |
| 136 | + logging.exception("An error occurred during a request.") |
| 137 | + return "An internal error occurred.", 500 |
0 commit comments