Skip to content

Commit 7149908

Browse files
author
Rodrigo Gomez Palacio
committed
Update RywData usage so rywToken is non-null
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.
1 parent cebf1de commit 7149908

File tree

7 files changed

+39
-43
lines changed

7 files changed

+39
-43
lines changed

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/backend/ISubscriptionBackendService.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ interface ISubscriptionBackendService {
2222
aliasLabel: String,
2323
aliasValue: String,
2424
subscription: SubscriptionObject,
25-
): Pair<String, RywData>?
25+
): Pair<String, RywData?>?
2626

2727
/**
2828
* Update an existing subscription with the properties provided.
@@ -35,7 +35,7 @@ interface ISubscriptionBackendService {
3535
appId: String,
3636
subscriptionId: String,
3737
subscription: SubscriptionObject,
38-
): RywData
38+
): RywData?
3939

4040
/**
4141
* Delete an existing subscription.

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/backend/IUserBackendService.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ interface IUserBackendService {
4848
properties: PropertiesObject,
4949
refreshDeviceMetadata: Boolean,
5050
propertyiesDelta: PropertiesDeltasObject,
51-
): RywData
51+
): RywData?
5252

5353
/**
5454
* Retrieve a user from the backend.

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/backend/impl/SubscriptionBackendService.kt

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package com.onesignal.user.internal.backend.impl
33
import com.onesignal.common.consistency.RywData
44
import com.onesignal.common.exceptions.BackendException
55
import com.onesignal.common.safeJSONObject
6+
import com.onesignal.common.safeLong
7+
import com.onesignal.common.safeString
68
import com.onesignal.common.toMap
79
import com.onesignal.core.internal.http.IHttpClient
810
import com.onesignal.user.internal.backend.ISubscriptionBackendService
@@ -17,7 +19,7 @@ internal class SubscriptionBackendService(
1719
aliasLabel: String,
1820
aliasValue: String,
1921
subscription: SubscriptionObject,
20-
): Pair<String, RywData>? {
22+
): Pair<String, RywData?>? {
2123
val jsonSubscription = JSONConverter.convertToJSON(subscription)
2224
jsonSubscription.remove("id")
2325
val requestJSON = JSONObject().put("subscription", jsonSubscription)
@@ -28,32 +30,28 @@ internal class SubscriptionBackendService(
2830
throw BackendException(response.statusCode, response.payload, response.retryAfterSeconds)
2931
}
3032

31-
val responseJSON = JSONObject(response.payload!!)
32-
val subscriptionJSON = responseJSON.safeJSONObject("subscription")
33+
val responseJSON = response.payload?.let { JSONObject(it) }
34+
val subscriptionJSON = responseJSON?.safeJSONObject("subscription")
35+
3336
if (subscriptionJSON == null || !subscriptionJSON.has("id")) {
3437
return null
3538
}
39+
val rywToken = responseJSON.safeString("ryw_token")
40+
val rywDelay = responseJSON.safeLong("ryw_delay")
41+
var rywData: RywData? = null
3642

37-
var rywToken: String? = null
38-
if (responseJSON.has("ryw_token")) {
39-
rywToken = responseJSON.getString("ryw_token")
40-
}
41-
42-
var rywDelay: Long? = null
43-
if (responseJSON.has("ryw_delay")) {
44-
rywDelay = responseJSON.getLong("ryw_delay")
43+
if (rywToken != null) {
44+
rywData = RywData(rywToken, rywDelay)
4545
}
4646

47-
var rywData = RywData(rywToken, rywDelay)
48-
4947
return Pair(subscriptionJSON.getString("id"), rywData)
5048
}
5149

5250
override suspend fun updateSubscription(
5351
appId: String,
5452
subscriptionId: String,
5553
subscription: SubscriptionObject,
56-
): RywData {
54+
): RywData? {
5755
val requestJSON =
5856
JSONObject()
5957
.put("subscription", JSONConverter.convertToJSON(subscription))
@@ -64,18 +62,16 @@ internal class SubscriptionBackendService(
6462
throw BackendException(response.statusCode, response.payload, response.retryAfterSeconds)
6563
}
6664

67-
val responseJSON = JSONObject(response.payload)
68-
var rywToken: String? = null
69-
if (responseJSON.has("ryw_token")) {
70-
rywToken = responseJSON.getString("ryw_token")
71-
}
65+
val responseJSON = response.payload?.let { JSONObject(it) }
7266

73-
var rywDelay: Long? = null
74-
if (responseJSON.has("ryw_delay")) {
75-
rywDelay = responseJSON.getLong("ryw_delay")
76-
}
67+
val rywToken = responseJSON?.safeString("ryw_token")
68+
val rywDelay = responseJSON?.safeLong("ryw_delay")
7769

78-
return RywData(rywToken, rywDelay)
70+
return if (rywToken !== null) {
71+
RywData(rywToken, rywDelay)
72+
} else {
73+
null
74+
}
7975
}
8076

8177
override suspend fun deleteSubscription(

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/backend/impl/UserBackendService.kt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package com.onesignal.user.internal.backend.impl
33
import com.onesignal.common.consistency.RywData
44
import com.onesignal.common.exceptions.BackendException
55
import com.onesignal.common.putMap
6+
import com.onesignal.common.safeLong
7+
import com.onesignal.common.safeString
68
import com.onesignal.core.internal.http.IHttpClient
79
import com.onesignal.user.internal.backend.CreateUserResponse
810
import com.onesignal.user.internal.backend.IUserBackendService
@@ -53,7 +55,7 @@ internal class UserBackendService(
5355
properties: PropertiesObject,
5456
refreshDeviceMetadata: Boolean,
5557
propertyiesDelta: PropertiesDeltasObject,
56-
): RywData {
58+
): RywData? {
5759
val jsonObject =
5860
JSONObject()
5961
.put("refresh_device_metadata", refreshDeviceMetadata)
@@ -72,18 +74,16 @@ internal class UserBackendService(
7274
throw BackendException(response.statusCode, response.payload, response.retryAfterSeconds)
7375
}
7476

75-
val responseJSON = JSONObject(response.payload)
76-
var rywToken: String? = null
77-
if (responseJSON.has("ryw_token")) {
78-
rywToken = responseJSON.getString("ryw_token")
79-
}
77+
val responseJSON = response.payload?.let { JSONObject(it) }
8078

81-
var rywDelay: Long? = null
82-
if (responseJSON.has("ryw_delay")) {
83-
rywDelay = responseJSON.getLong("ryw_delay")
84-
}
79+
val rywToken = responseJSON?.safeString("ryw_token")
80+
val rywDelay = responseJSON?.safeLong("ryw_delay")
8581

86-
return RywData(rywToken, rywDelay)
82+
return if (rywToken != null) {
83+
RywData(rywToken, rywDelay)
84+
} else {
85+
null
86+
}
8787
}
8888

8989
override suspend fun getUser(

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/operations/impl/executors/SubscriptionOperationExecutor.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ internal class SubscriptionOperationExecutor(
116116
val backendSubscriptionId = result.first
117117
val rywData = result.second
118118

119-
if (rywData.rywToken != null) {
119+
if (rywData != null) {
120120
_consistencyManager.setRywData(createOperation.onesignalId, IamFetchRywTokenKey.SUBSCRIPTION, rywData)
121121
} else {
122122
_consistencyManager.resolveConditionsWithID(IamFetchReadyCondition.ID)
@@ -190,7 +190,7 @@ internal class SubscriptionOperationExecutor(
190190

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

193-
if (rywData.rywToken != null) {
193+
if (rywData != null) {
194194
_consistencyManager.setRywData(startingOperation.onesignalId, IamFetchRywTokenKey.SUBSCRIPTION, rywData)
195195
} else {
196196
_consistencyManager.resolveConditionsWithID(IamFetchReadyCondition.ID)

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/operations/impl/executors/UpdateUserOperationExecutor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ internal class UpdateUserOperationExecutor(
148148
deltasObject,
149149
)
150150

151-
if (rywData.rywToken != null) {
151+
if (rywData != null) {
152152
_consistencyManager.setRywData(onesignalId, IamFetchRywTokenKey.USER, rywData)
153153
} else {
154154
_consistencyManager.resolveConditionsWithID(IamFetchReadyCondition.ID)

OneSignalSDK/onesignal/core/src/test/java/com/onesignal/user/internal/backend/SubscriptionBackendServiceTests.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class SubscriptionBackendServiceTests : FunSpec({
2727
val aliasLabel = "onesignal_id"
2828
val aliasValue = "11111111-1111-1111-1111-111111111111"
2929
val spyHttpClient = mockk<IHttpClient>()
30-
coEvery { spyHttpClient.post(any(), any()) } returns HttpResponse(202, "{ \"subscription\": { id: \"subscriptionId\" } }")
30+
coEvery { spyHttpClient.post(any(), any()) } returns HttpResponse(202, "{ \"subscription\": { id: \"subscriptionId\" }, \"ryw_token\": \"123\"}")
3131
val subscriptionBackendService = SubscriptionBackendService(spyHttpClient)
3232

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

4545
// Then
46-
response shouldBe Pair("subscriptionId", RywData(null, null))
46+
response shouldBe Pair("subscriptionId", RywData("123", null))
4747
coVerify {
4848
spyHttpClient.post(
4949
"apps/appId/users/by/$aliasLabel/$aliasValue/subscriptions",

0 commit comments

Comments
 (0)