From deb2ad04f42d321b2da8ff36c3b1a9fdbdc59589 Mon Sep 17 00:00:00 2001 From: arkon Date: Tue, 1 Feb 2022 12:55:02 -0500 Subject: [PATCH] Add OkHttp rate limiting extension function stubs --- .../interceptor/RateLimitInterceptor.kt | 25 +++++++++++++++++ .../SpecificHostRateLimitInterceptor.kt | 28 +++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 library/src/main/java/eu/kanade/tachiyomi/network/interceptor/RateLimitInterceptor.kt create mode 100644 library/src/main/java/eu/kanade/tachiyomi/network/interceptor/SpecificHostRateLimitInterceptor.kt diff --git a/library/src/main/java/eu/kanade/tachiyomi/network/interceptor/RateLimitInterceptor.kt b/library/src/main/java/eu/kanade/tachiyomi/network/interceptor/RateLimitInterceptor.kt new file mode 100644 index 0000000..bbe5ada --- /dev/null +++ b/library/src/main/java/eu/kanade/tachiyomi/network/interceptor/RateLimitInterceptor.kt @@ -0,0 +1,25 @@ +package eu.kanade.tachiyomi.network.interceptor + +import okhttp3.Interceptor +import okhttp3.OkHttpClient +import java.util.concurrent.TimeUnit + +/** + * An OkHttp interceptor that handles rate limiting. + * + * Examples: + * + * permits = 5, period = 1, unit = seconds => 5 requests per second + * permits = 10, period = 2, unit = minutes => 10 requests per 2 minutes + * + * @since extension-lib 1.3 + * + * @param permits {Int} Number of requests allowed within a period of units. + * @param period {Long} The limiting duration. Defaults to 1. + * @param unit {TimeUnit} The unit of time for the period. Defaults to seconds. + */ +fun OkHttpClient.Builder.rateLimit( + permits: Int, + period: Long = 1, + unit: TimeUnit = TimeUnit.SECONDS, +): Interceptor = throw Exception("Stub!") \ No newline at end of file diff --git a/library/src/main/java/eu/kanade/tachiyomi/network/interceptor/SpecificHostRateLimitInterceptor.kt b/library/src/main/java/eu/kanade/tachiyomi/network/interceptor/SpecificHostRateLimitInterceptor.kt new file mode 100644 index 0000000..6f7090c --- /dev/null +++ b/library/src/main/java/eu/kanade/tachiyomi/network/interceptor/SpecificHostRateLimitInterceptor.kt @@ -0,0 +1,28 @@ +package eu.kanade.tachiyomi.network.interceptor + +import okhttp3.HttpUrl +import okhttp3.Interceptor +import okhttp3.OkHttpClient +import java.util.concurrent.TimeUnit + +/** + * An OkHttp interceptor that handles given url host's rate limiting. + * + * Examples: + * + * httpUrl = "api.manga.com".toHttpUrlOrNull(), permits = 5, period = 1, unit = seconds => 5 requests per second to api.manga.com + * httpUrl = "imagecdn.manga.com".toHttpUrlOrNull(), permits = 10, period = 2, unit = minutes => 10 requests per 2 minutes to imagecdn.manga.com + * + * @since extension-lib 1.3 + * + * @param httpUrl {HttpUrl} The url host that this interceptor should handle. Will get url's host by using HttpUrl.host() + * @param permits {Int} Number of requests allowed within a period of units. + * @param period {Long} The limiting duration. Defaults to 1. + * @param unit {TimeUnit} The unit of time for the period. Defaults to seconds. + */ +fun OkHttpClient.Builder.rateLimitHost( + httpUrl: HttpUrl, + permits: Int, + period: Long = 1, + unit: TimeUnit = TimeUnit.SECONDS, +): Interceptor = throw Exception("Stub!")