com.lithic.api.core.Utils.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lithic-kotlin-core Show documentation
Show all versions of lithic-kotlin-core Show documentation
The Lithic Developer API is designed to provide a predictable programmatic
interface for accessing your Lithic account through an API and transaction
webhooks. Note that your API key is a secret and should be treated as such.
Don't share it with anyone, including us. We will never ask you for it.
@file:JvmName("Utils")
package com.lithic.api.core
import com.google.common.collect.ImmutableListMultimap
import com.google.common.collect.ListMultimap
import com.google.common.collect.Multimaps
import com.lithic.api.errors.LithicInvalidDataException
import java.util.Collections
internal fun T?.getOrThrow(name: String): T {
if (this == null) {
throw LithicInvalidDataException("'${name}' is not present")
}
return this
}
internal fun List.toUnmodifiable(): List {
if (isEmpty()) {
return Collections.emptyList()
}
return Collections.unmodifiableList(this)
}
internal fun Map.toUnmodifiable(): Map {
if (isEmpty()) {
return Collections.emptyMap()
}
return Collections.unmodifiableMap(this)
}
internal fun ListMultimap.toUnmodifiable(): ListMultimap {
if (isEmpty()) {
return ImmutableListMultimap.of()
}
return Multimaps.unmodifiableListMultimap(this)
}
internal fun ListMultimap.getRequiredHeader(header: String): String {
val value =
entries()
.stream()
.filter { entry -> entry.key.equals(header, ignoreCase = true) }
.map { entry -> entry.value }
.findFirst()
if (!value.isPresent) {
throw LithicInvalidDataException("Could not find $header header")
}
return value.get()
}
internal interface Enum