Skip to content

Repository files navigation

ConnectionLibrary

HTTP connection library to consume REST APIs in structured way with automatic support for offline data.

Implementation

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'
}

How to use?

Create Helper Class

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;
}
}

Create Models

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="";
}

Create Services

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)
}
}

Making the Request

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 ✅

Enable Offline Mode

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.

When fetching a list of items

ConnectionHelper(ctx, Post::class.java)
.payload(post)
.endpoint("/posts")
.offlineEndpoint("posts")
.loader(false)

When fetching a single item (pass the unique item it as second parameter)

ConnectionHelper(ctx, Post::class.java)
.payload(post)
.endpoint("/posts/$postId")
.offlineEndpoint("posts", postId)
.loader(false)

Recommended attributes for offlineEndpoint

  • 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

Features

  • Easy to use
  • Easy to customize & configure
  • Automated offline mode
  • Simple file structure: Create models & services
  • Syntax similar to modern programming notions

About

HTTP connection library with offline support for Android

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages