Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Legg til openapi-dokumentasjon for trenger-endepunkt #291

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ val mockOauth2ServerVersion: String by project
val tokenSupportVersion: String by project
val valiktorVersion: String by project

plugins {
id("org.hidetake.swagger.generator") version "2.19.2"
}

tasks {
test {
environment("IDPORTEN_WELL_KNOWN_URL", "http://localhost:6666/idporten-issuer/.well-known/openid-configuration")
Expand All @@ -13,12 +17,21 @@ tasks {
}
}

swaggerSources {
register("simba") {
setInputFile(file("src/main/resources/openapi/documentation.yaml"))
}
}

dependencies {
swaggerUI("org.webjars:swagger-ui:5.6.1")

implementation("io.ktor:ktor-serialization-kotlinx-json:$ktorVersion")
implementation("io.ktor:ktor-server-content-negotiation:$ktorVersion")
implementation("io.ktor:ktor-server-core:$ktorVersion")
implementation("io.ktor:ktor-server-netty:$ktorVersion")
implementation("io.ktor:ktor-server-status-pages:$ktorVersion")
implementation("io.ktor:ktor-server-swagger:$ktorVersion")
implementation("io.lettuce:lettuce-core:$lettuceVersion")
implementation("no.nav.helsearbeidsgiver:altinn-client:$altinnVersion")
implementation("no.nav.security:token-client-core:$tokenSupportVersion")
Expand All @@ -29,6 +42,4 @@ dependencies {
testImplementation("io.ktor:ktor-client-core:$ktorVersion")
testImplementation("io.ktor:ktor-server-test-host:$ktorVersion")
testImplementation("no.nav.security:mock-oauth2-server:$mockOauth2ServerVersion")
testImplementation(project(":felles-test"))
testImplementation(kotlin("test"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import io.ktor.server.engine.embeddedServer
import io.ktor.server.netty.Netty
import io.ktor.server.plugins.contentnegotiation.ContentNegotiation
import io.ktor.server.plugins.statuspages.StatusPages
import io.ktor.server.plugins.swagger.swaggerUI
import io.ktor.server.response.respondText
import io.ktor.server.routing.get
import io.ktor.server.routing.route
Expand Down Expand Up @@ -58,6 +59,9 @@ fun startServer(env: Map<String, String> = System.getenv()) {
}

fun Application.apiModule(rapid: RapidsConnection) {
val redisPoller = RedisPoller()
val tilgangCache = LocalCache<Tilgang>(60.minutes, 100)

customAuthentication()

install(ContentNegotiation) {
Expand All @@ -76,23 +80,17 @@ fun Application.apiModule(rapid: RapidsConnection) {
HelsesjekkerRouting()

routing {
get("/") {
call.respondText("helsearbeidsgiver inntektsmelding")
}

val redisPoller = RedisPoller()

val tilgangCache = LocalCache<Tilgang>(60.minutes, 100)

authenticate {
route(Routes.PREFIX) {
trengerRoute(rapid, redisPoller, tilgangCache)
routeExtra(rapid, redisPoller, tilgangCache) {
trengerRoute()
inntektRoute()
innsendingRoute()
kvitteringRoute()
}
}
}

swaggerUI(path = "swagger", swaggerFile = "openapi/documentation.yaml")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@ package no.nav.helsearbeidsgiver.inntektsmelding.api.trenger
import io.ktor.http.HttpStatusCode
import io.ktor.server.application.application
import io.ktor.server.application.call
import io.ktor.server.request.receive
import io.ktor.server.response.respond
import io.ktor.server.routing.Route
import io.ktor.server.routing.post
import io.ktor.server.routing.route
import io.prometheus.client.Summary
import kotlinx.serialization.builtins.serializer
import no.nav.helse.rapids_rivers.RapidsConnection
import no.nav.helsearbeidsgiver.felles.Tilgang
import no.nav.helsearbeidsgiver.felles.TrengerData
import no.nav.helsearbeidsgiver.inntektsmelding.api.RedisPoller
import no.nav.helsearbeidsgiver.inntektsmelding.api.RedisPollerTimeoutException
import no.nav.helsearbeidsgiver.inntektsmelding.api.Routes
import no.nav.helsearbeidsgiver.inntektsmelding.api.auth.ManglerAltinnRettigheterException
Expand All @@ -17,29 +23,30 @@ import no.nav.helsearbeidsgiver.inntektsmelding.api.logger
import no.nav.helsearbeidsgiver.inntektsmelding.api.response.RedisTimeoutResponse
import no.nav.helsearbeidsgiver.inntektsmelding.api.sikkerLogger
import no.nav.helsearbeidsgiver.inntektsmelding.api.tilgang.TilgangProducer
import no.nav.helsearbeidsgiver.inntektsmelding.api.utils.RouteExtra
import no.nav.helsearbeidsgiver.inntektsmelding.api.utils.receive
import no.nav.helsearbeidsgiver.inntektsmelding.api.utils.respond
import no.nav.helsearbeidsgiver.inntektsmelding.api.utils.respondBadRequest
import no.nav.helsearbeidsgiver.inntektsmelding.api.utils.respondForbidden
import no.nav.helsearbeidsgiver.inntektsmelding.api.utils.respondInternalServerError
import no.nav.helsearbeidsgiver.inntektsmelding.api.validation.ValidationError
import no.nav.helsearbeidsgiver.inntektsmelding.api.validation.ValidationResponse
import no.nav.helsearbeidsgiver.utils.cache.LocalCache
import no.nav.helsearbeidsgiver.utils.json.fromJson

fun RouteExtra.trengerRoute() {
val trengerProducer = TrengerProducer(connection)
val tilgangProducer = TilgangProducer(connection)
fun Route.trengerRoute(
rapid: RapidsConnection,
redis: RedisPoller,
tilgangCache: LocalCache<Tilgang>
) {
val trengerProducer = TrengerProducer(rapid)
val tilgangProducer = TilgangProducer(rapid)
val requestLatency = Summary.build()
.name("simba_trenger_latency_seconds")
.help("trenger endpoint latency in seconds")
.register()

route.route(Routes.TRENGER) {
route(Routes.TRENGER) {
post {
val requestTimer = requestLatency.startTimer()
runCatching {
receive(TrengerRequest.serializer())
call.receive<TrengerRequest>()
}
.onSuccess { request ->
logger.info("Henter data for uuid: ${request.uuid}")
Expand All @@ -58,12 +65,12 @@ fun RouteExtra.trengerRoute() {
val status = if (trengerResponse.feilReport == null) {
HttpStatusCode.Created
} else if (trengerResponse.feilReport.status() < 0) HttpStatusCode.ServiceUnavailable else HttpStatusCode.Created
respond(status, trengerResponse, TrengerResponse.serializer())
call.respond(status, trengerResponse)
} catch (e: ManglerAltinnRettigheterException) {
respondForbidden("Du har ikke rettigheter for organisasjon.", String.serializer())
call.respond(HttpStatusCode.Forbidden, "Du har ikke rettigheter for organisasjon.")
} catch (_: RedisPollerTimeoutException) {
logger.info("Fikk timeout for ${request.uuid}")
respondInternalServerError(RedisTimeoutResponse(request.uuid), RedisTimeoutResponse.serializer())
call.respond(HttpStatusCode.InternalServerError, RedisTimeoutResponse(request.uuid))
}
}.also {
requestTimer.observeDuration()
Expand All @@ -79,7 +86,7 @@ fun RouteExtra.trengerRoute() {
)
)
)
respondBadRequest(response, ValidationResponse.serializer())
call.respond(HttpStatusCode.BadRequest, response)
}
}
}
Expand Down
Loading