/
TwitchExtensionsBuilder.java
159 lines (139 loc) · 5.79 KB
/
TwitchExtensionsBuilder.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
package com.github.twitch4j.extensions;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.twitch4j.common.config.ProxyConfig;
import com.github.twitch4j.common.config.Twitch4JGlobal;
import com.github.twitch4j.common.util.TypeConvert;
import com.github.twitch4j.extensions.compat.TwitchExtensionsCompatibilityLayer;
import com.github.twitch4j.extensions.util.TwitchExtensionsClientIdInterceptor;
import com.github.twitch4j.extensions.util.TwitchExtensionsErrorDecoder;
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 lombok.*;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.TimeUnit;
/**
* Twitch API - Extensions
*
* @see <a href="https://discuss.dev.twitch.tv/t/how-extensions-are-affected-by-the-legacy-twitch-api-v5-shutdown/32708">Twitch Shutdown Announcement</a>
* @deprecated the Extensions API traditionally uses the decommissioned Kraken API. While the module now forwards calls to Helix, please migrate to using Helix directly as this module will be removed in the future.
*/
@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
@Deprecated
public class TwitchExtensionsBuilder {
/**
* Base Url
*/
@With
private String baseUrl = "https://api.twitch.tv/extensions";
/**
* 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 Timeout
*/
@With
private long timeout = 5000;
/**
* HTTP Request Queue Size
*/
@With
private int requestQueueSize = -1;
/**
* 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;
/**
* Whether the compatibility layer should be used to forward requests to the new Helix API
*/
@With
private boolean helixForwarding = true;
/**
* Twitch API Client (Extensions)
*
* @return TwitchExtensions
* @see <a href="https://discuss.dev.twitch.tv/t/how-extensions-are-affected-by-the-legacy-twitch-api-v5-shutdown/32708">Twitch Shutdown Announcement</a>
* @deprecated the Extensions API traditionally uses the decommissioned Kraken API. While the module now forwards calls to Helix, please migrate to using Helix directly as this module will be removed in the future.
*/
@Deprecated
public TwitchExtensions build() {
log.debug("Extensions: Initializing Module ...");
// Helix Compatibility Layer
if (helixForwarding) {
return TwitchExtensionsCompatibilityLayer.builder()
.clientId(clientId)
.clientSecret(clientSecret)
.userAgent(userAgent)
.timeout(timeout)
.requestQueueSize(requestQueueSize)
.logLevel(logLevel)
.proxyConfig(proxyConfig)
.build();
}
// 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("Extensions: The current feign loglevel will print sensitive information including your access token, please don't share this log!");
}
// Jackson ObjectMapper
ObjectMapper mapper = TypeConvert.getObjectMapper();
// Create HttpClient with proxy
okhttp3.OkHttpClient.Builder clientBuilder = new okhttp3.OkHttpClient.Builder();
if (proxyConfig != null)
proxyConfig.apply(clientBuilder);
// Feign
return HystrixFeign.builder()
.client(new OkHttpClient(clientBuilder.build()))
.encoder(new JacksonEncoder(mapper))
.decoder(new JacksonDecoder(mapper))
.logger(new Slf4jLogger())
.logLevel(logLevel)
.errorDecoder(new TwitchExtensionsErrorDecoder(mapper, new JacksonDecoder()))
.requestInterceptor(new TwitchExtensionsClientIdInterceptor(this))
.options(new Request.Options(timeout / 3, TimeUnit.MILLISECONDS, timeout, TimeUnit.MILLISECONDS, true))
.retryer(new Retryer.Default(500, timeout, 2))
.target(TwitchExtensions.class, baseUrl);
}
/**
* Initialize the builder
*
* @return Twitch Extensions Builder
* @see <a href="https://discuss.dev.twitch.tv/t/how-extensions-are-affected-by-the-legacy-twitch-api-v5-shutdown/32708">Twitch Shutdown Announcement</a>
* @deprecated the Extensions API traditionally uses the decommissioned Kraken API. While the module now forwards calls to Helix, please migrate to using Helix directly as this module will be removed in the future.
*/
@Deprecated
public static TwitchExtensionsBuilder builder() {
return new TwitchExtensionsBuilder();
}
}