Skip to content

Instantly share code, notes, and snippets.

@oharaandrew314
Last active March 16, 2022 01:37
Show Gist options
  • Select an option

  • Save oharaandrew314/b1eb49b7e322ce389e78714217c45666 to your computer and use it in GitHub Desktop.

Select an option

Save oharaandrew314/b1eb49b7e322ce389e78714217c45666 to your computer and use it in GitHub Desktop.
hexagonal-clients-catsDaoV2
fun CatsDao.Companion.v2(client: ClientV2) = CatsDao { id ->
val uuid = UUID.fromString(id)
val cat = client[uuid] ?: return@CatsDao null
val appointments = client.getAppointments(uuid)
Cat(
id = cat.id.toString(),
name = cat.name,
brown = ColourV2.Brown in cat.colours,
grey = ColourV2.Grey in cat.colours,
ownerId = cat.owner.id.toString(),
ownerName = cat.owner.name,
breed = when(cat.breed) {
BreedV2.AmericanShortHair -> Breed.american_short_hair
BreedV2.Persian -> Breed.persian
BreedV2.MaineCoon -> Breed.maine_coon
else -> null // ignore new breeds we don't understand
},
appointments = appointments.map { it.time },
favouriteFood = cat.favouriteFood
)
}
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