Skip to content

Commit

Permalink
优化拉取远程配置的判断
Browse files Browse the repository at this point in the history
  • Loading branch information
teble committed Dec 22, 2023
1 parent e25613a commit d9e01d0
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions app/src/main/java/me/teble/xposed/autodaily/utils/RepoUtil.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package me.teble.xposed.autodaily.utils

import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonObject
import okhttp3.Request

enum class FileEnum(val path: String) {
Expand All @@ -24,14 +27,23 @@ object RepoFileLoader {

private var repoUrl = originRepoUrl

private fun loadRepoFile(path: String): String? {
private fun loadRepoFile(path: String, verifyJson: Boolean): String? {
LogUtil.d("curr repo url: $repoUrl")
val url = "$repoUrl/$path"
val req = Request.Builder().url(url).build()
try {
val response = client.newCall(req).execute()
if (response.isSuccessful) {
return response.body!!.string()
val content = response.body!!.string()
if (verifyJson) {
val cls = content.parse<JsonElement>()::class.java
LogUtil.d("verify json content: $cls")
if (cls != JsonObject::class.java
&& cls != JsonArray::class.java) {
throw IllegalArgumentException("invalid json: $content")
}
}
return content
}
LogUtil.w("load repo file failed: $url, code: ${response.code}, response: ${response.body}")
return null
Expand All @@ -40,15 +52,15 @@ object RepoFileLoader {
val index = repoUrls.indexOf(repoUrl)
if (index < repoUrls.size - 1) {
repoUrl = repoUrls[index + 1]
return loadRepoFile(path)
return loadRepoFile(path, verifyJson)
}
throw e
}
}

fun load(fileEnum: FileEnum): String? {
return runCatching {
loadRepoFile(fileEnum.path)
loadRepoFile(fileEnum.path, fileEnum.path.endsWith(".json"))
}.getOrNull()
}
}

0 comments on commit d9e01d0

Please sign in to comment.