[FR] Improve Ability to detect if App has been Initialized
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 35/100
Hướng nghiên cứu
Bắt đầu bằng cách đọc firebase_admin/init.py, đặc biệt là initialize_app() và get_app() quanh các dòng được liên kết. So sánh hàm is_initialized được đề xuất với các cách tiếp cận dùng lỗi cụ thể, sau đó xác định hành vi công khai mà các maintainer mong muốn. Công việc được hoàn thành khi người dùng có thể xác định một cách đáng tin cậy liệu app được yêu cầu đã được khởi tạo hay chưa mà không cần kiểm tra biến riêng tư _apps hoặc phân tích một ValueError chung.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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) 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
From the discussion in this Stackoverflow post, 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
try:
app = firebase_admin.get_app()
except ValueError as e:
cred = credentials.Certificate(CREDENTIALS_FIREBASE_PATH)
firebase_admin.initialize_app(cred)
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
if not firebase_admin._apps:
cred = credentials.Certificate('path/to/serviceAccountKey.json')
default_app = firebase_admin.initialize_app(cred)
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:
class AppInitializedError(ValueError):
def __init__(self, message):
super().__init__(message)
This could then be used:
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) as well as in this issue.
- Ngôn ngữ chính
- Python
- Star
- 1.2k
- Fork
- 359
- Merge trung bình
- 5 ngày 6 phút
- Pull request đã merge (30 ngày)
- 2
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của firebase/firebase-admin-python
-
api: remoteconfig
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
firebase/firebase-admin-python#957 · 1 bình luận ·
-
api: database type: feature request
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 45/100
firebase/firebase-admin-python#978 · 1 bình luận ·
-
[FR] Support VERIFY_AND_CHANGE_EMAIL in generate_email_action_link (parity with firebase-admin-node) Đang mởapi: auth
firebase/firebase-admin-python#949 · 2 bình luận · 1 reaction · 1 người được giao ·
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 43/100
firebase/firebase-admin-python#945 · 1 bình luận · 1 reaction ·
Tất cả issue của firebase/firebase-admin-python
Issue tương tự
-
Add: hunch Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
AbdelStark/awesome-typesafe#104 ·
-
enhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
DiamondLightSource/dodal#2211 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
openml/openml-python#1749 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
sipyourdrink-ltd/bernstein#6191 ·