Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
34e800e
Fix: Lambda main
emyfops Mar 24, 2024
8845e69
Update .gitignore
emyfops Mar 24, 2024
92e3456
Test: Discord rpc
emyfops Mar 24, 2024
f3e0015
Added rpc coroutine scope
emyfops Mar 24, 2024
71f001c
Fix: Crash on disconnect
emyfops Mar 24, 2024
05146f4
Added shutdown hook
emyfops Mar 24, 2024
ef73ea7
Fix: Enum crash
emyfops Mar 24, 2024
39368e9
Merge branch 'master' into feature/discordrpc
emyfops Mar 24, 2024
5dbd361
Fix: Rpc not showing on profile
emyfops Mar 24, 2024
e75f8aa
Refactor: Use same thread for setup and connect
emyfops Mar 24, 2024
650575b
Added error event
emyfops Mar 24, 2024
1452852
Removed useless events
emyfops Mar 24, 2024
32b5dc7
Merge branch 'master' into feature/discordrpc
Avanatiker Mar 25, 2024
b5c844f
Added small image player
emyfops Mar 25, 2024
4464cf1
Fix DiscordRPC
Avanatiker Mar 26, 2024
0f11063
Feature: Discord RPC
emyfops Mar 27, 2024
89cae7e
Added extremely detailled kdoc for shutdown hook
emyfops Mar 30, 2024
d89bf47
Feature: Connection state events
emyfops Mar 30, 2024
b075f0c
Added kdoc
emyfops Mar 30, 2024
57d3080
Feature: Minimal http wrapper
emyfops Mar 30, 2024
ee60145
Fixed comment
emyfops Mar 30, 2024
535b891
Feature: RPC http request
emyfops Mar 30, 2024
3e7a1a6
Merge branch 'master' into feature/discordrpc
Avanatiker Apr 11, 2024
af2c2c1
Fix: Forge doesn't support ModifyArgs
emyfops Apr 13, 2024
dd52500
Fix: Missing import
emyfops Apr 13, 2024
eab5b12
Disable transitive include
emyfops Apr 13, 2024
dfc909d
Merge conflicts
emyfops Apr 15, 2024
8cdb410
Fix: Quilt
emyfops Apr 15, 2024
055ef2d
Merge branch 'master' into feature/discordrpc
emyfops Apr 22, 2024
63b7327
Merge branch 'master' into feature/discordrpc
emyfops Apr 27, 2024
ffcf4f6
Fix: thedarkcolour/KotlinForForge/94
emyfops Apr 27, 2024
3d6c729
Merge branch 'master' into feature/discordrpc
emyfops Apr 27, 2024
0e4a159
Added all current methods
emyfops Apr 28, 2024
0346552
Refactor!: Up to date with server
emyfops Apr 28, 2024
a9f875e
Working RPC system
emyfops Apr 28, 2024
087b0c3
Refactor: Pseudo setting consumer
emyfops Apr 28, 2024
56de50c
Feature: Join a party later with commands
emyfops Apr 29, 2024
9a89c7e
funny comment
emyfops Apr 29, 2024
32a242b
Fix: Token is not authorization
emyfops Apr 29, 2024
2922ac5
Create MiscArguments.kt
emyfops Apr 29, 2024
776f3c6
Fixed rpc command builder
emyfops Apr 29, 2024
8db6369
Merge branch 'master' into feature/discordrpc
emyfops May 19, 2024
816190e
Merge conflicts
emyfops May 19, 2024
b724798
Merge branch 'master' into feature/discordrpc
emyfops May 19, 2024
1d048d4
Update gradle.properties
emyfops May 19, 2024
6e659a9
Fix: RPC Connection
emyfops May 19, 2024
5a68b2e
Fix: IPC not found
emyfops May 19, 2024
f0a01e7
Add exception info on error
emyfops May 19, 2024
dfc8f97
Better error format
emyfops May 19, 2024
00b17af
Temp fix xray
emyfops May 20, 2024
80b7fa7
Better errors
emyfops May 26, 2024
51eeaab
Merge branch 'master' into feature/discordrpc
emyfops May 26, 2024
8fbe152
Update DiscordRPC.kt
emyfops May 26, 2024
ba3073d
Refactor: API update
emyfops Jun 28, 2024
4480696
Merge branch 'master' into feature/discordrpc
Avanatiker Jun 28, 2024
744a80a
Merge branch 'master' into feature/discordrpc
emyfops Jul 5, 2024
90b600e
Refactor: Use our fork
emyfops Jul 5, 2024
d5a11cb
Added test logging
emyfops Jul 5, 2024
4962c3f
Update lambda.mixins.common.json
emyfops Jul 5, 2024
f8adab2
Temp force variable
emyfops Jul 5, 2024
6975fa9
Temp: Local party
emyfops Jul 5, 2024
074304c
Update lambda.mixins.common.json
emyfops Jul 5, 2024
c0a2b8e
Fixed the update delay
emyfops Jul 5, 2024
c33b9f9
Merge branch 'master' into feature/discordrpc
emyfops Jul 8, 2024
e0e17a7
Merge branch 'master' into feature/discordrpc
emyfops Aug 19, 2024
b10d7f6
Update Player.kt
emyfops Aug 19, 2024
fda87bb
feat: cached downloads & request builder
emyfops Aug 19, 2024
7e7182e
added request builder to endpoints
emyfops Aug 19, 2024
4943a72
several fixes
emyfops Aug 19, 2024
ba1b21b
Update ConnectionEvent.kt
emyfops Aug 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: cached downloads & request builder
  • Loading branch information
