13
13
# limitations under the License.
14
14
15
15
"""Firebase Admin SDK for Python."""
16
+
16
17
import json
17
18
import os
18
19
import threading
19
- import typing
20
+ from typing import Any , Dict , Optional , TypeVar , overload
20
21
21
22
from google .auth .credentials import Credentials as GoogleAuthCredentials
22
23
from google .auth .exceptions import DefaultCredentialsError
23
24
from firebase_admin import credentials
24
25
from firebase_admin .__about__ import __version__
25
26
from firebase_admin import _typing
26
27
28
+ __all__ = (
29
+ 'App' ,
30
+ 'delete_app' ,
31
+ 'get_app' ,
32
+ 'initialize_app' ,
33
+ )
27
34
28
- _T = typing . TypeVar ('_T' )
35
+ _T = TypeVar ('_T' )
29
36
30
- _apps : typing . Dict [str , 'App' ] = {}
37
+ _apps : Dict [str , 'App' ] = {}
31
38
_apps_lock = threading .RLock ()
32
39
33
40
_DEFAULT_APP_NAME = '[DEFAULT]'
36
43
'storageBucket' ]
37
44
38
45
def initialize_app (
39
- credential : typing . Optional [_typing .CredentialLike ] = None ,
40
- options : typing . Optional [typing . Dict [str , typing . Any ]] = None ,
46
+ credential : Optional [_typing .CredentialLike ] = None ,
47
+ options : Optional [Dict [str , Any ]] = None ,
41
48
name : str = _DEFAULT_APP_NAME ,
42
49
) -> 'App' :
43
50
"""Initializes and returns a new App instance.
@@ -154,7 +161,7 @@ def get_app(name: str = _DEFAULT_APP_NAME) -> 'App':
154
161
class _AppOptions :
155
162
"""A collection of configuration options for an App."""
156
163
157
- def __init__ (self , options : typing . Optional [typing . Dict [str , typing . Any ]]) -> None :
164
+ def __init__ (self , options : Optional [Dict [str , Any ]]) -> None :
158
165
if options is None :
159
166
options = self ._load_from_environment ()
160
167
@@ -163,16 +170,16 @@ def __init__(self, options: typing.Optional[typing.Dict[str, typing.Any]]) -> No
163
170
'must be a dictionary.' .format (type (options )))
164
171
self ._options = options
165
172
166
- @typing . overload
167
- def get (self , key : str , default : None = None ) -> typing . Optional [typing . Any ]: ...
173
+ @overload
174
+ def get (self , key : str , default : None = None ) -> Optional [Any ]: ...
168
175
# possible issue: needs return Any | _T ?
169
- @typing . overload
176
+ @overload
170
177
def get (self , key : str , default : _T ) -> _T : ...
171
- def get (self , key : str , default : typing . Any = None ) -> typing . Optional [typing . Any ]:
178
+ def get (self , key : str , default : Any = None ) -> Optional [Any ]:
172
179
"""Returns the option identified by the provided key."""
173
180
return self ._options .get (key , default )
174
181
175
- def _load_from_environment (self ) -> typing . Dict [str , typing . Any ]:
182
+ def _load_from_environment (self ) -> Dict [str , Any ]:
176
183
"""Invoked when no options are passed to __init__, loads options from FIREBASE_CONFIG.
177
184
178
185
If the value of the FIREBASE_CONFIG environment variable starts with "{" an attempt is made
@@ -208,7 +215,7 @@ def __init__(
208
215
self ,
209
216
name : str ,
210
217
credential : _typing .CredentialLike ,
211
- options : typing . Optional [typing . Dict [str , typing . Any ]],
218
+ options : Optional [Dict [str , Any ]],
212
219
) -> None :
213
220
"""Constructs a new App using the provided name and options.
214
221
@@ -234,13 +241,13 @@ def __init__(
234
241
'with a valid credential instance.' )
235
242
self ._options = _AppOptions (options )
236
243
self ._lock = threading .RLock ()
237
- self ._services : typing . Optional [typing . Dict [str , typing . Any ]] = {}
244
+ self ._services : Optional [Dict [str , Any ]] = {}
238
245
239
246
App ._validate_project_id (self ._options .get ('projectId' ))
240
247
self ._project_id_initialized = False
241
248
242
249
@classmethod
243
- def _validate_project_id (cls , project_id : typing . Optional [str ]) -> None :
250
+ def _validate_project_id (cls , project_id : Optional [str ]) -> None :
244
251
if project_id is not None and not isinstance (project_id , str ):
245
252
raise ValueError (
246
253
'Invalid project ID: "{0}". project ID must be a string.' .format (project_id ))
@@ -258,13 +265,13 @@ def options(self) -> _AppOptions:
258
265
return self ._options
259
266
260
267
@property
261
- def project_id (self ) -> typing . Optional [str ]:
268
+ def project_id (self ) -> Optional [str ]:
262
269
if not self ._project_id_initialized :
263
270
self ._project_id = self ._lookup_project_id ()
264
271
self ._project_id_initialized = True
265
272
return self ._project_id
266
273
267
- def _lookup_project_id (self ) -> typing . Optional [str ]:
274
+ def _lookup_project_id (self ) -> Optional [str ]:
268
275
"""Looks up the Firebase project ID associated with an App.
269
276
270
277
If a ``projectId`` is specified in app options, it is returned. Then tries to
@@ -275,7 +282,7 @@ def _lookup_project_id(self) -> typing.Optional[str]:
275
282
Returns:
276
283
str: A project ID string or None.
277
284
"""
278
- project_id : typing . Optional [str ] = self ._options .get ('projectId' )
285
+ project_id : Optional [str ] = self ._options .get ('projectId' )
279
286
if not project_id :
280
287
try :
281
288
project_id = getattr (self ._credential , 'project_id' )
0 commit comments