From 8f8e5946d8f380bda0c72311d92283b0f548ce81 Mon Sep 17 00:00:00 2001 From: bayang Date: Tue, 5 Nov 2024 21:29:18 +0100 Subject: [PATCH] fix: add custom user-agent to calls to inventaireio --- .../providers/InventaireIoMetadataProvider.kt | 11 +++++++++++ .../providers/InventaireIoMetadataProviderTest.kt | 9 ++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/io/github/bayang/jelu/service/metadata/providers/InventaireIoMetadataProvider.kt b/src/main/kotlin/io/github/bayang/jelu/service/metadata/providers/InventaireIoMetadataProvider.kt index 5ad3fb18..f2b92743 100644 --- a/src/main/kotlin/io/github/bayang/jelu/service/metadata/providers/InventaireIoMetadataProvider.kt +++ b/src/main/kotlin/io/github/bayang/jelu/service/metadata/providers/InventaireIoMetadataProvider.kt @@ -7,6 +7,8 @@ import io.github.bayang.jelu.dto.MetadataDto import io.github.bayang.jelu.dto.MetadataRequestDto import jakarta.annotation.Resource import mu.KotlinLogging +import org.springframework.boot.info.BuildProperties +import org.springframework.http.HttpHeaders import org.springframework.http.HttpStatus import org.springframework.stereotype.Service import org.springframework.web.client.RestClient @@ -14,11 +16,14 @@ import java.util.Optional private val logger = KotlinLogging.logger {} +const val USER_AGENT = "jelu/" + @Service class InventaireIoMetadataProvider( @Resource(name = "springRestClient") private val restClient: RestClient, private val properties: JeluProperties, private val objectMapper: ObjectMapper, + private val buildProperties: BuildProperties, ) : IMetaDataProvider { private val _name = "inventaireio" @@ -40,6 +45,7 @@ class InventaireIoMetadataProvider( .queryParam("uris", "isbn:$isbn") .build() } + .header(HttpHeaders.USER_AGENT, USER_AGENT + buildProperties.version) .exchange { clientRequest, clientResponse -> if (clientResponse.statusCode == HttpStatus.OK) { val bodyString = clientResponse.bodyTo(String::class.java) @@ -79,6 +85,7 @@ class InventaireIoMetadataProvider( .queryParam("search", author) .build() } + .header(HttpHeaders.USER_AGENT, USER_AGENT + buildProperties.version) .exchange { clientRequest, clientResponse -> if (clientResponse.statusCode == HttpStatus.OK) { val bodyString = clientResponse.bodyTo(String::class.java) @@ -107,6 +114,7 @@ class InventaireIoMetadataProvider( .queryParam("search", title) .build() } + .header(HttpHeaders.USER_AGENT, USER_AGENT + buildProperties.version) .exchange { clientRequest, clientResponse -> if (clientResponse.statusCode == HttpStatus.OK) { val bodyString = clientResponse.bodyTo(String::class.java) @@ -263,6 +271,7 @@ class InventaireIoMetadataProvider( .queryParam("uri", authorUri) .build() } + .header(HttpHeaders.USER_AGENT, USER_AGENT + buildProperties.version) .exchange { clientRequest, clientResponse -> if (clientResponse.statusCode == HttpStatus.OK) { val bodyString = clientResponse.bodyTo(String::class.java) @@ -300,6 +309,7 @@ class InventaireIoMetadataProvider( .queryParam("uris", dto.editionClaim) .build() } + .header(HttpHeaders.USER_AGENT, USER_AGENT + buildProperties.version) .exchange { clientRequest, clientResponse -> if (clientResponse.statusCode == HttpStatus.OK) { val bodyString = clientResponse.bodyTo(String::class.java) @@ -357,6 +367,7 @@ class InventaireIoMetadataProvider( .queryParam("uris", dataId) .build() } + .header(HttpHeaders.USER_AGENT, USER_AGENT + buildProperties.version) .exchange { clientRequest, clientResponse -> if (clientResponse.statusCode == HttpStatus.OK) { val bodyString = clientResponse.bodyTo(String::class.java) diff --git a/src/test/kotlin/io/github/bayang/jelu/service/metadata/providers/InventaireIoMetadataProviderTest.kt b/src/test/kotlin/io/github/bayang/jelu/service/metadata/providers/InventaireIoMetadataProviderTest.kt index 4f80c4f5..9918c7b7 100644 --- a/src/test/kotlin/io/github/bayang/jelu/service/metadata/providers/InventaireIoMetadataProviderTest.kt +++ b/src/test/kotlin/io/github/bayang/jelu/service/metadata/providers/InventaireIoMetadataProviderTest.kt @@ -7,7 +7,9 @@ import io.github.bayang.jelu.dto.MetadataRequestDto import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.Test import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.info.BuildProperties import org.springframework.boot.test.context.SpringBootTest +import org.springframework.http.HttpHeaders import org.springframework.test.web.client.MockRestServiceServer import org.springframework.test.web.client.match.MockRestRequestMatchers import org.springframework.test.web.client.response.MockRestResponseCreators @@ -16,6 +18,7 @@ import org.springframework.web.client.RestClient @SpringBootTest class InventaireIoMetadataProviderTest( @Autowired private val springRestClient: RestClient, + @Autowired private val buildProperties: BuildProperties, ) { @Test @@ -53,7 +56,7 @@ class InventaireIoMetadataProviderTest( """, ) - serv.expect(MockRestRequestMatchers.requestTo("https://inventaire.io/api/entities?action=by-uris&uris=isbn:9782290349229")).andRespond(isbn) + serv.expect(MockRestRequestMatchers.requestTo("https://inventaire.io/api/entities?action=by-uris&uris=isbn:9782290349229")).andExpect(MockRestRequestMatchers.header(HttpHeaders.USER_AGENT, USER_AGENT + buildProperties.version)).andRespond(isbn) serv.expect(MockRestRequestMatchers.requestTo("https://inventaire.io/api/entities?action=by-uris&uris=wd:Q3203603")).andRespond(edition) serv.expect(MockRestRequestMatchers.requestTo("https://inventaire.io/api/entities?action=by-uris&uris=wd:Q237087")).andRespond(author) serv.expect(MockRestRequestMatchers.requestTo("https://inventaire.io/api/entities?action=by-uris&uris=wd:Q182015")).andRespond(genre1) @@ -74,7 +77,7 @@ class InventaireIoMetadataProviderTest( ), ), ) - val service = InventaireIoMetadataProvider(builder.build(), jeluProperties, ObjectMapper()) + val service = InventaireIoMetadataProvider(builder.build(), jeluProperties, ObjectMapper(), buildProperties) // When val result: MetadataDto = service.fetchMetadata( @@ -115,7 +118,7 @@ class InventaireIoMetadataProviderTest( ), ) val objectMapper = ObjectMapper() - val service = InventaireIoMetadataProvider(springRestClient, jeluProperties, objectMapper) + val service = InventaireIoMetadataProvider(springRestClient, jeluProperties, objectMapper, buildProperties) val res = """ { "wdt:P31" : [ "wd:Q3331189" ],