I would like to update multiple nodes in a single operation using the firebase REST API and Vapor framework.
When I make .patch
request to Firebase I get error from Vapor client:
The operation couldn’t be completed. Error 400 Bad Request
Could anyone tell me if finalDictionary
is initialized correctly before being converted in JSON?
Desired output for Users node:
brv-61e24
Users
usersUID
customer
bookingNumber
timeStampBookingCancelled
CleanersUID:"cleanersUID"
TimeStampBookingClaimed: "TimeStampBookingClaimed"
TimeStampBookingCancelledByCleaner: "TimeStampBookingCancelledByCleaner"
Cleaners
cleanersUID
bookingNumber
timeStampBookingCancelled
TimeStampBookingClaimed:"TimeStampBookingClaimed"
TimeStampBookingCancelledByCleaner: "TimeStampBookingCancelledByCleaner"
Cleaners
cleanersUID
bookingNumber
Claimed: "ClaimedValue"
var finalJSON:JSON?
func makeFinalDictionary() throws -> JSON? {
let customer = userStripeIdCustomer
var finalDictionary = Node([:])
//create base paths
let path0 = "Users/(usersUID!)/(customer!)/(bookingNumber!)/timeStampBookingCancelled"
let path1 = "Cleaners/(cleanersUID!)/(bookingNumber!)/timeStampBookingCancelled"
let path2 = "Cleaners/(cleanersUID!)/(bookingNumber!)"
//create final paths and assign values to keys
finalDictionary[path0] = try Node(node: ["CleanersUID": cleanersUID])
finalDictionary[path0] = try Node(node:["TimeStampBookingClaimed":timeStampBookingClaimed])
finalDictionary[path0] = try Node(node:["TimeStampBookingCancelledByCleaner":timeStampBookingCancelledByCleaner])
finalDictionary[path1] = try Node(node:["TimeStampBookingClaimed":timeStampBookingClaimed])
finalDictionary[path1] = try Node(node:["TimeStampBookingCancelledByCleaner":timeStampBookingCancelledByCleaner])
finalDictionary[path2] = try Node(node:["Claimed":claimedValue])
do {
finalJSON = try JSON(Node(node:finalDictionary))
} catch let error as NSError {
let message = error.localizedDescription
drop.log.error(message)
throw NSError(domain: "errorFirebase", code: error.code, userInfo: ["error" : message])
}
return finalJSON
}
func writeAsMultiPath(tokenReceived:String!, jsonObject:JSON?) throws -> Void {
let responseFirebase:Response?
do{
let token = tokenReceived ?? "defaultValue"
let response = try drop.client.patch(
"https://brv-61e24.firebaseio.com.json/", headers: ["Authorization":"Bearer (tokenReceived)"], query: [:],
body: jsonObject!)
responseFirebase = response
}catch let error as NSError {
guard let phrase = error.userInfo["error"] as? String else {
throw NSError(domain: "errorFirebase", code: error.code, userInfo: ["error" : error.localizedDescription])
}
throw NSError(domain: "errorFirebase", code: error.code, userInfo: ["error" : phrase])
}
let body = responseFirebase?.body.bytes
do {
jsonFromWritingUsers = try JSON(bytes:body!)
} catch let error as NSError{
let message = "jsonFromWritingUsers cannot be converted in JSON line 408 in(fileName)"
drop.log.error(message)
throw NSError(domain: "errorFirebase", code: error.code, userInfo: ["error" : message])
}
guard responseFirebase?.status.statusCode == 200 else{
let message1 = "write as multi-path failed line 410 in(fileName)"
drop.log.error(message1)
let message = "fail"
throw NSError(domain: "errorFirebase", code: 400, userInfo: ["error" : message])
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…