From 51f64575379c0adace2de826fe9b55034ec095f3 Mon Sep 17 00:00:00 2001 From: William Brawner Date: Sun, 30 May 2021 20:51:49 -0600 Subject: [PATCH] Convert APIClient to Kotlin --- .../com/joshsharp/wishkobone/APIClient.java | 158 ------------------ .../au/com/joshsharp/wishkobone/APIClient.kt | 154 +++++++++++++++++ 2 files changed, 154 insertions(+), 158 deletions(-) delete mode 100644 app/src/main/java/au/com/joshsharp/wishkobone/APIClient.java create mode 100644 app/src/main/java/au/com/joshsharp/wishkobone/APIClient.kt diff --git a/app/src/main/java/au/com/joshsharp/wishkobone/APIClient.java b/app/src/main/java/au/com/joshsharp/wishkobone/APIClient.java deleted file mode 100644 index 5d2b377..0000000 --- a/app/src/main/java/au/com/joshsharp/wishkobone/APIClient.java +++ /dev/null @@ -1,158 +0,0 @@ -package au.com.joshsharp.wishkobone; - -import android.content.Context; -import android.util.Log; - -import java.io.IOException; -import java.time.Duration; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; - -import org.jetbrains.annotations.NotNull; - -import okhttp3.Callback; -import okhttp3.Cookie; -import okhttp3.CookieJar; -import okhttp3.FormBody; -import okhttp3.HttpUrl; -import okhttp3.MediaType; -import okhttp3.OkHttpClient; -import okhttp3.Request; -import okhttp3.RequestBody; -import okhttp3.Response; - - -/** - * Created by Josh on 2/09/2014. - */ -public class APIClient { - private static final String BASE_URL = "https://www.kobo.com/"; - private static OkHttpClient client; - private static String agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0"; - private String cookieValue; - - public APIClient() { - client = new OkHttpClient.Builder().retryOnConnectionFailure(false) - .build(); - - } - - public void setCookie(String value){ - cookieValue = value; - } - - public void get(String url, HashMap params, Callback responseHandler) { - HttpUrl.Builder urlBuilder = HttpUrl.parse(getAbsoluteUrl(url)).newBuilder(); - if (params != null) { - for (String key : params.keySet()) { - urlBuilder = urlBuilder.setQueryParameter(key, params.get(key)); - } - } - - Request request = new 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(); - client.newCall(request).enqueue(responseHandler); - } - - public void post(String url, @NotNull HashMap params, Callback responseHandler) { - this.post(url, params, "application/x-www-form-urlencoded", responseHandler); - } - - public void post(String url, @NotNull HashMap params, String contentType, Callback responseHandler) { - FormBody.Builder builder = new FormBody.Builder(); - for (String key : params.keySet()) { - builder = builder.add(key, params.get(key)); - } - - FormBody formBody = builder.build(); - - Request request = new 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) - .build(); - client.newCall(request).enqueue(responseHandler); - - } - - public void post(String url, @NotNull String payload, String contentType, Callback responseHandler){ - - RequestBody body = RequestBody.create(payload, MediaType.get(contentType)); - - Request request = new 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) - .addHeader("Content-type", contentType) - .build(); - client.newCall(request).enqueue(responseHandler); - } - - public Response syncGet(String url, HashMap params) throws IOException { - HttpUrl.Builder urlBuilder = HttpUrl.parse(getAbsoluteUrl(url)).newBuilder(); - if (params != null) { - for (String key : params.keySet()) { - urlBuilder = urlBuilder.setQueryParameter(key, params.get(key)); - } - } - - Request request = new 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(); - return client.newCall(request).execute(); - } - - public Response syncPost(String url, @NotNull String payload, String contentType) throws IOException { - - - RequestBody body = RequestBody.create(payload, MediaType.get(contentType)); - - Request request = new 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) - .build(); - return client.newCall(request).execute(); - } - - public Response syncPost(String url, @NotNull HashMap params) throws IOException { - FormBody.Builder builder = new FormBody.Builder(); - for (String key : params.keySet()) { - builder = builder.add(key, params.get(key)); - } - - FormBody formBody = builder.build(); - - return this.syncPost(url, formBody.toString(), "application/x-www-form-urlencoded"); - } - - private static String getAbsoluteUrl(String relativeUrl) { - return BASE_URL + relativeUrl; - } -} diff --git a/app/src/main/java/au/com/joshsharp/wishkobone/APIClient.kt b/app/src/main/java/au/com/joshsharp/wishkobone/APIClient.kt new file mode 100644 index 0000000..123346f --- /dev/null +++ b/app/src/main/java/au/com/joshsharp/wishkobone/APIClient.kt @@ -0,0 +1,154 @@ +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 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 + } + + operator fun get(url: String, params: HashMap?, responseHandler: Callback?) { + var urlBuilder: Builder = HttpUrl.parse(getAbsoluteUrl(url))!!.newBuilder() + if (params != null) { + for (key in params.keys) { + urlBuilder = urlBuilder.setQueryParameter(key, params[key]) + } + } + val 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() + client.newCall(request).enqueue(responseHandler!!) + } + + fun post(url: String, params: HashMap, responseHandler: Callback?) { + this.post(url, params, "application/x-www-form-urlencoded", responseHandler) + } + + fun post( + url: String, + params: HashMap, + contentType: String?, + responseHandler: Callback? + ) { + var builder = Builder() + for (key in params.keys) { + builder = builder.add(key, params[key]) + } + val formBody: FormBody = builder.build() + val 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) + .build() + 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) + .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") + .addHeader("Content-type", contentType) + .build() + client.newCall(request).enqueue(responseHandler!!) + } + + @Throws(IOException::class) + fun syncGet(url: String, params: HashMap?): Response { + var urlBuilder: Builder = HttpUrl.parse(getAbsoluteUrl(url))!!.newBuilder() + if (params != null) { + for (key in params.keys) { + urlBuilder = urlBuilder.setQueryParameter(key, params[key]) + } + } + val 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() + return client.newCall(request).execute() + } + + @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) + .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) + .build() + return client.newCall(request).execute() + } + + @Throws(IOException::class) + fun syncPost(url: String, params: HashMap): Response { + var builder = Builder() + for (key in params.keys) { + builder = builder.add(key, params[key]) + } + val formBody: FormBody = builder.build() + return this.syncPost(url, formBody.toString(), "application/x-www-form-urlencoded") + } + + companion object { + private const val BASE_URL = "https://www.kobo.com/" + private var client: OkHttpClient + 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() + } +} \ No newline at end of file