|
| 1 | +package no.nav.api.syfo |
| 2 | + |
| 3 | +import io.ktor.client.* |
| 4 | +import io.ktor.client.call.* |
| 5 | +import io.ktor.client.engine.okhttp.* |
| 6 | +import io.ktor.client.request.* |
| 7 | +import io.ktor.http.* |
| 8 | +import kotlinx.serialization.Serializable |
| 9 | +import no.nav.utils.* |
| 10 | + |
| 11 | +class SyfoClient( |
| 12 | + private val syfoUrl: String, |
| 13 | + private val oboTokenProvider: BoundedOnBehalfOfTokenClient, |
| 14 | +) { |
| 15 | + @Serializable |
| 16 | + class PersonIdentValue( |
| 17 | + val value: String, |
| 18 | + ) |
| 19 | + |
| 20 | + @Serializable |
| 21 | + class SyfoTildeling( |
| 22 | + val personIdent: PersonIdentValue, |
| 23 | + val tildeltVeilederIdent: String?, |
| 24 | + val tildeltenhet: String?, |
| 25 | + ) |
| 26 | + |
| 27 | + private val client = |
| 28 | + HttpClient(OkHttp) { |
| 29 | + installContentNegotiationAndIgnoreUnknownKeys() |
| 30 | + engine { |
| 31 | + addInterceptor(XCorrelationIdInterceptor()) |
| 32 | + addInterceptor( |
| 33 | + LoggingInterceptor( |
| 34 | + name = "isyfooversikt", |
| 35 | + callIdExtractor = { getCallId() }, |
| 36 | + ), |
| 37 | + ) |
| 38 | + addInterceptor( |
| 39 | + HeadersInterceptor { |
| 40 | + mapOf( |
| 41 | + "Nav-Consumer-Id" to navConsumerId, |
| 42 | + ) |
| 43 | + }, |
| 44 | + ) |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + suspend fun hentSyfoVeileder( |
| 49 | + fnr: String, |
| 50 | + token: String, |
| 51 | + ): SyfoTildeling? = |
| 52 | + externalServiceCall { |
| 53 | + val response = |
| 54 | + client.get("$syfoUrl/v2/persontildeling/personer/single") { |
| 55 | + header("nav-personident", fnr) |
| 56 | + header("Authorization", "Bearer ${oboTokenProvider.exchangeOnBehalfOfToken(token)}") |
| 57 | + } |
| 58 | + when (response.status) { |
| 59 | + HttpStatusCode.NoContent -> null |
| 60 | + HttpStatusCode.OK -> response.body() |
| 61 | + else -> error("Ukjent status code: ${response.status}") |
| 62 | + } |
| 63 | + } |
| 64 | +} |
0 commit comments