-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
56 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 21 additions & 33 deletions
54
src/main/kotlin/io/github/jvmusin/polybacs/sybon/api/SybonApiFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
34 changes: 18 additions & 16 deletions
34
src/main/kotlin/io/github/jvmusin/polybacs/sybon/api/SybonArchiveApi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
28 changes: 15 additions & 13 deletions
28
src/main/kotlin/io/github/jvmusin/polybacs/sybon/api/SybonCheckingApi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
} |