Last active
March 15, 2022 22:41
-
-
Save oharaandrew314/1c273ce16614070cba076adbf8909805 to your computer and use it in GitHub Desktop.
hexagonal-clients-clientV2
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
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