KTor URLBuilder 编码路径与动态路径

meh*_*dok 6 android kotlin ktor kotlin-multiplatform

我正在尝试为多平台项目构建一个网络模块ktor。我的请求代码GET是这样的:

val result = httpClient.get<HttpResponse> {
                    url {
                        protocol = baseProtocol
                        host = baseUrl
                        encodedPath = urlPath

                    }
}
Run Code Online (Sandbox Code Playgroud)

在某些时候,我的路径包含这样的用户 ID /users/{user_id}。我可以在字符串中进行搜索和替换,并将其替换user_id为实际值,但是还有其他方法可以做到这一点吗?任何ktor具体方式。

例如Retrofit我们有这个:

@GET("users/{user_id}/")
SomeData getUserData(@Path("user_id") String userId);
Run Code Online (Sandbox Code Playgroud)

编辑:添加更多代码

val result = httpClient.get<HttpResponse> {
                    url {
                        protocol = baseProtocol
                        host = baseUrl

                        var requestPath = request.requestPath.value
                        request.path?.forEach {
                            requestPath = requestPath.replace(it.first, it.second)
                        }
                        encodedPath = requestPath

                        if (request.parameters != null) {
                            parameters.appendAll(getParametersFromList(request.parameters))
                        }
                    }
Run Code Online (Sandbox Code Playgroud)

替换request.path?.forEach { requestPath = requestPath.replace(it.first, it.second)}任何运行时路径值。