HttpsCallable 从 python 返回

Kyl*_*lie 6 python flask swift google-cloud-functions

我有一个 Xcode 应用程序(用 Swift 编写),它调用部署在 Firebase 中的 HTTP python 函数。它应该得到一个响应作为回报,但由于某种原因,它总是在数据为零时返回。

// swift function in xcode 

Functions.functions().httpsCallable("python_callable").call(["ID": ID, "time": String(currentTime)]) { (result, error) in
                        if error != nil {
                            //does not enter this
                            return
                        }
                        else {
                            guard let data = result?.data as? Data else {return}
                            print(data)
            }
    }
Run Code Online (Sandbox Code Playgroud)

以下是用Python编写的谷歌云函数。根据Google Cloud 文档,Firebase 功能使用 Flask 来处理 HTTP 请求。

#deployed python Firestore function 
    import Flask
    def python_callable(request):
        ** processes firestore data ** 
        result = {"text":"example", "score": 100}
        return jsonify(data=result)
Run Code Online (Sandbox Code Playgroud)

我知道 python_callable 函数正在被调用并且它正在接收请求,但是似乎无论我做什么,我都无法让 Swift 函数获得响应。它总是收到 null。是否存在 httpsCallable 期望/使用不正确的特定响应格式jsonify

Dou*_*son 4

仅通过使用适用于节点的 Firebase SDK 在 Cloud Functions 上支持“可调用”函数。它们不适用于 Python,除非您在函数本身中实现可调用协议。如果您想调用用 python 编写的常规 HTTP 函数,则无法在客户端上使用 Firebase SDK 调用它。

如果您想尝试在服务器端实现协议,可调用的工作原理文档位于:https: //firebase.google.com/docs/functions/callable-reference

  • 您是否看过[https://cloud.google.com/functions/docs/concepts/events-triggers#functions_parameters-python](https://cloud.google.com/functions/docs/concepts/events-triggers#函数参数-python)?它说 python 可以与 Flask 一起用于 HTTP 函数。 (2认同)
  • 当然可以,但这些不是“可调用”函数。只是常规的 HTTP 函数。 (2认同)