ted
05/08/2023, 12:56 PMdata class SomeRequestModel(
val account: String
)
to make this request?
localhost.com/account=1
Or do I really have to write “account” manually?
val someRequestModel =..
request = Request(Method.GET, ...)
.query("account", someRequestModel.account)
s4nchez
05/08/2023, 1:11 PMSomeRequestModel
would map to the query parameters you provided as example, but here's another example:
import org.http4k.core.Method
import org.http4k.core.Request
import org.http4k.core.with
import org.http4k.lens.Query
fun main() {
data class AccountId(val id: String)
val queryLens = Query.map(::AccountId, AccountId::id).multi.required("account")
//injecting
val ids = listOf(AccountId("1"), AccountId("2"))
val request = Request(Method.GET, "/")
.with(queryLens of ids)
println(request.uri) // prints /?accounts=1&accounts=2
//extracting
val extractedIds = queryLens(request)
println(extractedIds) // prints [AccountId(id=1), AccountId(id=2)]
}
Is that close to what you need?ted
05/08/2023, 1:12 PMted
05/08/2023, 1:12 PMs4nchez
05/08/2023, 1:12 PM.multi
and can use the map
to map a String to your specific request modeldave
05/08/2023, 1:12 PMted
05/08/2023, 1:14 PMCopy codeval queryLens = Query.map(::AccountId, AccountId::id).multi.required("account")
s4nchez
05/08/2023, 1:14 PMimport org.http4k.core.Method
import org.http4k.core.Request
import org.http4k.core.with
import org.http4k.lens.Query
fun main() {
data class SomeRequestModel(val accountId: String)
val queryLens = Query.map(::SomeRequestModel, SomeRequestModel::accountId).required("account")
//injecting
val request = Request(Method.GET, "/")
.with(queryLens of SomeRequestModel("12354"))
println(request.uri) // prints /?account=12354
//extracting
val extractedIds = queryLens(request)
println(extractedIds) // prints SomeRequestModel(accountId=12354)
}
ted
05/08/2023, 1:14 PMs4nchez
05/08/2023, 1:15 PMso you really need to write “account”Yes, we are explicit about that kind of thing, so that code refactoring won't accidentally change how messages will look like.
ted
05/08/2023, 1:35 PM