Refactor request system to hide away some of the generics cruft.
This commit is contained in:
parent
b18890851c
commit
5d0ec6738d
|
@ -0,0 +1,22 @@
|
|||
package com.danilafe.fencelessgrazing.requests
|
||||
|
||||
import com.android.volley.Response
|
||||
import com.android.volley.toolbox.StringRequest
|
||||
|
||||
open class AuthenticatedRequest(
|
||||
baseUrl: String,
|
||||
apiEndpoint: String,
|
||||
private val token: String,
|
||||
listener: Response.Listener<String>,
|
||||
error: Response.ErrorListener
|
||||
) : StringRequest(
|
||||
Method.GET, "${baseUrl}${apiEndpoint}",
|
||||
listener, error
|
||||
) {
|
||||
|
||||
override fun getHeaders(): MutableMap<String, String> {
|
||||
val newMap = HashMap(super.getHeaders())
|
||||
newMap["Authorization"] = "Bearer $token"
|
||||
return newMap
|
||||
}
|
||||
}
|
|
@ -10,19 +10,10 @@ import com.google.gson.reflect.TypeToken
|
|||
class CollarDetailRequest(
|
||||
baseUrl: String,
|
||||
collarId: Int,
|
||||
private val token : String,
|
||||
token : String,
|
||||
listener: Response.Listener<CollarDetails>,
|
||||
error: Response.ErrorListener
|
||||
) : StringRequest(
|
||||
Method.GET, "${baseUrl}/collars/$collarId/details",
|
||||
GsonListener(
|
||||
object : TypeToken<CollarDetails>() {}.type,
|
||||
listener
|
||||
), error
|
||||
) {
|
||||
|
||||
override fun getHeaders(): MutableMap<String, String> {
|
||||
return mutableMapOf("Authorization" to "Bearer $token")
|
||||
}
|
||||
|
||||
}
|
||||
) : AuthenticatedRequest(
|
||||
baseUrl, "/collars/$collarId/details", token,
|
||||
listener.toGsonListener(), error
|
||||
)
|
|
@ -9,19 +9,10 @@ import com.google.gson.reflect.TypeToken
|
|||
class CollarHistoryRequest(
|
||||
baseUrl: String,
|
||||
collarId: Int,
|
||||
private val token : String,
|
||||
token : String,
|
||||
listener: Response.Listener<List<CollarPos>>,
|
||||
error: Response.ErrorListener
|
||||
) : StringRequest(
|
||||
Method.GET, "${baseUrl}/collars/$collarId/history",
|
||||
GsonListener(
|
||||
object : TypeToken<List<CollarPos>>() {}.type,
|
||||
listener
|
||||
), error
|
||||
) {
|
||||
|
||||
override fun getHeaders(): MutableMap<String, String> {
|
||||
return mutableMapOf("Authorization" to "Bearer $token")
|
||||
}
|
||||
|
||||
}
|
||||
) : AuthenticatedRequest(
|
||||
baseUrl, "/collars/$collarId/history", token,
|
||||
listener.toGsonListener(), error
|
||||
)
|
|
@ -7,19 +7,10 @@ import com.google.gson.reflect.TypeToken
|
|||
|
||||
class CollarRequest(
|
||||
baseUrl: String,
|
||||
private val token : String,
|
||||
token : String,
|
||||
listener: Response.Listener<List<CollarSummary>>,
|
||||
error: Response.ErrorListener
|
||||
) : StringRequest(
|
||||
Method.GET, "${baseUrl}/collars",
|
||||
GsonListener(
|
||||
object : TypeToken<List<CollarSummary>>() {}.type,
|
||||
listener
|
||||
), error
|
||||
) {
|
||||
|
||||
override fun getHeaders(): MutableMap<String, String> {
|
||||
return mutableMapOf("Authorization" to "Bearer $token")
|
||||
}
|
||||
|
||||
}
|
||||
) : AuthenticatedRequest(
|
||||
baseUrl, "/collars", token,
|
||||
listener.toGsonListener(), error
|
||||
)
|
|
@ -7,19 +7,10 @@ import com.google.gson.reflect.TypeToken
|
|||
|
||||
class DistanceTraveledRequest(
|
||||
baseUrl: String,
|
||||
private val token : String,
|
||||
token : String,
|
||||
listener: Response.Listener<List<CollarDistance>>,
|
||||
error: Response.ErrorListener
|
||||
) : StringRequest(
|
||||
Method.GET, "${baseUrl}/collars/stats/distance",
|
||||
GsonListener(
|
||||
object : TypeToken<List<CollarDistance>>() {}.type,
|
||||
listener
|
||||
), error
|
||||
) {
|
||||
|
||||
override fun getHeaders(): MutableMap<String, String> {
|
||||
return mutableMapOf("Authorization" to "Bearer $token")
|
||||
}
|
||||
|
||||
}
|
||||
) : AuthenticatedRequest(
|
||||
baseUrl, "/collars/stats/distance", token,
|
||||
listener.toGsonListener(), error
|
||||
)
|
|
@ -3,15 +3,17 @@ package com.danilafe.fencelessgrazing.requests
|
|||
import android.util.Log
|
||||
import com.android.volley.Response
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.reflect.TypeToken
|
||||
import java.lang.reflect.Type
|
||||
|
||||
class GsonListener<T : Any>(private val targetType : Type, private val stringListener : Response.Listener<T>) : Response.Listener<String> {
|
||||
|
||||
private val gson = Gson()
|
||||
|
||||
class GsonListener<T>(val listener: Response.Listener<T>, private val type: Type) : Response.Listener<String> {
|
||||
override fun onResponse(response: String?) {
|
||||
if(response == null) { return stringListener.onResponse(null) }
|
||||
stringListener.onResponse(gson.fromJson<T>(response, targetType))
|
||||
val transformed = response?.let { Gson().fromJson<T>(it, type) }
|
||||
listener.onResponse(transformed)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
inline fun <reified T> Response.Listener<T>.toGsonListener(): Response.Listener<String> {
|
||||
return GsonListener(this, object : TypeToken<T>(){}.type)
|
||||
}
|
||||
|
||||
|
|
|
@ -12,7 +12,7 @@ class LoginRequest(
|
|||
error: Response.ErrorListener
|
||||
) : StringRequest (
|
||||
Method.POST, "${baseUrl}/login",
|
||||
GsonListener(LoginResult::class.java, listener), error
|
||||
listener.toGsonListener(), error
|
||||
) {
|
||||
|
||||
override fun getParams(): MutableMap<String, String> {
|
||||
|
|
Loading…
Reference in New Issue
Block a user