12
12
import okhttp3 .Interceptor ;
13
13
import okhttp3 .OkHttpClient ;
14
14
import okhttp3 .logging .HttpLoggingInterceptor ;
15
+ import org .jetbrains .annotations .NotNull ;
15
16
16
17
import java .io .IOException ;
17
18
import java .io .InputStream ;
18
19
import java .net .URL ;
19
20
import java .net .URLConnection ;
20
21
import java .util .concurrent .TimeUnit ;
21
22
23
+ import static com .pengrad .telegrambot .RequestPreprocessorKt .getEmptyRequestPreprocessor ;
24
+
22
25
/**
23
26
* Stas Parshin
24
27
* 16 October 2015
25
28
*/
26
- public class TelegramBot {
29
+ public class TelegramBot implements TelegramAware {
27
30
28
31
private final String token ;
29
32
private final TelegramBotClient api ;
30
33
private final FileApi fileApi ;
31
34
private final UpdatesHandler updatesHandler ;
35
+ private final RequestPreprocessor requestPreprocessor ;
32
36
33
37
public TelegramBot (String botToken ) {
34
38
this (new Builder (botToken ));
@@ -39,13 +43,17 @@ public TelegramBot(String botToken) {
39
43
this .api = builder .api ;
40
44
this .fileApi = builder .fileApi ;
41
45
this .updatesHandler = builder .updatesHandler ;
46
+ this .requestPreprocessor = builder .requestPreprocessor ;
42
47
}
43
48
44
- public <T extends BaseRequest <T , R >, R extends BaseResponse > R execute (BaseRequest <T , R > request ) {
49
+ @ NotNull
50
+ public <T extends BaseRequest <T , R >, R extends BaseResponse > R execute (@ NotNull BaseRequest <T , R > request ) {
51
+ requestPreprocessor .process (request );
45
52
return api .send (request );
46
53
}
47
54
48
55
public <T extends BaseRequest <T , R >, R extends BaseResponse > Cancellable execute (T request , Callback <T , R > callback ) {
56
+ requestPreprocessor .process (request );
49
57
return api .send (request , callback );
50
58
}
51
59
@@ -98,6 +106,7 @@ public static final class Builder {
98
106
private FileApi fileApi ;
99
107
private TelegramBotClient api ;
100
108
private UpdatesHandler updatesHandler ;
109
+ private RequestPreprocessor requestPreprocessor ;
101
110
102
111
private OkHttpClient okHttpClient ;
103
112
private String apiUrl ;
@@ -109,6 +118,7 @@ public Builder(String botToken) {
109
118
api = new TelegramBotClient (client (null ), gson (), apiUrl (API_URL , botToken , useTestServer ));
110
119
fileApi = new FileApi (botToken );
111
120
updatesHandler = new UpdatesHandler (100 );
121
+ requestPreprocessor = getEmptyRequestPreprocessor ();
112
122
}
113
123
114
124
public Builder debug () {
@@ -141,6 +151,11 @@ public Builder useTestServer(boolean useTestServer) {
141
151
return this ;
142
152
}
143
153
154
+ public Builder requestPreprocessor (RequestPreprocessor requestPreprocessor ) {
155
+ this .requestPreprocessor = requestPreprocessor ;
156
+ return this ;
157
+ }
158
+
144
159
public TelegramBot build () {
145
160
if (okHttpClient != null || apiUrl != null ) {
146
161
OkHttpClient client = okHttpClient != null ? okHttpClient : client (null );
0 commit comments