diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/mangasync/base/MangaSyncService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/mangasync/base/MangaSyncService.kt index d646b04ce..10dc26086 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/mangasync/base/MangaSyncService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/mangasync/base/MangaSyncService.kt @@ -5,6 +5,7 @@ import eu.kanade.tachiyomi.App import eu.kanade.tachiyomi.data.database.models.MangaSync import eu.kanade.tachiyomi.data.network.NetworkHelper import eu.kanade.tachiyomi.data.preference.PreferencesHelper +import okhttp3.OkHttpClient import okhttp3.Response import rx.Observable import javax.inject.Inject @@ -18,6 +19,9 @@ abstract class MangaSyncService(private val context: Context, val id: Int) { App.get(context).component.inject(this) } + open val client: OkHttpClient + get() = networkService.client + // Name of the manga sync service to display abstract val name: String diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/mangasync/services/MyAnimeList.kt b/app/src/main/java/eu/kanade/tachiyomi/data/mangasync/services/MyAnimeList.kt index f07dd8953..29ed4ac40 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/mangasync/services/MyAnimeList.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/mangasync/services/MyAnimeList.kt @@ -8,6 +8,7 @@ import eu.kanade.tachiyomi.data.database.models.MangaSync import eu.kanade.tachiyomi.data.mangasync.base.MangaSyncService import eu.kanade.tachiyomi.data.network.GET import eu.kanade.tachiyomi.data.network.POST +import eu.kanade.tachiyomi.data.network.asObservable import eu.kanade.tachiyomi.util.selectInt import eu.kanade.tachiyomi.util.selectText import okhttp3.* @@ -16,12 +17,6 @@ import org.xmlpull.v1.XmlSerializer import rx.Observable import java.io.StringWriter -fun XmlSerializer.inTag(tag: String, body: String, namespace: String = "") { - startTag(namespace, tag) - text(body) - endTag(namespace, tag) -} - class MyAnimeList(private val context: Context, id: Int) : MangaSyncService(context, id) { private lateinit var headers: Headers @@ -65,7 +60,8 @@ class MyAnimeList(private val context: Context, id: Int) : MangaSyncService(cont override fun login(username: String, password: String): Observable { createHeaders(username, password) - return networkService.request(GET(getLoginUrl(), headers)) + return client.newCall(GET(getLoginUrl(), headers)) + .asObservable() .doOnNext { it.close() } .map { it.code() == 200 } } @@ -78,7 +74,8 @@ class MyAnimeList(private val context: Context, id: Int) : MangaSyncService(cont } fun search(query: String): Observable> { - return networkService.request(GET(getSearchUrl(query), headers)) + return client.newCall(GET(getSearchUrl(query), headers)) + .asObservable() .map { Jsoup.parse(it.body().string()) } .flatMap { Observable.from(it.select("entry")) } .filter { it.select("type").text() != "Novel" } @@ -103,7 +100,9 @@ class MyAnimeList(private val context: Context, id: Int) : MangaSyncService(cont // MAL doesn't support score with decimals fun getList(): Observable> { - return networkService.request(GET(getListUrl(username), headers), networkService.forceCacheClient) + return networkService.forceCacheClient + .newCall(GET(getListUrl(username), headers)) + .asObservable() .map { Jsoup.parse(it.body().string()) } .flatMap { Observable.from(it.select("manga")) } .map { @@ -131,7 +130,8 @@ class MyAnimeList(private val context: Context, id: Int) : MangaSyncService(cont if (manga.total_chapters != 0 && manga.last_chapter_read == manga.total_chapters) { manga.status = COMPLETED } - networkService.request(POST(getUpdateUrl(manga), headers, getMangaPostPayload(manga))) + client.newCall(POST(getUpdateUrl(manga), headers, getMangaPostPayload(manga))) + .asObservable() } } @@ -145,7 +145,8 @@ class MyAnimeList(private val context: Context, id: Int) : MangaSyncService(cont override fun add(manga: MangaSync): Observable { return Observable.defer { - networkService.request(POST(getAddUrl(manga), headers, getMangaPostPayload(manga))) + client.newCall(POST(getAddUrl(manga), headers, getMangaPostPayload(manga))) + .asObservable() } } @@ -177,6 +178,12 @@ class MyAnimeList(private val context: Context, id: Int) : MangaSyncService(cont return form.build() } + fun XmlSerializer.inTag(tag: String, body: String, namespace: String = "") { + startTag(namespace, tag) + text(body) + endTag(namespace, tag) + } + override fun bind(manga: MangaSync): Observable { return getList() .flatMap { diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/network/NetworkHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/data/network/NetworkHelper.kt index 0d6375f4f..4e95ed564 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/network/NetworkHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/network/NetworkHelper.kt @@ -3,9 +3,6 @@ package eu.kanade.tachiyomi.data.network import android.content.Context import okhttp3.Cache import okhttp3.OkHttpClient -import okhttp3.Request -import okhttp3.Response -import rx.Observable import java.io.File class NetworkHelper(context: Context) { @@ -38,8 +35,4 @@ class NetworkHelper(context: Context) { val cookies: PersistentCookieStore get() = cookieManager.store - fun request(request: Request, client: OkHttpClient = this.client): Observable { - return client.newCall(request).asObservable() - } - } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/source/online/OnlineSource.kt b/app/src/main/java/eu/kanade/tachiyomi/data/source/online/OnlineSource.kt index e7bc35de4..696d9e345 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/source/online/OnlineSource.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/source/online/OnlineSource.kt @@ -97,8 +97,9 @@ abstract class OnlineSource(context: Context) : Source { * @param page the page object where the information will be saved, like the list of manga, * the current page and the next page url. */ - open fun fetchPopularManga(page: MangasPage): Observable = network - .request(popularMangaRequest(page), client) + open fun fetchPopularManga(page: MangasPage): Observable = client + .newCall(popularMangaRequest(page)) + .asObservable() .map { response -> page.apply { mangas = mutableListOf() @@ -141,8 +142,9 @@ abstract class OnlineSource(context: Context) : Source { * the current page and the next page url. * @param query the search query. */ - open fun fetchSearchManga(page: MangasPage, query: String): Observable = network - .request(searchMangaRequest(page, query), client) + open fun fetchSearchManga(page: MangasPage, query: String): Observable = client + .newCall(searchMangaRequest(page, query)) + .asObservable() .map { response -> page.apply { mangas = mutableListOf() @@ -187,8 +189,9 @@ abstract class OnlineSource(context: Context) : Source { * * @param manga the manga to be updated. */ - override fun fetchMangaDetails(manga: Manga): Observable = network - .request(mangaDetailsRequest(manga), client) + override fun fetchMangaDetails(manga: Manga): Observable = client + .newCall(mangaDetailsRequest(manga)) + .asObservable() .map { response -> Manga.create(manga.url, id).apply { mangaDetailsParse(response, this) @@ -220,8 +223,9 @@ abstract class OnlineSource(context: Context) : Source { * * @param manga the manga to look for chapters. */ - override fun fetchChapterList(manga: Manga): Observable> = network - .request(chapterListRequest(manga), client) + override fun fetchChapterList(manga: Manga): Observable> = client + .newCall(chapterListRequest(manga)) + .asObservable() .map { response -> mutableListOf().apply { chapterListParse(response, this) @@ -265,8 +269,9 @@ abstract class OnlineSource(context: Context) : Source { * * @param chapter the chapter whose page list has to be fetched. */ - open fun fetchPageListFromNetwork(chapter: Chapter): Observable> = network - .request(pageListRequest(chapter), client) + open fun fetchPageListFromNetwork(chapter: Chapter): Observable> = client + .newCall(pageListRequest(chapter)) + .asObservable() .map { response -> mutableListOf().apply { pageListParse(response, this) @@ -307,8 +312,9 @@ abstract class OnlineSource(context: Context) : Source { */ open protected fun fetchImageUrl(page: Page): Observable { page.status = Page.LOAD_PAGE - return network - .request(imageUrlRequest(page), client) + return client + .newCall(imageUrlRequest(page)) + .asObservable() .map { imageUrlParse(it) } .doOnError { page.status = Page.ERROR } .onErrorReturn { null } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/source/online/english/Batoto.kt b/app/src/main/java/eu/kanade/tachiyomi/data/source/online/english/Batoto.kt index 0615db32c..91c7036e2 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/source/online/english/Batoto.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/source/online/english/Batoto.kt @@ -7,6 +7,7 @@ import eu.kanade.tachiyomi.data.database.models.Chapter import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.network.GET import eu.kanade.tachiyomi.data.network.POST +import eu.kanade.tachiyomi.data.network.asObservable import eu.kanade.tachiyomi.data.source.EN import eu.kanade.tachiyomi.data.source.Language import eu.kanade.tachiyomi.data.source.model.MangasPage @@ -215,7 +216,8 @@ class Batoto(context: Context, override val id: Int) : ParsedOnlineSource(contex } override fun login(username: String, password: String) = - network.request(GET("$baseUrl/forums/index.php?app=core&module=global§ion=login", headers)) + client.newCall(GET("$baseUrl/forums/index.php?app=core&module=global§ion=login", headers)) + .asObservable() .flatMap { doLogin(it.body().string(), username, password) } .map { isAuthenticationSuccessful(it) } @@ -233,7 +235,7 @@ class Batoto(context: Context, override val id: Int) : ParsedOnlineSource(contex add("rememberMe", "1") }.build() - return network.request(POST(url, headers, payload)) + return client.newCall(POST(url, headers, payload)).asObservable() } override fun isLoginRequired() = true