How do I use `io.ktor.application.call` in my cust...
# ktor
a
How do I use
io.ktor.application.call
in my custom function without parametrizing it?
c
Could you please clarify what exactly are you trying to achieve ?
a
I have a function like
Copy code
fun <A>Routing.paginate(call: ApplicationCall, collectionName: String, genericGet: (Int, Int) -> Pair<Int, List<A>>): Map<String, Any> {
    val page = call.parameters["page"]?.toInt() ?: 1
    val perPage = call.parameters["perPage"]?.toInt() ?: 10
    val (nPages, collection) = genericGet(page, perPage)

    val jsonMap = mutableMapOf<String, Any>(collectionName to collection)

    if (page > 1)
        jsonMap["prev"] = "/${collectionName}?page=${page - 1}"

    if (nPages > page)
        jsonMap["next"] = "/${collectionName}?page=${page + 1}"

    return jsonMap
}
I want to implicitly pass
call
inside it. Is there any way?
e
just write the extension for the
ApplicationCall
instead of the not used
Routing
?
c
Why do you have receiver of type
Routing
?
yes, you can write it on ApplicationCall instead
or
PipelineContext<*, ApplicationCall>
(in the second case you don't need to specify
call.
on call-site
a
Thanks! That's what I wanted.
c
Copy code
private fun PipelineContext<*, ApplicationCall>.paginate(): Map<String, Any> {
    val param1 = call.request.queryParameters["...."]
    
    TODO()
}

private fun Routing.myAppComponent() {
    get("/table") {
        val myMap = paginate()
        // ....
        
        call.respond("....")
    }
}