feat: Add backend rate limit route

This commit is contained in:
oSumAtrIX 2024-06-06 23:21:03 +02:00
parent 71f58cf352
commit b9671703be
No known key found for this signature in database
GPG Key ID: A9B3094ACDB604B4
4 changed files with 36 additions and 1 deletions

View File

@ -41,7 +41,6 @@ abstract class BackendRepository internal constructor(
* @property bio The bio of the member. * @property bio The bio of the member.
* @property gpgKeys The GPG key of the member. * @property gpgKeys The GPG key of the member.
*/ */
@Serializable
class BackendMember( class BackendMember(
override val name: String, override val name: String,
override val avatarUrl: 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. * Get a release of a repository.
* *
@ -140,4 +152,11 @@ abstract class BackendRepository internal constructor(
* @return The members. * @return The members.
*/ */
abstract suspend fun members(organization: String): Set<BackendOrganization.BackendMember> abstract suspend fun members(organization: String): Set<BackendOrganization.BackendMember>
/**
* Get the rate limit of the backend.
*
* @return The rate limit.
*/
abstract suspend fun rateLimit(): BackendRateLimit?
} }

View File

@ -1,5 +1,6 @@
package app.revanced.api.configuration.routing.routes 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.ApiService
import app.revanced.api.configuration.services.AuthService import app.revanced.api.configuration.services.AuthService
import io.ktor.http.* import io.ktor.http.*
@ -28,6 +29,10 @@ internal fun Route.rootRoute() {
} }
} }
get("backend/rate_limit") {
call.respondOrNotFound(apiService.rateLimit())
}
authenticate("basic") { authenticate("basic") {
get("token") { get("token") {
call.respond(authService.newToken()) call.respond(authService.newToken())

View File

@ -106,3 +106,10 @@ class APILatestAnnouncement(
class APIAnnouncementArchivedAt( class APIAnnouncementArchivedAt(
val archivedAt: LocalDateTime, val archivedAt: LocalDateTime,
) )
@Serializable
class APIRateLimit(
val limit: Int,
val remaining: Int,
val reset: LocalDateTime,
)

View File

@ -42,4 +42,8 @@ internal class ApiService(
) )
} }
suspend fun rateLimit() = backendRepository.rateLimit()?.let {
APIRateLimit(it.limit, it.remaining, it.reset)
}
} }