Can a lens be used to convert from `String` to `T`...
# http4k
a
Can a lens be used to convert from
String
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)
Copy code
val bodyString = """
      {
        "login": "arnabkd",
        "id": 1014699
      }
    """.trimIndent()
    val lens = Body.auto<GithubUser>().toLens()
    // How to convert into GithubUser?
For more context, the function in its entirety:
Copy code
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
}
d
The body lens you have is designed to extract/inject an object from an http4k message object - it doesn't deal with strings. Under the covers it is using the Jackson/Moshi object (from which you import the auto() method) to call Jackson.asA(). You can do the same 🙃
If you use the http4k OkHttp client module to.make the request you can bypass what you're trying to do and use the lens
a
Copy code
val httpClient = OkHttpClient()
d
The uniform server/client interface provided by Server as a Function model is one of the most powerful aspects of http4k - you should definitely use it!
No use
OkHttp()
a
Ah, thanks
Suggest that you check out some of the talks on http4k concepts to see all the things we can do with it. 🙃
a
I did! It’s one of the things that inspired me to give http4k a try for a side-project 😄 It’s just very challenging to get my head around because I come from a world that is very very different xD
d
No problem - we know that it's very different - but beware - once you've used it it's very hard to go back to the old world... 😉
😢 1
But we're always here to answer questions about all this stuff and help 🙃
a
Thanks! That first link helped a lot 😄 Ended up with a neat little function like so:
Copy code
fun 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
}
d
You can also use a Cookie lens 😉
And you can use a filter to add the Bearer token:
ClientFilters.BearerAuth(token, "token").then(http)(request)
Once you start looking, http4k is function composition all the way down... 😉
And the Accept header isn't required for that GitHub call btw 😉😉