@@ -8,6 +8,7 @@ import io.squarescreen.core.result.SquareScreenResult
88import io.squarescreen.network.api.PairingApiService
99import io.squarescreen.network.interceptor.DemoBypassInterceptor
1010import io.squarescreen.network.dto.ActivateRequestDto
11+ import io.squarescreen.network.dto.PairingErrorDto
1112import io.squarescreen.network.dto.PairStatusRequestDto
1213import io.squarescreen.network.dto.RegisterRequestDto
1314import kotlinx.serialization.json.Json
@@ -86,42 +87,46 @@ class PairingNetworkClient {
8687 /* *
8788 * Calls `POST /screen/activate` with the developer-supplied device ID and token.
8889 *
89- * An alternative to [register] for integrators who already hold a SquareScreen device ID
90- * (created in the admin dashboard) and supply their own device token (IMEI, UUID, etc.).
91- * Returns the same [PairingRegistration] as [register] on success.
90+ * Unlike [register], activate resolves immediately — the server returns the permanent
91+ * device token on 200 with no pair-status polling required.
92+ *
93+ * Returns [PairingStatus.Approved] on success, or the appropriate terminal
94+ * [PairingStatus] on failure:
95+ * - [PairingStatus.InvalidToken] — device ID is invalid or expired (401)
96+ * - [PairingStatus.AlreadyPaired] — device is already paired (409 ALREADY_PAIRED)
97+ * - [PairingStatus.IdentifierMismatch] — device ID is bound to a different token (409 IDENTIFIER_MISMATCH)
9298 */
93- suspend fun activate (deviceId : String , deviceToken : String ): SquareScreenResult < PairingRegistration > {
99+ suspend fun activate (deviceId : String , deviceToken : String ): PairingStatus {
94100 return try {
95101 val response = api.activate(ActivateRequestDto (deviceId, deviceToken))
96102 when {
97103 response.isSuccessful -> {
98104 val body = response.body()
99- ? : return SquareScreenResult .Error (
100- SquareScreenError .ParseError (" Activate response body was null" )
101- )
102- SquareScreenResult .Success (
103- PairingRegistration (
104- pairingToken = body.pairingToken,
105- expiresIn = body.expiresIn
105+ ? : return PairingStatus .Error (
106+ IllegalStateException (" Activate response body was null" )
106107 )
107- )
108+ PairingStatus . Approved (deviceId, body.deviceToken )
108109 }
109- response.code() == 404 ->
110- SquareScreenResult .Error (SquareScreenError .NetworkError (404 , " DEVICE_NOT_FOUND" ))
111- response.code() == 409 ->
112- SquareScreenResult .Error (SquareScreenError .NetworkError (409 , " ALREADY_PAIRED" ))
113- else ->
114- SquareScreenResult .Error (
115- SquareScreenError .NetworkError (response.code(), response.message())
116- )
110+ response.code() == 401 -> PairingStatus .InvalidToken
111+ response.code() == 409 -> {
112+ val errorCode = parseErrorCode(response.errorBody()?.string())
113+ when (errorCode) {
114+ " ALREADY_PAIRED" -> PairingStatus .AlreadyPaired
115+ " IDENTIFIER_MISMATCH" -> PairingStatus .IdentifierMismatch
116+ else -> PairingStatus .Error (Exception (" 409: $errorCode " ))
117+ }
118+ }
119+ else -> PairingStatus .Error (
120+ Exception (" Unexpected HTTP ${response.code()} : ${response.message()} " )
121+ )
117122 }
118123 } catch (e: Exception ) {
119- SquareScreenResult .Error (SquareScreenError . Unknown (e) )
124+ PairingStatus .Error (e )
120125 }
121126 }
122127
123128 /* *
124- * Calls `GET /screen/pair-status` using the pairing token from [register].
129+ * Calls `POST /screen/pair-status` using the pairing token from [register].
125130 *
126131 * Returns [PairingStatus.Pending], [PairingStatus.Approved], [PairingStatus.InvalidToken],
127132 * [PairingStatus.Expired], or [PairingStatus.Error] for unexpected failures.
@@ -141,15 +146,15 @@ class PairingNetworkClient {
141146 when (body.status) {
142147 " pending" -> PairingStatus .Pending
143148 " approved" -> {
144- val deviceId = body.deviceId
149+ val id = body.deviceId
145150 ? : return PairingStatus .Error (
146151 IllegalStateException (" Approved response missing device_id" )
147152 )
148- val deviceToken = body.deviceToken
153+ val token = body.deviceToken
149154 ? : return PairingStatus .Error (
150155 IllegalStateException (" Approved response missing device_token" )
151156 )
152- PairingStatus .Approved (deviceId, deviceToken )
157+ PairingStatus .Approved (id, token )
153158 }
154159 else -> PairingStatus .Error (
155160 IllegalStateException (" Unknown pair-status value: '${body.status} '" )
@@ -166,4 +171,13 @@ class PairingNetworkClient {
166171 PairingStatus .Error (e)
167172 }
168173 }
174+
175+ private fun parseErrorCode (errorBody : String? ): String? {
176+ if (errorBody == null ) return null
177+ return try {
178+ json.decodeFromString<PairingErrorDto >(errorBody).code
179+ } catch (e: Exception ) {
180+ null
181+ }
182+ }
169183}
0 commit comments