IdentifyCallback

IdentifyCallback

Example Source Code

abstract class IdentifyCallback<T> : retrofit2.Callback<T> {

    abstract fun onSuccess(response: JsonObject)

    abstract fun onFailure(t: Throwable)

    override fun onResponse(call: Call<T>, response: Response<T>) {
        if (response.isSuccessful) {
            val body = response.body()
            val rawJson = body.toString()

            val json: JsonObject =  JsonParser.parseString(rawJson).asJsonObject 

            if (json.get(JSON_RESULT) != null) {
                onSuccess(json)
            } else {
                if(json.get(JSON_MESSAGE) != null){
                    onFailure(Throwable(json.get(JSON_MESSAGE).asString))
                } else onFailure(Throwable("Data not found"))
            }
        } else onFailure(Throwable(response.message() ?: "Response Invalid"))
    }

    override fun onFailure(call: Call<T>, t: Throwable) {
        onFailure(t)
    }
}