Skip to content

Commit

Permalink
#8 [feat] : 서버 통신 설정
Browse files Browse the repository at this point in the history
  • Loading branch information
gitsuhyun committed Nov 11, 2024
1 parent 3aad6ec commit 37c44af
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 0 deletions.
36 changes: 36 additions & 0 deletions app/src/main/java/org/sopt/and/data/ApiFactory.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.sopt.and.data

import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
import kotlinx.serialization.json.Json
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import org.sopt.and.BuildConfig
import org.sopt.and.data.network.service.UserService
import retrofit2.Retrofit

object ApiFactory {
private const val BASE_URL: String = BuildConfig.BASE_URL

private val loggingInterceptor = HttpLoggingInterceptor().apply {
level = HttpLoggingInterceptor.Level.BODY
}

private val client = OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.build()

val retrofit: Retrofit by lazy {
Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(Json.asConverterFactory("application/json".toMediaType()))
.build()
}

inline fun <reified T> create(): T = retrofit.create(T::class.java)
}

object ServicePool {
val userService = ApiFactory.create<UserService>()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.sopt.and.data.model.dto


import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class ResponseUserHobbyDto(
@SerialName("result")
val result: Result
) {
@Serializable
data class Result(
@SerialName("hobby")
val hobby: String
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.sopt.and.data.model.dto


import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class ResponseUserSignUpDto(
@SerialName("result")
val result: Result
) {
@Serializable
data class Result(
@SerialName("no")
val no: Int
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.sopt.and.data.model.dto


import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class ResponseUserTokenDto(
@SerialName("result")
val result: Result
) {
@Serializable
data class Result(
@SerialName("token")
val token: String
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.sopt.and.data.model.request


import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class UserLoginRequest(
@SerialName("username")
val username: String,
@SerialName("password")
val password: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.sopt.and.data.model.request


import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class UserSignUpRequest(
@SerialName("username")
val username: String,
@SerialName("password")
val password: String,
@SerialName("hobby")
val hobby: String
)
29 changes: 29 additions & 0 deletions app/src/main/java/org/sopt/and/data/network/service/UserService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.sopt.and.data.network.service

import org.sopt.and.data.model.dto.ResponseUserHobbyDto
import org.sopt.and.data.model.dto.ResponseUserSignUpDto
import org.sopt.and.data.model.dto.ResponseUserTokenDto
import org.sopt.and.data.model.request.UserLoginRequest
import org.sopt.and.data.model.request.UserSignUpRequest
import retrofit2.Call
import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.Header
import retrofit2.http.POST

interface UserService {
@POST("/user")
fun postUserSignUp(
@Body body: UserSignUpRequest
): Call<ResponseUserSignUpDto>

@POST("/login")
fun postUserLogin(
@Body body: UserLoginRequest
): Call<ResponseUserTokenDto>

@GET("/user/my-hobby")
fun getUserHobby(
@Header("token") token: String,
): Call<ResponseUserHobbyDto>
}

0 comments on commit 37c44af

Please sign in to comment.