Skip to content

Commit

Permalink
fix: add custom user-agent to calls to inventaireio
Browse files Browse the repository at this point in the history
  • Loading branch information
bayang committed Nov 5, 2024
1 parent 1fdbf37 commit 8f8e594
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,23 @@ 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
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"
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,6 +18,7 @@ import org.springframework.web.client.RestClient
@SpringBootTest
class InventaireIoMetadataProviderTest(
@Autowired private val springRestClient: RestClient,
@Autowired private val buildProperties: BuildProperties,
) {

@Test
Expand Down Expand Up @@ -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)
Expand All @@ -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(
Expand Down Expand Up @@ -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" ],
Expand Down

0 comments on commit 8f8e594

Please sign in to comment.