Skip to content

snk002/RestLib

Repository files navigation

RestLib

REST library based on OkHttp3 and Gson

Connect to project:

  1. Add the JitPack repository to project-level build.gradle file
repositories {
	...
	maven { url 'https://jitpack.io' }
}
  1. Add the dependency to module-level gradle file
dependencies {
	...
	implementation 'com.github.snk002:RestLib:Tag'
}

Sample usage:

class NewsRepository(private val api: SimpleRest) {

	// initially set endpoint address
	init {
		api.setBaseUrl("https://domain.tld/api/")
	}
  
	// request list of items from specified page
	suspend fun fetchNews(page: Int): List<NewsData>? =
		api.get("news").addParam("page", page).awaitData()
    
	// make flow with item, updated every 15 seconds
	fun getHeadNewsLine(): NewsData =
		api.get("breaking").toFlow(15000)
}

Usage with various return types:

class DataRepository(private val api: SimpleRest) {
	
	suspend fun postData(submission: MyRequest): SimpleResponse<PossibleResponses?> =
		// here is full path specified
		api.post("https://domain.tld/api/submit", submission)
			// different acceptable types based on HTTP response code
			.addResponseType(200, RegularExpected::class.java)
			.addResponseType(208, PartialSuccess::class.java)
			.addResponseType(400, DataError::class.java)
			.addResponseType(500, ServerError::class.java).awaitResponse()
}

// it is possible to use Any but better way is mark all acceptable types by interface
interface PossibleResponses

data class RegularExpected(
	val title: String,
) : PossibleResponses

data class PartialSuccess(
	val summary: String
) : PossibleResponses

//etc...

About

REST libruary based on OkHttp3

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages