Skip to content

Instantly share code, notes, and snippets.

@lovubuntu
Created November 24, 2017 16:01
Show Gist options
  • Save lovubuntu/21a9b4e848d8c837ea37ab9e87b13fef to your computer and use it in GitHub Desktop.
Save lovubuntu/21a9b4e848d8c837ea37ab9e87b13fef to your computer and use it in GitHub Desktop.
Make network calls using Fuel library
import com.fasterxml.jackson.databind.ObjectMapper
import com.github.kittinunf.fuel.httpGet
import com.github.kittinunf.fuel.httpPost
import tech.ajira.kitCoin.models.Block
object BlockService {
fun broadcastNewBlock(nodeAddresses: Set<String>, newBlock: Block) {
val jsonString = ObjectMapper().writeValueAsString(newBlock)
nodeAddresses.forEach { nodeAddress ->
"$nodeAddress/blocks"
.httpPost()
.body(jsonString)
.response { _, _, _ -> println("Posted to $nodeAddress") }
}
}
fun fetchChain(nodeAddress: String, callback: (ArrayList<Block>) -> Unit) {
"$nodeAddress/blocks"
.httpGet()
.responseObject(Block.ListDeserializer()) { _, _, result ->
println("Obtained result")
callback(result.get())
}
println("am called")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment