-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOppfolgingstilfelleClient.kt
152 lines (140 loc) · 6.39 KB
/
OppfolgingstilfelleClient.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package no.nav.syfo.client.oppfolgingstilfelle
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.plugins.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import net.logstash.logback.argument.StructuredArguments
import no.nav.syfo.application.cache.ValkeyStore
import no.nav.syfo.client.azuread.AzureAdV2Client
import no.nav.syfo.client.httpClientDefault
import no.nav.syfo.domain.PersonIdent
import no.nav.syfo.client.tokendings.TokendingsClient
import no.nav.syfo.domain.Virksomhetsnummer
import no.nav.syfo.util.*
import org.slf4j.LoggerFactory
import java.util.UUID
class OppfolgingstilfelleClient(
private val azureAdV2Client: AzureAdV2Client,
private val tokendingsClient: TokendingsClient,
private val isoppfolgingstilfelleClientId: String,
isoppfolgingstilfelleBaseUrl: String,
private val cache: ValkeyStore,
private val httpClient: HttpClient = httpClientDefault()
) {
private val personOppfolgingstilfelleUrl: String =
"$isoppfolgingstilfelleBaseUrl$ISOPPFOLGINGSTILFELLE_OPPFOLGINGSTILFELLE_PERSON_PATH"
private val personOppfolgingstilfelleSystemUrl: String =
"$isoppfolgingstilfelleBaseUrl$ISOPPFOLGINGSTILFELLE_OPPFOLGINGSTILFELLE_SYSTEM_PERSON_PATH"
private val oppfolgingstilfelleNLUrl: String =
"$isoppfolgingstilfelleBaseUrl$ISOPPFOLGINGSTILFELLE_OPPFOLGINGSTILFELLE_NARMESTELEDER_PATH"
private val CACHE_OPPFOLGINGSTILFELLE_NL_KEY_PREFIX = "oppfolgingstilfelle-nl-"
suspend fun oppfolgingstilfellePerson(
personIdent: PersonIdent,
token: String,
callId: String? = null,
): Oppfolgingstilfelle? =
oppfolgingstilfelle(
personIdent = personIdent,
path = personOppfolgingstilfelleUrl,
token = token,
callId = callId,
)
suspend fun oppfolgingstilfelleSystem(
personIdent: PersonIdent,
): Oppfolgingstilfelle? =
oppfolgingstilfelle(
personIdent = personIdent,
path = personOppfolgingstilfelleSystemUrl,
)
private suspend fun oppfolgingstilfelle(
personIdent: PersonIdent,
path: String,
token: String? = null,
callId: String? = null,
): Oppfolgingstilfelle? {
val callIdToUse = callId ?: UUID.randomUUID().toString()
return try {
val response: HttpResponse = httpClient.get(path) {
header(HttpHeaders.Authorization, bearerHeader(getAzureAccessToken(token)))
header(NAV_CALL_ID_HEADER, callIdToUse)
header(NAV_PERSONIDENT_HEADER, personIdent.value)
accept(ContentType.Application.Json)
}
val oppfolgingstilfellePerson = response.body<OppfolgingstilfellePersonDTO>()
.toLatestOppfolgingstilfelle()
COUNT_CALL_OPPFOLGINGSTILFELLE_PERSON_SUCCESS.increment()
oppfolgingstilfellePerson
} catch (responseException: ResponseException) {
log.error(
"Error while requesting OppfolgingstilfellePerson from Isoppfolgingstilfelle with {}, {}",
StructuredArguments.keyValue("statusCode", responseException.response.status.value),
callIdArgument(callIdToUse),
)
COUNT_CALL_OPPFOLGINGSTILFELLE_PERSON_FAIL.increment()
null
}
}
suspend fun oppfolgingstilfelleTokenx(
arbeidstakerPersonIdentNumber: PersonIdent,
narmesteLederPersonIdentNumber: PersonIdent,
tokenx: String,
virksomhetsnummer: Virksomhetsnummer,
callId: String,
): List<Oppfolgingstilfelle>? {
val oppfolgingstilfelleNLCacheKey =
"$CACHE_OPPFOLGINGSTILFELLE_NL_KEY_PREFIX$narmesteLederPersonIdentNumber-$arbeidstakerPersonIdentNumber-$virksomhetsnummer"
val cachedOppfolgingstilfelle = cache.getListObject<Oppfolgingstilfelle>(oppfolgingstilfelleNLCacheKey)
return if (cachedOppfolgingstilfelle != null) {
cachedOppfolgingstilfelle
} else {
val exchangedToken = tokendingsClient.getOnBehalfOfToken(
scopeClientId = isoppfolgingstilfelleClientId,
token = tokenx,
).accessToken
try {
val response: HttpResponse = httpClient.get(oppfolgingstilfelleNLUrl) {
header(HttpHeaders.Authorization, bearerHeader(exchangedToken))
header(NAV_CALL_ID_HEADER, callId)
header(NAV_PERSONIDENT_HEADER, arbeidstakerPersonIdentNumber.value)
header(NAV_VIRKSOMHETSNUMMER, virksomhetsnummer.value)
accept(ContentType.Application.Json)
}
val parsedResponse = response.body<List<OppfolgingstilfelleDTO>>().toOppfolgingstilfelle()
cache.setObject(
oppfolgingstilfelleNLCacheKey,
parsedResponse,
CACHE_NARMESTE_LEDER_EXPIRE_SECONDS
)
parsedResponse
} catch (responseException: ResponseException) {
log.error(
"Error while requesting four months date from Isoppfolgingstilfelle with {}, {}",
StructuredArguments.keyValue("statusCode", responseException.response.status.value),
callIdArgument(callId),
)
null
}
}
}
private suspend fun getAzureAccessToken(token: String? = null) =
if (token != null) {
azureAdV2Client.getOnBehalfOfToken(
scopeClientId = isoppfolgingstilfelleClientId,
token = token,
)
} else {
azureAdV2Client.getSystemToken(isoppfolgingstilfelleClientId)
}?.accessToken ?: throw RuntimeException("Failed to get azure token")
companion object {
const val ISOPPFOLGINGSTILFELLE_OPPFOLGINGSTILFELLE_PERSON_PATH =
"/api/internad/v1/oppfolgingstilfelle/personident"
const val ISOPPFOLGINGSTILFELLE_OPPFOLGINGSTILFELLE_SYSTEM_PERSON_PATH =
"/api/system/v1/oppfolgingstilfelle/personident"
const val ISOPPFOLGINGSTILFELLE_OPPFOLGINGSTILFELLE_NARMESTELEDER_PATH =
"/api/v1/narmesteleder/oppfolgingstilfelle"
private val log = LoggerFactory.getLogger(OppfolgingstilfelleClient::class.java)
const val CACHE_NARMESTE_LEDER_EXPIRE_SECONDS = 3600L
}
}