-
Notifications
You must be signed in to change notification settings - Fork 1
add azn rates #27
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
Merged
add azn rates #27
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
src/main/kotlin/dev/vality/rateboss/client/nbaz/NbazApiClient.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package dev.vality.rateboss.client.nbaz | ||
|
|
||
| import dev.vality.rateboss.config.properties.RatesProperties | ||
| import org.springframework.http.HttpEntity | ||
| import org.springframework.http.HttpMethod | ||
| import org.springframework.stereotype.Component | ||
| import org.springframework.web.client.RestTemplate | ||
| import org.springframework.web.client.exchange | ||
| import java.time.LocalDate | ||
| import java.time.format.DateTimeFormatter | ||
|
|
||
| @Component | ||
| class NbazApiClient( | ||
| private val restTemplate: RestTemplate, | ||
| private val ratesProperties: RatesProperties, | ||
| ) { | ||
| fun getExchangeRates(date: LocalDate): String { | ||
| val url = buildUrl(date) | ||
| return restTemplate.exchange<String>(url, HttpMethod.GET, HttpEntity.EMPTY).body!! | ||
| } | ||
|
|
||
| private fun buildUrl(date: LocalDate): String { | ||
| val rootUrl = ratesProperties.source.nbaz.rootUrl | ||
| val dateFormat = ratesProperties.source.nbaz.dateFormat | ||
| return "${rootUrl}${date.format(DateTimeFormatter.ofPattern(dateFormat))}.xml" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/main/kotlin/dev/vality/rateboss/job/NbazExchangeGrabberJob.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package dev.vality.rateboss.job | ||
|
|
||
| import dev.vality.rateboss.extensions.getApplicationContext | ||
| import dev.vality.rateboss.source.ExchangeRateSource | ||
| import dev.vality.rateboss.source.ExchangeRateSourceException | ||
| import dev.vality.rateboss.source.impl.NbazExchangeRateSource | ||
| import dev.vality.rateboss.source.model.ExchangeRates | ||
| import mu.KotlinLogging | ||
| import org.quartz.JobExecutionContext | ||
| import org.springframework.context.ApplicationContext | ||
| import org.springframework.retry.support.RetryTemplate | ||
|
|
||
| private val log = KotlinLogging.logger {} | ||
|
|
||
| class NbazExchangeGrabberJob : AbstractExchangeGrabberJob() { | ||
| override fun executeInternal(context: JobExecutionContext) { | ||
| val applicationContext = context.getApplicationContext() | ||
| val currencySymbolCode = context.jobDetail.jobDataMap["currencySymbolCode"] as String | ||
| val currencyExponent = context.jobDetail.jobDataMap["currencyExponent"] as Int | ||
| val exchangeRateSource = applicationContext.getBean(NbazExchangeRateSource::class.java) | ||
| val sourceId = exchangeRateSource.getSourceId() | ||
| log.info { "Process NbazExchangeGrabberJob for $sourceId" } | ||
| val exchangeRates = getExchangeRates(applicationContext, exchangeRateSource, currencySymbolCode) | ||
| saveExchangeRates(applicationContext, currencySymbolCode, currencyExponent, exchangeRates, sourceId) | ||
| } | ||
|
|
||
| private fun getExchangeRates( | ||
| applicationContext: ApplicationContext, | ||
| exchangeRateSource: ExchangeRateSource, | ||
| currencySymbolCode: String, | ||
| ): ExchangeRates { | ||
| val retryTemplate = applicationContext.getBean(RetryTemplate::class.java) | ||
| return retryTemplate.execute<ExchangeRates, ExchangeRateSourceException> { | ||
| exchangeRateSource.getExchangeRate(currencySymbolCode) | ||
| } | ||
| } | ||
| } |
18 changes: 18 additions & 0 deletions
18
src/main/kotlin/dev/vality/rateboss/job/NbazExchangeGrabberMasterJob.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package dev.vality.rateboss.job | ||
|
|
||
| import dev.vality.rateboss.config.properties.RatesProperties | ||
| import dev.vality.rateboss.extensions.getApplicationContext | ||
| import org.quartz.JobExecutionContext | ||
| import org.quartz.Scheduler | ||
|
|
||
| class NbazExchangeGrabberMasterJob : AbstractExchangeGrabberMasterJob() { | ||
| override fun executeInternal(context: JobExecutionContext) { | ||
| val applicationContext = context.getApplicationContext() | ||
| val ratesProperties = applicationContext.getBean(RatesProperties::class.java) | ||
| val currencies = ratesProperties.nbazJob.currencies | ||
| val schedulerFactoryBean = applicationContext.getBean(Scheduler::class.java) | ||
| launchJob(currencies, schedulerFactoryBean, NbazExchangeGrabberJob::class.java, getJobName()) | ||
| } | ||
|
|
||
| override fun getJobName(): String = "nbazJob" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/main/kotlin/dev/vality/rateboss/model/NbazDailyRatesXml.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package dev.vality.rateboss.model | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonIgnoreProperties | ||
| import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper | ||
| import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty | ||
| import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement | ||
|
|
||
| @JacksonXmlRootElement(localName = "ValCurs") | ||
| @JsonIgnoreProperties(ignoreUnknown = true) | ||
| data class NbazDailyRatesXml( | ||
| @field:JacksonXmlProperty(isAttribute = true, localName = "Date") | ||
| val date: String? = null, | ||
| @field:JacksonXmlElementWrapper(useWrapping = false) | ||
| @field:JacksonXmlProperty(localName = "ValType") | ||
| val valTypes: List<NbazValTypeXml>? = null, | ||
| ) | ||
|
|
||
| @JsonIgnoreProperties(ignoreUnknown = true) | ||
| data class NbazValTypeXml( | ||
| @field:JacksonXmlProperty(isAttribute = true, localName = "Type") | ||
| val type: String? = null, | ||
| @field:JacksonXmlElementWrapper(useWrapping = false) | ||
| @field:JacksonXmlProperty(localName = "Valute") | ||
| val valutes: List<NbazValuteXml>? = null, | ||
| ) | ||
|
|
||
| @JsonIgnoreProperties(ignoreUnknown = true) | ||
| data class NbazValuteXml( | ||
| @field:JacksonXmlProperty(isAttribute = true, localName = "Code") | ||
| val code: String? = null, | ||
| @field:JacksonXmlProperty(localName = "Nominal") | ||
| val nominal: String? = null, | ||
| @field:JacksonXmlProperty(localName = "Value") | ||
| val value: String? = null, | ||
| ) |
85 changes: 85 additions & 0 deletions
85
src/main/kotlin/dev/vality/rateboss/source/impl/NbazExchangeRateSource.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| package dev.vality.rateboss.source.impl | ||
|
|
||
| import com.fasterxml.jackson.dataformat.xml.XmlMapper | ||
| import dev.vality.rateboss.client.nbaz.NbazApiClient | ||
| import dev.vality.rateboss.config.properties.RatesProperties | ||
| import dev.vality.rateboss.job.constant.ExRateSources | ||
| import dev.vality.rateboss.model.NbazDailyRatesXml | ||
| import dev.vality.rateboss.source.ExchangeRateSource | ||
| import dev.vality.rateboss.source.ExchangeRateSourceException | ||
| import dev.vality.rateboss.source.model.ExchangeRates | ||
| import mu.KotlinLogging | ||
| import org.springframework.stereotype.Component | ||
| import java.math.BigDecimal | ||
| import java.time.LocalDate | ||
| import java.time.ZoneOffset | ||
| import java.time.format.DateTimeFormatter | ||
|
|
||
| private val log = KotlinLogging.logger {} | ||
|
|
||
| @Component | ||
| class NbazExchangeRateSource( | ||
| private val nbazApiClient: NbazApiClient, | ||
| private val ratesProperties: RatesProperties, | ||
| private val xmlMapper: XmlMapper, | ||
| ) : ExchangeRateSource { | ||
| override fun getExchangeRate(currencySymbolCode: String): ExchangeRates { | ||
| val timeZone = ratesProperties.source.nbaz.timeZone | ||
| val date = LocalDate.now(timeZone) | ||
| log.info { "Trying to get exchange rates from nbaz for currency=$currencySymbolCode, date=$date" } | ||
| val parsed = fetchDailyRates(date) | ||
| val rates = buildRatesMap(parsed) | ||
| if (rates.isEmpty()) { | ||
| throw ExchangeRateSourceException("Unsuccessful response from NbazApi") | ||
| } | ||
| val dayTimestamp = date.atStartOfDay().toEpochSecond(ZoneOffset.UTC) | ||
| log.info { | ||
| "Exchange rates from nbaz have been retrieved, date=$date, " + | ||
| "exchangeRates=$rates, targetTimestamp=$dayTimestamp" | ||
| } | ||
| return ExchangeRates( | ||
| rates = rates, | ||
| timestamp = dayTimestamp, | ||
| ) | ||
| } | ||
|
|
||
| override fun getSourceId(): String = ExRateSources.NBAZ | ||
|
|
||
| private fun fetchDailyRates(date: LocalDate): NbazDailyRatesXml = | ||
| try { | ||
| xmlMapper.readValue(nbazApiClient.getExchangeRates(date), NbazDailyRatesXml::class.java) | ||
| } catch (e: Exception) { | ||
| throw ExchangeRateSourceException("Failed to get daily rates", e) | ||
| } | ||
|
|
||
| private fun buildRatesMap(root: NbazDailyRatesXml): Map<String, BigDecimal> { | ||
| val rates = mutableMapOf<String, BigDecimal>() | ||
| val currencies = | ||
| root.valTypes | ||
| .orEmpty() | ||
| .firstOrNull { it.type?.trim { ch -> ch <= ' ' } == FOREIGN_CURRENCIES_TYPE } | ||
| ?.valutes | ||
| .orEmpty() | ||
| for (item in currencies) { | ||
| val code = item.code?.trim { it <= ' ' }.orEmpty() | ||
| val nominal = item.nominal?.extractDecimal()?.toBigDecimalOrNull() | ||
| val value = item.value?.extractDecimal()?.toBigDecimalOrNull() | ||
| if (code.isBlank() || nominal == null || value == null || nominal.compareTo(BigDecimal.ZERO) == 0) { | ||
| log.debug { "Skip malformed NBAZ currency record: $item" } | ||
| continue | ||
| } | ||
| rates[code] = value.divide(nominal) | ||
| } | ||
| return rates | ||
| } | ||
|
|
||
| private fun String.extractDecimal(): String = | ||
| trim() | ||
| .replace(",", ".") | ||
| .substringBefore(' ') | ||
|
|
||
| companion object { | ||
| private const val FOREIGN_CURRENCIES_TYPE = "Xarici valyutalar" | ||
| private val DATE_FORMATTER: DateTimeFormatter = DateTimeFormatter.ofPattern("dd.MM.yyyy") | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/test/kotlin/dev/vality/rateboss/client/nbaz/NbazApiClientTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package dev.vality.rateboss.client.nbaz | ||
|
|
||
| import dev.vality.rateboss.config.TestConfig | ||
| import dev.vality.rateboss.source.ExchangeRateSource | ||
| import dev.vality.rateboss.source.impl.NbazExchangeRateSource | ||
| import org.junit.jupiter.api.Assertions.assertNotNull | ||
| import org.junit.jupiter.api.Assertions.assertTrue | ||
| import org.junit.jupiter.api.Disabled | ||
| import org.junit.jupiter.api.Test | ||
| import org.junit.jupiter.api.extension.ExtendWith | ||
| import org.springframework.beans.factory.annotation.Autowired | ||
| import org.springframework.context.annotation.Import | ||
| import org.springframework.test.context.ContextConfiguration | ||
| import org.springframework.test.context.junit.jupiter.SpringExtension | ||
| import java.time.LocalDate | ||
|
|
||
| @Disabled("integration test") | ||
| @ExtendWith(SpringExtension::class) | ||
| @ContextConfiguration(classes = [NbazApiClient::class, NbazExchangeRateSource::class]) | ||
| @Import(TestConfig::class) | ||
| class NbazApiClientTest { | ||
| @Autowired | ||
| lateinit var nbazApiClient: NbazApiClient | ||
|
|
||
| @Autowired | ||
| lateinit var nbazExchangeRateSource: ExchangeRateSource | ||
|
|
||
| @Test | ||
| fun getExchangeRates() { | ||
| val response = nbazApiClient.getExchangeRates(LocalDate.now()) | ||
|
|
||
| assertNotNull(response) | ||
| assertTrue(response.isNotBlank()) | ||
| } | ||
|
|
||
| @Test | ||
| fun getExchangeRatesViaSource() { | ||
| val exchangeRates = nbazExchangeRateSource.getExchangeRate("AZN") | ||
|
|
||
| assertNotNull(exchangeRates) | ||
| assertTrue(exchangeRates.rates.isNotEmpty()) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.