Skip to content

Commit 0a73c2c

Browse files
authored
Merge pull request #412 from pengrad/request_extension
Add API method-specific extension functions
2 parents 935af7b + c9c33c1 commit 0a73c2c

File tree

59 files changed

+902
-555
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+902
-555
lines changed

library/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ dependencies {
1616
testImplementation 'junit:junit:4.13.1'
1717
testImplementation 'nl.jqno.equalsverifier:equalsverifier:3.1.13'
1818
testImplementation 'org.reflections:reflections:0.9.12'
19+
testImplementation 'org.jetbrains.kotlin:kotlin-reflect'
1920
}
2021

2122
jar {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.pengrad.telegrambot
2+
3+
import com.pengrad.telegrambot.request.BaseRequest
4+
import com.pengrad.telegrambot.response.BaseResponse
5+
6+
@get:JvmName("getEmptyRequestPreprocessor")
7+
val EMPTY_REQUEST_PREPROCESSOR: RequestPreprocessor = object : RequestPreprocessor {
8+
override fun <REQ : BaseRequest<REQ, RES>, RES : BaseResponse> process(request: BaseRequest<REQ, RES>) {
9+
// do nothing
10+
}
11+
}
12+
13+
interface RequestPreprocessor {
14+
15+
fun <REQ : BaseRequest<REQ, RES>, RES : BaseResponse> process(request: BaseRequest<REQ, RES>)
16+
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.pengrad.telegrambot
2+
3+
import com.pengrad.telegrambot.request.BaseRequest
4+
import com.pengrad.telegrambot.response.BaseResponse
5+
6+
interface TelegramAware {
7+
8+
fun <REQ : BaseRequest<REQ, RES>, RES : BaseResponse> execute(request: BaseRequest<REQ, RES>): RES
9+
10+
}

library/src/main/java/com/pengrad/telegrambot/TelegramBot.java

+17-2
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,27 @@
1212
import okhttp3.Interceptor;
1313
import okhttp3.OkHttpClient;
1414
import okhttp3.logging.HttpLoggingInterceptor;
15+
import org.jetbrains.annotations.NotNull;
1516

1617
import java.io.IOException;
1718
import java.io.InputStream;
1819
import java.net.URL;
1920
import java.net.URLConnection;
2021
import java.util.concurrent.TimeUnit;
2122

23+
import static com.pengrad.telegrambot.RequestPreprocessorKt.getEmptyRequestPreprocessor;
24+
2225
/**
2326
* Stas Parshin
2427
* 16 October 2015
2528
*/
26-
public class TelegramBot {
29+
public class TelegramBot implements TelegramAware {
2730

2831
private final String token;
2932
private final TelegramBotClient api;
3033
private final FileApi fileApi;
3134
private final UpdatesHandler updatesHandler;
35+
private final RequestPreprocessor requestPreprocessor;
3236

3337
public TelegramBot(String botToken) {
3438
this(new Builder(botToken));
@@ -39,13 +43,17 @@ public TelegramBot(String botToken) {
3943
this.api = builder.api;
4044
this.fileApi = builder.fileApi;
4145
this.updatesHandler = builder.updatesHandler;
46+
this.requestPreprocessor = builder.requestPreprocessor;
4247
}
4348

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);
4552
return api.send(request);
4653
}
4754

4855
public <T extends BaseRequest<T, R>, R extends BaseResponse> Cancellable execute(T request, Callback<T, R> callback) {
56+
requestPreprocessor.process(request);
4957
return api.send(request, callback);
5058
}
5159

@@ -98,6 +106,7 @@ public static final class Builder {
98106
private FileApi fileApi;
99107
private TelegramBotClient api;
100108
private UpdatesHandler updatesHandler;
109+
private RequestPreprocessor requestPreprocessor;
101110

102111
private OkHttpClient okHttpClient;
103112
private String apiUrl;
@@ -109,6 +118,7 @@ public Builder(String botToken) {
109118
api = new TelegramBotClient(client(null), gson(), apiUrl(API_URL, botToken, useTestServer));
110119
fileApi = new FileApi(botToken);
111120
updatesHandler = new UpdatesHandler(100);
121+
requestPreprocessor = getEmptyRequestPreprocessor();
112122
}
113123

114124
public Builder debug() {
@@ -141,6 +151,11 @@ public Builder useTestServer(boolean useTestServer) {
141151
return this;
142152
}
143153

154+
public Builder requestPreprocessor(RequestPreprocessor requestPreprocessor) {
155+
this.requestPreprocessor = requestPreprocessor;
156+
return this;
157+
}
158+
144159
public TelegramBot build() {
145160
if (okHttpClient != null || apiUrl != null) {
146161
OkHttpClient client = okHttpClient != null ? okHttpClient : client(null);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.pengrad.telegrambot.model
2+
3+
data class PreparedInlineMessage(
4+
@get:JvmName("id") val id: String,
5+
@get:JvmName("expirationDate") val expirationDate: Int
6+
)

library/src/main/java/com/pengrad/telegrambot/model/SuccessfulPayment.java

-92
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.pengrad.telegrambot.model
2+
3+
data class SuccessfulPayment(
4+
@get:JvmName("currency") val currency: String,
5+
@get:JvmName("totalAmount") val totalAmount: Int,
6+
@get:JvmName("invoicePayload") val invoicePayload: String,
7+
@get:JvmName("subscriptionExpirationDate") val subscriptionExpirationDate: Int? = null,
8+
@get:JvmName("isRecurring") val isRecurring: Boolean? = null,
9+
@get:JvmName("isFirstRecurring") val isFirstRecurring: Boolean? = null,
10+
@get:JvmName("shippingOptionId") val shippingOptionId: String? = null,
11+
@get:JvmName("orderInfo") val orderInfo: OrderInfo? = null,
12+
@get:JvmName("telegramPaymentChargeId") val telegramPaymentChargeId: String,
13+
@get:JvmName("providerPaymentChargeId") val providerPaymentChargeId: String
14+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.pengrad.telegrambot.model.gift
2+
3+
import com.pengrad.telegrambot.model.Sticker
4+
5+
data class Gift(
6+
@get:JvmName("id") val id: String,
7+
@get:JvmName("sticker") val sticker: Sticker,
8+
@get:JvmName("starCount") val starCount: Int,
9+
@get:JvmName("totalCount") val totalCount: Int,
10+
@get:JvmName("remainingCount") val remainingCount: Int
11+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.pengrad.telegrambot.model.gift
2+
3+
data class Gifts(
4+
@get:JvmName("gifts") val gifts: Array<Gift>
5+
) {
6+
override fun equals(other: Any?): Boolean {
7+
if (this === other) return true
8+
if (other !is Gifts) return false
9+
10+
if (!gifts.contentEquals(other.gifts)) return false
11+
12+
return true
13+
}
14+
15+
override fun hashCode(): Int {
16+
return gifts.contentHashCode()
17+
}
18+
}

library/src/main/java/com/pengrad/telegrambot/model/stars/StarTransaction.java

-70
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.pengrad.telegrambot.model.stars
2+
3+
import com.pengrad.telegrambot.model.stars.partner.TransactionPartner
4+
5+
data class StarTransaction(
6+
@get:JvmName("id") val id: String,
7+
@get:JvmName("amount") val amount: Int,
8+
@get:JvmName("nanostarAmount") val nanostarAmount: Int? = null,
9+
@get:JvmName("date") val date: Int,
10+
@get:JvmName("source") val source: TransactionPartner? = null,
11+
@get:JvmName("receiver") val receiver: TransactionPartner? = null
12+
)

library/src/main/java/com/pengrad/telegrambot/model/stars/StarTransactions.java

-38
This file was deleted.

0 commit comments

Comments
 (0)