From 18081fbc182e15971a5e426975c31c477de1498d Mon Sep 17 00:00:00 2001 From: ligi Date: Sat, 7 Jan 2023 11:28:37 +0100 Subject: [PATCH] Add initial httpsloader (#2104) Did this in several projects now - so moving it to a module --- httpsloader/build.gradle.kts | 19 ++++++++++++++++++ .../org/ethereum/lists/chains/https/Chain.kt | 20 +++++++++++++++++++ settings.gradle.kts | 1 + 3 files changed, 40 insertions(+) create mode 100644 httpsloader/build.gradle.kts create mode 100644 httpsloader/src/main/kotlin/org/ethereum/lists/chains/https/Chain.kt diff --git a/httpsloader/build.gradle.kts b/httpsloader/build.gradle.kts new file mode 100644 index 00000000..e86dd00c --- /dev/null +++ b/httpsloader/build.gradle.kts @@ -0,0 +1,19 @@ +plugins { + id("maven-publish") +} + +publishing { + publications { + create("maven") { + version = "1.2" + + from(components["java"]) + } + } +} + +dependencies { + implementation(project(":model")) + implementation("com.squareup.okhttp3:okhttp:4.9.3") + implementation("com.squareup.moshi:moshi:1.14.0") +} \ No newline at end of file diff --git a/httpsloader/src/main/kotlin/org/ethereum/lists/chains/https/Chain.kt b/httpsloader/src/main/kotlin/org/ethereum/lists/chains/https/Chain.kt new file mode 100644 index 00000000..0d18498c --- /dev/null +++ b/httpsloader/src/main/kotlin/org/ethereum/lists/chains/https/Chain.kt @@ -0,0 +1,20 @@ +package org.ethereum.lists.chains.https + +import com.squareup.moshi.JsonAdapter +import com.squareup.moshi.Moshi +import com.squareup.moshi.Types +import okhttp3.OkHttpClient +import okhttp3.Request +import org.ethereum.lists.chains.model.Chain + +fun getChains(okhttpClient: OkHttpClient = OkHttpClient()): List? { + val request = Request.Builder() + .url("https://chainid.network/chains.json") + .build() + + val listMyData = Types.newParameterizedType(MutableList::class.java, Chain::class.java) + val adapter: JsonAdapter> = Moshi.Builder().build().adapter(listMyData) + + val response = okhttpClient.newCall(request).execute() + return response.body?.let { adapter.fromJson(it.source()) } +} \ No newline at end of file diff --git a/settings.gradle.kts b/settings.gradle.kts index 7befe42f..45bd1adf 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,2 +1,3 @@ include(":model") +include(":httpsloader") include(":processor") \ No newline at end of file