2021-03-01 14:17:06 +00:00
|
|
|
package dev.inmo.kmppscriptbuilder.core.models
|
2019-11-20 15:16:34 +00:00
|
|
|
|
2021-03-01 14:17:06 +00:00
|
|
|
import dev.inmo.kmppscriptbuilder.core.utils.serialFormat
|
2019-11-20 15:16:34 +00:00
|
|
|
import io.ktor.client.HttpClient
|
|
|
|
import io.ktor.client.request.get
|
|
|
|
import io.ktor.client.request.url
|
|
|
|
import kotlinx.serialization.Serializable
|
|
|
|
import kotlinx.serialization.builtins.MapSerializer
|
|
|
|
import kotlinx.serialization.builtins.serializer
|
|
|
|
|
|
|
|
@Serializable
|
|
|
|
data class License(
|
|
|
|
val id: String,
|
|
|
|
val title: String,
|
|
|
|
val url: String? = null
|
|
|
|
)
|
|
|
|
|
|
|
|
private val commonLicensesListDeserializer = MapSerializer(String.serializer(), License.serializer())
|
|
|
|
|
|
|
|
private var licenses: Map<String, License>? = null
|
|
|
|
|
|
|
|
suspend fun HttpClient.getLicenses(): Map<String, License> {
|
|
|
|
val answer = get<String> {
|
|
|
|
url("https://licenses.opendefinition.org/licenses/groups/all.json")
|
|
|
|
}
|
|
|
|
return serialFormat.decodeFromString(commonLicensesListDeserializer, answer).also { gotLicenses ->
|
|
|
|
licenses = gotLicenses
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
suspend fun HttpClient.searchLicense(name: String): List<License> {
|
|
|
|
val licenses = licenses ?: getLicenses()
|
2021-09-04 13:34:24 +00:00
|
|
|
val lowerCase = name.lowercase()
|
|
|
|
val upperCase = name.uppercase()
|
2019-11-20 15:16:34 +00:00
|
|
|
return licenses.values.filter {
|
2021-09-04 13:34:24 +00:00
|
|
|
it.title.lowercase().contains(lowerCase) || it.title.uppercase().contains(upperCase) || it.title.contains(name)
|
|
|
|
|| it.id.lowercase().contains(lowerCase) || it.id.uppercase().contains(upperCase) || it.id.contains(name)
|
2019-11-20 15:16:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fun Map<String, License>.searchLicense(name: String): List<License> {
|
2021-09-04 13:34:24 +00:00
|
|
|
val lowerCase = name.lowercase()
|
|
|
|
val upperCase = name.uppercase()
|
2019-11-20 15:16:34 +00:00
|
|
|
return values.filter {
|
2021-09-04 13:34:24 +00:00
|
|
|
it.title.lowercase().contains(lowerCase) || it.title.uppercase().contains(upperCase) || it.title.contains(name)
|
|
|
|
|| it.id.lowercase().contains(lowerCase) || it.id.uppercase().contains(upperCase) || it.id.contains(name)
|
2019-11-20 15:16:34 +00:00
|
|
|
}
|
|
|
|
}
|