An open API service indexing awesome lists of open source software.

https://github.com/utsavdotpro/connectionlibrary

HTTP connection library with offline support for Android
https://github.com/utsavdotpro/connectionlibrary

android connection-lib http-request kotlin rest

Last synced: about 2 months ago
JSON representation

HTTP connection library with offline support for Android

Awesome Lists containing this project

README

          

[![](https://jitpack.io/v/u-barnwal/ConnectionLibrary.svg)](https://jitpack.io/#u-barnwal/ConnectionLibrary)
# 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
```gradle
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
```

**Step 2:** Add to app level build.gradle

```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

```kotlin
import android.content.Context
import com.isolpro.library.connection.Connection

class ConnectionHelper(private val ctx: Context, private val classType: Class) : Connection() {
override var config: Config = Config("API_BASE_ENDPOINT")

override fun getContext(): Context {
return ctx;
}

override fun showLoader() {
TODO("Write your function for showing loader")
}

override fun hideLoader() {
TODO("Write your function for hiding loader")
}

override fun handleOnRequestCreated(endpoint: String, data: Any?) {
TODO("Access the request endpoint and data")
}

override fun handleOnResponseReceived(data: String?) {
TODO("This is triggered everytime your receive a response, implement your logger")
}

override fun handleOnNoResponseError() {
TODO("Handle when nothing is received as response")
}

override fun handleOnOfflineDataUnsupported() {
TODO("Handle when the request made, doesn't store offline data")
}

override fun handleOnOfflineDataUnavailable() {
TODO("Handle when the request made, store offline data but doesn't have anything cache yet")
}

override fun handleOnError(e: Exception) {
TODO("Handle all other errors")
}

override fun getClassType(): Class {
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

```kotlin
class Post {
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

```kotlin
object PostService {

fun getPosts(ctx: Context): Connection {
return ConnectionHelper(ctx, Post::class.java)
.endpoint("/posts")
.loader(false)
}

fun createPost(ctx: Context, post: Post): Connection {
return ConnectionHelper(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
```kotlin
PostService.getPosts(this)
.post()
```

- Request with Callbacks
```kotlin
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
```kotlin
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*)
```kotlin
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]("./app/src/main")**

## Features

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