|
15 | 15 | """Tic Tac Toe with the Firebase API""" |
16 | 16 |
|
17 | 17 | import base64 |
| 18 | +try: |
| 19 | + from functools import lru_cache |
| 20 | +except ImportError: |
| 21 | + from functools32 import lru_cache |
18 | 22 | import json |
19 | 23 | import os |
20 | 24 | import re |
|
51 | 55 | app = flask.Flask(__name__) |
52 | 56 |
|
53 | 57 |
|
54 | | -def _get_firebase_db_url(_memo={}): |
| 58 | +# Memoize the value, to avoid parsing the code snippet every time |
| 59 | +@lru_cache() |
| 60 | +def _get_firebase_db_url(): |
55 | 61 | """Grabs the databaseURL from the Firebase config snippet. Regex looks |
56 | 62 | scary, but all it is doing is pulling the 'databaseURL' field from the |
57 | 63 | Firebase javascript snippet""" |
58 | | - if 'dburl' not in _memo: |
59 | | - # Memoize the value, to avoid parsing the code snippet every time |
60 | | - regex = re.compile(r'\bdatabaseURL\b.*?["\']([^"\']+)') |
61 | | - cwd = os.path.dirname(__file__) |
| 64 | + regex = re.compile(r'\bdatabaseURL\b.*?["\']([^"\']+)') |
| 65 | + cwd = os.path.dirname(__file__) |
| 66 | + try: |
62 | 67 | with open(os.path.join(cwd, 'templates', _FIREBASE_CONFIG)) as f: |
63 | 68 | url = next(regex.search(line) for line in f if regex.search(line)) |
64 | | - _memo['dburl'] = url.group(1) |
65 | | - return _memo['dburl'] |
| 69 | + except StopIteration: |
| 70 | + raise ValueError( |
| 71 | + 'Error parsing databaseURL. Please copy Firebase web snippet ' |
| 72 | + 'into templates/{}'.format(_FIREBASE_CONFIG)) |
| 73 | + return url.group(1) |
66 | 74 |
|
67 | 75 |
|
| 76 | +# Memoize the authorized http, to avoid fetching new access tokens |
| 77 | +@lru_cache() |
68 | 78 | # [START authed_http] |
69 | | -def _get_http(_memo={}): |
| 79 | +def _get_http(): |
70 | 80 | """Provides an authed http object.""" |
71 | | - if 'http' not in _memo: |
72 | | - # Memoize the authorized http, to avoid fetching new access tokens |
73 | | - http = httplib2.Http() |
74 | | - # Use application default credentials to make the Firebase calls |
75 | | - # https://firebase.google.com/docs/reference/rest/database/user-auth |
76 | | - creds = GoogleCredentials.get_application_default().create_scoped( |
77 | | - _FIREBASE_SCOPES) |
78 | | - creds.authorize(http) |
79 | | - _memo['http'] = http |
80 | | - return _memo['http'] |
| 81 | + http = httplib2.Http() |
| 82 | + # Use application default credentials to make the Firebase calls |
| 83 | + # https://firebase.google.com/docs/reference/rest/database/user-auth |
| 84 | + creds = GoogleCredentials.get_application_default().create_scoped( |
| 85 | + _FIREBASE_SCOPES) |
| 86 | + creds.authorize(http) |
| 87 | + return http |
81 | 88 | # [END authed_http] |
82 | 89 |
|
83 | 90 |
|
|
0 commit comments