From f2b124b9dda5a9103d3a6ea48d4e05f42f99e9ba Mon Sep 17 00:00:00 2001 From: Gregory Mitchell <54124162+gmitch215@users.noreply.github.com> Date: Wed, 31 Jul 2024 16:22:32 -0500 Subject: [PATCH] Create `network.kt` --- src/main/kotlin/io/codemc/api/network.kt | 38 +++++++++++++++++++ .../kotlin/io/codemc/api/nexus/endpoints.kt | 25 ------------ src/main/kotlin/io/codemc/api/nexus/nexus.kt | 7 +--- 3 files changed, 40 insertions(+), 30 deletions(-) create mode 100644 src/main/kotlin/io/codemc/api/network.kt diff --git a/src/main/kotlin/io/codemc/api/network.kt b/src/main/kotlin/io/codemc/api/network.kt new file mode 100644 index 0000000..62e619f --- /dev/null +++ b/src/main/kotlin/io/codemc/api/network.kt @@ -0,0 +1,38 @@ +@file:JvmName("Network") + +package io.codemc.api + +import io.codemc.api.nexus.nexusConfig +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext +import kotlinx.serialization.json.Json +import java.net.URI +import java.net.http.HttpClient +import java.net.http.HttpRequest +import java.net.http.HttpResponse +import java.time.Duration + +private const val USER_AGENT = "CodeMC Nexus API" + +internal val http = HttpClient.newBuilder() + .connectTimeout(Duration.ofSeconds(10)) + .build() + +internal val json = Json { + ignoreUnknownKeys = true + explicitNulls = true +} + +internal suspend fun req(url: String, request: HttpRequest.Builder.() -> Unit = { GET() }): HttpResponse + = withContext(Dispatchers.IO) { + val req = HttpRequest.newBuilder() + .uri(URI.create(url)) + .header("User-Agent", USER_AGENT) + .header("Authorization", nexusConfig.authorization) + + request(req) + + http.send(req.build(), HttpResponse.BodyHandlers.ofString()) +} + +suspend fun github(username: String, project: String) = req("https://api.github.com/repos/$username/$project") \ No newline at end of file diff --git a/src/main/kotlin/io/codemc/api/nexus/endpoints.kt b/src/main/kotlin/io/codemc/api/nexus/endpoints.kt index 181a739..f41c8ee 100644 --- a/src/main/kotlin/io/codemc/api/nexus/endpoints.kt +++ b/src/main/kotlin/io/codemc/api/nexus/endpoints.kt @@ -2,42 +2,17 @@ package io.codemc.api.nexus -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.withContext import kotlinx.serialization.json.buildJsonObject import kotlinx.serialization.json.put import kotlinx.serialization.json.putJsonObject -import java.net.URI -import java.net.http.HttpClient -import java.net.http.HttpRequest -import java.net.http.HttpResponse -import java.time.Duration // Fields -private const val USER_AGENT = "CodeMC Nexus API" - -private val client = HttpClient.newBuilder() - .connectTimeout(Duration.ofSeconds(10)) - .build() - val API_URL get() = "${nexusConfig.url}/service/rest/v1" // Implementation -internal suspend fun req(url: String, request: HttpRequest.Builder.() -> Unit = { GET() }): HttpResponse - = withContext(Dispatchers.IO) { - val req = HttpRequest.newBuilder() - .uri(URI.create(url)) - .header("User-Agent", USER_AGENT) - .header("Authorization", nexusConfig.authorization) - - request(req) - - client.send(req.build(), HttpResponse.BodyHandlers.ofString()) - } - internal fun createMavenRepository(name: String): String = buildJsonObject { put("name", name.lowercase()) put("online", true) diff --git a/src/main/kotlin/io/codemc/api/nexus/nexus.kt b/src/main/kotlin/io/codemc/api/nexus/nexus.kt index 6d1f884..0611a73 100644 --- a/src/main/kotlin/io/codemc/api/nexus/nexus.kt +++ b/src/main/kotlin/io/codemc/api/nexus/nexus.kt @@ -2,6 +2,8 @@ package io.codemc.api.nexus +import io.codemc.api.json +import io.codemc.api.req import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import kotlinx.serialization.ExperimentalSerializationApi @@ -28,11 +30,6 @@ data class NexusConfig( lateinit var nexusConfig: NexusConfig -private val json = Json { - ignoreUnknownKeys = true - explicitNulls = true -} - // Implementation suspend fun ping(): Boolean {