firebase / firebase/firebase-admin-python

[FR] Improve Ability to detect if App has been Initialized

Abierto
#701 2 comentarios 4 reacciones 0 asignados Ver en GitHub
api: core type: feature request
Lenguaje dominante
Python
Estrellas
1.2k
Forks
359
Merge medio
5 d 6 min
PR fusionados (30 d)
2

Descripción

## Is your feature request related to a problem? Please describe.
Currently there is not a way offered by the library to check if `firebase_admin.initialize_app(...)`([source](https://github.com/firebase/firebase-admin-python/blob/master/firebase_admin/__init__.py#L34)) has been called and an app has been initialized besides catching an exception. This can result in library users seeing the following exception:

> ValueError: The default Firebase app already exists. This means you called initialize_app() more than once without providing an app name as the second argument. In most cases you only need to call initialize_app() once. But if you do want to initialize multiple apps, pass a second argument to initialize_app() to give each app a unique name.
_[source](https://github.com/firebase/firebase-admin-python/blob/master/firebase_admin/__init__.py#L70-L83)_

From the discussion in [this Stackoverflow post](https://stackoverflow.com/questions/44293241/check-if-a-firebase-app-is-already-initialized-in-python), there are two main approaches library users have implemented:

**1. use a try/except block on `ValueError` to get the app and initialize it if there is an exception**
```python
try:
app = firebase_admin.get_app()
except ValueError as e:
cred = credentials.Certificate(CREDENTIALS_FIREBASE_PATH)
firebase_admin.initialize_app(cred)
```
_[source](https://stackoverflow.com/a/55174762/21030176)_

Pros: simple

Cons: `ValueError` is a general error so theoretically the use does not know for sure the error is regarding initialization, so further inspection is needed to verify that the error is related to app initialization, e.g. a string check on "already exists".

**2. check the "private" `_apps` variable**
```python
if not firebase_admin._apps:
cred = credentials.Certificate('path/to/serviceAccountKey.json')
default_app = firebase_admin.initialize_app(cred)
```
_[source](https://stackoverflow.com/a/44501290/21030176)_

Pros: not relying on try/except flow

Cons: accessing a "private" variable, as the library owners now if you change this variable it will cause breaking changes for many library users.

## **Describe the solution you'd like**
### Option 1: Implement a function like `is_initialized(name=_DEFAULT_APP_NAME)`
This could check `_apps` for the given name and return true / false for whether it is initialized.

### Option 2: Raise a specific error
Implement a new error that extends `ValueError` for backwards compatibility and raise the error with this new type.

For example:
```python
class AppInitializedError(ValueError):
def __init__(self, message):
super().__init__(message)
```
This could then be used:
```python
try:
app = firebase_admin.get_app()
except AppInitializedError:
cred = credentials.Certificate(CREDENTIALS_FIREBASE_PATH)
firebase_admin.initialize_app(cred)
```

---

These implementations could be done in conjunction, however option 1 is safest as it only adds new behavior and changes no existing behavior. Furthermore there will still be those against using try / except as the "expected" way to check if the app is initialized as offered in option 2.

## Describe alternatives you've considered
The alternatives are described in the stackoverflow post above ([link](https://stackoverflow.com/questions/44293241/check-if-a-firebase-app-is-already-initialized-in-python)) as well as in this issue.

Guía de contribución

Abrir la guía de contribución

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.