-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'api-openexchangerates'
- Loading branch information
Showing
23 changed files
with
350 additions
and
20 deletions.
There are no files selected for viewing
This file contains 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
108 changes: 108 additions & 0 deletions
108
app/src/main/kotlin/de/salomax/currencies/model/adapter/OpenExchangeratesRatesAdapter.kt
This file contains 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,108 @@ | ||
package de.salomax.currencies.model.adapter | ||
|
||
import com.squareup.moshi.FromJson | ||
import com.squareup.moshi.JsonReader | ||
import com.squareup.moshi.JsonWriter | ||
import com.squareup.moshi.ToJson | ||
import de.salomax.currencies.model.ApiProvider | ||
import de.salomax.currencies.model.Currency | ||
import de.salomax.currencies.model.ExchangeRates | ||
import de.salomax.currencies.model.Rate | ||
import java.io.IOException | ||
import java.time.Instant | ||
import java.time.LocalDate | ||
import java.time.ZoneId | ||
|
||
@Suppress("unused", "UNUSED_PARAMETER") | ||
internal class OpenExchangeratesRatesAdapter { | ||
|
||
@Suppress("ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE", "UNUSED_VALUE") | ||
@Synchronized | ||
@FromJson | ||
@Throws(IOException::class) | ||
fun fromJson(reader: JsonReader): ExchangeRates? { | ||
val rates = mutableListOf<Rate>() | ||
var base: Currency? = null | ||
var date: LocalDate? = null | ||
var errorMessage: String? = null | ||
var errorDescription: String? = null | ||
|
||
if (reader.peek() == JsonReader.Token.BEGIN_OBJECT) | ||
reader.beginObject() | ||
else | ||
return null | ||
|
||
// parse | ||
while (reader.hasNext()) { | ||
if (reader.peek() == JsonReader.Token.NAME) { | ||
when (reader.nextName()) { | ||
"rates" -> { | ||
reader.beginObject() | ||
// convert | ||
while (reader.hasNext()) { | ||
val name: Currency? = Currency.fromString(reader.nextName()) | ||
val value: Float = reader.nextDouble().toFloat() | ||
|
||
if (name != null) | ||
rates.add(Rate(name, value)) | ||
} | ||
reader.endObject() | ||
} | ||
"timestamp" -> { | ||
date = Instant.ofEpochSecond(reader.nextLong()) | ||
.atZone(ZoneId.systemDefault()).toLocalDate() | ||
} | ||
"base" -> { | ||
base = Currency.fromString(reader.nextString()) | ||
} | ||
"message" -> { | ||
errorMessage = reader.nextString() | ||
} | ||
"description" -> { | ||
errorDescription = reader.nextString() | ||
} | ||
else -> { | ||
reader.skipValue() | ||
} | ||
} | ||
} | ||
} | ||
|
||
reader.endObject() | ||
|
||
// also add Faroese króna (same as Danish krone) if it isn't already there - I simply like it! | ||
if (rates.find { it.currency == Currency.FOK } == null) | ||
rates.find { it.currency == Currency.DKK }?.value?.let { dkk -> | ||
rates.add(Rate(Currency.FOK, dkk)) | ||
} | ||
|
||
return if (rates.isNotEmpty()) | ||
ExchangeRates( | ||
success = true, | ||
error = null, | ||
base = base, | ||
date = date, | ||
rates = rates, | ||
provider = ApiProvider.INFOR_EURO | ||
) | ||
// error message | ||
else { | ||
ExchangeRates( | ||
success = false, | ||
error = errorMessage, | ||
base = base, | ||
date = date, | ||
rates = null, | ||
provider = ApiProvider.INFOR_EURO | ||
) | ||
} | ||
} | ||
|
||
@Synchronized | ||
@ToJson | ||
@Throws(IOException::class) | ||
fun toJson(writer: JsonWriter, value: ExchangeRates) { | ||
writer.nullValue() | ||
} | ||
|
||
} |
This file contains 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 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 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 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 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 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
91 changes: 91 additions & 0 deletions
91
app/src/main/kotlin/de/salomax/currencies/model/provider/OpenExchangerates.kt
This file contains 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,91 @@ | ||
package de.salomax.currencies.model.provider | ||
|
||
import android.content.Context | ||
import com.github.kittinunf.fuel.Fuel | ||
import com.github.kittinunf.fuel.core.FuelError | ||
import com.github.kittinunf.fuel.core.awaitResult | ||
import com.github.kittinunf.fuel.moshi.moshiDeserializerOf | ||
import com.github.kittinunf.result.Result | ||
import com.github.kittinunf.result.map | ||
import com.squareup.moshi.Moshi | ||
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory | ||
import de.salomax.currencies.R | ||
import de.salomax.currencies.model.ApiProvider | ||
import de.salomax.currencies.model.Currency | ||
import de.salomax.currencies.model.ExchangeRates | ||
import de.salomax.currencies.model.Timeline | ||
import de.salomax.currencies.model.adapter.OpenExchangeratesRatesAdapter | ||
import de.salomax.currencies.repository.Database | ||
import java.time.LocalDate | ||
import java.time.format.DateTimeFormatter | ||
|
||
class OpenExchangerates : ApiProvider.Api() { | ||
|
||
override val name = "Open Exchangerates" | ||
|
||
override fun descriptionShort(context: Context) = | ||
context.getText(R.string.api_openExchangeRates_descriptionShort) | ||
|
||
override fun getDescriptionLong(context: Context) = | ||
context.getText(R.string.api_openExchangeRates_descriptionFull) | ||
|
||
override fun descriptionUpdateInterval(context: Context) = | ||
context.getText(R.string.api_openExchangeRates_descriptionUpdateInterval) | ||
|
||
override fun descriptionHint(context: Context) = | ||
context.getText(R.string.api_openExchangeRates_hint) | ||
|
||
override val baseUrl = "https://openexchangerates.org/api" | ||
|
||
override suspend fun getRates(context: Context?, date: LocalDate?): Result<ExchangeRates, FuelError> { | ||
val apiKey = context?.let { Database(it).getOpenExchangeRatesApiKey() } | ||
if (apiKey.isNullOrBlank()) | ||
return Result.error(FuelError.wrap(Exception(context?.getString(R.string.error_no_api_key)))) | ||
|
||
val endpoint = | ||
if (date != null) | ||
"/historical/" + date.format(DateTimeFormatter.ISO_LOCAL_DATE) + ".json" | ||
else | ||
"/latest.json" | ||
|
||
val result = Fuel.get( | ||
baseUrl + | ||
endpoint + | ||
"?app_id=$apiKey" + | ||
"&prettyprint=false" + | ||
"&show_alternative=false" | ||
).awaitResult( | ||
moshiDeserializerOf( | ||
Moshi.Builder() | ||
.addLast(KotlinJsonAdapterFactory()) | ||
.apply { | ||
add(OpenExchangeratesRatesAdapter()) | ||
} | ||
.build() | ||
.adapter(ExchangeRates::class.java) | ||
) | ||
).map { rates -> | ||
rates.copy(provider = ApiProvider.OPEN_EXCHANGERATES) | ||
} | ||
|
||
if (result.component2()?.response?.statusCode == 401) { | ||
return Result.error( | ||
FuelError.wrap( | ||
Exception(context.getString(R.string.error_invalid_api_key)) | ||
) | ||
) | ||
} | ||
return result | ||
} | ||
|
||
override suspend fun getTimeline( | ||
context: Context?, | ||
base: Currency, | ||
symbol: Currency, | ||
startDate: LocalDate, | ||
endDate: LocalDate | ||
): Result<Timeline, FuelError> { | ||
return Result.error(FuelError.wrap(Exception(context?.getString(R.string.error_unsupported_timeline)))) | ||
} | ||
|
||
} |
This file contains 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.