如何使用Vapor在Firebase中发布值?

时间:2017-03-28 18:58:29

标签: json firebase firebase-realtime-database vapor

我遵循Vapor的一些教程,尽管我知道如何在服务器上获取或放置或修补一些JSON值,但我不知道如何从应用程序内部向Firebase发送字典。

我尝试使用Postman在Firebase上写一些数据,我没有问题,但是我不知道如何使用Vapor处理请求的参数?

请有人,请告诉我在哪里放置数据库的URL以及如何提出请求?或者甚至更好,如果在GitHub上有一些例子,我可以看一下吗?

2 个答案:

答案 0 :(得分:0)

没有用于Server-Side-Swift的Firebase SDK,因此您可以使用Firebase的REST API处理JSON值。

See this topic

答案 1 :(得分:0)

您需要创建JWT令牌才能访问Firebase数据库。这是最难的部分。

let drop = Droplet()
func readUsersNodeInFb()  {

    let customer = userStripeIdCustomer
    do {
        let responseFirebase = try drop.client.get(
            "https://bev-708.firebaseio.com/Users/\(usersUID!)/\(customer!)/\(bookingNumber!).json", headers: ["Authorization":"Bearer \(TokenData.acessTokenReceived!)"], query: [:],
            body: " a JSON Object")


        responseFirebaseAssigned = responseFirebase
    } catch let error {

        let errorString = "error line 115 in ClaimBookingCleaner received when trying to update  Firebase, \(error.localizedDescription)"
        errorFirebaseReceived = errorString
        return
    }


    //response from Firebase
    let bodyReceived = responseFirebaseAssigned?.body.bytes

    do {
        let jsonFire = try JSON(bytes:bodyReceived!)
         JsonFirebase = jsonFire
         print("jsonFire is \(jsonFire)")
    } catch let error {

        let errorString = "jsonFire could not be built with the payload received from server, line 55 in ClaimBookingCleaner error \(String(describing: error.localizedDescription))"
        errorFirebaseReceived = errorString
        return
    }