-
Notifications
You must be signed in to change notification settings - Fork 0
/
CatFactProvider.kt
37 lines (32 loc) · 1.1 KB
/
CatFactProvider.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package cat.fact.client
import com.fasterxml.jackson.databind.ObjectMapper
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.coroutineScope
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.jackson.JacksonConverterFactory
import retrofit2.create
private const val API_BASE_URL = "https://catfact.ninja/"
class CatFactProvider(objectMapper: ObjectMapper) {
private var client = Retrofit.Builder()
.baseUrl(API_BASE_URL)
.client(OkHttpClient.Builder().build())
.addConverterFactory(JacksonConverterFactory.create(objectMapper))
.build()
.create<CatFactClient>()
/**
* Get a random cat facts.
*
* @param numberOfFacts the number of facts to get, by default only 1 fact is returned.
* @return a set of cat facts.
*/
suspend fun get(numberOfFacts: Int = 1): Set<Fact> =
coroutineScope {
(1..numberOfFacts).map {
async { client.fact() }
}.awaitAll()
.map { Fact(it.fact) }
.toSet()
}
}