Fix warnings/formatting in APIClient and WishkoboneApplication

This commit is contained in:
William Brawner 2021-05-30 21:20:53 -06:00
parent f7b78a0a8a
commit 9664fcb4c0
2 changed files with 13 additions and 88 deletions

View file

@ -4,24 +4,19 @@ import okhttp3.*
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody.Companion.toRequestBody
import java.io.IOException
import kotlin.Throws
import java.util.HashMap
/**
* Created by Josh on 2/09/2014.
*/
class APIClient {
private var cookieValue: String? = null
fun setCookie(value: String?) {
cookieValue = value
}
var cookieValue: String? = null
operator fun get(url: String, params: HashMap<String?, String?>?, responseHandler: Callback?) {
operator fun get(url: String, params: HashMap<String, String?>?, responseHandler: Callback) {
var urlBuilder: HttpUrl.Builder = getAbsoluteUrl(url).toHttpUrl().newBuilder()
params?.let {
for (key in it.keys.filterNotNull()) {
urlBuilder = urlBuilder.setQueryParameter(key, params[key])
for (key in it.keys) {
urlBuilder = urlBuilder.setQueryParameter(key, it[key])
}
}
val request: Request = Request.Builder().url(urlBuilder.build())
@ -32,36 +27,10 @@ class APIClient {
.addHeader("X-Requested-With", "XMLHttpRequest")
.addHeader("Cookie", "KoboSession=$cookieValue")
.build()
client.newCall(request).enqueue(responseHandler!!)
client.newCall(request).enqueue(responseHandler)
}
fun post(url: String, params: HashMap<String?, String?>, responseHandler: Callback?) {
this.post(url, params, "application/x-www-form-urlencoded", responseHandler)
}
fun post(
url: String,
params: HashMap<String?, String?>,
contentType: String,
responseHandler: Callback?
) {
var builder = FormBody.Builder()
for (key in params.keys.filterNotNull()) {
builder = builder.add(key, params[key].toString())
}
val formBody: FormBody = builder.build()
val request: Request = Request.Builder().url(getAbsoluteUrl(url)).post(formBody)
.addHeader("User-Agent", agent)
.addHeader("Referer", "https://www.kobo.com/au/en/account/wishlist")
.addHeader("Accept", "application / json, text / javascript, * / *; q = 0.01")
.addHeader("X-Requested-With", "XMLHttpRequest")
.addHeader("Cookie", "KoboSession=$cookieValue")
.addHeader("Content-type", contentType)
.build()
client.newCall(request).enqueue(responseHandler!!)
}
fun post(url: String, payload: String, contentType: String, responseHandler: Callback?) {
fun post(url: String, payload: String, contentType: String, responseHandler: Callback) {
val body: RequestBody = payload.toRequestBody(contentType.toMediaType())
val request: Request = Request.Builder().url(getAbsoluteUrl(url)).post(body)
.addHeader("User-Agent", agent)
@ -72,50 +41,7 @@ class APIClient {
.addHeader("Cookie", "KoboSession=$cookieValue")
.addHeader("Content-type", contentType)
.build()
client.newCall(request).enqueue(responseHandler!!)
}
@Throws(IOException::class)
fun syncGet(url: String, params: HashMap<String, String>?): Response {
var urlBuilder: HttpUrl.Builder = getAbsoluteUrl(url).toHttpUrl().newBuilder()
params?.let {
for (key in it.keys) {
urlBuilder = urlBuilder.setQueryParameter(key, it[key])
}
}
val request: Request = Request.Builder().url(urlBuilder.build())
.get()
.addHeader("User-agent", agent)
.addHeader("Referer", "https://www.kobo.com/au/en/account/wishlist")
.addHeader("Accept", "application / json, text / javascript, * / *; q = 0.01")
.addHeader("X-Requested-With", "XMLHttpRequest")
.addHeader("Cookie", "KoboSession=$cookieValue")
.build()
return client.newCall(request).execute()
}
@Throws(IOException::class)
fun syncPost(url: String, payload: String, contentType: String): Response {
val body: RequestBody = payload.toRequestBody(contentType.toMediaType())
val request: Request = Request.Builder().url(getAbsoluteUrl(url)).post(body)
.addHeader("User-agent", agent)
.addHeader("Referer", "https://www.kobo.com/au/en/account/wishlist")
.addHeader("Accept", "application/json, text/javascript, */*; q=0.01")
.addHeader("X-Requested-With", "XMLHttpRequest")
.addHeader("Cookie", String.format("KoboSession=%s;", cookieValue))
.addHeader("Content-type", contentType)
.build()
return client.newCall(request).execute()
}
@Throws(IOException::class)
fun syncPost(url: String, params: HashMap<String, String>): Response {
var builder = FormBody.Builder()
for (key in params.keys) {
builder = builder.add(key, params[key].toString())
}
val formBody: FormBody = builder.build()
return this.syncPost(url, formBody.toString(), "application/x-www-form-urlencoded")
client.newCall(request).enqueue(responseHandler)
}
companion object {

View file

@ -3,27 +3,27 @@ package au.com.joshsharp.wishkobone
import android.app.Application
import android.content.SharedPreferences
class WishkoboneApplication: Application() {
class WishkoboneApplication : Application() {
var client = APIClient()
override fun onCreate() {
super.onCreate()
if (getCookie() != null){
client.setCookie(getCookie())
getCookie()?.let {
client.cookieValue = it
}
}
fun getCookie(): String? {
val prefs = getSharedPreferences(getString(R.string.app_name), 0)
return prefs.getString("cookie",null)
return prefs.getString("cookie", null)
}
fun setCookie(cookie: String) {
val editor = getSharedPreferences(getString(R.string.app_name), 0).edit()
editor.putString("cookie", cookie)
editor.apply()
client.setCookie(cookie)
client.cookieValue = cookie
}
@ -31,7 +31,6 @@ class WishkoboneApplication: Application() {
val editor = getSharedPreferences(getString(R.string.app_name), 0).edit()
editor.clear()
editor.apply()
client = APIClient() // remove cookie
client.cookieValue = null
}
}