Skip to content

Commit de45c73

Browse files
authored
Set User-Agent header for all clients (#480)
* Set User-Agent header for all clients - Define a property with the format of the header - Set the header on declarative clients - Make the app version available in a property * Remove unused import * Workaround for Testcontainers incompatibility * Format
1 parent 4855697 commit de45c73

File tree

7 files changed

+32
-10
lines changed

7 files changed

+32
-10
lines changed

.github/workflows/run-tests.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,17 @@ jobs:
1111
runs-on: ubuntu-latest
1212
timeout-minutes: 20
1313
steps:
14-
1514
- name: Check out code
1615
uses: actions/checkout@v6
1716
with:
1817
fetch-depth: 0
1918

19+
- name: Set up Docker
20+
uses: docker/setup-docker-action@v4
21+
with:
22+
version: v28.0.0
23+
set-host: 'true'
24+
2025
- name: Set up Java
2126
uses: actions/setup-java@v5
2227
with:

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ Framework: Micronaut
1616

1717
Data store: MongoDB Atlas
1818

19+
## Docker
20+
21+
:warning: For running tests Docker versions before v29 must be used due to incompatibility in the API version used by Testcontainers. This will be resolved once we upgrade to Micronaut Test Resources v3+.
22+
1923
## Contributing
2024

2125
See the [contributing guide](./CONTRIBUTING.md).

build.gradle.kts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,16 @@ tasks.register<Copy>("copyPublicOpenApiSpec") {
216216
from(layout.buildDirectory.file("generated/ksp/main/resources/META-INF/swagger/variable-definitions-public.yml"))
217217
into(layout.projectDirectory.dir("openapi"))
218218
}
219+
220+
val setVersionProperty by tasks.register<ProcessResources>("setVersionProperty") {
221+
from(layout.projectDirectory.dir("src/main/resources")) {
222+
include("**/application.yml")
223+
filter {
224+
it.replace("{{APP_VERSION}}", version.toString())
225+
}
226+
}
227+
into(layout.buildDirectory.dir("resources/main"))
228+
}
229+
230+
tasks.processResources { finalizedBy(setVersionProperty) }
231+
tasks.classes { mustRunAfter(setVersionProperty) }

src/main/kotlin/no/ssb/metadata/vardef/integrations/dapla/services/DaplaTeamApiClient.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import io.micronaut.http.HttpHeaders.USER_AGENT
66
import io.micronaut.http.HttpResponse
77
import io.micronaut.http.annotation.Get
88
import io.micronaut.http.annotation.Header
9-
import io.micronaut.http.annotation.Headers
109
import io.micronaut.http.annotation.PathVariable
1110
import io.micronaut.http.client.annotation.Client
1211
import no.ssb.metadata.vardef.integrations.dapla.models.Group
@@ -16,10 +15,8 @@ import no.ssb.metadata.vardef.integrations.dapla.models.Team
1615
* A declarative client for the Dapla Team API
1716
*/
1817
@Client(id = "dapla-team-api")
19-
@Headers(
20-
Header(name = USER_AGENT, value = "VarDef API"),
21-
Header(name = ACCEPT, value = "application/hal+json"),
22-
)
18+
@Header(name = USER_AGENT, value = $$"${micronaut.http.request-headers.user-agent}")
19+
@Header(name = ACCEPT, value = "application/hal+json")
2320
interface DaplaTeamApiClient {
2421
@Get("/teams/{teamId}")
2522
@SingleResult

src/main/kotlin/no/ssb/metadata/vardef/integrations/klass/service/KlassApiClient.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@ import no.ssb.metadata.vardef.models.SupportedLanguages
1414
* A declarative client for the Klass API
1515
*/
1616
@Client(id = "klass")
17-
@Headers(
18-
Header(name = USER_AGENT, value = "VarDef API"),
19-
Header(name = ACCEPT, value = "application/json"),
20-
)
17+
@Header(name = USER_AGENT, value = $$"${micronaut.http.request-headers.user-agent}")
18+
@Header(name = ACCEPT, value = "application/json")
2119
interface KlassApiClient {
2220
@Get("classifications/{classificationId}")
2321
@SingleResult

src/main/kotlin/no/ssb/metadata/vardef/integrations/vardok/client/VardokClient.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package no.ssb.metadata.vardef.integrations.vardok.client
22

33
import io.micronaut.core.async.annotation.SingleResult
44
import io.micronaut.http.HttpHeaders.ACCEPT
5+
import io.micronaut.http.HttpHeaders.USER_AGENT
56
import io.micronaut.http.MediaType
67
import io.micronaut.http.annotation.Get
78
import io.micronaut.http.annotation.Header
@@ -10,6 +11,7 @@ import io.micronaut.http.client.annotation.Client
1011

1112
@Client(id = "vardok")
1213
@Produces(MediaType.APPLICATION_XML)
14+
@Header(name = USER_AGENT, value = $$"${micronaut.http.request-headers.user-agent}")
1315
@Header(name = ACCEPT, value = "application/xml")
1416
interface VardokClient {
1517
@Produces(MediaType.APPLICATION_XML)

src/main/resources/application.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
micronaut:
22
application:
33
name: vardef
4+
version: {{APP_VERSION}} # Placeholder filled by the setVersionProperty task during the Gradle build
45
auth:
56
issuers:
67
labid:
@@ -108,6 +109,8 @@ micronaut:
108109
expire-after-write: 9h
109110
enabled: false
110111
http:
112+
request-headers:
113+
user-agent: ${micronaut.application.name}/${micronaut.application.version} (metadata@ssb.no)
111114
services:
112115
klass:
113116
url: https://data.ssb.no/api/klass/v1/

0 commit comments

Comments
 (0)