1313# limitations under the License.
1414
1515# [START all]
16+ from typing import Any
1617from urllib import parse as urllib_parse
1718
1819# [START import]
@@ -40,11 +41,12 @@ def addmessage(req: https_fn.Request) -> https_fn.Response:
4041 # [START adminSdkPush]
4142
4243 # Push the new message into the Realtime Database using the Firebase Admin SDK.
43- ref = db .reference ("/messages" ).push ({"original" : original })
44+ ref = db .reference ("/messages" ).push ({"original" : original }) # type: ignore
4445
4546 # Redirect with 303 SEE OTHER to the URL of the pushed object.
46- scheme , location , path , query , fragment = urllib_parse .urlsplit (
47- app .options .get ("databaseURL" )
47+ scheme , location , path , query , fragment = (
48+ b .decode ()
49+ for b in urllib_parse .urlsplit (app .options .get ("databaseURL" ))
4850 )
4951 path = f"{ ref .path } .json"
5052 return https_fn .Response (
@@ -63,21 +65,25 @@ def addmessage(req: https_fn.Request) -> https_fn.Response:
6365
6466# [START makeUppercase]
6567@db_fn .on_value_created (reference = "/messages/{pushId}/original" )
66- def makeuppercase (event : db_fn .Event [object ]) -> None :
68+ def makeuppercase (event : db_fn .Event [Any ]) -> None :
6769 """Listens for new messages added to /messages/{pushId}/original and
6870 creates an uppercase version of the message to /messages/{pushId}/uppercase
6971 """
7072
7173 # Grab the value that was written to the Realtime Database.
7274 original = event .data
73- if not hasattr (original , "upper" ):
75+ if not isinstance (original , str ):
7476 print (f"Not a string: { event .reference } " )
7577 return
7678
7779 # Use the Admin SDK to set an "uppercase" sibling.
7880 print (f"Uppercasing { event .params ['pushId' ]} : { original } " )
7981 upper = original .upper ()
80- db .reference (event .reference ).parent .child ("uppercase" ).set (upper )
82+ parent = db .reference (event .reference ).parent
83+ if parent is None :
84+ print ("Message can't be root node." )
85+ return
86+ parent .child ("uppercase" ).set (upper )
8187
8288
8389# [END makeUppercase]
@@ -107,7 +113,11 @@ def makeuppercase2(event: db_fn.Event[db_fn.Change]) -> None:
107113 # Use the Admin SDK to set an "uppercase" sibling.
108114 print (f"Uppercasing { event .params ['pushId' ]} : { original } " )
109115 upper = original .upper ()
110- db .reference (event .reference ).parent .child ("uppercase" ).set (upper )
116+ parent = db .reference (event .reference ).parent
117+ if parent is None :
118+ print ("Message can't be root node." )
119+ return
120+ parent .child ("uppercase" ).set (upper )
111121
112122
113123# [END makeUppercase2]
0 commit comments