Skip to content

Commit

Permalink
[ADD/#8] get follower list 서버통신 함수 설계
Browse files Browse the repository at this point in the history
  • Loading branch information
b1urrrr committed May 12, 2023
1 parent aec7577 commit 386777b
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ android {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

buildConfigField "String", "BASE_URL", properties['base.url']
buildConfigField "String", "REQRES_URL", properties['reqres.url']
}

buildTypes {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.android.go.sopt.data.entity.remote.response

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import org.android.go.sopt.domain.model.Follower

@Serializable
data class ResponseGetFollowerListDto(
@SerialName("page")
val page: Int,
@SerialName("per_page")
val perPage: Int,
@SerialName("total")
val total: Int,
@SerialName("total_pages")
val totalPages: Int,
@SerialName("data")
val data: List<Follower>,
@SerialName("support")
val support: Support,
) {
@Serializable
data class Follower(
@SerialName("id")
val id: Int,
@SerialName("email")
val email: String,
@SerialName("first_name")
val firstName: String,
@SerialName("last_name")
val lastName: String,
@SerialName("avatar")
val avatar: String,
)

@Serializable
data class Support(
@SerialName("url")
val url: String,
@SerialName("text")
val text: String,
)

fun toFollower() = data.map { follower ->
Follower(
name = "${follower.firstName} ${follower.lastName}",
profile = follower.avatar,
email = follower.email,
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.android.go.sopt.data.repository

import org.android.go.sopt.data.source.remote.FollowerDataSource
import org.android.go.sopt.domain.model.Follower
import org.android.go.sopt.domain.repository.FollowerRepository
import javax.inject.Inject

class FollowerRepositoryImpl @Inject constructor(
private val followerDataSource: FollowerDataSource,
) : FollowerRepository {
override suspend fun getFollowerList(page: Int): Result<List<Follower>> =
runCatching {
followerDataSource.getFollowerList(page).toFollower()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.android.go.sopt.data.service

import org.android.go.sopt.data.entity.remote.response.ResponseGetFollowerListDto
import retrofit2.http.GET
import retrofit2.http.Query

interface FollowerService {
@GET("users")
fun getFollowerList(
@Query("page") page: Int,
): ResponseGetFollowerListDto
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.android.go.sopt.data.source.remote

import org.android.go.sopt.data.entity.remote.response.ResponseGetFollowerListDto
import org.android.go.sopt.data.service.FollowerService
import javax.inject.Inject

class FollowerDataSource @Inject constructor(
private val followerService: FollowerService,
) {
fun getFollowerList(page: Int): ResponseGetFollowerListDto =
followerService.getFollowerList(page)
}
21 changes: 20 additions & 1 deletion app/src/main/java/org/android/go/sopt/di/RetrofitModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import org.android.go.sopt.BuildConfig.BASE_URL
import org.android.go.sopt.BuildConfig.REQRES_URL
import org.android.go.sopt.util.type.BaseUrlType
import retrofit2.Retrofit
import javax.inject.Qualifier
import javax.inject.Singleton

@Module
Expand All @@ -31,12 +34,28 @@ object RetrofitModule {

@Provides
@Singleton
fun providesRetrofit(
@Retrofit2(BaseUrlType.SOPT)
fun providesSoptRetrofit(
client: OkHttpClient,
): Retrofit =
Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(Json.asConverterFactory(APPLICATION_JSON.toMediaType()))
.client(client)
.build()

@Provides
@Singleton
@Retrofit2(BaseUrlType.REQRES)
fun providesReqresRetrofit(
client: OkHttpClient,
): Retrofit =
Retrofit.Builder()
.baseUrl(REQRES_URL)
.addConverterFactory(Json.asConverterFactory(APPLICATION_JSON.toMediaType()))
.client(client)
.build()

@Qualifier
annotation class Retrofit2(val baseUrlType: BaseUrlType)
}
6 changes: 6 additions & 0 deletions app/src/main/java/org/android/go/sopt/di/ServiceModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import org.android.go.sopt.data.service.AuthService
import org.android.go.sopt.data.service.FollowerService
import retrofit2.Retrofit
import javax.inject.Singleton

Expand All @@ -15,4 +16,9 @@ object ServiceModule {
@Singleton
fun providesAuthService(retrofit: Retrofit): AuthService =
retrofit.create(AuthService::class.java)

@Provides
@Singleton
fun providesFollowerService(retrofit: Retrofit): FollowerService =
retrofit.create(FollowerService::class.java)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.android.go.sopt.domain.model

data class Follower(
val name: String,
val profile: String,
val email: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.android.go.sopt.domain.repository

import org.android.go.sopt.domain.model.Follower

interface FollowerRepository {
suspend fun getFollowerList(page: Int): Result<List<Follower>>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.android.go.sopt.util.type

enum class BaseUrlType {
SOPT, REQRES
}

0 comments on commit 386777b

Please sign in to comment.