0

I followed some tutorials with Vapor and despite that I understood how to Get or Put or Patch some JSON values on the server, I have no idea how to send dictionary to Firebase from within the app.

I've tried to write some data on Firebase using Postman and I have no problem, but I have no idea how to handle the parameters of the request using Vapor ?

Can anyone, please, tell me where I have to put the URL of the database and how to make the request? Or even better, if there's some example on GitHub, I can look at?

HDJEMAI
  • 9,436
  • 46
  • 67
  • 93
John Smith Optional
  • 431
  • 2
  • 4
  • 18

2 Answers2

0

There is no Firebase SDK for Server-Side-Swift so you can handle JSON values using REST API of Firebase.

See this topic.

ohtwo
  • 170
  • 8
0

You need to create JWT token in order to get access to Firebase Database. That's the hardest part.

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
    }
bibscy
  • 2,598
  • 4
  • 34
  • 82
  • pretty useless since your code misses EVERYTHING. what is drop, what is responseFirebaseAssigned, what is xy... – David Seek Oct 24 '17 at 04:39
  • `let drop = Droplet()` A Droplet is the heart of a Vapor server. It contains a plethora of functions that will be the backbone of our server. – bibscy Oct 25 '17 at 07:16
  • Okay well. I have been able to do pretty much everything. The only thing I have not been able to figure out is how to create a token – David Seek Oct 25 '17 at 14:26
  • I have created a bounty on an old question if you want to check it out. https://stackoverflow.com/questions/46396224/how-do-i-generate-an-auth-token-using-jwt-for-google-firebase – David Seek Oct 25 '17 at 14:27
  • 1
    @DavidSeek I have answered you question. Let me know if it works for you. – bibscy Oct 26 '17 at 18:51
  • yes. looks very good and i'm sure that you will get the bounty. i will upvote your answer once the bounty ends. to see what else gets in. thank you for your effort – David Seek Oct 26 '17 at 19:05