From 19a7085989b261b2db8f2e3eff260de7fe1119f1 Mon Sep 17 00:00:00 2001 From: oSumAtrIX Date: Thu, 6 Jun 2024 23:21:03 +0200 Subject: [PATCH] feat: Add backend rate limit route --- .../repository/BackendRepository.kt | 21 ++++++++++++++++++- .../configuration/routing/routes/ApiRoute.kt | 5 +++++ .../api/configuration/schema/APISchema.kt | 7 +++++++ .../api/configuration/services/ApiService.kt | 4 ++++ 4 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/app/revanced/api/configuration/repository/BackendRepository.kt b/src/main/kotlin/app/revanced/api/configuration/repository/BackendRepository.kt index a024b511..b48ee81a 100644 --- a/src/main/kotlin/app/revanced/api/configuration/repository/BackendRepository.kt +++ b/src/main/kotlin/app/revanced/api/configuration/repository/BackendRepository.kt @@ -41,7 +41,6 @@ abstract class BackendRepository internal constructor( * @property bio The bio of the member. * @property gpgKeys The GPG key of the member. */ - @Serializable class BackendMember( override val name: String, override val avatarUrl: String, @@ -110,6 +109,19 @@ abstract class BackendRepository internal constructor( } } + /** + * The rate limit of the backend. + * + * @property limit The limit of the rate limit. + * @property remaining The remaining requests of the rate limit. + * @property reset The date and time the rate limit resets. + */ + class BackendRateLimit( + val limit: Int, + val remaining: Int, + val reset: LocalDateTime, + ) + /** * Get a release of a repository. * @@ -140,4 +152,11 @@ abstract class BackendRepository internal constructor( * @return The members. */ abstract suspend fun members(organization: String): Set + + /** + * Get the rate limit of the backend. + * + * @return The rate limit. + */ + abstract suspend fun rateLimit(): BackendRateLimit? } diff --git a/src/main/kotlin/app/revanced/api/configuration/routing/routes/ApiRoute.kt b/src/main/kotlin/app/revanced/api/configuration/routing/routes/ApiRoute.kt index 762bdff2..143e14b0 100644 --- a/src/main/kotlin/app/revanced/api/configuration/routing/routes/ApiRoute.kt +++ b/src/main/kotlin/app/revanced/api/configuration/routing/routes/ApiRoute.kt @@ -1,5 +1,6 @@ package app.revanced.api.configuration.routing.routes +import app.revanced.api.configuration.respondOrNotFound import app.revanced.api.configuration.services.ApiService import app.revanced.api.configuration.services.AuthService import io.ktor.http.* @@ -28,6 +29,10 @@ internal fun Route.rootRoute() { } } + get("backend/rate_limit") { + call.respondOrNotFound(apiService.rateLimit()) + } + authenticate("basic") { get("token") { call.respond(authService.newToken()) diff --git a/src/main/kotlin/app/revanced/api/configuration/schema/APISchema.kt b/src/main/kotlin/app/revanced/api/configuration/schema/APISchema.kt index 11d8446b..b6018e42 100644 --- a/src/main/kotlin/app/revanced/api/configuration/schema/APISchema.kt +++ b/src/main/kotlin/app/revanced/api/configuration/schema/APISchema.kt @@ -106,3 +106,10 @@ class APILatestAnnouncement( class APIAnnouncementArchivedAt( val archivedAt: LocalDateTime, ) + +@Serializable +class APIRateLimit( + val limit: Int, + val remaining: Int, + val reset: LocalDateTime, +) diff --git a/src/main/kotlin/app/revanced/api/configuration/services/ApiService.kt b/src/main/kotlin/app/revanced/api/configuration/services/ApiService.kt index 940c6e5a..bf408615 100644 --- a/src/main/kotlin/app/revanced/api/configuration/services/ApiService.kt +++ b/src/main/kotlin/app/revanced/api/configuration/services/ApiService.kt @@ -42,4 +42,8 @@ internal class ApiService( ) } + + suspend fun rateLimit() = backendRepository.rateLimit()?.let { + APIRateLimit(it.limit, it.remaining, it.reset) + } }