Skip to content

Instantly share code, notes, and snippets.

@oharaandrew314
Last active March 15, 2022 22:41
Show Gist options
  • Save oharaandrew314/1c273ce16614070cba076adbf8909805 to your computer and use it in GitHub Desktop.
Save oharaandrew314/1c273ce16614070cba076adbf8909805 to your computer and use it in GitHub Desktop.
hexagonal-clients-clientV2
class ClientV2(host: String) {
object Lenses {
val catId = Path.uuid().of("id")
val cat = Body.auto<CatDtoV2>().toLens()
val appointments = Body.auto<List<AppointmentDtoV2>>().toLens()
}
private val backend = ClientFilters.SetHostFrom(Uri.of(host))
.then(JavaHttpClient())
operator fun get(catId: UUID): CatDtoV2? {
val response = Request(Method.GET, "/v2/cats/${Lenses.catId}")
.with(Lenses.catId of catId)
.let(backend)
return when(response.status) {
Status.OK -> Lenses.cat(response)
Status.NOT_FOUND -> null
else -> throw IOException("Error getting cat: $response")
}
}
fun getAppointments(catId: UUID): List<AppointmentDtoV2> {
val response = Request(Method.GET, "/v2/cats/${Lenses.catId}/appointments")
.with(Lenses.catId of catId)
.let(backend)
return when(response.status) {
Status.OK -> Lenses.appointments(response)
else -> throw IOException("Error getting appoints for cat: $catId")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment