Skip to content

Commit

Permalink
feat: Add backend rate limit route
Browse files Browse the repository at this point in the history
  • Loading branch information
oSumAtrIX committed Jun 6, 2024
1 parent 2ce1f4d commit 19a7085
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -140,4 +152,11 @@ abstract class BackendRepository internal constructor(
* @return The members.
*/
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?
}
Original file line number Diff line number Diff line change
@@ -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.*
Expand Down Expand Up @@ -28,6 +29,10 @@ internal fun Route.rootRoute() {
}
}

get("backend/rate_limit") {
call.respondOrNotFound(apiService.rateLimit())
}

authenticate("basic") {
get("token") {
call.respond(authService.newToken())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,10 @@ class APILatestAnnouncement(
class APIAnnouncementArchivedAt(
val archivedAt: LocalDateTime,
)

@Serializable
class APIRateLimit(
val limit: Int,
val remaining: Int,
val reset: LocalDateTime,
)
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,8 @@ internal class ApiService(

)
}

suspend fun rateLimit() = backendRepository.rateLimit()?.let {
APIRateLimit(it.limit, it.remaining, it.reset)
}
}

0 comments on commit 19a7085

Please sign in to comment.