Update HttpSession.kt

This commit is contained in:
Arjix 2021-09-02 15:32:45 +03:00 committed by GitHub
parent 1ac8fb2700
commit cc0fdd8a7a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -14,7 +14,7 @@ import khttp.responses.Response
* *
* @property sessionCookies A cookie jar. * @property sessionCookies A cookie jar.
*/ */
class Session { class HttpSession {
companion object { companion object {
const val DEFAULT_TIMEOUT = 30.0 const val DEFAULT_TIMEOUT = 30.0
@ -32,11 +32,19 @@ class Session {
} }
return cookies.toMap() return cookies.toMap()
} }
fun mergeCookies(cookie1: Map<String, String>, cookie2: Map<String, String>?): Map<String, String> {
val a = cookie1.toMutableMap()
if (!cookie2.isNullOrEmpty()) {
a.putAll(cookie2.toMap())
}
return a.toMap()
}
} }
public val sessionCookies = CookieJar() public val sessionCookies = CookieJar()
public fun get( fun get(
url: String, headers: Map<String, String?> = mapOf(), url: String, headers: Map<String, String?> = mapOf(),
params: Map<String, String> = mapOf(), params: Map<String, String> = mapOf(),
data: Any? = null, json: Any? = null, data: Any? = null, json: Any? = null,
@ -49,7 +57,7 @@ class Session {
val res = khttp.get( val res = khttp.get(
url, headers, params, url, headers, params,
data, json, auth, data, json, auth,
cookies, timeout, mergeCookies(sessionCookies, cookies), timeout,
allowRedirects, allowRedirects,
stream, files stream, files
) )
@ -60,7 +68,7 @@ class Session {
return res return res
} }
public fun post( fun post(
url: String, headers: Map<String, String?> = mapOf(), url: String, headers: Map<String, String?> = mapOf(),
params: Map<String, String> = mapOf(), params: Map<String, String> = mapOf(),
data: Any? = null, json: Any? = null, data: Any? = null, json: Any? = null,
@ -73,7 +81,7 @@ class Session {
val res = khttp.post( val res = khttp.post(
url, headers, params, url, headers, params,
data, json, auth, data, json, auth,
cookies, timeout, mergeCookies(sessionCookies, cookies), timeout,
allowRedirects, allowRedirects,
stream, files stream, files
) )
@ -83,5 +91,4 @@ class Session {
} }
return res return res
} }
} }