From 1a1e28a1be94f5a14d859354296ac2a1eaf85eb5 Mon Sep 17 00:00:00 2001 From: Arie Trouw Date: Fri, 6 Dec 2024 09:28:22 -0800 Subject: [PATCH] Payload interface moved into model package --- ...oundWitnessTest.kt => BoundWitnessTest.kt} | 8 ++--- .../boundwitness/TestBoundWitnessSequence.kt | 3 +- .../xyo/client/boundwitness/TestPayload1.kt | 3 +- .../xyo/client/boundwitness/TestPayload2.kt | 3 +- .../java/network/xyo/client/lib/Constants.kt | 18 +++++------ .../xyo/client/node/client/NodeClientTest.kt | 12 ++++---- .../xyo/client/payload/EventPayload.kt | 13 ++++++++ .../{XyoPayloadTest.kt => PayloadTest.kt} | 8 ++--- .../xyo/client/payload/XyoEventPayload.kt | 10 ------- .../network/xyo/client/settings/XyoSdkTest.kt | 6 ++-- .../xyo/client/witness/LocationWitnessTest.kt | 12 ++++---- .../client/witness/SystemInfoWitnessTest.kt | 4 +-- .../witness/WitnessLocationHandlerTest.kt | 30 +++++++++---------- .../xyo/client/witness/XyoPanelTest.kt | 20 ++++++------- .../wrapper/ArchivistGetQueryPayload.kt | 10 +++---- .../wrapper/ArchivistInsertQueryPayload.kt | 11 ++++--- .../archivist/wrapper/ArchivistWrapper.kt | 4 +-- ...ssBodyInterface.kt => BoundWitnessBody.kt} | 4 +-- ...essBodyJson.kt => BoundWitnessBodyJson.kt} | 9 ++---- ...oundWitnessJson.kt => BoundWitnessJson.kt} | 6 ++-- .../boundwitness/QueryBoundWitnessBodyJson.kt | 2 +- .../boundwitness/QueryBoundWitnessBuilder.kt | 4 +-- .../boundwitness/QueryBoundWitnessJson.kt | 2 +- .../boundwitness/XyoBoundWitnessBuilder.kt | 14 ++++----- .../xyo/client/module/ModuleQueryResult.kt | 4 +-- .../xyo/client/node/client/NodeClient.kt | 14 ++++----- .../xyo/client/node/client/QueryResponse.kt | 18 +++++------ .../network/xyo/client/payload/Payload.kt | 25 +++++++++++----- .../network/xyo/client/payload/XyoPayload.kt | 27 ----------------- .../xyo/client/payload/model/Payload.kt | 13 ++++++++ .../network/xyo/client/witness/XyoPanel.kt | 20 ++++++------- .../network/xyo/client/witness/XyoWitness.kt | 6 ++-- .../client/witness/location/info/Handler.kt | 20 ++++++------- ...oLocationPayload.kt => LocationPayload.kt} | 17 +++++------ ...ionPayloadRaw.kt => LocationPayloadRaw.kt} | 16 ++++------ .../location/info/XyoLocationCurrent.kt | 8 ++--- .../location/info/XyoLocationPayloads.kt | 2 +- .../location/info/XyoLocationWitness.kt | 8 ++--- ...temInfoPayload.kt => SystemInfoPayload.kt} | 16 ++++------ .../system/info/XyoSystemInfoWitness.kt | 6 ++-- 40 files changed, 209 insertions(+), 227 deletions(-) rename sdk/src/androidTest/java/network/xyo/client/boundwitness/{XyoBoundWitnessTest.kt => BoundWitnessTest.kt} (96%) create mode 100644 sdk/src/androidTest/java/network/xyo/client/payload/EventPayload.kt rename sdk/src/androidTest/java/network/xyo/client/payload/{XyoPayloadTest.kt => PayloadTest.kt} (96%) delete mode 100644 sdk/src/androidTest/java/network/xyo/client/payload/XyoEventPayload.kt rename sdk/src/main/java/network/xyo/client/boundwitness/{XyoBoundWitnessBodyInterface.kt => BoundWitnessBody.kt} (79%) rename sdk/src/main/java/network/xyo/client/boundwitness/{XyoBoundWitnessBodyJson.kt => BoundWitnessBodyJson.kt} (76%) rename sdk/src/main/java/network/xyo/client/boundwitness/{XyoBoundWitnessJson.kt => BoundWitnessJson.kt} (75%) delete mode 100644 sdk/src/main/java/network/xyo/client/payload/XyoPayload.kt create mode 100644 sdk/src/main/java/network/xyo/client/payload/model/Payload.kt rename sdk/src/main/java/network/xyo/client/witness/location/info/{XyoLocationPayload.kt => LocationPayload.kt} (63%) rename sdk/src/main/java/network/xyo/client/witness/location/info/{XyoLocationPayloadRaw.kt => LocationPayloadRaw.kt} (87%) rename sdk/src/main/java/network/xyo/client/witness/system/info/{XyoSystemInfoPayload.kt => SystemInfoPayload.kt} (65%) diff --git a/sdk/src/androidTest/java/network/xyo/client/boundwitness/XyoBoundWitnessTest.kt b/sdk/src/androidTest/java/network/xyo/client/boundwitness/BoundWitnessTest.kt similarity index 96% rename from sdk/src/androidTest/java/network/xyo/client/boundwitness/XyoBoundWitnessTest.kt rename to sdk/src/androidTest/java/network/xyo/client/boundwitness/BoundWitnessTest.kt index 1766813..fd244f5 100644 --- a/sdk/src/androidTest/java/network/xyo/client/boundwitness/XyoBoundWitnessTest.kt +++ b/sdk/src/androidTest/java/network/xyo/client/boundwitness/BoundWitnessTest.kt @@ -12,16 +12,16 @@ import network.xyo.client.node.client.DiscoverPayload import network.xyo.client.datastore.previous_hash_store.PreviousHashStorePrefsRepository import network.xyo.client.lib.JsonSerializable import network.xyo.client.node.client.NodeClient -import network.xyo.client.payload.XyoPayload +import network.xyo.client.payload.Payload import org.json.JSONObject import org.junit.Before import org.junit.Rule import org.junit.Test import org.junit.jupiter.api.Assertions.* -data class RequestDependencies(val client: NodeClient, val query: XyoPayload, val payloads: List) +data class RequestDependencies(val client: NodeClient, val query: Payload, val payloads: List) -class XyoBoundWitnessTest { +class BoundWitnessTest { val apiDomainBeta = "https://beta.api.archivist.xyo.network" val apiDomainLocal = "http://10.0.2.2:8080" @@ -47,7 +47,7 @@ class XyoBoundWitnessTest { val account = Account.random() val client = NodeClient(nodeUrl, account, appContext) val query = DiscoverPayload() - val payloads = mutableListOf() + val payloads = mutableListOf() payloads.add(TestPayload1()) return RequestDependencies(client, query, payloads) } diff --git a/sdk/src/androidTest/java/network/xyo/client/boundwitness/TestBoundWitnessSequence.kt b/sdk/src/androidTest/java/network/xyo/client/boundwitness/TestBoundWitnessSequence.kt index 34126ef..eec52af 100644 --- a/sdk/src/androidTest/java/network/xyo/client/boundwitness/TestBoundWitnessSequence.kt +++ b/sdk/src/androidTest/java/network/xyo/client/boundwitness/TestBoundWitnessSequence.kt @@ -1,8 +1,7 @@ import network.xyo.client.payload.Payload -class IdPayload(val salt: String): Payload { +class IdPayload(val salt: String): Payload("network.xyo.id") { constructor(salt: UInt) : this("${salt}") - override var schema = "network.xyo.id" } data class BoundWitnessSequenceTestCase( diff --git a/sdk/src/androidTest/java/network/xyo/client/boundwitness/TestPayload1.kt b/sdk/src/androidTest/java/network/xyo/client/boundwitness/TestPayload1.kt index 1e80628..e4f64b0 100644 --- a/sdk/src/androidTest/java/network/xyo/client/boundwitness/TestPayload1.kt +++ b/sdk/src/androidTest/java/network/xyo/client/boundwitness/TestPayload1.kt @@ -5,8 +5,7 @@ class TestPayload1SubObject { var string_value = "yo" } -class TestPayload1: Payload { - override var schema ="network.xyo.test" +class TestPayload1: Payload("network.xyo.test") { var timestamp = 1_618_603_439_107 var number_field = 1 var object_field = TestPayload1SubObject() diff --git a/sdk/src/androidTest/java/network/xyo/client/boundwitness/TestPayload2.kt b/sdk/src/androidTest/java/network/xyo/client/boundwitness/TestPayload2.kt index 32e39cf..397c824 100644 --- a/sdk/src/androidTest/java/network/xyo/client/boundwitness/TestPayload2.kt +++ b/sdk/src/androidTest/java/network/xyo/client/boundwitness/TestPayload2.kt @@ -6,8 +6,7 @@ class TestPayload2SubObject { var optional_field: String? = null } -class TestPayload2: Payload { - override var schema ="network.xyo.test" +class TestPayload2: Payload("network.xyo.test") { var timestamp = 1_618_603_439_107 var object_field = TestPayload2SubObject() var string_field = "there" diff --git a/sdk/src/androidTest/java/network/xyo/client/lib/Constants.kt b/sdk/src/androidTest/java/network/xyo/client/lib/Constants.kt index 5c2e425..fd6b5ae 100644 --- a/sdk/src/androidTest/java/network/xyo/client/lib/Constants.kt +++ b/sdk/src/androidTest/java/network/xyo/client/lib/Constants.kt @@ -1,18 +1,18 @@ package network.xyo.client.lib import network.xyo.client.account.Account -import network.xyo.client.payload.XyoPayload +import network.xyo.client.payload.Payload -class DebugPayload(val nonce: Int) : XyoPayload() { - override var schema: String - get() = "network.xyo.debug" - set(value) = Unit +class DebugPayload(val nonce: Int) : Payload(SCHEMA) { + companion object { + const val SCHEMA = "network.xyo.debug" + } } -class BasicPayload : XyoPayload() { - override var schema: String - get() = "network.xyo.basic" - set(value) = Unit +class BasicPayload : Payload(SCHEMA) { + companion object { + const val SCHEMA = "network.xyo.basic" + } } diff --git a/sdk/src/androidTest/java/network/xyo/client/node/client/NodeClientTest.kt b/sdk/src/androidTest/java/network/xyo/client/node/client/NodeClientTest.kt index 0f7e62f..72c88ed 100644 --- a/sdk/src/androidTest/java/network/xyo/client/node/client/NodeClientTest.kt +++ b/sdk/src/androidTest/java/network/xyo/client/node/client/NodeClientTest.kt @@ -7,14 +7,16 @@ import kotlinx.coroutines.runBlocking import network.xyo.client.lib.TestConstants import network.xyo.client.account.Account import network.xyo.client.archivist.wrapper.ArchivistWrapper -import network.xyo.client.boundwitness.XyoBoundWitnessBodyJson -import network.xyo.client.payload.XyoPayload +import network.xyo.client.boundwitness.BoundWitnessBodyJson +import network.xyo.client.payload.Payload import org.junit.Before import org.junit.Test import org.junit.jupiter.api.Assertions.* -class DiscoverPayload : XyoPayload() { - override var schema = "network.xyo.query.module.discover" +class DiscoverPayload : Payload(SCHEMA) { + companion object { + const val SCHEMA = "network.xyo.query.module.discover" + } } class NodeClientTest { @@ -69,7 +71,7 @@ class NodeClientTest { assertEquals(errors, null) if (response != null) { - assertEquals(response.bw?.schema, XyoBoundWitnessBodyJson.schema) + assertEquals(response.bw?.schema, BoundWitnessBodyJson.SCHEMA) } else { throw(Error("Response should not be null")) } diff --git a/sdk/src/androidTest/java/network/xyo/client/payload/EventPayload.kt b/sdk/src/androidTest/java/network/xyo/client/payload/EventPayload.kt new file mode 100644 index 0000000..dcbba60 --- /dev/null +++ b/sdk/src/androidTest/java/network/xyo/client/payload/EventPayload.kt @@ -0,0 +1,13 @@ +package network.xyo.client.payload + +import com.squareup.moshi.JsonClass +import java.util.* + +@JsonClass(generateAdapter = true) +open class EventPayload(val event: String): Payload(SCHEMA) { + val timestamp = Date().time + + companion object { + const val SCHEMA = "network.xyo.event" + } +} \ No newline at end of file diff --git a/sdk/src/androidTest/java/network/xyo/client/payload/XyoPayloadTest.kt b/sdk/src/androidTest/java/network/xyo/client/payload/PayloadTest.kt similarity index 96% rename from sdk/src/androidTest/java/network/xyo/client/payload/XyoPayloadTest.kt rename to sdk/src/androidTest/java/network/xyo/client/payload/PayloadTest.kt index 13309a1..573d873 100644 --- a/sdk/src/androidTest/java/network/xyo/client/payload/XyoPayloadTest.kt +++ b/sdk/src/androidTest/java/network/xyo/client/payload/PayloadTest.kt @@ -17,8 +17,8 @@ import org.junit.Rule import org.junit.Test import org.junit.jupiter.api.Assertions.* -open class TestSubjectPayload: XyoPayload() { - override var schema = "network.xyo.test" +open class TestSubjectPayload: Payload("network.xyo.test") { + } class TestPayload1SubObject { @@ -55,7 +55,7 @@ class TestInvalidSchemaPayload: TestSubjectPayload() { val knownAddress = Account.fromPrivateKey(ByteArray(32) {index -> index.toByte()}) const val knownHash = "6e173bbfc0577ebde66b44b090316eca5ecad8ecdb5c51886211d805c769d2ea" -class XyoPayloadTest { +class PayloadTest { @Rule @JvmField @@ -96,7 +96,7 @@ class XyoPayloadTest { @Test fun testRoundTripPanel() { val address = Account.fromPrivateKey("5a95531488b4d0d3645aea49678297ae9e2034879ce0389b80eb788e8b533592") - val witness = XyoWitness(address, fun(_: Context): List { + val witness = XyoWitness(address, fun(_: Context): List { return listOf(BasicPayload()) }) diff --git a/sdk/src/androidTest/java/network/xyo/client/payload/XyoEventPayload.kt b/sdk/src/androidTest/java/network/xyo/client/payload/XyoEventPayload.kt deleted file mode 100644 index 96c852e..0000000 --- a/sdk/src/androidTest/java/network/xyo/client/payload/XyoEventPayload.kt +++ /dev/null @@ -1,10 +0,0 @@ -package network.xyo.client.payload - -import com.squareup.moshi.JsonClass -import java.util.* - -@JsonClass(generateAdapter = true) -open class XyoEventPayload(val event: String): XyoPayload() { - override var schema = "network.xyo.event" - val time_stamp = Date().time -} \ No newline at end of file diff --git a/sdk/src/androidTest/java/network/xyo/client/settings/XyoSdkTest.kt b/sdk/src/androidTest/java/network/xyo/client/settings/XyoSdkTest.kt index 6f1976e..69e3b80 100644 --- a/sdk/src/androidTest/java/network/xyo/client/settings/XyoSdkTest.kt +++ b/sdk/src/androidTest/java/network/xyo/client/settings/XyoSdkTest.kt @@ -4,7 +4,7 @@ import android.content.Context import androidx.test.platform.app.InstrumentationRegistry import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.runBlocking -import network.xyo.client.payload.XyoEventPayload +import network.xyo.client.payload.EventPayload import network.xyo.client.witness.XyoPanel import network.xyo.client.account.model.AccountInstance import org.junit.Before @@ -69,8 +69,8 @@ class XyoSdkTest { val instance = XyoSdk.getInstance(appContext) val testAccount = instance.getAccount(appContext) - val panel = XyoPanel(appContext, testAccount, fun(_:Context): List { - return listOf(XyoEventPayload("test_event")) + val panel = XyoPanel(appContext, testAccount, fun(_:Context): List { + return listOf(EventPayload("test_event")) }) val result = panel.reportAsyncQuery() val bw = result.bw diff --git a/sdk/src/androidTest/java/network/xyo/client/witness/LocationWitnessTest.kt b/sdk/src/androidTest/java/network/xyo/client/witness/LocationWitnessTest.kt index 5144fcc..b9e0d8c 100644 --- a/sdk/src/androidTest/java/network/xyo/client/witness/LocationWitnessTest.kt +++ b/sdk/src/androidTest/java/network/xyo/client/witness/LocationWitnessTest.kt @@ -13,8 +13,8 @@ import kotlinx.coroutines.runBlocking import network.xyo.client.account.Account import network.xyo.client.lib.TestConstants import network.xyo.client.witness.location.info.LocationActivity -import network.xyo.client.witness.location.info.XyoLocationPayload -import network.xyo.client.witness.location.info.XyoLocationPayloadRaw +import network.xyo.client.witness.location.info.LocationPayload +import network.xyo.client.witness.location.info.LocationPayloadRaw import network.xyo.client.witness.location.info.XyoLocationWitness import org.junit.Before import org.junit.Rule @@ -43,15 +43,15 @@ class LocationWitnessTest { val witness = XyoLocationWitness() val locationPayload = witness.observe(appContext)?.first() - assertInstanceOf(locationPayload) - assert(locationPayload.schema == XyoLocationPayload.schema) + assertInstanceOf(locationPayload) + assert(locationPayload.schema == LocationPayload.SCHEMA) assert(locationPayload.currentLocation !== null) assert(locationPayload.currentLocation?.coords?.latitude !== null) assert(locationPayload.currentLocation?.coords?.longitude !== null) val locationRawPayload = witness.observe(appContext)?.get(1) - assertInstanceOf(locationRawPayload) - assert(locationRawPayload.schema == XyoLocationPayloadRaw.schema) + assertInstanceOf(locationRawPayload) + assert(locationRawPayload.schema == LocationPayloadRaw.SCHEMA) } } diff --git a/sdk/src/androidTest/java/network/xyo/client/witness/SystemInfoWitnessTest.kt b/sdk/src/androidTest/java/network/xyo/client/witness/SystemInfoWitnessTest.kt index 035c43c..f1adece 100644 --- a/sdk/src/androidTest/java/network/xyo/client/witness/SystemInfoWitnessTest.kt +++ b/sdk/src/androidTest/java/network/xyo/client/witness/SystemInfoWitnessTest.kt @@ -5,7 +5,7 @@ import androidx.test.core.app.ApplicationProvider import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch -import network.xyo.client.witness.system.info.XyoSystemInfoPayload +import network.xyo.client.witness.system.info.SystemInfoPayload import network.xyo.client.witness.system.info.XyoSystemInfoWitness import org.junit.Test import org.junit.jupiter.api.assertInstanceOf @@ -20,7 +20,7 @@ class SystemInfoWitnessTest { val witness = XyoSystemInfoWitness() val payload = witness.observe(context)?.first() - assertInstanceOf(payload) + assertInstanceOf(payload) assert(payload.os != null) assert(payload.device != null) assert(payload.network != null) diff --git a/sdk/src/androidTest/java/network/xyo/client/witness/WitnessLocationHandlerTest.kt b/sdk/src/androidTest/java/network/xyo/client/witness/WitnessLocationHandlerTest.kt index 4b8467c..7a9eedf 100644 --- a/sdk/src/androidTest/java/network/xyo/client/witness/WitnessLocationHandlerTest.kt +++ b/sdk/src/androidTest/java/network/xyo/client/witness/WitnessLocationHandlerTest.kt @@ -9,14 +9,14 @@ import androidx.test.rule.GrantPermissionRule import kotlinx.coroutines.runBlocking import network.xyo.client.lib.TestConstants import network.xyo.client.account.Account -import network.xyo.client.boundwitness.XyoBoundWitnessBodyJson -import network.xyo.client.boundwitness.XyoBoundWitnessJson +import network.xyo.client.boundwitness.BoundWitnessBodyJson +import network.xyo.client.boundwitness.BoundWitnessJson import network.xyo.client.datastore.previous_hash_store.PreviousHashStorePrefsRepository import network.xyo.client.witness.types.WitnessResult -import network.xyo.client.payload.XyoPayload +import network.xyo.client.payload.Payload import network.xyo.client.witness.location.info.LocationActivity -import network.xyo.client.witness.location.info.XyoLocationPayload -import network.xyo.client.witness.location.info.XyoLocationPayloadRaw +import network.xyo.client.witness.location.info.LocationPayload +import network.xyo.client.witness.location.info.LocationPayloadRaw import org.junit.Before import org.junit.Rule import org.junit.Test @@ -49,28 +49,28 @@ class WitnessLocationHandlerTest { @Test fun testObserve() { runBlocking { - var firstBw: XyoBoundWitnessJson? = null + var firstBw: BoundWitnessJson? = null val result1 = WitnessLocationHandler().witness(appContext.applicationContext, arrayListOf(Pair(apiDomainBeta, null))) when (result1) { - is WitnessResult.Success> -> { + is WitnessResult.Success> -> { firstBw = result1.data.first - assertInstanceOf(firstBw) - assertInstanceOf(result1.data.second) - assertInstanceOf(result1.data.third) + assertInstanceOf(firstBw) + assertInstanceOf(result1.data.second) + assertInstanceOf(result1.data.third) } is WitnessResult.Error -> { assert(result1.exception.size > 0) } } - var secondBw: XyoBoundWitnessJson? = null + var secondBw: BoundWitnessJson? = null val result2 = WitnessLocationHandler().witness(appContext.applicationContext, arrayListOf(Pair(apiDomainBeta, null))) when (result2) { - is WitnessResult.Success> -> { + is WitnessResult.Success> -> { secondBw = result2.data.first - assertInstanceOf(secondBw) - assertInstanceOf(result2.data.second) - assertInstanceOf(result2.data.third) + assertInstanceOf(secondBw) + assertInstanceOf(result2.data.second) + assertInstanceOf(result2.data.third) } is WitnessResult.Error -> { diff --git a/sdk/src/androidTest/java/network/xyo/client/witness/XyoPanelTest.kt b/sdk/src/androidTest/java/network/xyo/client/witness/XyoPanelTest.kt index f5d2894..957c8b1 100644 --- a/sdk/src/androidTest/java/network/xyo/client/witness/XyoPanelTest.kt +++ b/sdk/src/androidTest/java/network/xyo/client/witness/XyoPanelTest.kt @@ -9,14 +9,14 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.runBlocking import network.xyo.client.lib.BasicPayload import network.xyo.client.lib.TestConstants -import network.xyo.client.payload.XyoEventPayload +import network.xyo.client.payload.EventPayload import network.xyo.client.account.Account -import network.xyo.client.boundwitness.XyoBoundWitnessJson +import network.xyo.client.boundwitness.BoundWitnessJson import network.xyo.client.datastore.previous_hash_store.PreviousHashStorePrefsRepository -import network.xyo.client.payload.XyoPayload +import network.xyo.client.payload.Payload import network.xyo.client.witness.location.info.LocationActivity import network.xyo.client.witness.location.info.XyoLocationWitness -import network.xyo.client.witness.system.info.XyoSystemInfoPayload +import network.xyo.client.witness.system.info.SystemInfoPayload import network.xyo.client.witness.system.info.XyoSystemInfoWitness import org.junit.Before import org.junit.Rule @@ -54,7 +54,7 @@ class XyoPanelTest { } private fun testCreatePanel(nodeUrl: String) { - val witness = XyoWitness(Account.random()) + val witness = XyoWitness(Account.random()) val panel = XyoPanel(appContext, Account.random(), arrayListOf(Pair(nodeUrl, Account.random())), listOf(witness)) assertNotNull(panel) } @@ -74,7 +74,7 @@ class XyoPanelTest { runBlocking { val witnessAccount = Account.fromPrivateKey("9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08") val witness2Account = Account.fromPrivateKey("5a95531488b4d0d3645aea49678297ae9e2034879ce0389b80eb788e8b533592") - val witness = XyoWitness(witnessAccount, fun(_: Context): List { + val witness = XyoWitness(witnessAccount, fun(_: Context): List { return listOf(BasicPayload()) }) val panel = XyoPanel(appContext, Account.random(), arrayListOf(Pair(nodeUrl, Account.random())), listOf(witness, XyoSystemInfoWitness(witness2Account), XyoLocationWitness())) @@ -104,8 +104,8 @@ class XyoPanelTest { fun testSimplePanelReport() { runBlocking { val testAccount = Account.random() - val panel = XyoPanel(appContext, testAccount, fun(_:Context): List { - return listOf(XyoEventPayload("test_event")) + val panel = XyoPanel(appContext, testAccount, fun(_:Context): List { + return listOf(EventPayload("test_event")) }) val result = panel.reportAsyncQuery() if (result.apiResults === null) throw NullPointerException("apiResults should not be null") @@ -134,8 +134,8 @@ class XyoPanelTest { runBlocking { val panel = XyoPanel(appContext, Account.random(), arrayListOf(), listOf(XyoSystemInfoWitness())) val results = panel.reportAsyncQuery() - assertInstanceOf(results.bw) - assertInstanceOf(results.payloads?.first()) + assertInstanceOf(results.bw) + assertInstanceOf(results.payloads?.first()) } } } \ No newline at end of file diff --git a/sdk/src/main/java/network/xyo/client/archivist/wrapper/ArchivistGetQueryPayload.kt b/sdk/src/main/java/network/xyo/client/archivist/wrapper/ArchivistGetQueryPayload.kt index ceddbe4..2752cbc 100644 --- a/sdk/src/main/java/network/xyo/client/archivist/wrapper/ArchivistGetQueryPayload.kt +++ b/sdk/src/main/java/network/xyo/client/archivist/wrapper/ArchivistGetQueryPayload.kt @@ -1,11 +1,11 @@ package network.xyo.client.archivist.wrapper -import network.xyo.client.payload.XyoPayload - -open class ArchivistGetQueryPayload(val hashes: List): XyoPayload() { - override var schema = ArchivistGetQueryPayload.schema +import com.squareup.moshi.JsonClass +import network.xyo.client.payload.Payload +@JsonClass(generateAdapter = true) +open class ArchivistGetQueryPayload(val hashes: List): Payload(SCHEMA) { companion object { - val schema = "network.xyo.query.archivist.get" + const val SCHEMA = "network.xyo.query.archivist.get" } } \ No newline at end of file diff --git a/sdk/src/main/java/network/xyo/client/archivist/wrapper/ArchivistInsertQueryPayload.kt b/sdk/src/main/java/network/xyo/client/archivist/wrapper/ArchivistInsertQueryPayload.kt index 2c079e0..1a851e3 100644 --- a/sdk/src/main/java/network/xyo/client/archivist/wrapper/ArchivistInsertQueryPayload.kt +++ b/sdk/src/main/java/network/xyo/client/archivist/wrapper/ArchivistInsertQueryPayload.kt @@ -1,12 +1,11 @@ package network.xyo.client.archivist.wrapper -import network.xyo.client.payload.XyoPayload - - -class ArchivistInsertQueryPayload : XyoPayload() { - override var schema = ArchivistInsertQueryPayload.schema +import com.squareup.moshi.JsonClass +import network.xyo.client.payload.Payload +@JsonClass(generateAdapter = true) +class ArchivistInsertQueryPayload : Payload(SCHEMA) { companion object { - val schema = "network.xyo.query.archivist.insert" + const val SCHEMA = "network.xyo.query.archivist.insert" } } \ No newline at end of file diff --git a/sdk/src/main/java/network/xyo/client/archivist/wrapper/ArchivistWrapper.kt b/sdk/src/main/java/network/xyo/client/archivist/wrapper/ArchivistWrapper.kt index c92710a..9dcbc45 100644 --- a/sdk/src/main/java/network/xyo/client/archivist/wrapper/ArchivistWrapper.kt +++ b/sdk/src/main/java/network/xyo/client/archivist/wrapper/ArchivistWrapper.kt @@ -5,7 +5,7 @@ import androidx.annotation.RequiresApi import kotlinx.coroutines.ExperimentalCoroutinesApi import network.xyo.client.node.client.NodeClient import network.xyo.client.node.client.PostQueryResult -import network.xyo.client.payload.XyoPayload +import network.xyo.client.payload.Payload open class ArchivistWrapper(private val nodeClient: NodeClient) { @OptIn(ExperimentalCoroutinesApi::class) @@ -17,7 +17,7 @@ open class ArchivistWrapper(private val nodeClient: NodeClient) { @OptIn(ExperimentalCoroutinesApi::class) @RequiresApi(Build.VERSION_CODES.M) - suspend fun insert(payloads: List): PostQueryResult { + suspend fun insert(payloads: List): PostQueryResult { val query = ArchivistInsertQueryPayload() return nodeClient.query(query, payloads) } diff --git a/sdk/src/main/java/network/xyo/client/boundwitness/XyoBoundWitnessBodyInterface.kt b/sdk/src/main/java/network/xyo/client/boundwitness/BoundWitnessBody.kt similarity index 79% rename from sdk/src/main/java/network/xyo/client/boundwitness/XyoBoundWitnessBodyInterface.kt rename to sdk/src/main/java/network/xyo/client/boundwitness/BoundWitnessBody.kt index 1511ec0..a0a12e7 100644 --- a/sdk/src/main/java/network/xyo/client/boundwitness/XyoBoundWitnessBodyInterface.kt +++ b/sdk/src/main/java/network/xyo/client/boundwitness/BoundWitnessBody.kt @@ -1,8 +1,8 @@ package network.xyo.client.boundwitness -import network.xyo.client.payload.Payload +import network.xyo.client.payload.model.Payload -interface XyoBoundWitnessBodyInterface : Payload { +interface BoundWitnessBodyFields : Payload { var addresses: List var payload_hashes: List var payload_schemas: List diff --git a/sdk/src/main/java/network/xyo/client/boundwitness/XyoBoundWitnessBodyJson.kt b/sdk/src/main/java/network/xyo/client/boundwitness/BoundWitnessBodyJson.kt similarity index 76% rename from sdk/src/main/java/network/xyo/client/boundwitness/XyoBoundWitnessBodyJson.kt rename to sdk/src/main/java/network/xyo/client/boundwitness/BoundWitnessBodyJson.kt index 1e4e720..461b8aa 100644 --- a/sdk/src/main/java/network/xyo/client/boundwitness/XyoBoundWitnessBodyJson.kt +++ b/sdk/src/main/java/network/xyo/client/boundwitness/BoundWitnessBodyJson.kt @@ -1,13 +1,10 @@ package network.xyo.client.boundwitness import com.squareup.moshi.JsonClass -import network.xyo.client.payload.XyoPayload +import network.xyo.client.payload.Payload @JsonClass(generateAdapter = true) -open class XyoBoundWitnessBodyJson(): XyoBoundWitnessBodyInterface, XyoPayload() { - override var schema: String - get() = XyoBoundWitnessBodyJson.schema - set(value) = Unit +open class BoundWitnessBodyJson(): BoundWitnessBodyFields, Payload(SCHEMA) { final override var addresses = emptyList() final override var payload_hashes = emptyList() final override var payload_schemas = emptyList() @@ -27,6 +24,6 @@ open class XyoBoundWitnessBodyJson(): XyoBoundWitnessBodyInterface, XyoPayload() } companion object { - val schema = "network.xyo.boundwitness" + const val SCHEMA = "network.xyo.boundwitness" } } \ No newline at end of file diff --git a/sdk/src/main/java/network/xyo/client/boundwitness/XyoBoundWitnessJson.kt b/sdk/src/main/java/network/xyo/client/boundwitness/BoundWitnessJson.kt similarity index 75% rename from sdk/src/main/java/network/xyo/client/boundwitness/XyoBoundWitnessJson.kt rename to sdk/src/main/java/network/xyo/client/boundwitness/BoundWitnessJson.kt index dec2792..8980a79 100644 --- a/sdk/src/main/java/network/xyo/client/boundwitness/XyoBoundWitnessJson.kt +++ b/sdk/src/main/java/network/xyo/client/boundwitness/BoundWitnessJson.kt @@ -11,7 +11,7 @@ class XyoBoundWitnessMeta: XyoBoundWitnessMetaInterface, JsonSerializable() { } @JsonClass(generateAdapter = true) -open class XyoBoundWitnessJson: XyoBoundWitnessBodyJson() { +open class BoundWitnessJson: BoundWitnessBodyJson() { @Json(ignore = true) val _meta: XyoBoundWitnessMeta = XyoBoundWitnessMeta() @@ -28,7 +28,7 @@ open class XyoBoundWitnessJson: XyoBoundWitnessBodyJson() { return sha256String(this) } - open fun getBodyJson(): XyoBoundWitnessBodyJson { - return XyoBoundWitnessBodyJson(this.addresses, this.previous_hashes, this.payload_hashes, this.payload_schemas, this.timestamp) + open fun getBodyJson(): BoundWitnessBodyJson { + return BoundWitnessBodyJson(this.addresses, this.previous_hashes, this.payload_hashes, this.payload_schemas, this.timestamp) } } \ No newline at end of file diff --git a/sdk/src/main/java/network/xyo/client/boundwitness/QueryBoundWitnessBodyJson.kt b/sdk/src/main/java/network/xyo/client/boundwitness/QueryBoundWitnessBodyJson.kt index a3db021..f329a4b 100644 --- a/sdk/src/main/java/network/xyo/client/boundwitness/QueryBoundWitnessBodyJson.kt +++ b/sdk/src/main/java/network/xyo/client/boundwitness/QueryBoundWitnessBodyJson.kt @@ -5,4 +5,4 @@ import com.squareup.moshi.JsonClass @JsonClass(generateAdapter = true) open class QueryBoundWitnessBodyJson( addresses: List, previous_hashes: List, payload_hashes: List, payload_schemas: List, val query: String, timestamp: Long -): XyoBoundWitnessBodyJson(addresses, previous_hashes, payload_hashes, payload_schemas, timestamp) \ No newline at end of file +): BoundWitnessBodyJson(addresses, previous_hashes, payload_hashes, payload_schemas, timestamp) \ No newline at end of file diff --git a/sdk/src/main/java/network/xyo/client/boundwitness/QueryBoundWitnessBuilder.kt b/sdk/src/main/java/network/xyo/client/boundwitness/QueryBoundWitnessBuilder.kt index dfab433..fe49cf8 100644 --- a/sdk/src/main/java/network/xyo/client/boundwitness/QueryBoundWitnessBuilder.kt +++ b/sdk/src/main/java/network/xyo/client/boundwitness/QueryBoundWitnessBuilder.kt @@ -3,14 +3,14 @@ package network.xyo.client.boundwitness import android.os.Build import androidx.annotation.RequiresApi import network.xyo.client.account.model.AccountInstance -import network.xyo.client.payload.XyoPayload +import network.xyo.client.payload.Payload @RequiresApi(Build.VERSION_CODES.M) class QueryBoundWitnessBuilder : BoundWitnessBuilder() { private lateinit var queryHash: String - fun query(query: XyoPayload): QueryBoundWitnessBuilder { + fun query(query: Payload): QueryBoundWitnessBuilder { this.queryHash = query.dataHash() this.payload(query.schema, query) return this diff --git a/sdk/src/main/java/network/xyo/client/boundwitness/QueryBoundWitnessJson.kt b/sdk/src/main/java/network/xyo/client/boundwitness/QueryBoundWitnessJson.kt index 7dbb7a6..59571a0 100644 --- a/sdk/src/main/java/network/xyo/client/boundwitness/QueryBoundWitnessJson.kt +++ b/sdk/src/main/java/network/xyo/client/boundwitness/QueryBoundWitnessJson.kt @@ -3,7 +3,7 @@ package network.xyo.client.boundwitness import com.squareup.moshi.JsonClass @JsonClass(generateAdapter = true) -open class QueryBoundWitnessJson: XyoBoundWitnessJson() { +open class QueryBoundWitnessJson: BoundWitnessJson() { var query: String? = null // override to return a bound witness json body that has query in its hashable fields diff --git a/sdk/src/main/java/network/xyo/client/boundwitness/XyoBoundWitnessBuilder.kt b/sdk/src/main/java/network/xyo/client/boundwitness/XyoBoundWitnessBuilder.kt index 654fa83..317b4e5 100644 --- a/sdk/src/main/java/network/xyo/client/boundwitness/XyoBoundWitnessBuilder.kt +++ b/sdk/src/main/java/network/xyo/client/boundwitness/XyoBoundWitnessBuilder.kt @@ -5,7 +5,7 @@ import androidx.annotation.RequiresApi import network.xyo.client.lib.JsonSerializable import network.xyo.client.account.hexStringToByteArray import network.xyo.client.account.model.AccountInstance -import network.xyo.client.payload.XyoPayload +import network.xyo.client.payload.Payload import network.xyo.client.payload.XyoValidationException @RequiresApi(Build.VERSION_CODES.M) @@ -13,8 +13,8 @@ open class BoundWitnessBuilder { protected var _signers = mutableListOf() protected var _payload_hashes = mutableListOf() protected var _payload_schemas = mutableListOf() - protected var _payloads = mutableListOf() - protected open var bw: XyoBoundWitnessJson = XyoBoundWitnessJson() + protected var _payloads = mutableListOf() + protected open var bw: BoundWitnessJson = BoundWitnessJson() var _timestamp: Long? = null @@ -33,7 +33,7 @@ open class BoundWitnessBuilder { } @OptIn(ExperimentalStdlibApi::class) - private fun hashableFields(): XyoBoundWitnessBodyJson { + private fun hashableFields(): BoundWitnessBodyJson { // if a timestamp is not provided, set one at the time hashable fields are set bw.timestamp = _timestamp ?: System.currentTimeMillis() @@ -46,7 +46,7 @@ open class BoundWitnessBuilder { } @Throws(XyoValidationException::class) - fun payload(schema: String, payload: T): BoundWitnessBuilder { + fun payload(schema: String, payload: T): BoundWitnessBuilder { payload.validate() _payloads.add(payload) _payload_hashes.add(payload.dataHash()) @@ -55,7 +55,7 @@ open class BoundWitnessBuilder { } @Throws(XyoValidationException::class) - fun payloads(payloads: List): BoundWitnessBuilder { + fun payloads(payloads: List): BoundWitnessBuilder { payloads.forEach { payload(it.schema, it) } @@ -92,7 +92,7 @@ open class BoundWitnessBuilder { bw.meta.signatures = this.sign(hash) } - open suspend fun build(): XyoBoundWitnessJson { + open suspend fun build(): BoundWitnessJson { return bw.let{ // update fields constructFields() diff --git a/sdk/src/main/java/network/xyo/client/module/ModuleQueryResult.kt b/sdk/src/main/java/network/xyo/client/module/ModuleQueryResult.kt index e9ba478..e313ea0 100644 --- a/sdk/src/main/java/network/xyo/client/module/ModuleQueryResult.kt +++ b/sdk/src/main/java/network/xyo/client/module/ModuleQueryResult.kt @@ -1,5 +1,5 @@ package network.xyo.client.module -import network.xyo.client.boundwitness.XyoBoundWitnessJson +import network.xyo.client.boundwitness.BoundWitnessJson -typealias ModuleQueryResult = Triple, List> \ No newline at end of file +typealias ModuleQueryResult = Triple, List> \ No newline at end of file diff --git a/sdk/src/main/java/network/xyo/client/node/client/NodeClient.kt b/sdk/src/main/java/network/xyo/client/node/client/NodeClient.kt index a1fc0c7..220fec8 100644 --- a/sdk/src/main/java/network/xyo/client/node/client/NodeClient.kt +++ b/sdk/src/main/java/network/xyo/client/node/client/NodeClient.kt @@ -13,7 +13,7 @@ import network.xyo.client.account.Account import network.xyo.client.account.model.AccountInstance import network.xyo.client.boundwitness.QueryBoundWitnessBuilder import network.xyo.client.boundwitness.QueryBoundWitnessJson -import network.xyo.client.payload.XyoPayload +import network.xyo.client.payload.Payload import okhttp3.MediaType.Companion.toMediaType import okhttp3.OkHttpClient import okhttp3.Request @@ -39,7 +39,7 @@ class NodeClient(private val url: String, private val accountToUse: AccountInsta @ExperimentalCoroutinesApi @RequiresApi(Build.VERSION_CODES.M) - suspend fun query(query: XyoPayload, payloads: List?): PostQueryResult { + suspend fun query(query: Payload, payloads: List?): PostQueryResult { val bodyString = buildQuery(query, payloads) val postBody = bodyString.toRequestBody(MEDIA_TYPE_JSON) val request = Request.Builder() @@ -81,7 +81,7 @@ class NodeClient(private val url: String, private val accountToUse: AccountInsta } @RequiresApi(Build.VERSION_CODES.M) - private suspend fun buildQuery(query: XyoPayload, payloads: List?): String { + private suspend fun buildQuery(query: Payload, payloads: List?): String { val builtQuery = queryBuilder(query, payloads) val queryPayloads = buildQueryPayloads(query, payloads) val queryPayloadsJsonArray = queryPayloadsJsonArray(queryPayloads) @@ -89,7 +89,7 @@ class NodeClient(private val url: String, private val accountToUse: AccountInsta return builtQueryTuple.joinToString(",", "[", "]") } - private suspend fun queryBuilder(query: XyoPayload, payloads: List?): QueryBoundWitnessJson { + private suspend fun queryBuilder(query: Payload, payloads: List?): QueryBoundWitnessJson { return QueryBoundWitnessBuilder().let { payloads?.let { payload -> it.payloads(payload) @@ -100,8 +100,8 @@ class NodeClient(private val url: String, private val accountToUse: AccountInsta } // combine payloads and query - private fun buildQueryPayloads(query: XyoPayload, payloads: List?): List{ - return arrayListOf().let { queryPayloads -> + private fun buildQueryPayloads(query: Payload, payloads: List?): List{ + return arrayListOf().let { queryPayloads -> payloads?.let { payloads -> payloads.forEach { payload -> queryPayloads.add(payload) @@ -113,7 +113,7 @@ class NodeClient(private val url: String, private val accountToUse: AccountInsta } // stringify combined payloads - private fun queryPayloadsJsonArray(payloads: List): JSONArray { + private fun queryPayloadsJsonArray(payloads: List): JSONArray { return JSONArray().apply { payloads.forEach { payload -> val serializedPayload = JsonSerializable.toJson(payload) diff --git a/sdk/src/main/java/network/xyo/client/node/client/QueryResponse.kt b/sdk/src/main/java/network/xyo/client/node/client/QueryResponse.kt index 5bafa26..8ce1959 100644 --- a/sdk/src/main/java/network/xyo/client/node/client/QueryResponse.kt +++ b/sdk/src/main/java/network/xyo/client/node/client/QueryResponse.kt @@ -3,16 +3,16 @@ package network.xyo.client.node.client import com.squareup.moshi.Moshi import com.squareup.moshi.Types import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory -import network.xyo.client.boundwitness.XyoBoundWitnessBodyJson -import network.xyo.client.payload.XyoPayload +import network.xyo.client.boundwitness.BoundWitnessBodyJson +import network.xyo.client.payload.Payload import org.json.JSONArray import org.json.JSONObject open class QueryResponseWrapper(val rawResponse: String) { val moshi: Moshi = Moshi.Builder().addLast(KotlinJsonAdapterFactory()).build() var bwHash: String? = null - var bw: XyoBoundWitnessBodyJson? = null - var payloads: List? = null + var bw: BoundWitnessBodyJson? = null + var payloads: List? = null private fun unwrap() { val response = JSONObject(rawResponse) @@ -32,8 +32,8 @@ open class QueryResponseWrapper(val rawResponse: String) { payloads = parsePayloads(payloadsString) } - protected open fun parseBW(bwString: String): XyoBoundWitnessBodyJson? { - val bwAdapter = moshi.adapter(XyoBoundWitnessBodyJson::class.java) + protected open fun parseBW(bwString: String): BoundWitnessBodyJson? { + val bwAdapter = moshi.adapter(BoundWitnessBodyJson::class.java) val bw = bwAdapter.fromJson(bwString) return bw } @@ -47,9 +47,9 @@ open class QueryResponseWrapper(val rawResponse: String) { * @param payloadsString * @return */ - open fun parsePayloads(payloadsString: String): List? { - val type = Types.newParameterizedType(List::class.java, XyoPayload::class.java) - val payloadAdapter = moshi.adapter>(type) + open fun parsePayloads(payloadsString: String): List? { + val type = Types.newParameterizedType(List::class.java, Payload::class.java) + val payloadAdapter = moshi.adapter>(type) return payloadAdapter.fromJson(payloadsString) } diff --git a/sdk/src/main/java/network/xyo/client/payload/Payload.kt b/sdk/src/main/java/network/xyo/client/payload/Payload.kt index 7b1839c..7391bd6 100644 --- a/sdk/src/main/java/network/xyo/client/payload/Payload.kt +++ b/sdk/src/main/java/network/xyo/client/payload/Payload.kt @@ -1,13 +1,22 @@ package network.xyo.client.payload -import com.squareup.moshi.Json -import java.io.Serializable +import com.squareup.moshi.JsonClass +import network.xyo.client.lib.JsonSerializable -interface Payload : Serializable { - var schema: String -} +open class XyoException(message: String): Throwable(message) +open class XyoValidationException(message: String): XyoException(message) +open class XyoInvalidSchemaException(val schema: String): XyoValidationException("'schema' must be lowercase [${schema}]") -interface PayloadWithMeta : Serializable { - @Json(name = "\$meta") - var _meta: Serializable +@JsonClass(generateAdapter = true) +open class Payload(override var schema: String) : network.xyo.client.payload.model.Payload, JsonSerializable() { + @Throws(XyoValidationException::class) + open fun validate() { + if (schema != schema.lowercase()) { + throw XyoInvalidSchemaException(schema) + } + } + + open fun dataHash(): String { + return sha256String(this) + } } \ No newline at end of file diff --git a/sdk/src/main/java/network/xyo/client/payload/XyoPayload.kt b/sdk/src/main/java/network/xyo/client/payload/XyoPayload.kt deleted file mode 100644 index 7db12bb..0000000 --- a/sdk/src/main/java/network/xyo/client/payload/XyoPayload.kt +++ /dev/null @@ -1,27 +0,0 @@ -package network.xyo.client.payload - -import com.squareup.moshi.JsonClass -import network.xyo.client.lib.JsonSerializable - -open class XyoException(message: String): Throwable(message) -open class XyoValidationException(message: String): XyoException(message) -open class XyoInvalidSchemaException(val schema: String): XyoValidationException("'schema' must be lowercase [${schema}]") - -@JsonClass(generateAdapter = true) -open class XyoPayload : Payload, JsonSerializable() { - // Note: Has to be var because "Moshi does not consider a val as a serializable member because - // it cannot be symmetrically deserialized." - // see - https://github.com/square/moshi/issues/1803 - override var schema: String = "network.xyo.base.schema" - - @Throws(XyoValidationException::class) - open fun validate() { - if (schema != schema.lowercase()) { - throw XyoInvalidSchemaException(schema) - } - } - - open fun dataHash(): String { - return sha256String(this) - } -} \ No newline at end of file diff --git a/sdk/src/main/java/network/xyo/client/payload/model/Payload.kt b/sdk/src/main/java/network/xyo/client/payload/model/Payload.kt new file mode 100644 index 0000000..6f18495 --- /dev/null +++ b/sdk/src/main/java/network/xyo/client/payload/model/Payload.kt @@ -0,0 +1,13 @@ +package network.xyo.client.payload.model + +import com.squareup.moshi.Json +import java.io.Serializable + +interface Payload : Serializable { + var schema: String +} + +interface WithMeta : Serializable { + @Json(name = "\$meta") + var _meta: Serializable +} \ No newline at end of file diff --git a/sdk/src/main/java/network/xyo/client/witness/XyoPanel.kt b/sdk/src/main/java/network/xyo/client/witness/XyoPanel.kt index f9a56dd..417405b 100644 --- a/sdk/src/main/java/network/xyo/client/witness/XyoPanel.kt +++ b/sdk/src/main/java/network/xyo/client/witness/XyoPanel.kt @@ -7,18 +7,18 @@ import androidx.annotation.RequiresApi import network.xyo.client.account.model.AccountInstance import network.xyo.client.archivist.wrapper.ArchivistWrapper import network.xyo.client.boundwitness.BoundWitnessBuilder -import network.xyo.client.boundwitness.XyoBoundWitnessJson +import network.xyo.client.boundwitness.BoundWitnessJson import network.xyo.client.node.client.NodeClient import network.xyo.client.node.client.PostQueryResult -import network.xyo.client.payload.XyoPayload +import network.xyo.client.payload.Payload -data class XyoPanelReportQueryResult(val bw: XyoBoundWitnessJson, val apiResults: List?, val payloads: List?) +data class XyoPanelReportQueryResult(val bw: BoundWitnessJson, val apiResults: List?, val payloads: List?) @RequiresApi(Build.VERSION_CODES.M) class XyoPanel( val context: Context, val account: AccountInstance, - private val witnesses: List>?, + private val witnesses: List>?, private val nodeUrlsAndAccounts: ArrayList>? ) { private var nodes: MutableList? = null @@ -27,7 +27,7 @@ class XyoPanel( context: Context, account: AccountInstance, nodeUrlsAndAccounts: ArrayList>, - witnesses: List>? = null + witnesses: List>? = null ): this( context, account, @@ -38,7 +38,7 @@ class XyoPanel( constructor( context: Context, account: AccountInstance, - observe: ((context: Context) -> List?)? + observe: ((context: Context) -> List?)? ): this( context, account, @@ -60,7 +60,7 @@ class XyoPanel( } } - private suspend fun generateBoundWitnessJson(payloads: List): XyoBoundWitnessJson { + private suspend fun generateBoundWitnessJson(payloads: List): BoundWitnessJson { return BoundWitnessBuilder() .payloads(payloads) .signer(account) @@ -68,8 +68,8 @@ class XyoPanel( } - private suspend fun generatePayloads(adhocWitnesses: List> = emptyList()): List { - val witnesses: List> = (this.witnesses ?: emptyList()).plus(adhocWitnesses) + private suspend fun generatePayloads(adhocWitnesses: List> = emptyList()): List { + val witnesses: List> = (this.witnesses ?: emptyList()).plus(adhocWitnesses) val payloads = witnesses.map { witness -> witness.observe(context) } @@ -78,7 +78,7 @@ class XyoPanel( } @kotlinx.coroutines.ExperimentalCoroutinesApi - suspend fun reportAsyncQuery(adhocWitnesses: List> = emptyList()): XyoPanelReportQueryResult { + suspend fun reportAsyncQuery(adhocWitnesses: List> = emptyList()): XyoPanelReportQueryResult { if (nodes == null) resolveNodes() val payloads = generatePayloads(adhocWitnesses) val bw = generateBoundWitnessJson(payloads) diff --git a/sdk/src/main/java/network/xyo/client/witness/XyoWitness.kt b/sdk/src/main/java/network/xyo/client/witness/XyoWitness.kt index 226716c..9a54334 100644 --- a/sdk/src/main/java/network/xyo/client/witness/XyoWitness.kt +++ b/sdk/src/main/java/network/xyo/client/witness/XyoWitness.kt @@ -5,14 +5,14 @@ import android.os.Build import androidx.annotation.RequiresApi import network.xyo.client.account.Account import network.xyo.client.account.model.AccountInstance -import network.xyo.client.payload.XyoPayload +import network.xyo.client.payload.Payload -abstract class DeferredObserver { +abstract class DeferredObserver { abstract suspend fun deferredDetect(context: Context): List? } @RequiresApi(Build.VERSION_CODES.M) -open class XyoWitness ( +open class XyoWitness ( val address: AccountInstance = Account.random(), private val observer: ((context: Context) -> List?)? = null, val deferredObserver: DeferredObserver? = null diff --git a/sdk/src/main/java/network/xyo/client/witness/location/info/Handler.kt b/sdk/src/main/java/network/xyo/client/witness/location/info/Handler.kt index daf0b61..23ea4e1 100644 --- a/sdk/src/main/java/network/xyo/client/witness/location/info/Handler.kt +++ b/sdk/src/main/java/network/xyo/client/witness/location/info/Handler.kt @@ -10,13 +10,13 @@ import network.xyo.client.witness.types.WitnessHandlerInterface import network.xyo.client.witness.types.WitnessResult import network.xyo.client.witness.XyoPanel import network.xyo.client.account.model.AccountInstance -import network.xyo.client.boundwitness.XyoBoundWitnessJson -import network.xyo.client.payload.XyoPayload +import network.xyo.client.boundwitness.BoundWitnessJson +import network.xyo.client.payload.Payload import network.xyo.client.settings.XyoSdk -open class WitnessLocationHandler : WitnessHandlerInterface> { +open class WitnessLocationHandler : WitnessHandlerInterface> { @RequiresApi(Build.VERSION_CODES.M) - override suspend fun witness(context: Context, nodeUrlsAndAccounts: ArrayList>): WitnessResult> { + override suspend fun witness(context: Context, nodeUrlsAndAccounts: ArrayList>): WitnessResult> { val account = XyoSdk.getInstance(context.applicationContext).getAccount(context) val panel = XyoPanel(context, account, nodeUrlsAndAccounts, listOf( XyoLocationWitness(account) @@ -26,19 +26,19 @@ open class WitnessLocationHandler : WitnessHandlerInterface> { + private suspend fun getLocation(panel: XyoPanel): WitnessResult> { return withContext(Dispatchers.IO) { - var locationPayload: XyoPayload? = null - var locationPayloadRaw: XyoPayload? = null - var bw: XyoBoundWitnessJson? = null + var locationPayload: Payload? = null + var locationPayloadRaw: Payload? = null + var bw: BoundWitnessJson? = null val errors: MutableList = mutableListOf() panel.let { it.reportAsyncQuery().let { result -> val actualPayloads = result.payloads actualPayloads?.forEach { payload -> when (payload) { - is XyoLocationPayload -> locationPayload = payload - is XyoLocationPayloadRaw -> locationPayloadRaw = payload + is LocationPayload -> locationPayload = payload + is LocationPayloadRaw -> locationPayloadRaw = payload } } diff --git a/sdk/src/main/java/network/xyo/client/witness/location/info/XyoLocationPayload.kt b/sdk/src/main/java/network/xyo/client/witness/location/info/LocationPayload.kt similarity index 63% rename from sdk/src/main/java/network/xyo/client/witness/location/info/XyoLocationPayload.kt rename to sdk/src/main/java/network/xyo/client/witness/location/info/LocationPayload.kt index 161888d..f0be30a 100644 --- a/sdk/src/main/java/network/xyo/client/witness/location/info/XyoLocationPayload.kt +++ b/sdk/src/main/java/network/xyo/client/witness/location/info/LocationPayload.kt @@ -1,7 +1,7 @@ package network.xyo.client.witness.location.info import com.squareup.moshi.JsonClass -import network.xyo.client.payload.XyoPayload +import network.xyo.client.payload.Payload @JsonClass(generateAdapter = true) data class Coordinates( @@ -21,21 +21,18 @@ data class CurrentLocation( ) @JsonClass(generateAdapter = true) -class XyoLocationPayload( +class LocationPayload( val currentLocation: CurrentLocation? = null, -): XyoPayload() { - override var schema: String - get() = XyoLocationPayload.schema - set(value) = Unit - +): Payload(SCHEMA) { override fun dataHash(): String { return sha256String(this) } companion object { - val schema = "network.xyo.location.current" - fun detect(currentLocation: CurrentLocation?): XyoLocationPayload { - return XyoLocationPayload(currentLocation) + const val SCHEMA = "network.xyo.location.current" + + fun detect(currentLocation: CurrentLocation?): LocationPayload { + return LocationPayload(currentLocation) } } } \ No newline at end of file diff --git a/sdk/src/main/java/network/xyo/client/witness/location/info/XyoLocationPayloadRaw.kt b/sdk/src/main/java/network/xyo/client/witness/location/info/LocationPayloadRaw.kt similarity index 87% rename from sdk/src/main/java/network/xyo/client/witness/location/info/XyoLocationPayloadRaw.kt rename to sdk/src/main/java/network/xyo/client/witness/location/info/LocationPayloadRaw.kt index 0d26569..a2d3e30 100644 --- a/sdk/src/main/java/network/xyo/client/witness/location/info/XyoLocationPayloadRaw.kt +++ b/sdk/src/main/java/network/xyo/client/witness/location/info/LocationPayloadRaw.kt @@ -2,7 +2,7 @@ package network.xyo.client.witness.location.info import android.os.Bundle import com.squareup.moshi.JsonClass -import network.xyo.client.payload.XyoPayload +import network.xyo.client.payload.Payload // Extension function for safely retrieving typed values from a Bundle private fun Bundle.getTypedValue(key: String): Any? { @@ -23,7 +23,7 @@ private fun Bundle.getTypedValue(key: String): Any? { } @JsonClass(generateAdapter = true) -open class XyoLocationPayloadRaw( +open class LocationPayloadRaw( val provider: String?, val latitude: Double, val longitude: Double, @@ -37,17 +37,13 @@ open class XyoLocationPayloadRaw( val time: Long, val isMock: Boolean?, val extras: Map? = null -): XyoPayload() { - override var schema: String - get() = XyoLocationPayloadRaw.schema - set(value) = Unit - +): Payload(SCHEMA) { override fun dataHash(): String { return sha256String(this) } companion object { - val schema = "network.xyo.location.android" + const val SCHEMA = "network.xyo.location.android" fun detect( provider: String?, latitude: Double, @@ -62,12 +58,12 @@ open class XyoLocationPayloadRaw( time: Long, isMock: Boolean?, extras: Bundle? = null - ): XyoLocationPayloadRaw { + ): LocationPayloadRaw { val extrasMap = extras?.keySet()?.associateWith { key -> extras.getTypedValue(key) } - return XyoLocationPayloadRaw( + return LocationPayloadRaw( provider, latitude, longitude, diff --git a/sdk/src/main/java/network/xyo/client/witness/location/info/XyoLocationCurrent.kt b/sdk/src/main/java/network/xyo/client/witness/location/info/XyoLocationCurrent.kt index 44e277a..8c4ee9a 100644 --- a/sdk/src/main/java/network/xyo/client/witness/location/info/XyoLocationCurrent.kt +++ b/sdk/src/main/java/network/xyo/client/witness/location/info/XyoLocationCurrent.kt @@ -18,7 +18,7 @@ class XyoLocationCurrent { @RequiresApi(Build.VERSION_CODES.O) @SuppressLint("MissingPermission") - suspend fun detect(context: Context): Pair? { + suspend fun detect(context: Context): Pair? { if (LocationPermissions.check((context)) && LocationPermissions.checkGooglePlayServices(context)) { val fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(context) return suspendCancellableCoroutine { continuation -> @@ -34,7 +34,7 @@ class XyoLocationCurrent { val currentLocation = CurrentLocation(coordinates, System.currentTimeMillis()) // Resume the coroutine with the retrieved location - continuation.resumeWith(Result.success(Pair(XyoLocationPayload(currentLocation), locationRaw))) + continuation.resumeWith(Result.success(Pair(LocationPayload(currentLocation), locationRaw))) } .addOnFailureListener { exception -> // Resume the coroutine with an exception if the task fails @@ -63,8 +63,8 @@ class XyoLocationCurrent { } @RequiresApi(Build.VERSION_CODES.O) - private fun buildRawLocationPayload(location: Location): XyoLocationPayloadRaw { - return XyoLocationPayloadRaw.detect( + private fun buildRawLocationPayload(location: Location): LocationPayloadRaw { + return LocationPayloadRaw.detect( location.provider, location.latitude, location.longitude, diff --git a/sdk/src/main/java/network/xyo/client/witness/location/info/XyoLocationPayloads.kt b/sdk/src/main/java/network/xyo/client/witness/location/info/XyoLocationPayloads.kt index 8c28585..6072d23 100644 --- a/sdk/src/main/java/network/xyo/client/witness/location/info/XyoLocationPayloads.kt +++ b/sdk/src/main/java/network/xyo/client/witness/location/info/XyoLocationPayloads.kt @@ -10,7 +10,7 @@ class XyoLocationPayloads { @RequiresApi(Build.VERSION_CODES.O) @SuppressLint("MissingPermission") - suspend fun detect(context: Context): Pair? { + suspend fun detect(context: Context): Pair? { return XyoLocationCurrent.detect(context) } } diff --git a/sdk/src/main/java/network/xyo/client/witness/location/info/XyoLocationWitness.kt b/sdk/src/main/java/network/xyo/client/witness/location/info/XyoLocationWitness.kt index 5dc4c81..72ec598 100644 --- a/sdk/src/main/java/network/xyo/client/witness/location/info/XyoLocationWitness.kt +++ b/sdk/src/main/java/network/xyo/client/witness/location/info/XyoLocationWitness.kt @@ -8,13 +8,13 @@ import network.xyo.client.witness.DeferredObserver import network.xyo.client.witness.XyoWitness import network.xyo.client.account.Account import network.xyo.client.account.model.AccountInstance -import network.xyo.client.payload.XyoPayload +import network.xyo.client.payload.Payload -class DeferredLocationObserver : DeferredObserver() { +class DeferredLocationObserver : DeferredObserver() { @RequiresApi(Build.VERSION_CODES.O) override suspend fun deferredDetect( context: Context, - ): List? { + ): List? { try { val payloads = XyoLocationPayloads.detect(context) // only return the payloads that were found @@ -32,7 +32,7 @@ class DeferredLocationObserver : DeferredObserver() { } @RequiresApi(Build.VERSION_CODES.M) -class XyoLocationWitness(address: AccountInstance = Account.random()) : XyoWitness( +class XyoLocationWitness(address: AccountInstance = Account.random()) : XyoWitness( DeferredLocationObserver(), address ) \ No newline at end of file diff --git a/sdk/src/main/java/network/xyo/client/witness/system/info/XyoSystemInfoPayload.kt b/sdk/src/main/java/network/xyo/client/witness/system/info/SystemInfoPayload.kt similarity index 65% rename from sdk/src/main/java/network/xyo/client/witness/system/info/XyoSystemInfoPayload.kt rename to sdk/src/main/java/network/xyo/client/witness/system/info/SystemInfoPayload.kt index f62fe88..1480e3b 100644 --- a/sdk/src/main/java/network/xyo/client/witness/system/info/XyoSystemInfoPayload.kt +++ b/sdk/src/main/java/network/xyo/client/witness/system/info/SystemInfoPayload.kt @@ -4,28 +4,24 @@ import android.content.Context import android.os.Build import androidx.annotation.RequiresApi import com.squareup.moshi.JsonClass -import network.xyo.client.payload.XyoPayload +import network.xyo.client.payload.Payload @JsonClass(generateAdapter = true) -class XyoSystemInfoPayload( +class SystemInfoPayload( val device: XyoSystemInfoDevice? = null, val network: XyoSystemInfoNetwork? = null, val os: XyoSystemInfoOs? = null -): XyoPayload () { - override var schema: String - get() = XyoSystemInfoPayload.schema - set(value) = Unit - +): Payload (SCHEMA) { override fun dataHash(): String { return sha256String(this) } companion object { - val schema = "network.xyo.system.info.android" + const val SCHEMA = "network.xyo.system.info.android" @RequiresApi(Build.VERSION_CODES.M) - fun detect(context: Context): XyoSystemInfoPayload { - return XyoSystemInfoPayload( + fun detect(context: Context): SystemInfoPayload { + return SystemInfoPayload( XyoSystemInfoDevice.detect(context), XyoSystemInfoNetwork.detect(context), XyoSystemInfoOs.detect(context) diff --git a/sdk/src/main/java/network/xyo/client/witness/system/info/XyoSystemInfoWitness.kt b/sdk/src/main/java/network/xyo/client/witness/system/info/XyoSystemInfoWitness.kt index 783e364..7bac2fe 100644 --- a/sdk/src/main/java/network/xyo/client/witness/system/info/XyoSystemInfoWitness.kt +++ b/sdk/src/main/java/network/xyo/client/witness/system/info/XyoSystemInfoWitness.kt @@ -8,9 +8,9 @@ import network.xyo.client.account.Account import network.xyo.client.account.model.AccountInstance @RequiresApi(Build.VERSION_CODES.M) -class XyoSystemInfoWitness(address: AccountInstance = Account.random()) : XyoWitness( - fun (context: Context): List { - return listOf(XyoSystemInfoPayload.detect(context)) +class XyoSystemInfoWitness(address: AccountInstance = Account.random()) : XyoWitness( + fun (context: Context): List { + return listOf(SystemInfoPayload.detect(context)) }, address ) \ No newline at end of file