Skip to content

Commit

Permalink
fix: fix sign util not work
Browse files Browse the repository at this point in the history
  • Loading branch information
RTAkland committed Sep 11, 2024
1 parent eb6ce24 commit df0d56b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 17 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
kotlin.code.style=official
sdkVersion=0.5.2
sdkVersion=0.5.3
5 changes: 1 addition & 4 deletions src/main/kotlin/cn/rtast/qwsdk/QWeatherSDK.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@ import com.google.gson.Gson
import java.util.logging.Logger


class QWeatherSDK(key: String, publicID: String, plan: Plans) {

class QWeatherSDK(key: String, plan: Plans) {
companion object {
lateinit var apiKey: String
lateinit var rootAPI: String
lateinit var publicIDKey: String
const val GEO_API = "https://geoapi.qweather.com/v2"
val logger: Logger = Logger.getLogger("QWSDK-MAIN")
val gson = Gson()
Expand All @@ -36,7 +34,6 @@ class QWeatherSDK(key: String, publicID: String, plan: Plans) {
init {
apiKey = key
rootAPI = plan.apiUrl
publicIDKey = publicID
logger.info("Current Plan: $plan, Current API Host: $rootAPI")
}

Expand Down
18 changes: 6 additions & 12 deletions src/main/kotlin/cn/rtast/qwsdk/utils/RequestURLUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package cn.rtast.qwsdk.utils
import cn.rtast.qwsdk.QWeatherSDK
import cn.rtast.qwsdk.enums.ApiType
import java.net.URLEncoder
import java.time.Instant


fun buildRequestURL(prefix: String, params: Map<String, Any?>, type: ApiType = ApiType.Common): String {
Expand All @@ -30,21 +29,16 @@ fun buildRequestURL(prefix: String, params: Map<String, Any?>, type: ApiType = A
}

val url: StringBuilder = StringBuilder("$rootUrl/$prefix?")

val newParamsMap = params.toMutableMap()

newParamsMap["publicid"] = QWeatherSDK.publicIDKey
newParamsMap["t"] = Instant.now().epochSecond


// newParamsMap["publicid"] = QWeatherSDK.publicIDKey
// newParamsMap["t"] = Instant.now().epochSecond
val sortedParams = newParamsMap.toSortedMap().filterValues { it != null }
.map { (k, v) -> k to (v?.toString()?.let { URLEncoder.encode(it, "UTF-8") }) }
.joinToString("&") { (k, v) -> "$k=$v" }

val sign = getSignature(sortedParams, QWeatherSDK.apiKey)


url.append(sortedParams).append("&sign=$sign")
// val sign = getSignature(sortedParams, QWeatherSDK.apiKey)
// url.append(sortedParams).append("&sign=$sign")
url.append(sortedParams).append("&key=${QWeatherSDK.apiKey}")
println(url)

return url.toString()
}

0 comments on commit df0d56b

Please sign in to comment.