You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This library is a collection of extension functions for OkHttp that enables OkHttp to be used in a way similar to Retrofit without all of the annotation magic. It also includes Gson for receiveConversion to and from JSON.
Key functions and classes
OkHttpApi - Make your API object extends this to use headers common to entire API, as well as a common root.
Request.Builder.lambdaGson<T> - Uses your request builder to create a lambda that executes the request, with the response converted to T using Gson. This is useful because it allows you to use .invokeAsync{ response -> } from kotlin-core.
TypedResponse<T> - A typed network response. Important things it contains: .isSuccessful(), .result, .errorString, .code.
Example usage
object ExampleAPI : OkHttpApi("https://jsonplaceholder.typicode.com") {
//This will get from "https://jsonplaceholder.typicode.com/posts" with a return type of List<Post>//getPosts() returns a ()->List<Post>fungetPosts() = requestBuilder("/posts").get().lambdaGson<List<Post>>()
//This will post `post` to "https://jsonplaceholder.typicode.com/posts" with a return type of PostfuncreatePost(post:Post) = requestBuilder("/posts").post(post).lambdaGson<Post>()
}
//The model. Gson takes care of the matching of names to JSON keys.classPost(
varuserId:Long = -1,
varid:Long? = null,
vartitle:String = "",
varbody:String = ""
)
funtest(){
//synchronousval response =ExampleAPI.getPosts().invoke()
if(response.isSuccessful()){
println("Post list obtained. Number of posts: ${response.result!!.size}")
} else {
println("There was an error. ${response.errorString}")
}
//asynchronousval post =Post(userId =3, title ="New Post", body ="This is a new post!")
ExampleApi.createPost(post).invokeAsync{ response ->if(response.isSuccessful()){
println("Post creation was successful. Post ID: ${response.result!!.id}")
} else {
println("There was an error. ${response.errorString}")
}
}
}