Arnab
07/20/2022, 10:58 AMString
to T
? I have the following hardcoded bodyString (which I get from making a request via okHttp3 to Github’s user API). To me, it seems like I might have to use some http4k specific way of doing this? The documentation isn’t clear to me as to how I should proceed (doesn’t mean it’s lacking, just that I don’t understand it)
val bodyString = """
{
"login": "arnabkd",
"id": 1014699
}
""".trimIndent()
val lens = Body.auto<GithubUser>().toLens()
// How to convert into GithubUser?
fun contextFnGithub(request: Request): Principal {
val user = request.cookie("GithubAccessToken")?.value?.let { accessTokenString ->
val response = httpClient.newCall(
OkHttpRequest.Builder()
.header("Accept", "application/vnd.github+json")
.header("Authorization", "token $accessTokenString")
.url("<https://api.github.com/user>")
.get()
.build()
).execute()
println(response)
val bodyString = """
{
"login": "arnabkd",
"id": 1014699
}
""".trimIndent()
val lens = Body.auto<GithubUser>().toLens()
// How to convert into GithubUser?
null // TODO
} ?: AnonymousUser
return user
}
dave
07/20/2022, 11:05 AMArnab
07/20/2022, 11:07 AMval httpClient = OkHttpClient()
dave
07/20/2022, 11:08 AMOkHttp()
Arnab
07/20/2022, 11:08 AMdave
07/20/2022, 11:08 AMArnab
07/20/2022, 11:10 AMdave
07/20/2022, 11:11 AMArnab
07/20/2022, 11:26 AMfun contextFnGithub(request: Request): Principal {
val user = request.cookie("GithubAccessToken")?.value?.let { accessTokenString ->
val response = httpClient(
Request(Method.GET, "<https://api.github.com/user>")
.header("Accept", "application/vnd.github+json")
.header("Authorization", "token $accessTokenString")
)
when (response.status) {
Status.OK -> lens(response)
else -> null
}
} ?: AnonymousUser
return user
}
dave
07/20/2022, 11:27 AMClientFilters.BearerAuth(token, "token").then(http)(request)