A simple RESTful API framework depends on language spec, without invasive.
HTTP request based on OkHttp3 by default, but you can build your own request executor if any requiring implementation changes.
uiScope.launch {
val result = GetUrlQuerApi().suspend() //Worker thread.
update(result)
}
uiScope.launch {
val result = GetUrlQuerApi().suspend(requestExecutor) //Worker thread.
update(result)
}
val executor: RequestExecutor = OkHttpRequestExecutor(okHttpClient)
val getApi = GetUrlQuerApi().start(executor, { error ->
// Error handling.
}) { result ->
// Process your result
}
class GetUrlQueryApi : RespectApi<ApiResult, GetUrlQueryApi>() {
override fun parse(bytes: ByteArray): ApiResult {
return ApiResult.parseToModels(bytes) // Parse result to your model classes
}
override val url = "https://jsonplaceholder.typicode.com/comments"
override val httpMethod = HttpMethod.GET
override val urlQueries = mapOf("postId" to "1") // ?postId=1
}
val postApi = PostJsonApi("post id").start(executor, { error ->
// Error handling.
}) { result ->
// Process your result
}
class PostJsonApi(private val id: String) : RespectApi<ApiResult, PostJsonApi>() {
override fun parse(bytes: ByteArray): ApiResult {
return ApiResult.parseToModels(bytes) // Parse result to your model classes
}
override val url = "https://jsonplaceholder.typicode.com/posts"
override val httpMethod = HttpMethod.POST
override val contentType = ContentType.JSON
override val body: ByteArray
get() = "{\"id\"=\"$id\"}".toByteArray()
}
...etc
To get a Git project into your build:
gradle maven sbt leiningen Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.showang:Respect:{last_version}'
}