diff --git a/parsely/src/main/java/com/parsely/parselyandroid/ParselyAPIConnection.kt b/parsely/src/main/java/com/parsely/parselyandroid/ParselyAPIConnection.kt index 91d4aa3e..c1c1e422 100644 --- a/parsely/src/main/java/com/parsely/parselyandroid/ParselyAPIConnection.kt +++ b/parsely/src/main/java/com/parsely/parselyandroid/ParselyAPIConnection.kt @@ -17,12 +17,13 @@ package com.parsely.parselyandroid import java.net.HttpURLConnection import java.net.URL -import kotlinx.coroutines.CoroutineDispatcher -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.withContext -internal open class ParselyAPIConnection(private val url: String) { - open suspend fun send(payload: String): Result { +internal interface RestClient { + suspend fun send(payload: String): Result +} + +internal class ParselyAPIConnection(private val url: String) : RestClient { + override suspend fun send(payload: String): Result { var connection: HttpURLConnection? = null try { connection = URL(url).openConnection() as HttpURLConnection diff --git a/parsely/src/main/java/com/parsely/parselyandroid/SendEvents.kt b/parsely/src/main/java/com/parsely/parselyandroid/SendEvents.kt index e613f13b..b1f06e20 100644 --- a/parsely/src/main/java/com/parsely/parselyandroid/SendEvents.kt +++ b/parsely/src/main/java/com/parsely/parselyandroid/SendEvents.kt @@ -9,7 +9,7 @@ import kotlinx.coroutines.sync.withLock internal class SendEvents( private val flushManager: FlushManager, private val repository: QueueRepository, - private val parselyAPIConnection: ParselyAPIConnection, + private val restClient: RestClient, private val scope: CoroutineScope ) { @@ -35,7 +35,7 @@ internal class SendEvents( repository.remove(eventsToSend) } else { ParselyTracker.PLog("Requested %s", ParselyTracker.ROOT_URL) - parselyAPIConnection.send(jsonPayload) + restClient.send(jsonPayload) .fold( onSuccess = { ParselyTracker.PLog("Pixel request success") diff --git a/parsely/src/test/java/com/parsely/parselyandroid/SendEventsTest.kt b/parsely/src/test/java/com/parsely/parselyandroid/SendEventsTest.kt index cb3a2524..77be99d6 100644 --- a/parsely/src/test/java/com/parsely/parselyandroid/SendEventsTest.kt +++ b/parsely/src/test/java/com/parsely/parselyandroid/SendEventsTest.kt @@ -21,7 +21,7 @@ class SendEventsTest { sut = SendEvents( FakeFlushManager(), FakeRepository(), - FakeParselyAPIConnection(), + FakeRestClient(), this ) @@ -40,7 +40,7 @@ class SendEventsTest { val repository = FakeRepository().apply { insertEvents(listOf(mapOf("test" to 123))) } - val parselyAPIConnection = FakeParselyAPIConnection().apply { + val parselyAPIConnection = FakeRestClient().apply { nextResult = Result.success(Unit) } sut = SendEvents( @@ -68,7 +68,7 @@ class SendEventsTest { sut = SendEvents( FakeFlushManager(), repository, - FakeParselyAPIConnection(), + FakeRestClient(), this ) @@ -87,7 +87,7 @@ class SendEventsTest { val repository = FakeRepository().apply { insertEvents(listOf(mapOf("test" to 123))) } - val parselyAPIConnection = FakeParselyAPIConnection().apply { + val parselyAPIConnection = FakeRestClient().apply { nextResult = Result.failure(Exception()) } sut = SendEvents( @@ -113,7 +113,7 @@ class SendEventsTest { val repository = FakeRepository().apply { insertEvents(listOf(mapOf("test" to 123))) } - val parselyAPIConnection = FakeParselyAPIConnection().apply { + val parselyAPIConnection = FakeRestClient().apply { nextResult = Result.success(Unit) } sut = SendEvents( @@ -139,7 +139,7 @@ class SendEventsTest { val repository = FakeRepository().apply { insertEvents(listOf(mapOf("test" to 123))) } - val parselyAPIConnection = FakeParselyAPIConnection().apply { + val parselyAPIConnection = FakeRestClient().apply { nextResult = Result.failure(Exception()) } sut = SendEvents( @@ -167,7 +167,7 @@ class SendEventsTest { return ArrayList(listOf(mapOf("test" to 123))) } } - val parselyAPIConnection = FakeParselyAPIConnection().apply { + val parselyAPIConnection = FakeRestClient().apply { nextResult = Result.success(Unit) } sut = SendEvents( @@ -192,7 +192,7 @@ class SendEventsTest { sut = SendEvents( flushManager, FakeRepository(), - FakeParselyAPIConnection(), + FakeRestClient(), this ) @@ -236,7 +236,7 @@ class SendEventsTest { } } - private class FakeParselyAPIConnection : ParselyAPIConnection("") { + private class FakeRestClient : RestClient { var nextResult: Result? = null