Skip to content

Commit

Permalink
Added caching in get request with default value of false (#27)
Browse files Browse the repository at this point in the history
* Added caching in get request with default value of false

* Updated publish workflow
  • Loading branch information
Harsh3305 authored Jun 17, 2023
1 parent 0f98454 commit dbdd1e2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ jobs:
env:
MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }}
MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }}
VERSION: ${{ github.ref_name }}
VERSION: ${{ github.event.release.name }}
11 changes: 10 additions & 1 deletion src/main/kotlin/com/hrv/mart/apicall/APICaller.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import org.springframework.stereotype.Service
import org.springframework.web.reactive.function.client.WebClient.Builder
import org.springframework.web.reactive.function.client.WebClientResponseException
import reactor.core.publisher.Mono
import java.time.Duration
import java.time.temporal.ChronoUnit
import kotlin.reflect.full.findAnnotation

@Service
Expand All @@ -19,13 +21,20 @@ class APICaller(
fun <T>getData(
path: String,
responseClassType: Class<T>,
response: ServerHttpResponse
response: ServerHttpResponse,
isCacheable: Boolean = false
): Mono<T> {
val webClient = webClientBuilder.baseUrl(path)
.build()
val duration =
if (isCacheable)
2L
else
0L
return webClient.get()
.retrieve()
.bodyToMono(responseClassType)
.cache(Duration.of(duration, ChronoUnit.SECONDS))
.onErrorResume {
setResponse(response, it as WebClientResponseException, responseClassType)
}
Expand Down

0 comments on commit dbdd1e2

Please sign in to comment.