Skip to content

Instantly share code, notes, and snippets.

@marrek13
Created October 14, 2022 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marrek13/89fc75d0cb2323b7ca0c9b3d8a91bafd to your computer and use it in GitHub Desktop.
Save marrek13/89fc75d0cb2323b7ca0c9b3d8a91bafd to your computer and use it in GitHub Desktop.
data class ApiVersionCondition(private val apiVersion: ApiVersionValue) : RequestCondition<ApiVersionCondition> {
override fun combine(other: ApiVersionCondition) = ApiVersionCondition(other.apiVersion)
override fun getMatchingCondition(request: HttpServletRequest): ApiVersionCondition? {
val requestApiVersion =
request.getHeader(ACCEPT_VERSION_HEADER)
?.let { ApiVersionValue.valueOf("V${it.replace('.', '_')}") }
?: ApiVersionValue.V1_0_0
if (requestApiVersion >= apiVersion) {
return this
}
return null
}
override fun compareTo(other: ApiVersionCondition, request: HttpServletRequest): Int = other.apiVersion.compareTo(apiVersion)
companion object {
const val ACCEPT_VERSION_HEADER = "accept-version"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment