How do you register a python function?
Closed this issue · 6 comments
Hi, I see that you imported your WriteSkills
go-lang function directly but I'm wondering how to register a python function to the emulator. Do I use python functions-framework for that?
So i believe you would use the python functions-framework for your regular firestore event handler code. When you are running it locally you will need to do the following to register your python function to the emulator
Setting up the emulator
Grab the latest firestore emulator with using firebase-tools cli by running firebase setup:emulators:firestore
.
Invoke the help menu of the jar with java -jar ~/.cache/firebase/emulators/cloud-firestore-emulator-v1.11.7.jar --help
, as of the time i am writing this the current version is v1.11.7 so thats what this sample will be with.
Run the emulator with java -jar ~/.cache/firebase/emulators/cloud-firestore-emulator-v1.11.7.jar --functions_emulator localhost:6000
this will set the callback url for functions events to localhost:6000
and thats where we will be running our local function at.
Register our function resource with the firestore emulator. We want to watch for all document writes to the root collection of skills/{id}
and the name of our Function is called "WriteSkills" .
curl --location --request PUT 'http://localhost:8080/emulator/v1/projects/dummy/triggers/WriteSkills' \
--header 'Content-Type: application/json' \
--data-raw '{
"eventTrigger": {
"resource": "projects/dummy/databases/(default)/documents/skills/{id}",
"eventType": "providers/cloud.firestore/eventTypes/document.write",
"service": "firestore.googleapis.com"
}
}'
As a quick schema reference you can use the following.
curl --location --request PUT 'http://[HOST:PORT]/emulator/v1/projects/[PROJECT_ID]/triggers/[FUNCTION_NAME]' \
--header 'Content-Type: application/json' \
--data-raw '{
"eventTrigger": {
"resource": "projects/[PROJECT_ID]/databases/(default)/documents/[RESOURCE_PATH]",
"eventType": [EVENT_TYPE],
"service": "firestore.googleapis.com"
}
}'
the event types for firestore can be
"providers/cloud.firestore/eventTypes/document.create"
"providers/cloud.firestore/eventTypes/document.update"
"providers/cloud.firestore/eventTypes/document.delete"
"providers/cloud.firestore/eventTypes/document.write"
Did you mean localhost:6000 here following your example? This is how to register the python function in the emulator right?
--header 'Content-Type: application/json' \
--data-raw '{
"eventTrigger": {
"resource": "projects/dummy/databases/(default)/documents/skills/{id}",
"eventType": "providers/cloud.firestore/eventTypes/document.write",
"service": "firestore.googleapis.com"
}
}'
Also, where is the reference to the python source file - main.py
?
By the way, I have a few node.js functions already running in the emulator. I hope there's a way to run the python function side by side. But if there isn't any way, I'll test one at a time.
Did you mean localhost:6000 here following your example? This is how to register the python function in the emulator right?
--header 'Content-Type: application/json' \ --data-raw '{ "eventTrigger": { "resource": "projects/dummy/databases/(default)/documents/skills/{id}", "eventType": "providers/cloud.firestore/eventTypes/document.write", "service": "firestore.googleapis.com" } }'
nope it would be the host and port you have the the firestore emulator running on
By the way, I have a few node.js functions already running in the emulator. I hope there's a way to run the python function side by side. But if there isn't any way, I'll test one at a time.
you would have to run one or the other, since you would be invoking the firestore emulator jar directly you would be rebinding the functionss host:port to your python program. the firebase-tools cli will try to auto setup the connections and thats what you dont want