All network calls are now done directly through the client

This commit is contained in:
len 2016-05-27 17:17:30 +02:00
parent efd36388b0
commit 8bd20c39aa
5 changed files with 44 additions and 32 deletions

View File

@ -5,6 +5,7 @@ import eu.kanade.tachiyomi.App
import eu.kanade.tachiyomi.data.database.models.MangaSync import eu.kanade.tachiyomi.data.database.models.MangaSync
import eu.kanade.tachiyomi.data.network.NetworkHelper import eu.kanade.tachiyomi.data.network.NetworkHelper
import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import okhttp3.OkHttpClient
import okhttp3.Response import okhttp3.Response
import rx.Observable import rx.Observable
import javax.inject.Inject import javax.inject.Inject
@ -18,6 +19,9 @@ abstract class MangaSyncService(private val context: Context, val id: Int) {
App.get(context).component.inject(this) App.get(context).component.inject(this)
} }
open val client: OkHttpClient
get() = networkService.client
// Name of the manga sync service to display // Name of the manga sync service to display
abstract val name: String abstract val name: String

View File

@ -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.mangasync.base.MangaSyncService
import eu.kanade.tachiyomi.data.network.GET import eu.kanade.tachiyomi.data.network.GET
import eu.kanade.tachiyomi.data.network.POST 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.selectInt
import eu.kanade.tachiyomi.util.selectText import eu.kanade.tachiyomi.util.selectText
import okhttp3.* import okhttp3.*
@ -16,12 +17,6 @@ import org.xmlpull.v1.XmlSerializer
import rx.Observable import rx.Observable
import java.io.StringWriter 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) { class MyAnimeList(private val context: Context, id: Int) : MangaSyncService(context, id) {
private lateinit var headers: Headers 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<Boolean> { override fun login(username: String, password: String): Observable<Boolean> {
createHeaders(username, password) createHeaders(username, password)
return networkService.request(GET(getLoginUrl(), headers)) return client.newCall(GET(getLoginUrl(), headers))
.asObservable()
.doOnNext { it.close() } .doOnNext { it.close() }
.map { it.code() == 200 } .map { it.code() == 200 }
} }
@ -78,7 +74,8 @@ class MyAnimeList(private val context: Context, id: Int) : MangaSyncService(cont
} }
fun search(query: String): Observable<List<MangaSync>> { fun search(query: String): Observable<List<MangaSync>> {
return networkService.request(GET(getSearchUrl(query), headers)) return client.newCall(GET(getSearchUrl(query), headers))
.asObservable()
.map { Jsoup.parse(it.body().string()) } .map { Jsoup.parse(it.body().string()) }
.flatMap { Observable.from(it.select("entry")) } .flatMap { Observable.from(it.select("entry")) }
.filter { it.select("type").text() != "Novel" } .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 // MAL doesn't support score with decimals
fun getList(): Observable<List<MangaSync>> { fun getList(): Observable<List<MangaSync>> {
return networkService.request(GET(getListUrl(username), headers), networkService.forceCacheClient) return networkService.forceCacheClient
.newCall(GET(getListUrl(username), headers))
.asObservable()
.map { Jsoup.parse(it.body().string()) } .map { Jsoup.parse(it.body().string()) }
.flatMap { Observable.from(it.select("manga")) } .flatMap { Observable.from(it.select("manga")) }
.map { .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) { if (manga.total_chapters != 0 && manga.last_chapter_read == manga.total_chapters) {
manga.status = COMPLETED 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<Response> { override fun add(manga: MangaSync): Observable<Response> {
return Observable.defer { 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() 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<Response> { override fun bind(manga: MangaSync): Observable<Response> {
return getList() return getList()
.flatMap { .flatMap {

View File

@ -3,9 +3,6 @@ package eu.kanade.tachiyomi.data.network
import android.content.Context import android.content.Context
import okhttp3.Cache import okhttp3.Cache
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import rx.Observable
import java.io.File import java.io.File
class NetworkHelper(context: Context) { class NetworkHelper(context: Context) {
@ -38,8 +35,4 @@ class NetworkHelper(context: Context) {
val cookies: PersistentCookieStore val cookies: PersistentCookieStore
get() = cookieManager.store get() = cookieManager.store
fun request(request: Request, client: OkHttpClient = this.client): Observable<Response> {
return client.newCall(request).asObservable()
}
} }

View File

@ -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, * @param page the page object where the information will be saved, like the list of manga,
* the current page and the next page url. * the current page and the next page url.
*/ */
open fun fetchPopularManga(page: MangasPage): Observable<MangasPage> = network open fun fetchPopularManga(page: MangasPage): Observable<MangasPage> = client
.request(popularMangaRequest(page), client) .newCall(popularMangaRequest(page))
.asObservable()
.map { response -> .map { response ->
page.apply { page.apply {
mangas = mutableListOf<Manga>() mangas = mutableListOf<Manga>()
@ -141,8 +142,9 @@ abstract class OnlineSource(context: Context) : Source {
* the current page and the next page url. * the current page and the next page url.
* @param query the search query. * @param query the search query.
*/ */
open fun fetchSearchManga(page: MangasPage, query: String): Observable<MangasPage> = network open fun fetchSearchManga(page: MangasPage, query: String): Observable<MangasPage> = client
.request(searchMangaRequest(page, query), client) .newCall(searchMangaRequest(page, query))
.asObservable()
.map { response -> .map { response ->
page.apply { page.apply {
mangas = mutableListOf<Manga>() mangas = mutableListOf<Manga>()
@ -187,8 +189,9 @@ abstract class OnlineSource(context: Context) : Source {
* *
* @param manga the manga to be updated. * @param manga the manga to be updated.
*/ */
override fun fetchMangaDetails(manga: Manga): Observable<Manga> = network override fun fetchMangaDetails(manga: Manga): Observable<Manga> = client
.request(mangaDetailsRequest(manga), client) .newCall(mangaDetailsRequest(manga))
.asObservable()
.map { response -> .map { response ->
Manga.create(manga.url, id).apply { Manga.create(manga.url, id).apply {
mangaDetailsParse(response, this) mangaDetailsParse(response, this)
@ -220,8 +223,9 @@ abstract class OnlineSource(context: Context) : Source {
* *
* @param manga the manga to look for chapters. * @param manga the manga to look for chapters.
*/ */
override fun fetchChapterList(manga: Manga): Observable<List<Chapter>> = network override fun fetchChapterList(manga: Manga): Observable<List<Chapter>> = client
.request(chapterListRequest(manga), client) .newCall(chapterListRequest(manga))
.asObservable()
.map { response -> .map { response ->
mutableListOf<Chapter>().apply { mutableListOf<Chapter>().apply {
chapterListParse(response, this) chapterListParse(response, this)
@ -265,8 +269,9 @@ abstract class OnlineSource(context: Context) : Source {
* *
* @param chapter the chapter whose page list has to be fetched. * @param chapter the chapter whose page list has to be fetched.
*/ */
open fun fetchPageListFromNetwork(chapter: Chapter): Observable<List<Page>> = network open fun fetchPageListFromNetwork(chapter: Chapter): Observable<List<Page>> = client
.request(pageListRequest(chapter), client) .newCall(pageListRequest(chapter))
.asObservable()
.map { response -> .map { response ->
mutableListOf<Page>().apply { mutableListOf<Page>().apply {
pageListParse(response, this) pageListParse(response, this)
@ -307,8 +312,9 @@ abstract class OnlineSource(context: Context) : Source {
*/ */
open protected fun fetchImageUrl(page: Page): Observable<Page> { open protected fun fetchImageUrl(page: Page): Observable<Page> {
page.status = Page.LOAD_PAGE page.status = Page.LOAD_PAGE
return network return client
.request(imageUrlRequest(page), client) .newCall(imageUrlRequest(page))
.asObservable()
.map { imageUrlParse(it) } .map { imageUrlParse(it) }
.doOnError { page.status = Page.ERROR } .doOnError { page.status = Page.ERROR }
.onErrorReturn { null } .onErrorReturn { null }

View File

@ -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.database.models.Manga
import eu.kanade.tachiyomi.data.network.GET import eu.kanade.tachiyomi.data.network.GET
import eu.kanade.tachiyomi.data.network.POST 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.EN
import eu.kanade.tachiyomi.data.source.Language import eu.kanade.tachiyomi.data.source.Language
import eu.kanade.tachiyomi.data.source.model.MangasPage 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) = override fun login(username: String, password: String) =
network.request(GET("$baseUrl/forums/index.php?app=core&module=global&section=login", headers)) client.newCall(GET("$baseUrl/forums/index.php?app=core&module=global&section=login", headers))
.asObservable()
.flatMap { doLogin(it.body().string(), username, password) } .flatMap { doLogin(it.body().string(), username, password) }
.map { isAuthenticationSuccessful(it) } .map { isAuthenticationSuccessful(it) }
@ -233,7 +235,7 @@ class Batoto(context: Context, override val id: Int) : ParsedOnlineSource(contex
add("rememberMe", "1") add("rememberMe", "1")
}.build() }.build()
return network.request(POST(url, headers, payload)) return client.newCall(POST(url, headers, payload)).asObservable()
} }
override fun isLoginRequired() = true override fun isLoginRequired() = true