Refactor request system to hide away some of the generics cruft.

This commit is contained in:
Danila Fedorin 2020-05-13 15:04:48 -07:00
parent b18890851c
commit 5d0ec6738d
7 changed files with 52 additions and 64 deletions

View File

@ -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
}
}

View File

@ -10,19 +10,10 @@ import com.google.gson.reflect.TypeToken
class CollarDetailRequest( class CollarDetailRequest(
baseUrl: String, baseUrl: String,
collarId: Int, collarId: Int,
private val token : String, token : String,
listener: Response.Listener<CollarDetails>, listener: Response.Listener<CollarDetails>,
error: Response.ErrorListener error: Response.ErrorListener
) : StringRequest( ) : AuthenticatedRequest(
Method.GET, "${baseUrl}/collars/$collarId/details", baseUrl, "/collars/$collarId/details", token,
GsonListener( listener.toGsonListener(), error
object : TypeToken<CollarDetails>() {}.type, )
listener
), error
) {
override fun getHeaders(): MutableMap<String, String> {
return mutableMapOf("Authorization" to "Bearer $token")
}
}

View File

@ -9,19 +9,10 @@ import com.google.gson.reflect.TypeToken
class CollarHistoryRequest( class CollarHistoryRequest(
baseUrl: String, baseUrl: String,
collarId: Int, collarId: Int,
private val token : String, token : String,
listener: Response.Listener<List<CollarPos>>, listener: Response.Listener<List<CollarPos>>,
error: Response.ErrorListener error: Response.ErrorListener
) : StringRequest( ) : AuthenticatedRequest(
Method.GET, "${baseUrl}/collars/$collarId/history", baseUrl, "/collars/$collarId/history", token,
GsonListener( listener.toGsonListener(), error
object : TypeToken<List<CollarPos>>() {}.type, )
listener
), error
) {
override fun getHeaders(): MutableMap<String, String> {
return mutableMapOf("Authorization" to "Bearer $token")
}
}

View File

@ -7,19 +7,10 @@ import com.google.gson.reflect.TypeToken
class CollarRequest( class CollarRequest(
baseUrl: String, baseUrl: String,
private val token : String, token : String,
listener: Response.Listener<List<CollarSummary>>, listener: Response.Listener<List<CollarSummary>>,
error: Response.ErrorListener error: Response.ErrorListener
) : StringRequest( ) : AuthenticatedRequest(
Method.GET, "${baseUrl}/collars", baseUrl, "/collars", token,
GsonListener( listener.toGsonListener(), error
object : TypeToken<List<CollarSummary>>() {}.type, )
listener
), error
) {
override fun getHeaders(): MutableMap<String, String> {
return mutableMapOf("Authorization" to "Bearer $token")
}
}

View File

@ -7,19 +7,10 @@ import com.google.gson.reflect.TypeToken
class DistanceTraveledRequest( class DistanceTraveledRequest(
baseUrl: String, baseUrl: String,
private val token : String, token : String,
listener: Response.Listener<List<CollarDistance>>, listener: Response.Listener<List<CollarDistance>>,
error: Response.ErrorListener error: Response.ErrorListener
) : StringRequest( ) : AuthenticatedRequest(
Method.GET, "${baseUrl}/collars/stats/distance", baseUrl, "/collars/stats/distance", token,
GsonListener( listener.toGsonListener(), error
object : TypeToken<List<CollarDistance>>() {}.type, )
listener
), error
) {
override fun getHeaders(): MutableMap<String, String> {
return mutableMapOf("Authorization" to "Bearer $token")
}
}

View File

@ -3,15 +3,17 @@ package com.danilafe.fencelessgrazing.requests
import android.util.Log import android.util.Log
import com.android.volley.Response import com.android.volley.Response
import com.google.gson.Gson import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import java.lang.reflect.Type import java.lang.reflect.Type
class GsonListener<T : Any>(private val targetType : Type, private val stringListener : Response.Listener<T>) : Response.Listener<String> { class GsonListener<T>(val listener: Response.Listener<T>, private val type: Type) : Response.Listener<String> {
private val gson = Gson()
override fun onResponse(response: String?) { override fun onResponse(response: String?) {
if(response == null) { return stringListener.onResponse(null) } val transformed = response?.let { Gson().fromJson<T>(it, type) }
stringListener.onResponse(gson.fromJson<T>(response, targetType)) listener.onResponse(transformed)
} }
} }
inline fun <reified T> Response.Listener<T>.toGsonListener(): Response.Listener<String> {
return GsonListener(this, object : TypeToken<T>(){}.type)
}

View File

@ -12,7 +12,7 @@ class LoginRequest(
error: Response.ErrorListener error: Response.ErrorListener
) : StringRequest ( ) : StringRequest (
Method.POST, "${baseUrl}/login", Method.POST, "${baseUrl}/login",
GsonListener(LoginResult::class.java, listener), error listener.toGsonListener(), error
) { ) {
override fun getParams(): MutableMap<String, String> { override fun getParams(): MutableMap<String, String> {