jwstegemann / fritz2

Easily build reactive web-apps in Kotlin based on flows and coroutines.
https://www.fritz2.dev
MIT License
653 stars 28 forks source link

Parameter function for `http()` method #757

Closed violabs closed 11 months ago

violabs commented 1 year ago

It would be nice to have a convenient way to use request params like:

val item = "Hello"

http("api/endpoint").requestParams(
    "limited" to true,
    "option" to item
)

and have it construct:

api/endpoint?limited=true&option=Hello

I understand you can just create the http function with that string, but it would be nice to be able to reuse the base url without calling it every time.

Inkemann commented 1 year ago

You could easily solve this with your own extension function:


fun Request.requestParams(vararg params: Pair<Any, Any>) =
    copy(url = "${url}?${params.joinToString("&") { "${it.first}=${it.second}" }}")
ghost commented 1 year ago

@Inkemann Nice proposal, but it needs enhancements in order to become compliant to http: You need to encode the parameters to apply appropriate url parameter formating.

The overall idea is nice, but we have to carefully decide, where we draw the line between a dedicated http lib and the convenient wrappers we offer in fritz2. So not sure yet, whether we should and will offer this as built-in.

Inkemann commented 1 year ago

@chausknecht Unlike the built-in append function this also requires particular awareness if one doesn't want to create a completely broken url as it always has to be called last in a chain of url-building functions. There would have to be major changes to the api to allow for adding url parameters one at a time or comparable complex logic.

My quick and admittedly very dirty solution could be useful for the specific request made here, but it's not very versatile.

ghost commented 1 year ago

I would suggest having a look at the ktor http client. It comes with lots of features, runs on JS and Java and is quite mature. So for more than real basic use cases, I would always prefer some dedicated solution.

@violabs Does this proposal helps you? Can we close this issue?

violabs commented 1 year ago

Yeah. That should be fine. Maybe a mention in the docs would help not have this brought up again. Thanks!

Lysander commented 11 months ago

This feature is now implemented by PR #790 in https://github.com/jwstegemann/fritz2/releases/tag/v1.0-RC12