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.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

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.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<Boolean> {
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<List<MangaSync>> {
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<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()) }
.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<Response> {
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<Response> {
return getList()
.flatMap {

View file

@ -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<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,
* the current page and the next page url.
*/
open fun fetchPopularManga(page: MangasPage): Observable<MangasPage> = network
.request(popularMangaRequest(page), client)
open fun fetchPopularManga(page: MangasPage): Observable<MangasPage> = client
.newCall(popularMangaRequest(page))
.asObservable()
.map { response ->
page.apply {
mangas = mutableListOf<Manga>()
@ -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<MangasPage> = network
.request(searchMangaRequest(page, query), client)
open fun fetchSearchManga(page: MangasPage, query: String): Observable<MangasPage> = client
.newCall(searchMangaRequest(page, query))
.asObservable()
.map { response ->
page.apply {
mangas = mutableListOf<Manga>()
@ -187,8 +189,9 @@ abstract class OnlineSource(context: Context) : Source {
*
* @param manga the manga to be updated.
*/
override fun fetchMangaDetails(manga: Manga): Observable<Manga> = network
.request(mangaDetailsRequest(manga), client)
override fun fetchMangaDetails(manga: Manga): Observable<Manga> = 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<List<Chapter>> = network
.request(chapterListRequest(manga), client)
override fun fetchChapterList(manga: Manga): Observable<List<Chapter>> = client
.newCall(chapterListRequest(manga))
.asObservable()
.map { response ->
mutableListOf<Chapter>().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<List<Page>> = network
.request(pageListRequest(chapter), client)
open fun fetchPageListFromNetwork(chapter: Chapter): Observable<List<Page>> = client
.newCall(pageListRequest(chapter))
.asObservable()
.map { response ->
mutableListOf<Page>().apply {
pageListParse(response, this)
@ -307,8 +312,9 @@ abstract class OnlineSource(context: Context) : Source {
*/
open protected fun fetchImageUrl(page: Page): Observable<Page> {
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 }

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.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&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) }
.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