-
Notifications
You must be signed in to change notification settings - Fork 2
/
DingRepository.kt
99 lines (86 loc) · 3.57 KB
/
DingRepository.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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package com.manna.monitor.report.dingtalk
import android.annotation.SuppressLint
import android.content.res.Resources
import com.google.gson.JsonObject
import com.manna.monitor.stone.BaseApplication
import com.manna.monitor.stone.http.RetrofitProvider
class DingRepository private constructor() {
companion object {
val instance = DingRepository()
}
private val userApiService by lazy {
RetrofitProvider.instance.getApiService(
"https://api.dingtalk.com/", DingService::class.java
)
}
private val reportApiService by lazy {
RetrofitProvider.instance.getApiService(
"https://oapi.dingtalk.com/", DingService::class.java, isCreate = true
)
}
/**
* report robot
*/
suspend fun report(token: String, params: MutableMap<String, Any>): JsonObject? {
return reportApiService.report(token, params)
}
/**
* async report workspace
*/
suspend fun asyncReport(token: String, title: String, content: String): JsonObject? {
val markdown = mutableMapOf("title" to title, "text" to content)
val params = mutableMapOf(
"agent_id" to getAgentId(),
"userid_list" to getUsersId(),
"msg" to mutableMapOf("msgtype" to "markdown", "markdown" to markdown)
)
return reportApiService.asyncReport(token, params)
}
/**
* ak/sk to get access token
*/
suspend fun getAccessToken(): JsonObject? {
val params = mutableMapOf<String, Any>(
"appKey" to getAppKey(), "appSecret" to getSecretKey()
)
return userApiService.getAccessToken(params)
}
/**
* token/mobile to get user id
*/
suspend fun getUseId(token: String, mobile: String): JsonObject? {
return userApiService.getUseId(token, mobile)
}
@SuppressLint("DiscouragedApi")
private fun getAppKey(): String {
val resId = BaseApplication.getInstance().resources.getIdentifier(
"DING_AK", "string", BaseApplication.getInstance().packageName
)
if (resId == 0) throw Resources.NotFoundException("Please define [DING_AK] in your root project gradle.properties file")
return BaseApplication.getInstance().resources.getString(resId)
}
@SuppressLint("DiscouragedApi")
private fun getSecretKey(): String {
val resId = BaseApplication.getInstance().resources.getIdentifier(
"DING_SK", "string", BaseApplication.getInstance().packageName
)
if (resId == 0) throw Resources.NotFoundException("Please define [DING_SK] in your root project gradle.properties file")
return BaseApplication.getInstance().resources.getString(resId)
}
@SuppressLint("DiscouragedApi")
private fun getAgentId(): String {
val resId = BaseApplication.getInstance().resources.getIdentifier(
"DING_AGENT", "string", BaseApplication.getInstance().packageName
)
if (resId == 0) throw Resources.NotFoundException("Please define [DING_AGENT] in your root project gradle.properties file")
return BaseApplication.getInstance().resources.getString(resId)
}
@SuppressLint("DiscouragedApi")
private fun getUsersId(): String {
val resId = BaseApplication.getInstance().resources.getIdentifier(
"DING_USERS", "string", BaseApplication.getInstance().packageName
)
if (resId == 0) throw Resources.NotFoundException("Please define [DING_USERS] in your root project gradle.properties file")
return BaseApplication.getInstance().resources.getString(resId)
}
}