Created
November 24, 2017 16:01
-
-
Save lovubuntu/21a9b4e848d8c837ea37ab9e87b13fef to your computer and use it in GitHub Desktop.
Make network calls using Fuel library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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