-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathStart0.java
62 lines (51 loc) · 2.04 KB
/
Start0.java
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
package io.github.kloping.qqbot;
import io.github.kloping.judge.Judge;
import io.github.kloping.qqbot.entities.qqpd.Channel;
import io.github.kloping.qqbot.http.AuthV2Base;
import io.github.kloping.qqbot.http.data.Token;
import io.github.kloping.spt.annotations.AutoStand;
import io.github.kloping.spt.annotations.ComponentScan;
import io.github.kloping.spt.interfaces.component.ContextManager;
import java.util.HashMap;
import java.util.Map;
/**
* @author github.kloping
*/
@ComponentScan(value = "io.github.kloping.qqbot")
public class Start0 {
public Map<String, String> headers = new HashMap();
public Map<String, String> v2headers = new HashMap();
@AutoStand
ContextManager contextManager;
public Map<String, String> getHeaders() {
if (headers.isEmpty()) {
headers.put("Authorization", contextManager.getContextEntity(String.class, Starter.AUTH_ID));
headers.put("Accept-Encoding", "application/json");
}
return headers;
}
public Map<String, String> getV2Headers() {
if (isExpired(token)) v2headers.clear();
if (v2headers.isEmpty()) {
String secret = contextManager.getContextEntity(String.class, Starter.SECRET_ID);
if (Judge.isEmpty(secret)) return v2headers;
v2headers.put("Authorization", String.format("QQBot %s", getV2Token(secret)));
v2headers.put("X-Union-Appid", contextManager.getContextEntity(String.class, Starter.APPID_ID));
}
return v2headers;
}
private boolean isExpired(Token token) {
return token == null || token.isExpired();
}
@AutoStand
AuthV2Base authV2Base;
private Token token;
private String getV2Token(String secret) {
token = authV2Base.auth(
String.format("{\"appId\": \"%s\",\"clientSecret\": \"%s\"}\n", contextManager.getContextEntity(String.class, Starter.APPID_ID), secret)
, Channel.SEND_MESSAGE_HEADERS
);
token.setT0(System.currentTimeMillis());
return token.getAccess_token();
}
}