HTTP connection library to consume REST APIs in structured way with automatic support for offline data.
Step 1: Add to project level build.gradle
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}Step 2: Add to app level build.gradle
dependencies {
implementation 'com.github.utsavdotpro:ConnectionLibrary:VERSION'
}You need to create a helper class that configures the Connection and provides callbacks for all events.
Expand: ConnectionHelper.kt
importandroid.content.Contextimportcom.isolpro.library.connection.ConnectionclassConnectionHelper<T>(privatevalctx:Context, private valclassType:Class<T>) : Connection<T>() {
overridevar config:Config=Config("API_BASE_ENDPOINT")
overridefungetContext(): Context {
return ctx;
}
overridefunshowLoader() {
TODO("Write your function for showing loader")
}
overridefunhideLoader() {
TODO("Write your function for hiding loader")
}
overridefunhandleOnRequestCreated(endpoint:String, data:Any?) {
TODO("Access the request endpoint and data")
}
overridefunhandleOnResponseReceived(data:String?) {
TODO("This is triggered everytime your receive a response, implement your logger")
}
overridefunhandleOnNoResponseError() {
TODO("Handle when nothing is received as response")
}
overridefunhandleOnOfflineDataUnsupported() {
TODO("Handle when the request made, doesn't store offline data")
}
overridefunhandleOnOfflineDataUnavailable() {
TODO("Handle when the request made, store offline data but doesn't have anything cache yet")
}
overridefunhandleOnError(e:Exception) {
TODO("Handle all other errors")
}
overridefungetClassType(): Class<T> {
return classType;
}
}While you are free to structure your requests in any way you like, we suggest to create model classes for all your data.
Expand Example Model
classPost {
val userId:Number=0;
val id:Number=0;
val title:String="";
val body:String="";
}We also suggest to create service class with all request functions required for the data model.
Expand Example Service
object PostService {
fungetPosts(ctx:Context): Connection<Post> {
returnConnectionHelper(ctx, Post::class.java)
.endpoint("/posts")
.loader(false)
}
funcreatePost(ctx:Context, post:Post): Connection<Post> {
returnConnectionHelper(ctx, Post::class.java)
.payload(post)
.endpoint("/posts/insert")
.loader(false)
}
}Once you have created your models and services, making a request is a piece of cake
Simple Request
PostService.getPosts(this) .post()
Request with Callbacks
PostService.getPosts(this) .success { TODO("Use your data from $it") // use $it.userId to get userId from Post } .failure { TODO("Let user know that the request has failed") } .post() }
And you're done ✅
To make a request to start caching data for offline usage, just pass a unique offlineEndpoint . Not to be used with data creation/modification requests.
ConnectionHelper(ctx, Post::class.java)
.payload(post)
.endpoint("/posts")
.offlineEndpoint("posts")
.loader(false)ConnectionHelper(ctx, Post::class.java)
.payload(post)
.endpoint("/posts/$postId")
.offlineEndpoint("posts", postId)
.loader(false)- should be unique
- avoid using any symbols
- cannot be empty (empty indicates that request doesn't support offline mode)
And you're done ✅
| See sample app
- Easy to use
- Easy to customize & configure
- Automated offline mode
- Simple file structure: Create models & services
- Syntax similar to modern programming notions