Skip to content

Commit

Permalink
Merge pull request #1026 from toastkidjp/refactor/#605_data
Browse files Browse the repository at this point in the history
Refactor/#605 data
  • Loading branch information
toastkidjp committed Jun 20, 2023
2 parents 3e154ce + 42d4213 commit 60326e3
Show file tree
Hide file tree
Showing 121 changed files with 657 additions and 392 deletions.
6 changes: 6 additions & 0 deletions api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import property.BuildTool
import property.LibraryVersion

plugins {
id("org.jetbrains.kotlin.plugin.serialization") version("1.6.21")
id("com.android.library")
id("kotlin-android")
id("jacoco.definition")
Expand Down Expand Up @@ -36,8 +37,13 @@ android {
}

dependencies {
implementation("androidx.core:core-ktx:${LibraryVersion.ktx}")
implementation("com.squareup.okhttp3:okhttp:${LibraryVersion.okhttp}")
implementation("com.jakewharton.timber:timber:${LibraryVersion.timber}")
implementation("com.squareup.retrofit2:retrofit:2.6.2")
implementation("org.jsoup:jsoup:${LibraryVersion.jsoup}")
implementation("com.jakewharton.retrofit:retrofit2-kotlinx-serialization-converter:0.8.0")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.3")

testImplementation("junit:junit:${LibraryVersion.junit}")
testImplementation("io.mockk:mockk:${LibraryVersion.mockk}")
Expand Down
19 changes: 17 additions & 2 deletions api/src/main/java/jp/toastkid/api/DownloadApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,30 @@
package jp.toastkid.api

import jp.toastkid.api.lib.HttpClientFactory
import okhttp3.Call
import okhttp3.Callback
import okhttp3.Request
import okhttp3.Response
import timber.log.Timber
import java.io.IOException
import java.io.InputStream

class DownloadApi {

private val httpClient = HttpClientFactory().withTimeout(3)

operator fun invoke(url: String, callback: Callback) {
httpClient.newCall(Request.Builder().url(url).get().build()).enqueue(callback)
operator fun invoke(url: String, callback: (InputStream?) -> Unit) {
httpClient.newCall(Request.Builder().url(url).get().build()).enqueue(object : Callback {

override fun onFailure(call: Call, e: IOException) {
Timber.e(e)
}

override fun onResponse(call: Call, response: Response) {
callback(response.body?.byteStream())
}

})
}

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
/*
* Copyright (c) 2019 toastkidjp.
* Copyright (c) 2023 toastkidjp.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html.
*/
package jp.toastkid.rss.api
package jp.toastkid.api.rss

import jp.toastkid.rss.RssResponseConverter
import jp.toastkid.rss.model.Parser
import jp.toastkid.api.rss.model.Parser
import okhttp3.ResponseBody
import retrofit2.Converter
import retrofit2.Retrofit
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/*
* Copyright (c) 2019 toastkidjp.
* Copyright (c) 2023 toastkidjp.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html.
*/
package jp.toastkid.rss.api
package jp.toastkid.api.rss

import androidx.annotation.WorkerThread
import androidx.core.net.toUri
import jp.toastkid.rss.model.Rss
import jp.toastkid.api.rss.model.Rss
import retrofit2.Retrofit

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/*
* Copyright (c) 2019 toastkidjp.
* Copyright (c) 2023 toastkidjp.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html.
*/
package jp.toastkid.rss
package jp.toastkid.api.rss

import jp.toastkid.rss.model.Parser
import jp.toastkid.rss.model.Rss
import jp.toastkid.api.rss.model.Parser
import jp.toastkid.api.rss.model.Rss
import okhttp3.ResponseBody
import retrofit2.Converter

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/*
* Copyright (c) 2019 toastkidjp.
* Copyright (c) 2023 toastkidjp.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html.
*/
package jp.toastkid.rss.api
package jp.toastkid.api.rss

import jp.toastkid.rss.model.Rss
import jp.toastkid.api.rss.model.Rss
import retrofit2.Call
import retrofit2.http.GET
import retrofit2.http.Url
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html.
*/
package jp.toastkid.rss.model
package jp.toastkid.api.rss.model

/**
* @author toastkidjp
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* Copyright (c) 2019 toastkidjp.
* Copyright (c) 2023 toastkidjp.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html.
*/
package jp.toastkid.rss.model
package jp.toastkid.api.rss.model

import java.util.regex.Pattern

Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
/*
* Copyright (c) 2019 toastkidjp.
* Copyright (c) 2023 toastkidjp.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html.
*/
package jp.toastkid.rss.model
package jp.toastkid.api.rss.model

/**
* @author toastkidjp
*/
data class Rss(
/** RSS' title. */
/** RSS' title. */
var title: String? = null,

/** RSS' subjects. */
/** RSS' subjects. */
var subjects: MutableList<String> = mutableListOf(),

/** RSS' creator. */
/** RSS' creator. */
var creator: String? = null,

/** RSS' date. */
/** RSS' date. */
var date: String? = null,

/** RSS' items. */
/** RSS' items. */
var items: MutableList<Item> = mutableListOf(),

/** RSS' Link. */
/** RSS' Link. */
var link: String? = null,

/** RSS' URL. */
/** RSS' URL. */
var url: String? = null,

/** RSS' description. */
/** RSS' description. */
var description: String? = null
) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html.
*/
package jp.toastkid.yobidashi.search.trend
package jp.toastkid.api.trend

/**
* @author toastkidjp
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* Copyright (c) 2019 toastkidjp.
* Copyright (c) 2023 toastkidjp.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html.
*/
package jp.toastkid.yobidashi.search.trend
package jp.toastkid.api.trend

import androidx.annotation.WorkerThread
import retrofit2.Retrofit
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* Copyright (c) 2019 toastkidjp.
* Copyright (c) 2023 toastkidjp.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html.
*/
package jp.toastkid.yobidashi.search.trend
package jp.toastkid.api.trend

import org.jsoup.Jsoup
import org.jsoup.parser.Parser
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* Copyright (c) 2019 toastkidjp.
* Copyright (c) 2023 toastkidjp.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html.
*/
package jp.toastkid.yobidashi.search.trend
package jp.toastkid.api.trend

import okhttp3.ResponseBody
import retrofit2.Converter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* Copyright (c) 2019 toastkidjp.
* Copyright (c) 2023 toastkidjp.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html.
*/
package jp.toastkid.yobidashi.search.trend
package jp.toastkid.api.trend

import okhttp3.ResponseBody
import retrofit2.Converter
Expand All @@ -18,9 +18,9 @@ import java.lang.reflect.Type
class TrendResponseConverterFactory private constructor(): Converter.Factory() {

override fun responseBodyConverter(
type: Type?,
annotations: Array<Annotation>?,
retrofit: Retrofit?
type: Type,
annotations: Array<out Annotation>,
retrofit: Retrofit
): Converter<ResponseBody, *> = TrendResponseConverter(TrendParser())

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* Copyright (c) 2019 toastkidjp.
* Copyright (c) 2023 toastkidjp.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html.
*/
package jp.toastkid.yobidashi.search.trend
package jp.toastkid.api.trend

import retrofit2.Call
import retrofit2.http.GET
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
/*
* Copyright (c) 2019 toastkidjp.
* Copyright (c) 2023 toastkidjp.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html.
*/
package jp.toastkid.yobidashi.wikipedia.random
package jp.toastkid.api.wikipedia

import androidx.annotation.WorkerThread
import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
import jp.toastkid.yobidashi.wikipedia.random.model.Article
import jp.toastkid.api.wikipedia.model.Article
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.Json
import okhttp3.MediaType.Companion.toMediaType
import retrofit2.Retrofit
Expand All @@ -20,21 +21,24 @@ import retrofit2.Retrofit
* @author toastkidjp
*/
class WikipediaApi(
/**
* Wikipedia URL decider.
*/
private val urlDecider: UrlDecider = UrlDecider()
/**
* Wikipedia URL decider.
*/
private val baseUrl: String
) {

private val json = Json { ignoreUnknownKeys = true }

/**
* You should call this method on worker-thread.
*/
@OptIn(ExperimentalSerializationApi::class)
@WorkerThread
operator fun invoke(): Array<Article>? {
val retrofit = Retrofit.Builder()
.baseUrl(urlDecider())
.baseUrl(baseUrl)
.addConverterFactory(
Json {ignoreUnknownKeys = true }
json
.asConverterFactory("application/json".toMediaType())
)
.build()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/*
* Copyright (c) 2019 toastkidjp.
* Copyright (c) 2023 toastkidjp.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html.
*/
package jp.toastkid.yobidashi.wikipedia.random
package jp.toastkid.api.wikipedia

import jp.toastkid.yobidashi.wikipedia.random.model.Response
import jp.toastkid.api.wikipedia.model.Response
import retrofit2.Call
import retrofit2.http.GET

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html.
*/
package jp.toastkid.yobidashi.wikipedia.random.model
package jp.toastkid.api.wikipedia.model

import kotlinx.serialization.Serializable

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html.
*/
package jp.toastkid.yobidashi.wikipedia.random.model
package jp.toastkid.api.wikipedia.model

import kotlinx.serialization.Serializable

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html.
*/
package jp.toastkid.yobidashi.wikipedia.random.model
package jp.toastkid.api.wikipedia.model

import kotlinx.serialization.Serializable

Expand Down

0 comments on commit 60326e3

Please sign in to comment.