13
13
# limitations under the License.
14
14
15
15
# [START all]
16
+ from typing import Any
16
17
from urllib import parse as urllib_parse
17
18
18
19
# [START import]
@@ -40,11 +41,12 @@ def addmessage(req: https_fn.Request) -> https_fn.Response:
40
41
# [START adminSdkPush]
41
42
42
43
# 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
44
45
45
46
# 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" ))
48
50
)
49
51
path = f"{ ref .path } .json"
50
52
return https_fn .Response (
@@ -63,21 +65,25 @@ def addmessage(req: https_fn.Request) -> https_fn.Response:
63
65
64
66
# [START makeUppercase]
65
67
@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 :
67
69
"""Listens for new messages added to /messages/{pushId}/original and
68
70
creates an uppercase version of the message to /messages/{pushId}/uppercase
69
71
"""
70
72
71
73
# Grab the value that was written to the Realtime Database.
72
74
original = event .data
73
- if not hasattr (original , "upper" ):
75
+ if not isinstance (original , str ):
74
76
print (f"Not a string: { event .reference } " )
75
77
return
76
78
77
79
# Use the Admin SDK to set an "uppercase" sibling.
78
80
print (f"Uppercasing { event .params ['pushId' ]} : { original } " )
79
81
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 )
81
87
82
88
83
89
# [END makeUppercase]
@@ -107,7 +113,11 @@ def makeuppercase2(event: db_fn.Event[db_fn.Change]) -> None:
107
113
# Use the Admin SDK to set an "uppercase" sibling.
108
114
print (f"Uppercasing { event .params ['pushId' ]} : { original } " )
109
115
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 )
111
121
112
122
113
123
# [END makeUppercase2]
0 commit comments