/
TwitchHelixBuilder.java
187 lines (161 loc) · 6.44 KB
/
TwitchHelixBuilder.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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package com.github.twitch4j.helix;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.philippheuer.credentialmanager.domain.OAuth2Credential;
import com.github.twitch4j.common.config.ProxyConfig;
import com.github.twitch4j.common.config.Twitch4JGlobal;
import com.github.twitch4j.common.util.ThreadUtils;
import com.github.twitch4j.common.util.TypeConvert;
import com.github.twitch4j.helix.domain.CustomReward;
import com.github.twitch4j.helix.interceptor.CustomRewardEncodeMixIn;
import com.github.twitch4j.helix.interceptor.TwitchHelixTokenManager;
import com.github.twitch4j.helix.interceptor.TwitchHelixClientIdInterceptor;
import com.github.twitch4j.helix.interceptor.TwitchHelixDecoder;
import com.github.twitch4j.helix.interceptor.TwitchHelixHttpClient;
import com.github.twitch4j.helix.interceptor.TwitchHelixRateLimitTracker;
import com.netflix.config.ConfigurationManager;
import feign.Logger;
import feign.Request;
import feign.Retryer;
import feign.hystrix.HystrixFeign;
import feign.jackson.JacksonDecoder;
import feign.jackson.JacksonEncoder;
import feign.okhttp.OkHttpClient;
import feign.slf4j.Slf4jLogger;
import io.github.bucket4j.Bandwidth;
import io.github.bucket4j.Refill;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import java.time.Duration;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* Twitch API - Helix
*/
@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
public class TwitchHelixBuilder {
/**
* The official base URL used by production Twitch Helix API servers.
*/
public static final String OFFICIAL_BASE_URL = "https://api.twitch.tv/helix";
/**
* The default URL used by the Mock API server generated by the Twitch CLI.
*
* @see <a href="https://github.com/twitchdev/twitch-cli/blob/main/docs/mock-api.md">Mock API Docs</a>
*/
public static final String MOCK_BASE_URL = "http://localhost:8080/mock";
/**
* @see <a href="https://dev.twitch.tv/docs/api/guide#rate-limits">Helix Rate Limit Reference</a>
*/
public static final Bandwidth DEFAULT_BANDWIDTH = Bandwidth.classic(800, Refill.greedy(600, Duration.ofMinutes(1)));
/**
* Client Id
*/
@With
private String clientId = Twitch4JGlobal.clientId;
/**
* Client Secret
*/
@With
private String clientSecret = Twitch4JGlobal.clientSecret;
/**
* User Agent
*/
@With
private String userAgent = Twitch4JGlobal.userAgent;
/**
* Default Auth Token for API Requests
*/
@With
private OAuth2Credential defaultAuthToken = null;
/**
* HTTP Request Queue Size
*/
@With
private Integer requestQueueSize = -1;
/**
* BaseUrl
*/
@With
private String baseUrl = OFFICIAL_BASE_URL;
/**
* Default Timeout
*/
@With
private Integer timeout = 5000;
/**
* you can overwrite the feign loglevel to print the full requests + responses if needed
*/
@With
private Logger.Level logLevel = Logger.Level.NONE;
/**
* Proxy Configuration
*/
@With
private ProxyConfig proxyConfig = null;
/**
* Scheduler Thread Pool Executor
*/
@With
private ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = null;
/**
* Custom Rate Limit to use for Helix calls
*/
@With
private Bandwidth apiRateLimit = DEFAULT_BANDWIDTH;
/**
* Initialize the builder
*
* @return Twitch Helix Builder
*/
public static TwitchHelixBuilder builder() {
return new TwitchHelixBuilder();
}
/**
* Twitch API Client (Helix)
*
* @return TwitchHelix
*/
public TwitchHelix build() {
log.debug("Helix: Initializing Module ...");
// Hystrix
ConfigurationManager.getConfigInstance().setProperty("hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds", timeout);
ConfigurationManager.getConfigInstance().setProperty("hystrix.command.default.requestCache.enabled", false);
ConfigurationManager.getConfigInstance().setProperty("hystrix.threadpool.default.maxQueueSize", getRequestQueueSize());
ConfigurationManager.getConfigInstance().setProperty("hystrix.threadpool.default.queueSizeRejectionThreshold", getRequestQueueSize());
// Warning
if (logLevel == Logger.Level.HEADERS || logLevel == Logger.Level.FULL) {
log.warn("Helix: The current feign loglevel will print sensitive information including your access token, please don't share this log!");
}
// Jackson ObjectMapper
ObjectMapper mapper = TypeConvert.getObjectMapper();
ObjectMapper serializer = mapper.copy().addMixIn(CustomReward.class, CustomRewardEncodeMixIn.class);
// Create HttpClient with proxy
okhttp3.OkHttpClient.Builder clientBuilder = new okhttp3.OkHttpClient.Builder();
if (proxyConfig != null)
proxyConfig.apply(clientBuilder);
// Executor for rate limiting
if (scheduledThreadPoolExecutor == null)
scheduledThreadPoolExecutor = ThreadUtils.getDefaultScheduledThreadPoolExecutor("twitch4j-" + RandomStringUtils.random(4, true, true), 1);
// Enforce non-null rate limit bandwidth
if (apiRateLimit == null)
apiRateLimit = DEFAULT_BANDWIDTH;
// Feign
TwitchHelixTokenManager tokenManager = new TwitchHelixTokenManager(clientId, clientSecret, defaultAuthToken);
TwitchHelixRateLimitTracker rateLimitTracker = new TwitchHelixRateLimitTracker(apiRateLimit, tokenManager);
return HystrixFeign.builder()
.client(new TwitchHelixHttpClient(new OkHttpClient(clientBuilder.build()), scheduledThreadPoolExecutor, tokenManager, rateLimitTracker, timeout))
.encoder(new JacksonEncoder(serializer))
.decoder(new TwitchHelixDecoder(mapper, rateLimitTracker))
.logger(new Slf4jLogger())
.logLevel(logLevel)
.errorDecoder(new TwitchHelixErrorDecoder(new JacksonDecoder(), rateLimitTracker))
.requestInterceptor(new TwitchHelixClientIdInterceptor(userAgent, tokenManager))
.options(new Request.Options(timeout / 3, TimeUnit.MILLISECONDS, timeout, TimeUnit.MILLISECONDS, true))
.retryer(new Retryer.Default(500, timeout, 2))
.target(TwitchHelix.class, baseUrl);
}
}