-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a bit more logging and refactor of PDL consumer (#536)
- Loading branch information
1 parent
5830ff9
commit 0b3e2d8
Showing
7 changed files
with
79 additions
and
156 deletions.
There are no files selected for viewing
147 changes: 40 additions & 107 deletions
147
src/main/kotlin/no/nav/syfo/consumer/pdl/PdlConsumer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,141 +1,74 @@ | ||
package no.nav.syfo.consumer.pdl | ||
|
||
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 io.ktor.network.sockets.* | ||
import no.nav.syfo.UrlEnv | ||
import no.nav.syfo.auth.AzureAdTokenConsumer | ||
import no.nav.syfo.metrics.COUNT_CALL_PDL_FAIL | ||
import no.nav.syfo.metrics.COUNT_CALL_PDL_SUCCESS | ||
import no.nav.syfo.utils.httpClientWithRetry | ||
import no.nav.syfo.utils.isAlderMindreEnnGittAr | ||
import org.slf4j.LoggerFactory | ||
import java.io.FileNotFoundException | ||
|
||
open class PdlConsumer(private val urlEnv: UrlEnv, private val azureAdTokenConsumer: AzureAdTokenConsumer) { | ||
private val client = httpClientWithRetry() | ||
private val httpClient = httpClientWithRetry(expectSuccess = true) | ||
private val log = LoggerFactory.getLogger(PdlConsumer::class.qualifiedName) | ||
|
||
open suspend fun getFnr(aktorId: String): String? { | ||
val response = callPdl(IDENTER_QUERY, aktorId) | ||
|
||
return when (response?.status) { | ||
HttpStatusCode.OK -> { | ||
val pdlResponse = response.body<PdlIdentResponse>().data?.hentIdenter?.identer?.first()?.ident | ||
pdlResponse | ||
} | ||
|
||
HttpStatusCode.NoContent -> { | ||
log.error("Could not get fnr from PDL: No content found in the response body") | ||
null | ||
} | ||
|
||
HttpStatusCode.Unauthorized -> { | ||
log.error("Could not get fnr from PDL: Unable to authorize") | ||
null | ||
} | ||
|
||
else -> { | ||
log.error("Could not get fnr from PDL: $response") | ||
null | ||
} | ||
suspend fun isBrukerYngreEnnGittMaxAlder(ident: String, maxAlder: Int): Boolean { | ||
val fodselsdato = hentPerson(ident)?.getFodselsdato() | ||
if (fodselsdato == null) { | ||
log.warn("Returnert fødselsdato for en person fra PDL er null. Fortsetter som om bruker er yngre enn $maxAlder år da fødselsdato er ukjent.") | ||
return true | ||
} else { | ||
return isAlderMindreEnnGittAr(fodselsdato, maxAlder) | ||
} | ||
} | ||
|
||
suspend fun isBrukerYngreEnnGittMaxAlder(ident: String, maxAlder: Int): Boolean { | ||
val response = callPdl(PERSON_QUERY, ident) | ||
suspend fun hentPerson( | ||
personIdent: String, | ||
): HentPersonData? { | ||
val token = azureAdTokenConsumer.getToken(urlEnv.pdlScope) | ||
val query = getPdlQuery("/pdl/hentPerson.graphql") | ||
val request = PdlRequest(query, Variables(personIdent)) | ||
|
||
val response: HttpResponse = httpClient.post(urlEnv.pdlUrl) { | ||
setBody(request) | ||
header(HttpHeaders.ContentType, "application/json") | ||
header(HttpHeaders.Authorization, "Bearer $token") | ||
header(PDL_BEHANDLINGSNUMMER_HEADER, BEHANDLINGSNUMMER_VURDERE_RETT_TIL_SYKEPENGER) | ||
} | ||
|
||
return when (response?.status) { | ||
when (response.status) { | ||
HttpStatusCode.OK -> { | ||
val fodselsdato = response.body<HentPersonResponse>().data.getFodselsdato() | ||
if (fodselsdato == null) { | ||
log.warn("Returnert fødselsdato for en person fra PDL er null. Fortsetter som om bruker er yngre enn $maxAlder år da fødselsdato er ukjent.") | ||
return true | ||
val pdlPersonReponse = response.body<HentPersonResponse>() | ||
return if (!pdlPersonReponse.errors.isNullOrEmpty()) { | ||
COUNT_CALL_PDL_FAIL.increment() | ||
pdlPersonReponse.errors.forEach { | ||
log.error("Error while requesting person from PersonDataLosningen: ${it.errorMessage()}") | ||
} | ||
null | ||
} else { | ||
return isAlderMindreEnnGittAr(fodselsdato, maxAlder) | ||
COUNT_CALL_PDL_SUCCESS.increment() | ||
pdlPersonReponse.data | ||
} | ||
} | ||
|
||
HttpStatusCode.NoContent -> { | ||
log.error("Could not get adressesperre from PDL: No content found in the response body") | ||
true | ||
} | ||
|
||
HttpStatusCode.Unauthorized -> { | ||
log.error("Could not get adressesperre from PDL: Unable to authorize") | ||
true | ||
} | ||
|
||
else -> { | ||
log.error("Could not get adressesperre from PDL: $response") | ||
true | ||
COUNT_CALL_PDL_FAIL.increment() | ||
log.error("Request with url: $urlEnv.pdlUrl failed with reponse code ${response.status.value}") | ||
return null | ||
} | ||
} | ||
} | ||
|
||
open suspend fun hentPerson(fnr: String): HentPersonData? { | ||
val response = callPdl(PERSON_QUERY, fnr) | ||
|
||
return when (response?.status) { | ||
HttpStatusCode.OK -> { | ||
val pdlResponse = response.body<HentPersonResponse>().data | ||
pdlResponse | ||
} | ||
|
||
HttpStatusCode.NoContent -> { | ||
log.error("Could not get navn from PDL: No content found in the response body") | ||
null | ||
} | ||
|
||
HttpStatusCode.Unauthorized -> { | ||
log.error("Could not get navn from PDL: Unable to authorize") | ||
null | ||
} | ||
|
||
else -> { | ||
log.error("Could not get fnr from PDL: $response") | ||
null | ||
} | ||
} | ||
} | ||
|
||
private suspend fun callPdl(service: String, ident: String): HttpResponse? { | ||
val token = azureAdTokenConsumer.getToken(urlEnv.pdlScope) | ||
val bearerTokenString = "Bearer $token" | ||
val graphQueryResourcePath = "$QUERY_PATH_PREFIX/$service" | ||
val graphQuery = | ||
this::class.java.getResource(graphQueryResourcePath)?.readText()?.replace("[\n\r]", "") | ||
?: throw FileNotFoundException("Could not found resource: $graphQueryResourcePath") | ||
val requestBody = PdlRequest(graphQuery, Variables(ident)) | ||
return try { | ||
log.info("Calling PDL") | ||
val response = client.post(urlEnv.pdlUrl) { | ||
headers { | ||
append(PDL_BEHANDLINGSNUMMER_HEADER, BEHANDLINGSNUMMER_VURDERE_RETT_TIL_SYKEPENGER) | ||
append(HttpHeaders.ContentType, ContentType.Application.Json) | ||
append(HttpHeaders.Authorization, bearerTokenString) | ||
} | ||
setBody(requestBody) | ||
} | ||
log.info("Received response from PDL with status: ${response.status}") | ||
response | ||
} catch (e: SocketTimeoutException) { | ||
log.error("SocketTimeoutException while calling PDL ($service): ${e.message}", e) | ||
null | ||
} catch (e: ClientRequestException) { | ||
log.error("ClientRequestException while calling PDL ($service): ${e.message}", e) | ||
null | ||
} catch (e: Exception) { | ||
log.error("Error while calling PDL ($service): ${e.message}", e) | ||
null | ||
} | ||
private fun getPdlQuery(graphQueryResourcePath: String): String { | ||
return this::class.java.getResource(graphQueryResourcePath)?.readText()?.replace("[\n\r]", "") | ||
?: throw FileNotFoundException("Could not found resource: $graphQueryResourcePath") | ||
} | ||
|
||
class LocalPdlConsumer(urlEnv: UrlEnv, azureAdTokenConsumer: AzureAdTokenConsumer) : | ||
PdlConsumer(urlEnv, azureAdTokenConsumer) { | ||
override suspend fun getFnr(aktorId: String): String { | ||
return aktorId.substring(0, 11) | ||
} | ||
} | ||
PdlConsumer(urlEnv, azureAdTokenConsumer) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters