Skip to content

Commit

Permalink
Remove retrofit from sybon api
Browse files Browse the repository at this point in the history
  • Loading branch information
jvmusin committed Feb 28, 2024
1 parent 2e4abd2 commit 31b3590
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 77 deletions.
17 changes: 2 additions & 15 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ repositories {
mavenCentral()
}

extra["springCloudVersion"] = "2023.0.0"
val coroutinesVersion = "1.8.0"
val retrofitVersion = "2.9.0"
val okhttp3Version = "5.0.0-alpha.12"
Expand All @@ -37,21 +36,17 @@ dependencies {
implementation("org.springframework.boot:spring-boot-starter-websocket")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.springframework.cloud:spring-cloud-starter-openfeign")
implementation("org.springframework.session:spring-session-core")
developmentOnly("org.springframework.boot:spring-boot-devtools")
implementation("org.springframework.session:spring-session-core") // TODO: Do we need this?
developmentOnly("org.springframework.boot:spring-boot-devtools") // TODO: Does it work?
testImplementation("org.springframework.boot:spring-boot-starter-test")


// https://mvnrepository.com/artifact/com.squareup.retrofit2/retrofit
implementation("com.squareup.retrofit2:retrofit:$retrofitVersion")
implementation("com.squareup.retrofit2:converter-scalars:$retrofitVersion")

implementation("com.squareup.okhttp3:logging-interceptor:$okhttp3Version")
implementation("com.squareup.okhttp3:okhttp:$okhttp3Version")
implementation("com.jakewharton.retrofit:retrofit2-kotlinx-serialization-converter:0.8.0")


implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:${coroutinesVersion}")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor:${coroutinesVersion}")

Expand All @@ -65,18 +60,10 @@ dependencies {

implementation("org.jsoup:jsoup:$jsoupVersion")


testImplementation("io.kotest:kotest-runner-junit5:$kotestVersion")
testImplementation("io.kotest:kotest-assertions-core:$kotestVersion")
testImplementation("io.kotest:kotest-property:$kotestVersion")
testImplementation("io.kotest.extensions:kotest-extensions-spring:1.1.3")

}

dependencyManagement {
imports {
mavenBom("org.springframework.cloud:spring-cloud-dependencies:${property("springCloudVersion")}")
}
}

tasks.withType<KotlinCompile> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,34 @@
package io.github.jvmusin.polybacs.sybon.api

import io.github.jvmusin.polybacs.retrofit.RetrofitClientFactory
import okhttp3.Interceptor
import okhttp3.Response
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Component
import org.springframework.web.reactive.function.client.ClientRequest
import org.springframework.web.reactive.function.client.WebClient
import org.springframework.web.reactive.function.client.support.WebClientAdapter
import org.springframework.web.service.invoker.HttpServiceProxyFactory
import org.springframework.web.service.invoker.createClient
import org.springframework.web.util.UriComponentsBuilder

/**
* Sybon API factory.
*
* Used to create [SybonArchiveApi] and [SybonCheckingApi].
*
* It uses [RetrofitClientFactory] under the hood to make the actual requests.
*
* @constructor Creates Sybon API factory.
* @property config Sybon configuration, used to configure proper *apiKey* and system urls.
*/
@Component
class SybonApiFactory(
@Value("\${sybon.apiKey}")
private val apiKey: String,
) {

/**
* ApiKey injector interceptor
*
* Injects *apiKey* to every request made to the API.
*
* @constructor Creates *apiKey* injector interceptor.
*/
private inner class ApiKeyInjectorInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val newUrl = chain.request().url.newBuilder().addQueryParameter("api_key", apiKey).build()
val newRequest = chain.request().newBuilder().url(newUrl).build()
return chain.proceed(newRequest)
}
}

private inline fun <reified T> createApi(url: String): T = RetrofitClientFactory.create(url) {
addInterceptor(ApiKeyInjectorInterceptor())
private inline fun <reified T : Any> createApi(): T {
val webClientBuilder = WebClient.builder()
.filter { request, next ->
val newUri = UriComponentsBuilder.fromUri(request.url()).queryParam("api_key", apiKey).build().toUri()
val newRequest = ClientRequest.from(request).url(newUri).build()
next.exchange(newRequest)
}.codecs {
it.defaultCodecs().maxInMemorySize(16 * 1024 * 1024) // 16 MB
}
return HttpServiceProxyFactory
.builderFor(WebClientAdapter.create(webClientBuilder.build()))
.build()
.createClient<T>()
}

fun createArchiveApi(): SybonArchiveApi = createApi("https://archive.sybon.org/api/")
fun createCheckingApi(): SybonCheckingApi = createApi("https://checking.sybon.org/api/")
fun createArchiveApi(): SybonArchiveApi = createApi()
fun createCheckingApi(): SybonCheckingApi = createApi()
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
package io.github.jvmusin.polybacs.sybon.api

import retrofit2.http.GET
import retrofit2.http.POST
import retrofit2.http.Path
import retrofit2.http.Query
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.service.annotation.GetExchange
import org.springframework.web.service.annotation.HttpExchange
import org.springframework.web.service.annotation.PostExchange

@HttpExchange("https://archive.sybon.org/api/")
interface SybonArchiveApi {
@GET("Collections")
@GetExchange("Collections")
suspend fun getCollections(
@Query("Offset") offset: Int = 0,
@Query("Limit") limit: Int = 100,
@RequestParam("Offset") offset: Int = 0,
@RequestParam("Limit") limit: Int = 100,
): List<SybonCollection>

@GET("Collections/{collectionId}")
suspend fun getCollection(@Path("collectionId") collectionId: Int): SybonCollection
@GetExchange("Collections/{collectionId}")
suspend fun getCollection(@PathVariable("collectionId") collectionId: Int): SybonCollection

@GET("Problems/{problemId}")
suspend fun getProblem(@Path("problemId") problemId: Int): SybonProblem
@GetExchange("Problems/{problemId}")
suspend fun getProblem(@PathVariable("problemId") problemId: Int): SybonProblem

@GET("Problems/{problemId}/statement")
suspend fun getProblemStatementUrl(@Path("problemId") problemId: Int): String
@GetExchange("Problems/{problemId}/statement")
suspend fun getProblemStatementUrl(@PathVariable("problemId") problemId: Int): String

@POST("Collections/{collectionId}/problems")
@PostExchange("Collections/{collectionId}/problems")
suspend fun importProblem(
@Path("collectionId") collectionId: Int,
@Query("internalProblemId") internalProblemId: String,
@PathVariable("collectionId") collectionId: Int,
@RequestParam("internalProblemId") internalProblemId: String,
): Int
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
package io.github.jvmusin.polybacs.sybon.api

import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.POST
import retrofit2.http.Query
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.service.annotation.GetExchange
import org.springframework.web.service.annotation.HttpExchange
import org.springframework.web.service.annotation.PostExchange

@HttpExchange("https://checking.sybon.org/api/")
interface SybonCheckingApi {
@GET("Compilers")
@GetExchange("Compilers")
suspend fun getCompilers(): List<SybonCompiler>

@POST("Submits/send")
suspend fun submitSolution(@Body solution: SybonSubmitSolution): Int
@PostExchange("Submits/send")
suspend fun submitSolution(@RequestBody solution: SybonSubmitSolution): Int

@POST("Submits/sendall")
suspend fun submitSolutions(@Body solutions: List<SybonSubmitSolution>): List<Int>
@PostExchange("Submits/sendall")
suspend fun submitSolutions(@RequestBody solutions: List<SybonSubmitSolution>): List<Int>

@POST("Submits/rejudge")
suspend fun rejudge(@Body ids: List<Int>)
@PostExchange("Submits/rejudge")
suspend fun rejudge(@RequestBody ids: List<Int>)

@GET("Submits/results")
suspend fun getResults(@Query("ids") ids: String): List<SybonSubmissionResult>
@GetExchange("Submits/results")
suspend fun getResults(@RequestParam("ids") ids: String): List<SybonSubmissionResult>
}

0 comments on commit 31b3590

Please sign in to comment.