Fix APIClient compilation errors
This commit is contained in:
parent
51f6457537
commit
f7b78a0a8a
1 changed files with 30 additions and 52 deletions
|
@ -1,22 +1,9 @@
|
|||
package au.com.joshsharp.wishkobone
|
||||
|
||||
import okhttp3.OkHttpClient.Builder.retryOnConnectionFailure
|
||||
import okhttp3.OkHttpClient.Builder.build
|
||||
import okhttp3.HttpUrl.newBuilder
|
||||
import okhttp3.HttpUrl.Builder.setQueryParameter
|
||||
import okhttp3.Request.Builder.url
|
||||
import okhttp3.HttpUrl.Builder.build
|
||||
import okhttp3.Request.Builder.get
|
||||
import okhttp3.Request.Builder.addHeader
|
||||
import okhttp3.Request.Builder.build
|
||||
import okhttp3.OkHttpClient.newCall
|
||||
import okhttp3.Call.enqueue
|
||||
import okhttp3.FormBody.Builder.add
|
||||
import okhttp3.FormBody.Builder.build
|
||||
import okhttp3.Request.Builder.post
|
||||
import okhttp3.Call.execute
|
||||
import au.com.joshsharp.wishkobone.APIClient
|
||||
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
|
||||
|
@ -31,18 +18,17 @@ class APIClient {
|
|||
}
|
||||
|
||||
operator fun get(url: String, params: HashMap<String?, String?>?, responseHandler: Callback?) {
|
||||
var urlBuilder: Builder = HttpUrl.parse(getAbsoluteUrl(url))!!.newBuilder()
|
||||
if (params != null) {
|
||||
for (key in params.keys) {
|
||||
var urlBuilder: HttpUrl.Builder = getAbsoluteUrl(url).toHttpUrl().newBuilder()
|
||||
params?.let {
|
||||
for (key in it.keys.filterNotNull()) {
|
||||
urlBuilder = urlBuilder.setQueryParameter(key, params[key])
|
||||
}
|
||||
}
|
||||
val request: Request = Builder().url(urlBuilder.build())
|
||||
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("Accept-Language", "en-GB,en-AU;q=0.7,en;q=0.3")
|
||||
.addHeader("X-Requested-With", "XMLHttpRequest")
|
||||
.addHeader("Cookie", "KoboSession=$cookieValue")
|
||||
.build()
|
||||
|
@ -56,19 +42,18 @@ class APIClient {
|
|||
fun post(
|
||||
url: String,
|
||||
params: HashMap<String?, String?>,
|
||||
contentType: String?,
|
||||
contentType: String,
|
||||
responseHandler: Callback?
|
||||
) {
|
||||
var builder = Builder()
|
||||
for (key in params.keys) {
|
||||
builder = builder.add(key, params[key])
|
||||
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 = Builder().url(getAbsoluteUrl(url)).post(formBody)
|
||||
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("Accept-Language", "en-GB,en-AU;q=0.7,en;q=0.3")
|
||||
.addHeader("X-Requested-With", "XMLHttpRequest")
|
||||
.addHeader("Cookie", "KoboSession=$cookieValue")
|
||||
.addHeader("Content-type", contentType)
|
||||
|
@ -76,13 +61,12 @@ class APIClient {
|
|||
client.newCall(request).enqueue(responseHandler!!)
|
||||
}
|
||||
|
||||
fun post(url: String, payload: String, contentType: String?, responseHandler: Callback?) {
|
||||
val body: RequestBody = RequestBody.create(payload, get.get(contentType))
|
||||
val request: Request = Builder().url(getAbsoluteUrl(url)).post(body)
|
||||
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)
|
||||
.addHeader("Referer", "https://www.kobo.com/au/en/account/wishlist")
|
||||
.addHeader("Accept", "application/json, text/javascript, */*; q=0.01")
|
||||
.addHeader("Accept-Language", "en-GB,en-AU;q=0.7,en;q=0.3")
|
||||
.addHeader("X-Requested-With", "XMLHttpRequest")
|
||||
.addHeader("Origin", "https://www.kobo.com")
|
||||
.addHeader("Cookie", "KoboSession=$cookieValue")
|
||||
|
@ -92,19 +76,18 @@ class APIClient {
|
|||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun syncGet(url: String, params: HashMap<String?, String?>?): Response {
|
||||
var urlBuilder: Builder = HttpUrl.parse(getAbsoluteUrl(url))!!.newBuilder()
|
||||
if (params != null) {
|
||||
for (key in params.keys) {
|
||||
urlBuilder = urlBuilder.setQueryParameter(key, params[key])
|
||||
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 = Builder().url(urlBuilder.build())
|
||||
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("Accept-Language", "en-GB,en-AU;q=0.7,en;q=0.3")
|
||||
.addHeader("X-Requested-With", "XMLHttpRequest")
|
||||
.addHeader("Cookie", "KoboSession=$cookieValue")
|
||||
.build()
|
||||
|
@ -112,13 +95,12 @@ class APIClient {
|
|||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun syncPost(url: String, payload: String, contentType: String?): Response {
|
||||
val body: RequestBody = RequestBody.create(payload, get.get(contentType))
|
||||
val request: Request = Builder().url(getAbsoluteUrl(url)).post(body)
|
||||
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("Accept-Language", "en-GB,en-AU;q=0.7,en;q=0.3")
|
||||
.addHeader("X-Requested-With", "XMLHttpRequest")
|
||||
.addHeader("Cookie", String.format("KoboSession=%s;", cookieValue))
|
||||
.addHeader("Content-type", contentType)
|
||||
|
@ -127,10 +109,10 @@ class APIClient {
|
|||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun syncPost(url: String, params: HashMap<String?, String?>): Response {
|
||||
var builder = Builder()
|
||||
fun syncPost(url: String, params: HashMap<String, String>): Response {
|
||||
var builder = FormBody.Builder()
|
||||
for (key in params.keys) {
|
||||
builder = builder.add(key, params[key])
|
||||
builder = builder.add(key, params[key].toString())
|
||||
}
|
||||
val formBody: FormBody = builder.build()
|
||||
return this.syncPost(url, formBody.toString(), "application/x-www-form-urlencoded")
|
||||
|
@ -138,17 +120,13 @@ class APIClient {
|
|||
|
||||
companion object {
|
||||
private const val BASE_URL = "https://www.kobo.com/"
|
||||
private var client: OkHttpClient
|
||||
private var client: OkHttpClient = OkHttpClient.Builder()
|
||||
.retryOnConnectionFailure(false)
|
||||
.build()
|
||||
private const val agent =
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0"
|
||||
|
||||
private fun getAbsoluteUrl(relativeUrl: String): String {
|
||||
return BASE_URL + relativeUrl
|
||||
}
|
||||
}
|
||||
|
||||
init {
|
||||
client = Builder().retryOnConnectionFailure(false)
|
||||
.build()
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue