Last active
June 26, 2020 20:34
-
-
Save Bradleycorn/53a4a3603b29c64000ad74872a05726b to your computer and use it in GitHub Desktop.
Example of abstracting Java based network responses, since they can always have null values
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
A class that makes api calls to the backend, process responses, and returns proper data models | |
*/ | |
class NetworkLayer(val api: CdiApi) { | |
fun fetchProgramData(track: String, race: Number): List<ProgramEntry> { | |
// Get a ProgramResponse object (the network model) back from the api | |
val response: ProgramResponse = api.fetchProgram(track, race) | |
// Convert the network model to a List<ProgramEntry> using the static methods | |
// on the ProgramEntry model, and return the list. | |
return ProgramEntry.fromNetworkResponse(response) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
A data model that represents a single runner in a race. | |
Typically created fromthe ProgramResponse network model | |
*/ | |
data class ProgramEntry(val programNumber: Int, val horseName: String, val odds: Long) { | |
// In Kotlin a “companion object” is how we define static methods. | |
companion object { | |
// A static method to create a list of ProgramEntries from the network response | |
fun fromNetworkResponse(response: ProgramResponse): List<ProgramEntry> { | |
// map the list of entries, creating a ProgramEntry for each, | |
// or if the list is null (handled by the ?: operator), return an empty list | |
return response.entries?.map { entry -> | |
fromNetworkResponse(entry) | |
} ?: emptyListOf<ProgramEntry>() | |
} | |
// A static method to convert a single entry from the network response into a program entry | |
// The nice thing is, if the network response model ever changes, this is the ONLY place we'll need | |
// need to update in our entire code base, because this is the only place that actually uses the network | |
// response model. Everywhere else will use the ProgramEntry data model that this method returns. | |
fun fromNetworkResponse(entry: ProgramResponseEntry): ProgramEntry { | |
// create a new program entry from the response, setting defaults if any values come back as null from Java. | |
return ProgramEntry(entry.programNumnber ?: 0, entry.horseName ?: “”, entry.odds ?: 0L) | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Response for Program calls. Returned json looks like: | |
entries: [ | |
{ | |
programNumber: 1, | |
horseName: "The Horse", | |
odds: 2 | |
}, | |
{ | |
programNumber: 3, | |
horseName: "Another Horse", | |
odds: -1 | |
} | |
] | |
*/ | |
/** A Model that respresents a single runner entry in the array returned from the program api | |
Because the api we're calling is Java, we have to account for the fact that these properties could be null, so we make them nullable */ | |
data class ProgramResponseEntry( | |
val programNumber: Int?, | |
val horseName: String?, | |
val odds: Long?) | |
/** A Model that represents the list of runner entries returned from the program api | |
Again, this is coming from java, so the entire list could in fact be null, so we make it nullable. */ | |
data class ProgramResponse(val entries: List<ProgramResponseEntry>?) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment