Skip to content

Repository files navigation

app-api

easy api hit using customized retrofit

Release

App-Api is an easy,flexible library for you Java/kotlin & Android http calls based on Retrofit2.

you can use it for all your get, put, post, delete & download files via http/https calls in your android project, the main feature of this library is not prevent/limit you to use you own api service,adapters,or even OkHttpClient.Builder. this library build with love & kotlin. with fully support to java.

add dependence

in project level build.gradle

allprojects {
repositories {
google()
jcenter()
//...
maven { url 'https://jitpack.io' }
}
}

in app level build.gradle

dependencies {
implementation 'com.github.MohammedSDev:app-api:last-build'
}

#How to Ues

first thing..all your response classes or just your main response class must extend from ResponseModel & your error response class must extend from ErrorResponseModel

AppConfig

appConfig{ ... } is function should called once before any other functions.. usuallly in Application class. you can specifiy your BASE_URL,DEBUG_LEVEL,TIMEOUT,custom ADAPTERS,GENERAL ERROR MESSAGE..& more

appConfig {
//BASE_URL should be end with `/`BASE_URL="https://github.com/MohammedSDev/"//you can provide generic error message based on your user localeGENERAL_ERROR_MESSAGE=if(Locale.getDefault().language.equals("ar"))
"حدث خطأ ماء, الرجاء المحاولة لاحقاً"else"Some thing went wrong,tray later"
}

don't forgot to add you Application class to AndroidManifest file

...
<applicationandroid:name=".MainApplication"android:icon="@mipmap/ic_launcher"
...

GET

val yourParams =HashMap<String,String>()
yourParams["your_key"] ="your_value"//..
get("repositories", MainResponse::class.java, MainErrorModel::class.java)
.preRequest {
queryParam = yourParams
headerParam = yourHeaderParams
this.delay =2000
}
.onSuccess { response ->//do stuff.//e.g: setData(response)
}
.onError { error ->//do stuff//e.g: alertUser(error.errorMessage)
}
.onStatusChange {
val c =when (it) {
isAppNetworkStatus.InProgress-> {
//myLoader.start()
}
isAppNetworkStatus.OnSuccess-> {
//myLoader.hide()
}
isAppNetworkStatus.OnError-> {
//myLoader.error()
}
isAppNetworkStatus.OnCustom-> {
//myLoader.stop()
}
isAppNetworkStatus.OnCancel-> {
//myLoader.stop()
}
}
}
.call()

POST

yous can use hash map or AppParamMap for complex params in both bodyParam and queryParam

val yourParams =AppParamMap<String,Any>()
// val yourParams = HashMap<String,String>()
yourParams["your_key"] =listOf("value1","value2","value3")
yourParams["your_key"] ="your_value"//..val request = post("repositories", MainResponse::class.java, MainErrorModel::class.java)
.preRequest {
bodyParam = yourParams
headerParam = yourHeaderParams
}
.onSuccess { response ->//do stuff.//e.g: setData(response)
}
.onError { error ->//do stuff//e.g: alertUser(error.errorMessage)
}
.onStatusChange {
val c =when (it) {
isAppNetworkStatus.InProgress-> {
//myLoader.start()
}
isAppNetworkStatus.OnSuccess-> {
//myLoader.hide()
}
isAppNetworkStatus.OnError-> {
//myLoader.error()
}
isAppNetworkStatus.OnCustom-> {
//myLoader.stop()
}
isAppNetworkStatus.OnCancel-> {
//myLoader.stop()
}
}
}
.call()
//to cancel//request.cancel()

PUT

put("repositories", MainResponse::class.java, MainErrorModel::class.java)
.preRequest {
bodyParam = hashMapOf(...)
this.delay =2000
}
...
.call()

DELETE

delete("repositories", MainResponse::class.java, MainErrorModel::class.java)
.preRequest {
bodyParam = hashMapOf(...)
this.delay =2000
}
...
.call()

download

val file =File.createTempFile("prefix_name","suffix")
val fileUrl ="https://..."val appRequest =AppRequestParam(fileUrl)
//appRequest.headerParam = ...
download(file, appRequest,{
//displayFileSize(file.length())//..
},{error->//alertUser(error.errorMessage)
})

webSocket

websocket using OkHttp

val ws = webSocket("ws://<YOUR SERVER URL>",listener =object:WebSocketListener(){
overridefunonOpen(webSocket:WebSocket, response:Response) {
}
overridefunonFailure(webSocket:WebSocket, t:Throwable, response:Response?) {
}
overridefunonClosing(webSocket:WebSocket, code:Int, reason:String) {
}
overridefunonMessage(webSocket:WebSocket, text:String) {
}
overridefunonMessage(webSocket:WebSocket, bytes:ByteString) {
}
overridefunonClosed(webSocket:WebSocket, code:Int, reason:String) {
}
})

Messaging puplish/subscribe (webSocket)

ws = webSocketMessaging(URI("ws://"), listener =object:AppMessagingEvents {
overridefunonConnect() {
ws.subscribe("/users/200")
}
overridefunonMessage(channel:String, status:Boolean, text:String) {
//TODO: do your stuff with message data in `text` param 
}
overridefunonDisConnect() {
//alert user 
}
overridefunonSubscribe(channel:String, status:Boolean) {
println("onSubscribe: $channel")
}
overridefunonUnSubscribe(channel:String, status:Boolean) {
println("onUnSubscribe: $channel")
}
overridefunonError(ex:Exception?) {
println("onError:${ex?.message}")
}
})
// start connectionval param =mapOf(
"token" to "...",
"emp_id" to "..."
)
ws.connect(param)

using Java

importkotlin.Unit;
importkotlin.jvm.functions.Function1;
importstaticcom.digital.app.api.ActionsKt.get;
...
get("categories", CategoryRes.class, ErrorModel.class)
.preRequest(newFunction1<AppRequestParam, Unit>() {
@OverridepublicUnitinvoke(AppRequestParamappRequestParam) {
HashMap<String, String> p = newHashMap<>();
p.put("locale","ar");
appRequestParam.setQueryParam(p);
//appRequestParam.setHeaderParam();returnnull;
}
})
.onSuccess(newFunction1<CategoryRes, Unit>() {
@OverridepublicUnitinvoke(CategoryRescategoryRes) {
System.out.println(categoryRes.getMessage());
System.out.println(categoryRes.getCategories().get(0));
returnnull;
}
})
.onError(newFunction1<ErrorModel, Unit>() {
@OverridepublicUnitinvoke(ErrorModelerrorModel) {
System.out.println("categories error, ${it.errorCode}, ${it.errorMessage}");
returnnull;
}
})
.call();

Enjoy using app-api library,report any bugs you found, or even drop me email gg.goo.mobile@gmail.com

About

easy api hit using customized retrofit

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages