update tools for urls

This commit is contained in:
InsanusMokrassar 2020-08-31 00:46:53 +06:00
parent 617ac31ca1
commit 9a5585957f
2 changed files with 23 additions and 0 deletions

View File

@ -7,3 +7,17 @@ fun buildStandardUrl(
) = "$basePart/$subpart".includeQueryParams(
parameters
)
fun buildStandardUrl(
basePart: String,
subpart: String,
parameters: List<QueryParam>
) = "$basePart/$subpart".includeQueryParams(
parameters
)
fun buildStandardUrl(
basePart: String,
subpart: String,
vararg parameters: QueryParam
) = buildStandardUrl(basePart, subpart, parameters.toList())

View File

@ -1,13 +1,22 @@
package com.insanusmokrassar.postssystem.ktor
typealias QueryParam = Pair<String, String?>
typealias QueryParams = Map<String, String?>
val QueryParams.asUrlQuery: String
get() = keys.joinToString("&") { "${it}${get(it) ?.let { value -> "=$value" }}" }
val List<QueryParam>.asUrlQuery: String
get() = joinToString("&") { (key, value) -> "${key}${value ?.let { _ -> "=$value" }}" }
fun String.includeQueryParams(
queryParams: QueryParams
): String = "$this${if (contains("?")) "&" else "?"}${queryParams.asUrlQuery}"
fun String.includeQueryParams(
queryParams: List<QueryParam>
): String = "$this${if (contains("?")) "&" else "?"}${queryParams.asUrlQuery}"
val String.parseUrlQuery: QueryParams
get() = split("&").map {
it.split("=").let { pair ->