Skip to content
This repository has been archived by the owner on Jun 11, 2023. It is now read-only.

Commit

Permalink
Add user agent
Browse files Browse the repository at this point in the history
  • Loading branch information
SilverAndro committed Jul 3, 2022
1 parent 1924a8d commit c1a473d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ abstract class APIInterface {
open val ratelimitRemainingHeader = ""
open val ratelimitResetHeader = ""

val client = HttpClient(CIO) {
open val client = HttpClient(CIO) {
install(JsonFeature)
}
open fun HttpRequestBuilder.attachAuth() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ import com.github.p03w.modifold.api_core.APIInterface
import com.github.p03w.modifold.api_core.Ratelimit
import com.github.p03w.modifold.cli.ModifoldArgs
import com.github.p03w.modifold.cli.warn
import com.github.p03w.modifold.curseforge_schema.*
import com.github.p03w.modifold.curseforge_schema.CurseforgeAsset
import com.github.p03w.modifold.curseforge_schema.CurseforgeFile
import com.github.p03w.modifold.curseforge_schema.CurseforgeProject
import com.github.p03w.modifold.modrinth_schema.*
import com.google.gson.GsonBuilder
import io.ktor.client.*
import io.ktor.client.engine.cio.*
import io.ktor.client.features.*
import io.ktor.client.features.json.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
Expand All @@ -15,12 +21,29 @@ import java.io.BufferedInputStream
import java.net.URL
import kotlin.time.Duration.Companion.milliseconds


object ModrinthAPI : APIInterface() {
override val ratelimit = Ratelimit(150.milliseconds, false)
override val ratelimitRemainingHeader = "X-Ratelimit-Remaining"
override val ratelimitResetHeader = "X-Ratelimit-Reset"
lateinit var AuthToken: String

fun getUserAgent(): String {
return "SilverAndro/Modifold/${getFileVersion()} (Silver <3#0955)"
}

private fun getFileVersion(): String {
return this.javaClass.getResourceAsStream("/version.txt")
?.bufferedReader()?.readText() ?: "Unknown Version"
}

override val client = HttpClient(CIO) {
install(JsonFeature)
install(UserAgent) {
agent = getUserAgent()
}
}

override fun HttpRequestBuilder.attachAuth() {
headers {
append(HttpHeaders.Authorization, AuthToken)
Expand Down
18 changes: 17 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
}

group = "com.github.p03w"
version = "2.1.6"
version = "2.2.1"

repositories {
mavenCentral()
Expand Down Expand Up @@ -69,3 +69,19 @@ tasks.withType<KotlinCompile> {
application {
mainClass.set("com.github.p03w.modifold.MainKt")
}

task("embedVersion") {
doLast {
val file = File("src/main/resources/version.txt")
file.delete()
file.parentFile.mkdirs()
file.createNewFile()
file.writer().use {
it.write(version.toString())
}
}
}

tasks.build {
dependsOn("embedVersion")
}
1 change: 1 addition & 0 deletions src/main/resources/version.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.2.1

0 comments on commit c1a473d

Please sign in to comment.