-
-
Notifications
You must be signed in to change notification settings - Fork 468
Expand file tree
/
Copy pathUpdateResponseParser.kt
More file actions
82 lines (70 loc) · 2.85 KB
/
UpdateResponseParser.kt
File metadata and controls
82 lines (70 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package io.sentry.android.distribution
import io.sentry.SentryLevel
import io.sentry.SentryOptions
import io.sentry.UpdateInfo
import io.sentry.UpdateStatus
import org.json.JSONException
import org.json.JSONObject
/** Parser for distribution API responses. */
internal class UpdateResponseParser(private val options: SentryOptions) {
/**
* Parses the API response and returns the appropriate UpdateStatus.
*
* @param statusCode HTTP status code
* @param responseBody Response body as string
* @return UpdateStatus indicating the result
*/
fun parseResponse(statusCode: Int, responseBody: String): UpdateStatus {
return when (statusCode) {
200 -> parseSuccessResponse(responseBody)
in 400..499 -> UpdateStatus.UpdateError("Client error: $statusCode")
in 500..599 -> UpdateStatus.UpdateError("Server error: $statusCode")
else -> UpdateStatus.UpdateError("Unexpected response code: $statusCode")
}
}
private fun parseSuccessResponse(responseBody: String): UpdateStatus {
return try {
val json = JSONObject(responseBody)
options.logger.log(SentryLevel.DEBUG, "Parsing distribution API response")
// Check if there's a new release available
val updateAvailable = json.optBoolean("updateAvailable", false)
if (updateAvailable) {
val updateInfo = parseUpdateInfo(json)
UpdateStatus.NewRelease(updateInfo)
} else {
UpdateStatus.UpToDate.getInstance()
}
} catch (e: JSONException) {
options.logger.log(SentryLevel.ERROR, e, "Failed to parse API response")
UpdateStatus.UpdateError("Invalid response format: ${e.message}")
} catch (e: Exception) {
options.logger.log(SentryLevel.ERROR, e, "Unexpected error parsing response")
UpdateStatus.UpdateError("Failed to parse response: ${e.message}")
}
}
private fun parseUpdateInfo(json: JSONObject): UpdateInfo {
val id = json.optString("id", "")
val buildVersion = json.optString("buildVersion", "")
val buildNumber = json.optInt("buildNumber", 0)
val downloadUrl = json.optString("downloadUrl", "")
val appName = json.optString("appName", "")
val createdDate = json.optString("createdDate", "")
// Validate required fields (optString returns "null" for null values)
val missingFields = mutableListOf<String>()
if (id.isEmpty() || id == "null") {
missingFields.add("id")
}
if (buildVersion.isEmpty() || buildVersion == "null") {
missingFields.add("buildVersion")
}
if (downloadUrl.isEmpty() || downloadUrl == "null") {
missingFields.add("downloadUrl")
}
if (missingFields.isNotEmpty()) {
throw IllegalArgumentException(
"Missing required fields in API response: ${missingFields.joinToString(", ")}"
)
}
return UpdateInfo(id, buildVersion, buildNumber, downloadUrl, appName, createdDate)
}
}