-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
0.0.1
- Loading branch information
There are no files selected for viewing
This file was deleted.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
# android-template | ||
# RaCooin | ||
|
||
Template for Android Projects. | ||
|
||
# TODO | ||
|
||
RaCooin (a mix of **Ra**te, **Coin** and the cute animal) | ||
is a minimalistic app for tracking the current fiat money value of one's crypto currencies. | ||
|
||
# TODO | ||
- Add CI Workflow to publish to google play | ||
- Add Jetpack Compose once stable |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
plugins { | ||
id(Plugins.LIB_JAVA) | ||
id(Plugins.LIB_KOTLIN) | ||
kotlin(Plugins.SERIALIZATION) version BuildPluginsVersions.KOTLIN | ||
} | ||
|
||
java { | ||
sourceCompatibility = JavaVersion.VERSION_11 | ||
targetCompatibility = JavaVersion.VERSION_11 | ||
} | ||
|
||
dependencies { | ||
implementation(project(mapOf("path" to ":core"))) | ||
|
||
implementation(Core.KOTLINX_COROUTINES) // TODO: remove? | ||
|
||
implementation(Utils.KOTLIN_SERIALIZATION) | ||
|
||
implementation(Server.KTOR_CLIENT_CORE) | ||
implementation(Server.KTOR_CLIENT_ANDROID) | ||
implementation(Server.KTOR_CLIENT_SERIALIZATION) | ||
implementation(Server.KTOR_CLIENT_LOGGING) | ||
implementation(Server.LOGBACK) | ||
|
||
testImplementation(Testing.KOTEST_RUNNER) | ||
testImplementation(Testing.KOTEST_JUNIT_RUNNER) | ||
testImplementation(Testing.KOTEST_ASSERTIONS) | ||
testImplementation(Testing.KOTEST_EXTENSIONS_ARROW) | ||
testImplementation(Testing.KOTEST_PROPERTIES) | ||
testImplementation(Testing.MOCKK) | ||
testImplementation(Testing.KOTLINX_COROUTINES_TEST) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.greyhairredbear.racooin.apiclient | ||
|
||
import arrow.core.Either | ||
import com.greyhairredbear.racooin.core.interfaces.ApiClient | ||
import com.greyhairredbear.racooin.core.model.ApiCallFailed | ||
import com.greyhairredbear.racooin.core.model.ApiClientError | ||
import com.greyhairredbear.racooin.core.model.CryptoCurrency | ||
import com.greyhairredbear.racooin.core.model.CryptoCurrencyRate | ||
import com.greyhairredbear.racooin.core.model.FiatBalance | ||
import com.greyhairredbear.racooin.core.model.FiatCurrency | ||
import io.ktor.client.HttpClient | ||
import io.ktor.client.features.defaultRequest | ||
import io.ktor.client.features.json.JsonFeature | ||
import io.ktor.client.features.json.serializer.KotlinxSerializer | ||
import io.ktor.client.features.logging.DEFAULT | ||
import io.ktor.client.features.logging.LogLevel | ||
import io.ktor.client.features.logging.Logger | ||
import io.ktor.client.features.logging.Logging | ||
import io.ktor.client.request.accept | ||
import io.ktor.client.request.get | ||
import io.ktor.client.request.host | ||
import io.ktor.client.request.parameter | ||
import io.ktor.http.ContentType | ||
import io.ktor.http.URLProtocol | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class CurrencyRatesResponse( | ||
val bitcoin: FiatBalanceResponse, | ||
val ethereum: FiatBalanceResponse, | ||
val dogecoin: FiatBalanceResponse, | ||
val litecoin: FiatBalanceResponse, | ||
val ripple: FiatBalanceResponse, | ||
) { | ||
fun toCurrencyRates(): List<CryptoCurrencyRate> = | ||
mapOf( | ||
CryptoCurrency.BITCOIN to bitcoin, | ||
CryptoCurrency.ETHEREUM to ethereum, | ||
CryptoCurrency.DOGECOIN to dogecoin, | ||
CryptoCurrency.LITECOIN to litecoin, | ||
CryptoCurrency.RIPPLE to ripple, | ||
).flatMap { | ||
listOf( | ||
CryptoCurrencyRate(it.key, FiatBalance(FiatCurrency.EURO, it.value.eur)), | ||
CryptoCurrencyRate(it.key, FiatBalance(FiatCurrency.US_DOLLAR, it.value.usd)), | ||
) | ||
} | ||
} | ||
|
||
@Serializable | ||
data class FiatBalanceResponse(val eur: Double, val usd: Double) | ||
|
||
// TODO extract client setup etc | ||
class CoingeckoApiClient : ApiClient { | ||
private val client = HttpClient { | ||
|
||
install(Logging) { | ||
logger = Logger.DEFAULT | ||
level = LogLevel.ALL | ||
} | ||
|
||
install(JsonFeature) { | ||
serializer = KotlinxSerializer() | ||
} | ||
|
||
defaultRequest { | ||
host = "api.coingecko.com/api/v3/simple" | ||
url { | ||
protocol = URLProtocol.HTTPS | ||
} | ||
accept(ContentType.Application.Json) | ||
} | ||
|
||
} | ||
|
||
override suspend fun fetchCurrencyRates(): Either<ApiClientError, List<CryptoCurrencyRate>> = | ||
Either.catch { | ||
val result = client.get<CurrencyRatesResponse> { | ||
url { encodedPath = "/price" } | ||
parameter("ids", "ethereum,bitcoin,dogecoin,litecoin,ripple") | ||
parameter("vs_currencies", "eur,usd") | ||
} | ||
result.toCurrencyRates() | ||
}.mapLeft { ApiCallFailed } | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.greyhairredbear.racooin.apiclient | ||
|
||
import com.greyhairredbear.racooin.core.model.CryptoCurrency | ||
import com.greyhairredbear.racooin.core.model.CryptoCurrencyRate | ||
import com.greyhairredbear.racooin.core.model.FiatCurrency | ||
import io.kotest.assertions.arrow.core.shouldBeRight | ||
import io.kotest.core.spec.style.FunSpec | ||
import io.kotest.matchers.collections.shouldContainAll | ||
import io.kotest.matchers.collections.shouldHaveSize | ||
|
||
val sut = CoingeckoApiClient() | ||
|
||
class ApiClientTest : FunSpec({ | ||
test("should fetch currencies from api") { | ||
val result = sut.fetchCurrencyRates() | ||
.shouldBeRight() | ||
|
||
result.shouldHaveSize(10) | ||
result.shouldContainAllCurrencyCombinations() | ||
} | ||
}) | ||
|
||
private fun List<CryptoCurrencyRate>.shouldContainAllCurrencyCombinations() { | ||
val expectedCombinations = CryptoCurrency.values() | ||
.flatMap { crypto -> FiatCurrency.values().map { crypto to it } } | ||
|
||
currencyCombinations().shouldContainAll(expectedCombinations) | ||
} | ||
|
||
private fun List<CryptoCurrencyRate>.currencyCombinations() = | ||
map { it.cryptoCurrency to it.fiatBalance.fiatCurrency } |