Skip to content

Commit 9805758

Browse files
authored
fix(rtdb): Fixed a bug in the Reference.listen() API (#418)
1 parent 69c940c commit 9805758

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

firebase_admin/db.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -373,8 +373,7 @@ def listen(self, callback):
373373
Raises:
374374
FirebaseError: If an error occurs while starting the initial HTTP connection.
375375
"""
376-
session = _sseclient.KeepAuthSession(self._client.credential)
377-
return self._listen_with_session(callback, session)
376+
return self._listen_with_session(callback)
378377

379378
def transaction(self, transaction_update):
380379
"""Atomically modifies the data at this location.
@@ -463,8 +462,11 @@ def order_by_value(self):
463462
def _add_suffix(self, suffix='.json'):
464463
return self._pathurl + suffix
465464

466-
def _listen_with_session(self, callback, session):
465+
def _listen_with_session(self, callback, session=None):
467466
url = self._client.base_url + self._add_suffix()
467+
if not session:
468+
session = self._client.create_listener_session()
469+
468470
try:
469471
sse = _sseclient.SSEClient(url, session)
470472
return ListenerRegistration(callback, sse)
@@ -907,6 +909,7 @@ def __init__(self, credential, base_url, timeout, params=None):
907909
super().__init__(
908910
credential=credential, base_url=base_url,
909911
timeout=timeout, headers={'User-Agent': _USER_AGENT})
912+
self.credential = credential
910913
self.params = params if params else {}
911914

912915
def request(self, method, url, **kwargs):
@@ -941,6 +944,9 @@ def request(self, method, url, **kwargs):
941944
except requests.exceptions.RequestException as error:
942945
raise _Client.handle_rtdb_error(error)
943946

947+
def create_listener_session(self):
948+
return _sseclient.KeepAuthSession(self.credential)
949+
944950
@classmethod
945951
def handle_rtdb_error(cls, error):
946952
"""Converts an error encountered while calling RTDB into a FirebaseError."""

tests/test_db.py

+11
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,17 @@ def callback(_):
551551
finally:
552552
testutils.cleanup_apps()
553553

554+
def test_listener_session(self):
555+
firebase_admin.initialize_app(testutils.MockCredential(), {
556+
'databaseURL' : 'https://test.firebaseio.com',
557+
})
558+
try:
559+
ref = db.reference()
560+
session = ref._client.create_listener_session()
561+
assert isinstance(session, _sseclient.KeepAuthSession)
562+
finally:
563+
testutils.cleanup_apps()
564+
554565
def test_single_event(self):
555566
self.events = []
556567
def callback(event):

0 commit comments

Comments
 (0)