As part of an android app, I've written a simple wrapper that makes requests to an api and fetches stuff.
import android.app.Activity
import android.util.Log
import android.widget.Toast
import com.github.kittinunf.fuel.core.Parameters
import com.github.kittinunf.fuel.httpGet
import com.github.kittinunf.fuel.json.responseJson
import com.github.kittinunf.result.Result
import com.google.gson.Gson
data class Xkcd(val id: Int, val content: String, val link: String, val title: String)
class XkcdClient(private val main: Activity) {
private val API = "https://279fbce3.ngrok.io"
fun search(p: Parameters, callback: (Array<Xkcd>) -> Unit) {
makeRequest("$API/search", p, callback)
}
private inline fun <reified T> makeRequest(url: String, p: Parameters, crossinline callback: (T) -> Unit) {
buildPath(url, p)
.httpGet()
.responseJson { _, _, result ->
when (result) {
is Result.Failure -> {
Log.i("failed", "request failed")
main.runOnUiThread {
Toast.makeText(
main,
"Request Failed!",
Toast.LENGTH_LONG
).show()
}
}
is Result.Success -> {
val json = result.get().obj()
Log.i("json", json.toString())
val res = deserialize<T>(json.get("results").toString())
main.runOnUiThread { callback(res) }
}
}
}
}
}
inline fun <reified T> deserialize(content: String): T {
return Gson().fromJson(content, T::class.java)
}
fun buildPath(url: String, params: Parameters): String {
var url = url
if (params.isNotEmpty()) {
url += "?"
}
for ((p, v) in params) {
url += "$p=$v&"
}
return url.substring(0, url.lastIndex)
}
It works as I want it to. As such, I'm looking for some advice on how I can make it more idiomatic/elegant.
Just FYI: I've written the buildPath
myself, as the http library I'm using (kittinuf.fuel
) has a bug in its parameter setting method. When it's fixed, I'll be switching to its (hopefully correct) implementation. My method has at least one known bug in it, where if the base url has a trailing slash, it will return a url with double slashes. To avoid getting into all hairy edges cases, I've made sure my hardcoded base url is fine and stripped of a trailing slash.