Skip to content

Commit

Permalink
refactor start
Browse files Browse the repository at this point in the history
  • Loading branch information
1zun4 committed Jan 2, 2025
1 parent 8a3efe5 commit 799d4dd
Show file tree
Hide file tree
Showing 27 changed files with 674 additions and 433 deletions.
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ dependencies {
// includeDependency "org.graalvm.polyglot:ruby-community:24.0.2"
// includeDependency "org.graalvm.polyglot:llvm-native-community:24.0.2"

// HTTP library
includeDependency ("com.squareup.okhttp3:okhttp:5.0.0-alpha.14") {
exclude group: "org.jetbrains.kotlin", module: "kotlin-stdlib"
}

// SOCKS5 Proxy Support
includeDependency ("io.netty:netty-handler-proxy:4.1.97.Final") {
exclude group: "io.netty", module: "netty-common"
Expand Down
98 changes: 98 additions & 0 deletions src/main/kotlin/net/ccbluex/liquidbounce/api/BaseApi.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* This file is part of LiquidBounce (https://github.com/CCBlueX/LiquidBounce)
*
* Copyright (c) 2016 - 2025 CCBlueX
*
* LiquidBounce is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LiquidBounce is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LiquidBounce. If not, see <https://www.gnu.org/licenses/>.
*/
package net.ccbluex.liquidbounce.api

import net.ccbluex.liquidbounce.utils.io.HttpClient
import net.ccbluex.liquidbounce.utils.io.HttpMethod
import net.ccbluex.liquidbounce.utils.io.parse
import okhttp3.Headers
import okhttp3.RequestBody
import org.apache.commons.lang3.RandomStringUtils
import java.util.*

const val API_V1_ENDPOINT = "https://api.liquidbounce.net/api/v1"
const val API_V3_ENDPOINT = "https://api.liquidbounce.net/api/v3"

private const val AVATAR_UUID_URL = "https://avatar.liquidbounce.net/avatar/%s/100"
private const val AVATAR_USERNAME_URL = "https://avatar.liquidbounce.net/avatar/%s"

fun formatAvatarUrl(uuid: UUID?, username: String): String {
return if (uuid != null) {
AVATAR_UUID_URL.format(uuid)
} else {
AVATAR_USERNAME_URL.format(username)
}
}

/**
* This makes sense because we want forks to be able to use this API and not only the official client.
* It also allows us to use API endpoints for legacy on other branches.
*/
internal const val HARD_CODED_BRANCH = "nextgen"

val SESSION_TOKEN: String = RandomStringUtils.randomAlphanumeric(16)

/**
* Base API class
*
* @param baseUrl The base URL of the API
*/
abstract class BaseApi(protected val baseUrl: String) {

/**
* Makes a request and parses the response to the specified type
*/
protected suspend inline fun <reified T> request(
endpoint: String,
method: HttpMethod,
noinline headers: Headers.Builder.() -> Unit = {},
body: RequestBody? = null
): T = HttpClient.request("$baseUrl$endpoint", method, headers = {
add("X-Session-Token", SESSION_TOKEN)
headers(this)
}, body = body).parse()

protected suspend inline fun <reified T> get(
endpoint: String,
noinline headers: Headers.Builder.() -> Unit = {}
): T = request(endpoint, HttpMethod.GET, headers)

protected suspend inline fun <reified T> post(
endpoint: String,
body: RequestBody? = null,
noinline headers: Headers.Builder.() -> Unit = {}
): T = request(endpoint, HttpMethod.POST, headers, body)

protected suspend inline fun <reified T> put(
endpoint: String,
body: RequestBody? = null,
noinline headers: Headers.Builder.() -> Unit = {}
): T = request(endpoint, HttpMethod.PUT, headers, body)

protected suspend inline fun <reified T> patch(
endpoint: String,
body: RequestBody? = null,
noinline headers: Headers.Builder.() -> Unit = {}
): T = request(endpoint, HttpMethod.PATCH, headers, body)

protected suspend inline fun <reified T> delete(
endpoint: String,
noinline headers: Headers.Builder.() -> Unit = {}
): T = request(endpoint, HttpMethod.DELETE, headers)
}
186 changes: 0 additions & 186 deletions src/main/kotlin/net/ccbluex/liquidbounce/api/ClientApi.kt

This file was deleted.

Loading

0 comments on commit 799d4dd

Please sign in to comment.