Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions lib/src/main/java/com/telemetrydeck/sdk/TelemetryApiClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,3 @@ interface TelemetryApiClient {
fun getServiceUrl(): URL
}

interface TelemetryApiClientFactory {
fun create(
apiBaseURL: URL,
showDebugLogs: Boolean,
logger:DebugLogger?
): TelemetryApiClient
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.telemetrydeck.sdk

import java.net.URL

interface TelemetryApiClientFactory {
fun create(
apiBaseURL: URL,
showDebugLogs: Boolean,
namespace: String?,
logger: DebugLogger?
): TelemetryApiClient
}
26 changes: 10 additions & 16 deletions lib/src/main/java/com/telemetrydeck/sdk/TelemetryClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,14 @@ import io.ktor.http.HttpHeaders
import io.ktor.serialization.kotlinx.json.json
import java.net.URL

internal class TelemetryClientFactory: TelemetryApiClientFactory {
override fun create(
apiBaseURL: URL,
showDebugLogs: Boolean,
logger: DebugLogger?
): TelemetryApiClient {
return TelemetryClient(
apiBaseURL,
showDebugLogs,
logger
)
}
}


/**
* The HTTP client to communicate with TelemetryDeck's API
*/
internal class TelemetryClient(
private val apiBaseURL: URL,
private val showDebugLogs: Boolean,
private val namespace: String?,
private val debugLogger: DebugLogger?
) : TelemetryApiClient {
private val client: HttpClient = HttpClient(OkHttp) {
Expand Down Expand Up @@ -71,8 +58,15 @@ internal class TelemetryClient(

override fun getServiceUrl(): URL {
val baseUri = apiBaseURL.toURI()
val serviceUri = baseUri.resolve("/v2/")
serviceUri.normalize()
val serviceUri = if (!namespace.isNullOrBlank()) {
val uri = baseUri.resolve("/v2/namespace/$namespace/")
uri.normalize()
uri
} else {
val uri = baseUri.resolve("/v2/")
uri.normalize()
uri
}
return serviceUri.toURL()
}
}
19 changes: 19 additions & 0 deletions lib/src/main/java/com/telemetrydeck/sdk/TelemetryClientFactory.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.telemetrydeck.sdk

import java.net.URL

internal class TelemetryClientFactory: TelemetryApiClientFactory {
override fun create(
apiBaseURL: URL,
showDebugLogs: Boolean,
namespace: String?,
logger: DebugLogger?
): TelemetryApiClient {
return TelemetryClient(
apiBaseURL,
showDebugLogs,
namespace,
logger
)
}
}
3 changes: 2 additions & 1 deletion lib/src/main/java/com/telemetrydeck/sdk/TelemetryDeck.kt
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ class TelemetryDeck(
val client = telemetryClientFactory.create(
configuration.apiBaseURL,
configuration.showDebugLogs,
logger
configuration.namespace,
logger,
)
client.send(signals)
success(Unit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ data class TelemetryManagerConfiguration(
*/
var apiBaseURL: URL = URL("https://nom.telemetrydeck.com"),


/**
* The TelemetryDeck namespace of your organization.
*/
var namespace: String? = null,

/**
* If `true`, sends a "newSessionBegan" Signal on each app foreground or cold launch
* Defaults to true. Set to false to prevent automatically sending this signal.
Expand Down
19 changes: 14 additions & 5 deletions lib/src/test/java/com/telemetrydeck/sdk/SignalsUnitTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -87,30 +87,39 @@ class SignalsUnitTest {

@Test
fun telemetryClient_correct_service_url() {
val appID = UUID.fromString("32CB6574-6732-4238-879F-582FEBEB6536")
val client = TelemetryClient(URL("https://nom.telemetrydeck.com"), false, null)
val client = TelemetryClient(URL("https://nom.telemetrydeck.com"), false, null, null)

val endpointUrl = client.getServiceUrl()

// date equality comparison with precision up to milliseconds
assertEquals(
"https://nom.telemetrydeck.com/v2/",
endpointUrl.toString()
)
}

@Test
fun telemetryClient_correct_service_url_with_namespace() {
val client = TelemetryClient(URL("https://nom.telemetrydeck.com"), false, "deltaquadrant", null)

val endpointUrl = client.getServiceUrl()

assertEquals(
"https://nom.telemetrydeck.com/v2/namespace/deltaquadrant/",
endpointUrl.toString()
)
}


@Test
fun signal_serialize_floatValue() {
val float: Double = 3.444444444444445
val float = 3.444444444444445

val signal = Signal(UUID.randomUUID(), "type", "clientUser", SignalPayload())
signal.floatValue = float

val signalJson = Json.encodeToString(signal)
val decodedSignal = Json.decodeFromString<Signal>(signalJson)

// date equality comparison with precision up to milliseconds
assertEquals(float, decodedSignal.floatValue)
}
}