-
Notifications
You must be signed in to change notification settings - Fork 339
Auto init #105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Auto init #105
Changes from all commits
353aa0d
2c6f3ad
f06d5e1
f05ee27
caedea9
fb63e16
4159bbc
657e0bd
3432c34
1c524ec
fb920fc
a07b216
9eb492a
0f44837
44c1efa
284f7d7
417e062
c3c54be
e20592d
58caa4b
140461c
9b59bef
2bc327d
ade3983
b07314d
10747d4
2837e41
12929d0
48650dd
d85801b
3b69a8b
a4002aa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
|
||
"""Firebase Admin SDK for Python.""" | ||
import datetime | ||
import json | ||
import os | ||
import threading | ||
|
||
|
@@ -31,7 +32,9 @@ | |
_clock = datetime.datetime.utcnow | ||
|
||
_DEFAULT_APP_NAME = '[DEFAULT]' | ||
|
||
_FIREBASE_CONFIG_ENV_VAR = 'FIREBASE_CONFIG' | ||
_CONFIG_VALID_KEYS = ['databaseAuthVariableOverride', 'databaseURL', 'httpTimeout', 'projectId', | ||
'storageBucket'] | ||
|
||
def initialize_app(credential=None, options=None, name=_DEFAULT_APP_NAME): | ||
"""Initializes and returns a new App instance. | ||
|
@@ -46,10 +49,14 @@ def initialize_app(credential=None, options=None, name=_DEFAULT_APP_NAME): | |
credential: A credential object used to initialize the SDK (optional). If none is provided, | ||
Google Application Default Credentials are used. | ||
options: A dictionary of configuration options (optional). Supported options include | ||
``databaseURL``, ``storageBucket`` and ``httpTimeout``. If ``httpTimeout`` is not set, | ||
HTTP connections initiated by client modules such as ``db`` will not time out. | ||
``databaseURL``, ``storageBucket``, ``projectId``, ``databaseAuthVariableOverride`` | ||
and ``httpTimeout``. If ``httpTimeout`` is not set, HTTP connections initiated by client | ||
modules such as ``db`` will not time out. | ||
If options are not provided an attempt is made to load the options from the environment. | ||
This is done by looking up the ``FIREBASE_CONFIG`` environment variable. If the value of | ||
the variable starts with ``"{"``, it is parsed as a JSON object. Otherwise it is treated | ||
as a file name and the JSON content is read from the corresponding file. | ||
name: Name of the app (optional). | ||
|
||
Returns: | ||
App: A newly initialized instance of App. | ||
|
||
|
@@ -145,7 +152,8 @@ class _AppOptions(object): | |
|
||
def __init__(self, options): | ||
if options is None: | ||
options = {} | ||
options = self._load_from_environment() | ||
|
||
if not isinstance(options, dict): | ||
raise ValueError('Illegal Firebase app options type: {0}. Options ' | ||
'must be a dictionary.'.format(type(options))) | ||
|
@@ -155,6 +163,30 @@ def get(self, key, default=None): | |
"""Returns the option identified by the provided key.""" | ||
return self._options.get(key, default) | ||
|
||
def _load_from_environment(self): | ||
"""Invoked when no options are passed to __init__, loads options from FIREBASE_CONFIG. | ||
|
||
If the value of the FIREBASE_CONFIG environment variable starts with "{" an attempt is made | ||
to parse it as a JSON object, otherwise it is assumed to be pointing to a JSON file. | ||
""" | ||
|
||
config_file = os.getenv(_FIREBASE_CONFIG_ENV_VAR) | ||
if not config_file: | ||
return {} | ||
if config_file.startswith('{'): | ||
json_str = config_file | ||
else: | ||
try: | ||
with open(config_file, 'r') as json_file: | ||
json_str = json_file.read() | ||
except Exception as err: | ||
raise ValueError('Unable to read file {}. {}'.format(config_file, err)) | ||
try: | ||
json_data = json.loads(json_str) | ||
except Exception as err: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we not handle this exception, and just let it be thrown? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can, |
||
raise ValueError('JSON string "{0}" is not valid json. {1}'.format(json_str, err)) | ||
return {k: v for k, v in json_data.items() if k in _CONFIG_VALID_KEYS} | ||
|
||
|
||
class App(object): | ||
"""The entry point for Firebase Python SDK. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"databaseAuthVariableOverride": {"some_key": "some_val"}, | ||
"databaseURL": "https://hipster-chat.firebaseio.mock", | ||
"projectId": "hipster-chat-mock", | ||
"storageBucket": "hipster-chat.appspot.mock" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
baaaaad |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"databaseUrrrrL": "https://hipster-chat.firebaseio.mock", | ||
"projectId": "hipster-chat-mock" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"databaseURL": "https://hipster-chat.firebaseio.mock", | ||
"projectId": "hipster-chat-mock" | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be part of the method description, rather than the argument description.