-
Notifications
You must be signed in to change notification settings - Fork 2
/
DingProvider.kt
48 lines (42 loc) · 1.84 KB
/
DingProvider.kt
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
package com.manna.monitor.report.dingtalk
import android.util.Log
import com.manna.monitor.stone.data.getLongValue
import com.manna.monitor.stone.data.getStringValue
import com.manna.monitor.stone.data.setLongValue
import com.manna.monitor.stone.data.setStringValue
import kotlinx.coroutines.flow.first
class DingProvider {
companion object {
val instance = DingProvider()
}
/**
* report work space
*/
suspend fun reportWorkSpace(title: String, content: String, success: (Boolean) -> Unit) {
kotlin.runCatching {
var token = getStringValue("ding-token").first()
val expiredTime = getLongValue("ding-expired").first()
if (token.isEmpty() || System.currentTimeMillis() > expiredTime) {
Log.d("Monitor", "Report token is empty or expired")
val data = DingRepository.instance.getAccessToken()
Log.d("Monitor", "Get access token")
token = data?.get("accessToken")?.asString ?: ""
val expired = data?.get("expireIn")?.asLong ?: 0L
Log.d("Monitor", "Cache access token and expired time")
setStringValue("ding-token", token)
setLongValue("ding-expired", expired)
}
Log.d("Monitor", "Starting async report work space")
val jsonData = DingRepository.instance.asyncReport(token, title, content)
Log.d("Monitor", "Finish async report work space, jsonData : $jsonData")
val errorCode = jsonData?.get("errcode")?.asInt ?: -1
success(errorCode == 0)
}
}
suspend fun reportDataServer(title: String, content: String, success: (Boolean) -> Unit) {
//do something
}
suspend fun reportDataOther(title: String, content: String, success: (Boolean) -> Unit) {
//do something
}
}