emyfops committed Aug 19, 2024
commit fda87bb45d69d1258eadc3e1e0a82c59709626f2
20 changes: 9 additions & 11 deletions common/src/main/kotlin/com/lambda/http/Method.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ package com.lambda.http

/**
* Enum representing HTTP methods.
*
* @property value The string representation of the HTTP method.
*/
enum class Method(val value: String) {
GET("GET"),
HEAD("HEAD"),
POST("POST"),
PUT("PUT"),
DELETE("DELETE"),
OPTIONS("OPTIONS"),
TRACE("TRACE"),
PATCH("PATCH")
enum class Method {
GET,
HEAD,
POST,
PUT,
DELETE,
OPTIONS,
TRACE,
PATCH;
}
49 changes: 45 additions & 4 deletions common/src/main/kotlin/com/lambda/http/Request.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
package com.lambda.http

import com.lambda.Lambda
import com.lambda.Lambda.LOG
import com.lambda.util.FolderRegister.cache
import java.io.File
import java.net.HttpURLConnection
import java.net.URL
import java.time.Instant
import kotlin.time.Duration
import kotlin.time.Duration.Companion.days

/**
* Represents an HTTP request handler that allows making various HTTP requests.
* Represents an HTTP request.
*
* @property url The URL to which the request will be made.
* @property method The HTTP method to be used for the request. Default is [Method.GET].
* @property parameters A map of query parameters to be included in the request. Default is an empty map.
* @property headers A map of headers to be included in the request. Default is an empty map.
* @property config A lambda function to configure the HTTP connection. Default is an empty lambda.
*/
class Request(
data class Request(
val url: String,
val method: Method = Method.GET,
val parameters: Map<String, Any> = mapOf(),
Expand All @@ -26,6 +30,43 @@ class Request(
val canBeEncoded: Boolean
get() = method != Method.POST && method != Method.PUT && method != Method.PATCH

/**
* Downloads the resource at the specified path and caches it for future use.
*
* @param path The path to the resource.
* @param maxAge The maximum age of the cached resource. Default is 4 days.
*/
fun maybeDownload(path: String, maxAge: Duration = 4.days): ByteArray {
val file = File("${cache}/${path.substringAfterLast("/").hashCode()}")

if (file.exists() && Instant.now().toEpochMilli() - file.lastModified() < maxAge.inWholeMilliseconds)
return file.readBytes()

file.writeText("") // Clear the file before writing to it.

val url = URL(
if (parameters.isNotEmpty() && canBeEncoded) "$url?${parameters.query}"
else url
)

val connection = url.openConnection() as HttpURLConnection
config.invoke(connection)

connection.requestMethod = method.name

headers.forEach { (key, value) -> connection.setRequestProperty(key, value) }

connection.connect()

connection.inputStream.use { input ->
file.outputStream().use { output ->
input.copyTo(output)
}
}

return file.readBytes()
}

/**
* Executes the HTTP request synchronously.
*/
Expand All @@ -38,7 +79,7 @@ class Request(
val connection = url.openConnection() as HttpURLConnection
config.invoke(connection)

connection.requestMethod = method.value
connection.requestMethod = method.name

headers.forEach { (key, value) -> connection.setRequestProperty(key, Lambda.gson.toJson(value)) }

Expand Down
59 changes: 59 additions & 0 deletions common/src/main/kotlin/com/lambda/http/RequestBuilder.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.lambda.http

import java.net.HttpURLConnection

@Target(AnnotationTarget.CLASS, AnnotationTarget.TYPE)
@DslMarker
annotation class RequestDsl

@RequestDsl
class RequestBuilder(
private val url: String,
) {
private var method: Method = Method.GET
private val parameters: MutableMap<String, Any> = mutableMapOf()
private val headers: MutableMap<String, String> = mutableMapOf()
private var config: ((HttpURLConnection) -> Unit) = {}

/**
* Sets the HTTP method to be used for the request.
*/
fun method(method: Method): RequestBuilder {
this.method = method
return this
}

/**
* Sets the query parameters to be included in the request.
*/
fun parameters(parameters: Map<String, Any>): RequestBuilder {
this.parameters.putAll(parameters)
return this
}

/**
* Sets the headers to be included in the request.
*/
fun headers(headers: Map<String, String>): RequestBuilder {
this.headers.putAll(headers)
return this
}

/**
* Sets the lambda function to configure the HTTP connection.
*/
fun config(config: (HttpURLConnection) -> Unit): RequestBuilder {
this.config = config
return this
}

fun build() = Request(url, method, parameters, headers, config)
}

/**
* Creates an HTTP request.
*
* @param url The URL to which the request will be made.
* @param block A lambda function to configure the request.
*/
inline fun request(url: String, block: (@RequestDsl RequestBuilder).() -> Unit) = RequestBuilder(url).apply(block).build()
1 change: 1 addition & 0 deletions common/src/main/kotlin/com/lambda/util/FolderRegister.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ object FolderRegister {
val config: File = File(lambda, "config")
val packetLogs: File = File(lambda, "packet-log")
val replay: File = File(lambda, "replay")
val cache: File = File(lambda, "cache")

fun File.createIfNotExists() {
if (!exists()) {
Expand Down