-
-
Notifications
You must be signed in to change notification settings - Fork 513
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
27 changed files
with
674 additions
and
433 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
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 |
---|---|---|
@@ -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
186
src/main/kotlin/net/ccbluex/liquidbounce/api/ClientApi.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.