Skip to content

Commit

Permalink
Create network.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
gmitch215 committed Jul 31, 2024
1 parent e737214 commit f2b124b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 30 deletions.
38 changes: 38 additions & 0 deletions src/main/kotlin/io/codemc/api/network.kt
Original file line number Diff line number Diff line change
@@ -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<String>
= 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")
25 changes: 0 additions & 25 deletions src/main/kotlin/io/codemc/api/nexus/endpoints.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>
= 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)
Expand Down
7 changes: 2 additions & 5 deletions src/main/kotlin/io/codemc/api/nexus/nexus.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,11 +30,6 @@ data class NexusConfig(

lateinit var nexusConfig: NexusConfig

private val json = Json {
ignoreUnknownKeys = true
explicitNulls = true
}

// Implementation

suspend fun ping(): Boolean {
Expand Down

0 comments on commit f2b124b

Please sign in to comment.