blob: ddf2fccad6f6d1157d6bcaebbd6072845c2b5411 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
package moe.nea.ledger.utils.network
import com.google.gson.JsonElement
import java.net.URL
data class Request(
val url: URL,
val method: Method,
val body: String?,
val headers: Map<String, String>,
) {
enum class Method {
GET, POST
}
enum class MediaType(val text: String) {
JSON("application/json"),
TEXT("text/plain"),
HTML("text/html"),
ANY("*/*"),
}
fun withHeaders(map: Map<String, String>): Request {
// TODO: enforce caselessness?
return this.copy(headers = headers + map)
}
fun post() = copy(method = Method.POST)
fun get() = copy(method = Method.GET)
fun json(element: JsonElement) = copy(
headers = headers + mapOf("content-type" to "application/json"),
body = element.toString())
fun accept(request: MediaType) = withHeaders(mapOf("accept" to request.text))
fun acceptJson() = accept(MediaType.JSON)
fun execute(requestUtil: RequestUtil) = requestUtil.executeRequest(this)
}
|