Skip to content

Commit

Permalink
Update RywData usage so rywToken is non-null
Browse files Browse the repository at this point in the history
Motivation: rywData shouldn't be considered a valid rywData object if rywToken is undefined.

We update usage across the board to account for this type change.
  • Loading branch information
Rodrigo Gomez Palacio committed Oct 30, 2024
1 parent 592cca2 commit f6e4a90
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ interface ISubscriptionBackendService {
aliasLabel: String,
aliasValue: String,
subscription: SubscriptionObject,
): Pair<String, RywData>?
): Pair<String, RywData?>?

/**
* Update an existing subscription with the properties provided.
Expand All @@ -35,7 +35,7 @@ interface ISubscriptionBackendService {
appId: String,
subscriptionId: String,
subscription: SubscriptionObject,
): RywData
): RywData?

/**
* Delete an existing subscription.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ interface IUserBackendService {
properties: PropertiesObject,
refreshDeviceMetadata: Boolean,
propertyiesDelta: PropertiesDeltasObject,
): RywData
): RywData?

/**
* Retrieve a user from the backend.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal class SubscriptionBackendService(
aliasLabel: String,
aliasValue: String,
subscription: SubscriptionObject,
): Pair<String, RywData>? {
): Pair<String, RywData?>? {
val jsonSubscription = JSONConverter.convertToJSON(subscription)
jsonSubscription.remove("id")
val requestJSON = JSONObject().put("subscription", jsonSubscription)
Expand All @@ -28,23 +28,23 @@ internal class SubscriptionBackendService(
throw BackendException(response.statusCode, response.payload, response.retryAfterSeconds)
}

val responseJSON = JSONObject(response.payload!!)
val subscriptionJSON = responseJSON.safeJSONObject("subscription")
val responseJSON = response.payload?.let { JSONObject(it) }
val subscriptionJSON = responseJSON?.safeJSONObject("subscription")

if (subscriptionJSON == null || !subscriptionJSON.has("id")) {
return null
}

var rywToken: String? = null
if (responseJSON.has("ryw_token")) {
rywToken = responseJSON.getString("ryw_token")
}
fun JSONObject.safeString(key: String): String? = if (this.has(key)) this.getString(key) else null

var rywDelay: Long? = null
if (responseJSON.has("ryw_delay")) {
rywDelay = responseJSON.getLong("ryw_delay")
}
fun JSONObject.safeLong(key: String): Long? = if (this.has(key)) this.getLong(key) else null
val rywToken = responseJSON.safeString("ryw_token")
val rywDelay = responseJSON.safeLong("ryw_delay")
var rywData: RywData? = null

var rywData = RywData(rywToken, rywDelay)
if (rywToken != null) {
rywData = RywData(rywToken, rywDelay)
}

return Pair(subscriptionJSON.getString("id"), rywData)
}
Expand All @@ -53,7 +53,7 @@ internal class SubscriptionBackendService(
appId: String,
subscriptionId: String,
subscription: SubscriptionObject,
): RywData {
): RywData? {
val requestJSON =
JSONObject()
.put("subscription", JSONConverter.convertToJSON(subscription))
Expand All @@ -64,18 +64,20 @@ internal class SubscriptionBackendService(
throw BackendException(response.statusCode, response.payload, response.retryAfterSeconds)
}

val responseJSON = JSONObject(response.payload)
var rywToken: String? = null
if (responseJSON.has("ryw_token")) {
rywToken = responseJSON.getString("ryw_token")
}
fun JSONObject.safeString(key: String): String? = if (this.has(key)) this.getString(key) else null

var rywDelay: Long? = null
if (responseJSON.has("ryw_delay")) {
rywDelay = responseJSON.getLong("ryw_delay")
}
fun JSONObject.safeLong(key: String): Long? = if (this.has(key)) this.getLong(key) else null

val responseJSON = response.payload?.let { JSONObject(it) }

return RywData(rywToken, rywDelay)
val rywToken = responseJSON?.safeString("ryw_token")
val rywDelay = responseJSON?.safeLong("ryw_delay")

return if (rywToken !== null) {
RywData(rywToken, rywDelay)
} else {
null
}
}

override suspend fun deleteSubscription(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ internal class UserBackendService(
properties: PropertiesObject,
refreshDeviceMetadata: Boolean,
propertyiesDelta: PropertiesDeltasObject,
): RywData {
): RywData? {
val jsonObject =
JSONObject()
.put("refresh_device_metadata", refreshDeviceMetadata)
Expand All @@ -72,18 +72,20 @@ internal class UserBackendService(
throw BackendException(response.statusCode, response.payload, response.retryAfterSeconds)
}

val responseJSON = JSONObject(response.payload)
var rywToken: String? = null
if (responseJSON.has("ryw_token")) {
rywToken = responseJSON.getString("ryw_token")
}
fun JSONObject.safeString(key: String): String? = if (this.has(key)) this.getString(key) else null

var rywDelay: Long? = null
if (responseJSON.has("ryw_delay")) {
rywDelay = responseJSON.getLong("ryw_delay")
}
fun JSONObject.safeLong(key: String): Long? = if (this.has(key)) this.getLong(key) else null

val responseJSON = response.payload?.let { JSONObject(it) }

return RywData(rywToken, rywDelay)
val rywToken = responseJSON?.safeString("ryw_token")
val rywDelay = responseJSON?.safeLong("ryw_delay")

return if (rywToken != null) {
RywData(rywToken, rywDelay)
} else {
null
}
}

override suspend fun getUser(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ internal class SubscriptionOperationExecutor(
val backendSubscriptionId = result.first
val rywData = result.second

if (rywData.rywToken != null) {
if (rywData?.rywToken != null) {
_consistencyManager.setRywData(createOperation.onesignalId, IamFetchRywTokenKey.SUBSCRIPTION, rywData)
} else {
_consistencyManager.resolveConditionsWithID(IamFetchReadyCondition.ID)
Expand Down Expand Up @@ -190,7 +190,7 @@ internal class SubscriptionOperationExecutor(

val rywData = _subscriptionBackend.updateSubscription(lastOperation.appId, lastOperation.subscriptionId, subscription)

if (rywData.rywToken != null) {
if (rywData?.rywToken != null) {
_consistencyManager.setRywData(startingOperation.onesignalId, IamFetchRywTokenKey.SUBSCRIPTION, rywData)
} else {
_consistencyManager.resolveConditionsWithID(IamFetchReadyCondition.ID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ internal class UpdateUserOperationExecutor(
deltasObject,
)

if (rywData.rywToken != null) {
if (rywData?.rywToken != null) {
_consistencyManager.setRywData(onesignalId, IamFetchRywTokenKey.USER, rywData)
} else {
_consistencyManager.resolveConditionsWithID(IamFetchReadyCondition.ID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class SubscriptionBackendServiceTests : FunSpec({
val aliasLabel = "onesignal_id"
val aliasValue = "11111111-1111-1111-1111-111111111111"
val spyHttpClient = mockk<IHttpClient>()
coEvery { spyHttpClient.post(any(), any()) } returns HttpResponse(202, "{ \"subscription\": { id: \"subscriptionId\" } }")
coEvery { spyHttpClient.post(any(), any()) } returns HttpResponse(202, "{ \"subscription\": { id: \"subscriptionId\" }, \"ryw_token\": \"123\"}")
val subscriptionBackendService = SubscriptionBackendService(spyHttpClient)

// When
Expand All @@ -43,7 +43,7 @@ class SubscriptionBackendServiceTests : FunSpec({
val response = subscriptionBackendService.createSubscription("appId", aliasLabel, aliasValue, subscription)

// Then
response shouldBe Pair("subscriptionId", RywData(null, null))
response shouldBe Pair("subscriptionId", RywData("123", null))
coVerify {
spyHttpClient.post(
"apps/appId/users/by/$aliasLabel/$aliasValue/subscriptions",
Expand Down

0 comments on commit f6e4a90

Please sign in to comment.