-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* app ๋ชจ๋์ domain ๋ชจ๋์ ์์กดํด์ผ ํ๋ค. * step2 README ์์ฑ * domain ๋ชจ๋ Model, Repository ์์ฑ * domain - ๋คํธ์ํฌ ๊ฒฐ๊ณผ ํธ๋ค๋ง ๋ก์ง ์์ฑ * data - Response ์์ฑ * data - Service ์์ฑ * data - ๊ธฐ๋ณธ Retrofit, Moshi Converter ์ค์ * data - RetrofitProvider ์์ฑ * data - ๋คํธ์ํฌ ์๋ฌ ํธ๋ค๋ง ๋ก์ง ์์ฑ * data - DefaultRepository ๊ตฌํ * data - mapper ์์ฑ * data - MockWebServer๋ฅผ ์ด์ฉํ HTTP ์์ฒญ ํ ์คํธ * data - ๊ตฌํ์ฒด internal --------- Co-authored-by: jayden.d <[email protected]>
- Loading branch information
Showing
18 changed files
with
6,948 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
# TODO | ||
## Best Practice | ||
## Step1 | ||
## Step2 | ||
|
||
### ์ด๋ฒ ๋ฏธ์ TODO | ||
- ์์ ์ฝํ๋ฆฐ ๋ชจ๋์ธ domain ๋ชจ๋์ ๋ง๋ ๋ค. | ||
- ์์ ์ฝํ๋ฆฐ ๋ชจ๋์ธ data ๋ชจ๋์ ๋ง๋ ๋ค. | ||
- data ๋ชจ๋์ domain ๋ชจ๋์ ์์กดํด์ผ ํ๋ค. | ||
- app ๋ชจ๋์ domain ๋ชจ๋์ ์์กดํด์ผ ํ๋ค. | ||
- Repository ๋ชฉ๋ก์ ๊ฐ์ ธ์ค๋ ๊ธฐ๋ฅ์ data ๋ชจ๋์ ๊ตฌํ๋์ด์ผ ํ๋ค. | ||
- data ๋ชจ๋์ ๊ตฌํ์ฒด๋ ๋ชจ๋ internal class๋ก ์ ์ธํ๋ค. | ||
- HTTP ์์ฒญ์ ํตํด ๊ฐ์ ธ์ค๋ ๊ตฌํ์ฒด์ ๋ํ ํ ์คํธ ์ฝ๋๋ฅผ ์์ฑํ๋ค. | ||
- OkHttp MockWebServer ์ด์ฉ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
data/src/main/java/camp/nextstep/edu/github/data/Mapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* @author Daewon on 01,September,2023 | ||
* | ||
*/ | ||
|
||
package camp.nextstep.edu.github.data | ||
|
||
import camp.nextstep.edu.github.data.response.GithubRepositoryResponse | ||
import camp.nextstep.edu.github.domain.model.GithubRepository | ||
|
||
internal fun List<GithubRepositoryResponse>.toDomainModels(): List<GithubRepository> { | ||
return this.map { it.toDomainModel() } | ||
} | ||
|
||
internal fun GithubRepositoryResponse.toDomainModel(): GithubRepository { | ||
return GithubRepository( | ||
fullName = fullName, | ||
description = description | ||
) | ||
} |
13 changes: 13 additions & 0 deletions
13
data/src/main/java/camp/nextstep/edu/github/data/di/NetworkProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package camp.nextstep.edu.github.data.di | ||
|
||
import camp.nextstep.edu.github.data.retrofit.BASE_URL | ||
import camp.nextstep.edu.github.data.retrofit.GithubService | ||
import camp.nextstep.edu.github.data.retrofit.RetrofitNetwork | ||
|
||
internal object NetworkProvider { | ||
fun provideGithubService(baseUrl: String = BASE_URL): GithubService { | ||
return RetrofitNetwork | ||
.create(baseUrl) | ||
.create(GithubService::class.java) | ||
} | ||
} |
Empty file.
22 changes: 22 additions & 0 deletions
22
data/src/main/java/camp/nextstep/edu/github/data/network/DefaultNetworkRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* @author Daewon on 31,August,2023 | ||
* | ||
*/ | ||
|
||
package camp.nextstep.edu.github.data.network | ||
|
||
import camp.nextstep.edu.github.data.retrofit.GithubService | ||
import camp.nextstep.edu.github.data.retrofit.networkResult | ||
import camp.nextstep.edu.github.data.toDomainModels | ||
import camp.nextstep.edu.github.domain.Result | ||
import camp.nextstep.edu.github.domain.model.GithubRepository | ||
import camp.nextstep.edu.github.domain.repository.NetworkRepository | ||
|
||
|
||
internal class DefaultNetworkRepository( | ||
private val githubService: GithubService | ||
) : NetworkRepository { | ||
override suspend fun getRepositories(): Result<List<GithubRepository>> = | ||
networkResult { githubService.getRepositories().toDomainModels() } | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
data/src/main/java/camp/nextstep/edu/github/data/response/GithubRepositoryResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package camp.nextstep.edu.github.data.response | ||
|
||
import com.squareup.moshi.Json | ||
|
||
internal data class GithubRepositoryResponse( | ||
@Json(name = "full_name") | ||
val fullName: String, | ||
@Json(name = "description") | ||
val description: String? | ||
) |
15 changes: 15 additions & 0 deletions
15
data/src/main/java/camp/nextstep/edu/github/data/retrofit/GithubService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* @author Daewon on 31,August,2023 | ||
* | ||
*/ | ||
|
||
package camp.nextstep.edu.github.data.retrofit | ||
|
||
import camp.nextstep.edu.github.data.response.GithubRepositoryResponse | ||
import retrofit2.http.GET | ||
|
||
|
||
internal interface GithubService { | ||
@GET("repositories") | ||
suspend fun getRepositories(): List<GithubRepositoryResponse> | ||
} |
14 changes: 14 additions & 0 deletions
14
data/src/main/java/camp/nextstep/edu/github/data/retrofit/NetworkResult.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* @author Daewon on 22,April,2023 | ||
* | ||
*/ | ||
|
||
package camp.nextstep.edu.github.data.retrofit | ||
|
||
import camp.nextstep.edu.github.domain.Result | ||
|
||
internal inline fun <T> networkResult(transform: () -> T): Result<T> = try { | ||
Result.Success(transform.invoke()) | ||
} catch (e: Exception) { | ||
Result.Error(e) | ||
} |
35 changes: 35 additions & 0 deletions
35
data/src/main/java/camp/nextstep/edu/github/data/retrofit/RetrofitNetwork.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package camp.nextstep.edu.github.data.retrofit | ||
|
||
import com.squareup.moshi.Moshi | ||
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory | ||
import okhttp3.OkHttpClient | ||
import okhttp3.logging.HttpLoggingInterceptor | ||
import retrofit2.Converter | ||
import retrofit2.Retrofit | ||
import retrofit2.converter.moshi.MoshiConverterFactory | ||
|
||
const val BASE_URL = "https://api.github.com/" | ||
|
||
internal object RetrofitNetwork { | ||
|
||
private fun createOkhttpClient(): OkHttpClient { | ||
return OkHttpClient.Builder().apply { | ||
addInterceptor(HttpLoggingInterceptor()) | ||
}.build() | ||
} | ||
|
||
private fun createJsonAdapterFactory(): Converter.Factory = | ||
MoshiConverterFactory.create( | ||
Moshi.Builder() | ||
.add(KotlinJsonAdapterFactory()) | ||
.build() | ||
) | ||
|
||
fun create(baseUrl: String): Retrofit { | ||
return Retrofit.Builder() | ||
.client(createOkhttpClient()) | ||
.baseUrl(baseUrl) | ||
.addConverterFactory(createJsonAdapterFactory()) | ||
.build() | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
data/src/test/kotlin/camp/nextstep/edu/github/data/GithubServiceTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/** | ||
* @author Daewon on 01,September,2023 | ||
* | ||
*/ | ||
|
||
package camp.nextstep.edu.github.data | ||
|
||
import camp.nextstep.edu.github.data.di.NetworkProvider | ||
import camp.nextstep.edu.github.data.response.GithubRepositoryResponse | ||
import camp.nextstep.edu.github.data.retrofit.GithubService | ||
import com.google.common.truth.Truth.assertThat | ||
import kotlinx.coroutines.test.runTest | ||
import okhttp3.mockwebserver.MockResponse | ||
import okhttp3.mockwebserver.MockWebServer | ||
import org.junit.Before | ||
import org.junit.Test | ||
import java.io.File | ||
|
||
|
||
class GithubServiceTest { | ||
|
||
private lateinit var server: MockWebServer | ||
private lateinit var githubService: GithubService | ||
|
||
@Before | ||
fun setUp() { | ||
server = MockWebServer() | ||
githubService = NetworkProvider.provideGithubService(server.url("").toString()) | ||
} | ||
|
||
@Test | ||
fun `test_json_์์ฒญ`() = runTest { | ||
// given | ||
val response = MockResponse().setBody(File("src/test/resources/repositories.json").readText()) | ||
server.enqueue(response) | ||
|
||
// when | ||
val actual = githubService.getRepositories() | ||
|
||
// then | ||
val expected = GithubRepositoryResponse( | ||
fullName = "mojombo/grit", | ||
description = "**Grit is no longer maintained. Check out libgit2/rugged.** Grit gives you object oriented read/write access to Git repositories via Ruby." | ||
) | ||
assertThat(actual).contains(expected) | ||
} | ||
|
||
@Test | ||
fun `test_json_์์ฒญ2`() = runTest { | ||
// given | ||
val response = MockResponse().setBody(File("src/test/resources/repositories.json").readText()) | ||
server.enqueue(response) | ||
|
||
// when | ||
val actual = githubService.getRepositories() | ||
|
||
// then | ||
val expected = GithubRepositoryResponse( | ||
fullName = "jnicklas/uploadcolumn", | ||
description = "UploadColumn is no longer maintained, check out CarrierWave for an alternative" | ||
) | ||
assertThat(actual).contains(expected) | ||
} | ||
} |
Oops, something went wrong.