From fdde4eecd1be8800791fb458073d1aab532e7edc Mon Sep 17 00:00:00 2001 From: Dev-Corgi Date: Mon, 5 Feb 2024 17:54:11 +0900 Subject: [PATCH] rest fix --- .../campusnote/ApiService/ApiService.kt | 40 ++++++++++++ .../campusnote/ApiService/Datas/CrawlData.kt | 5 ++ .../campusnote/ApiService/Datas/LogInData.kt | 5 ++ .../campusnote/ApiService/RestHelper.kt | 65 +++++++++++++++++++ .../ApiService/Results/CrawlResult.kt | 24 +++++++ .../ApiService/Results/LogInResult.kt | 13 ++++ .../ApiService/Results/RefreashTokenResult.kt | 12 ++++ .../ApiService/Results/SignUpResult.kt | 15 +++++ .../ApiService/Results/UserResult.kt | 8 +++ .../campusnote/ApiService/RetrofitClient.kt | 19 ++++++ 10 files changed, 206 insertions(+) create mode 100644 app/src/main/java/com/notation/campusnote/ApiService/ApiService.kt create mode 100644 app/src/main/java/com/notation/campusnote/ApiService/Datas/CrawlData.kt create mode 100644 app/src/main/java/com/notation/campusnote/ApiService/Datas/LogInData.kt create mode 100644 app/src/main/java/com/notation/campusnote/ApiService/RestHelper.kt create mode 100644 app/src/main/java/com/notation/campusnote/ApiService/Results/CrawlResult.kt create mode 100644 app/src/main/java/com/notation/campusnote/ApiService/Results/LogInResult.kt create mode 100644 app/src/main/java/com/notation/campusnote/ApiService/Results/RefreashTokenResult.kt create mode 100644 app/src/main/java/com/notation/campusnote/ApiService/Results/SignUpResult.kt create mode 100644 app/src/main/java/com/notation/campusnote/ApiService/Results/UserResult.kt create mode 100644 app/src/main/java/com/notation/campusnote/ApiService/RetrofitClient.kt diff --git a/app/src/main/java/com/notation/campusnote/ApiService/ApiService.kt b/app/src/main/java/com/notation/campusnote/ApiService/ApiService.kt new file mode 100644 index 00000000..6c51d494 --- /dev/null +++ b/app/src/main/java/com/notation/campusnote/ApiService/ApiService.kt @@ -0,0 +1,40 @@ +import com.notation.campusnote.ApiService.Datas.CrawlData +import com.notation.campusnote.ApiService.Datas.LogInData +import retrofit2.Call +import retrofit2.http.* +import com.notation.campusnote.ApiService.Datas.SignUpData +import com.notation.campusnote.ApiService.Results.CrawlResult +import com.notation.campusnote.ApiService.Results.LogInResult +import com.notation.campusnote.ApiService.Results.RefreashTokenResult +import com.notation.campusnote.ApiService.Results.SignUpResult +import com.notation.campusnote.ApiService.Results.UserResult + +data class Post( + val data: Any // 또는 다른 적절한 타입으로 지정할 수 있음 +) + +data class SignUpRequest( + val clientId: String, + val name: String, + val university: String, + val semester: String, + val img: String +) + +interface ApiService { + + @POST("api/v1/crawl") + fun postCrawl(@Body data: CrawlData): Call + + @POST("api/v1/auth/refresh-token") + fun postRefreshToken(): Call + + @POST("api/v1/auth/signUp") + fun postSignUp(@Body data: SignUpData): Call + + @POST("api/v1/login") + fun postLogin(@Body data: LogInData): Call + + @GET("api/v1/user") + fun getUser(): Call +} diff --git a/app/src/main/java/com/notation/campusnote/ApiService/Datas/CrawlData.kt b/app/src/main/java/com/notation/campusnote/ApiService/Datas/CrawlData.kt new file mode 100644 index 00000000..ff3fee41 --- /dev/null +++ b/app/src/main/java/com/notation/campusnote/ApiService/Datas/CrawlData.kt @@ -0,0 +1,5 @@ +package com.notation.campusnote.ApiService.Datas + +data class CrawlData ( + val url: String +) \ No newline at end of file diff --git a/app/src/main/java/com/notation/campusnote/ApiService/Datas/LogInData.kt b/app/src/main/java/com/notation/campusnote/ApiService/Datas/LogInData.kt new file mode 100644 index 00000000..e7baa1ca --- /dev/null +++ b/app/src/main/java/com/notation/campusnote/ApiService/Datas/LogInData.kt @@ -0,0 +1,5 @@ +package com.notation.campusnote.ApiService.Datas + +data class LogInData( + val clientId: String +) \ No newline at end of file diff --git a/app/src/main/java/com/notation/campusnote/ApiService/RestHelper.kt b/app/src/main/java/com/notation/campusnote/ApiService/RestHelper.kt new file mode 100644 index 00000000..9ef2979f --- /dev/null +++ b/app/src/main/java/com/notation/campusnote/ApiService/RestHelper.kt @@ -0,0 +1,65 @@ +import android.util.Log +import com.notation.campusnote.ApiService.Datas.CrawlData +import com.notation.campusnote.ApiService.Datas.LogInData +import retrofit2.Call +import retrofit2.Callback +import retrofit2.Response +import com.notation.campusnote.ApiService.Datas.SignUpData +import com.notation.campusnote.ApiService.Results.CrawlResult +import com.notation.campusnote.ApiService.Results.LogInResult +import com.notation.campusnote.ApiService.Results.RefreashTokenResult +import com.notation.campusnote.ApiService.Results.SignUpResult +import com.notation.campusnote.ApiService.Results.UserResult + +object RestHelper { + + fun restHelper(call: Call, callback: (result: T?) -> Unit) { + call.enqueue(object : Callback { + override fun onResponse(call: Call, response: Response) { + if (response.isSuccessful) { + val output = response.body() + if (output != null) { + Log.d("REST 테스팅", "REST Success, output: $output") + callback(output) + } else { + // Handle null response + Log.e("REST 테스팅", "Error: Response Body is null") + callback(null) + } + } else { + // Handle error + Log.e("REST 테스팅", "Error: ${response.code()} - ${response.message()}") + callback(null) + } + } + + override fun onFailure(call: Call, t: Throwable) { + // Handle failure + Log.e("REST 테스팅", "Error: ${t.message}") + callback(null) + } + }) + } + + fun postKakaosignUp(data: SignUpData, callback: (result: SignUpResult?) -> Unit) { + restHelper(ApiClient.apiService.postSignUp(data), callback) + } + + fun postRefreashToken(callback: (result: RefreashTokenResult?) -> Unit) { + restHelper(ApiClient.apiService.postRefreshToken(), callback) + } + + fun postCrawl(data: CrawlData, callback: (result: CrawlResult?) -> Unit) { + restHelper(ApiClient.apiService.postCrawl(data), callback) + } + + fun postKakaologIn(data: LogInData, callback: (result: LogInResult?) -> Unit) { + restHelper(ApiClient.apiService.postLogin(data), callback) + } + + fun getUser(callback: (result: UserResult?) -> Unit) { + restHelper(ApiClient.apiService.getUser(), callback) + } + + + } \ No newline at end of file diff --git a/app/src/main/java/com/notation/campusnote/ApiService/Results/CrawlResult.kt b/app/src/main/java/com/notation/campusnote/ApiService/Results/CrawlResult.kt new file mode 100644 index 00000000..8e7d5c25 --- /dev/null +++ b/app/src/main/java/com/notation/campusnote/ApiService/Results/CrawlResult.kt @@ -0,0 +1,24 @@ +package com.notation.campusnote.ApiService.Results + +data class CrawlResult( + val code: String, + val isSuccess: Boolean, + val message: String, + val result: List +){ + data class Result( + val id: Int, + val lessonDetailsDtoList: List, + val lessonName: String, + val semester: String, + val university: String + ) + + data class LessonDetailsDto( + val dayOfWeek: String, + val location: String, + val professorName: String, + val runningTime: String, + val startTime: String + ) +} \ No newline at end of file diff --git a/app/src/main/java/com/notation/campusnote/ApiService/Results/LogInResult.kt b/app/src/main/java/com/notation/campusnote/ApiService/Results/LogInResult.kt new file mode 100644 index 00000000..dff4e267 --- /dev/null +++ b/app/src/main/java/com/notation/campusnote/ApiService/Results/LogInResult.kt @@ -0,0 +1,13 @@ +package com.notation.campusnote.ApiService.Results + +data class LogInResult( + val code: String, + val isSuccess: Boolean, + val message: String, + val result: Result +){ + data class Result( + val accessToken: String, + val refreshToken: String + ) +} \ No newline at end of file diff --git a/app/src/main/java/com/notation/campusnote/ApiService/Results/RefreashTokenResult.kt b/app/src/main/java/com/notation/campusnote/ApiService/Results/RefreashTokenResult.kt new file mode 100644 index 00000000..922c935e --- /dev/null +++ b/app/src/main/java/com/notation/campusnote/ApiService/Results/RefreashTokenResult.kt @@ -0,0 +1,12 @@ +package com.notation.campusnote.ApiService.Results + +data class RefreashTokenResult( + val code: String, + val isSuccess: Boolean, + val message: String, + val result: Result +){ + data class Result( + val accessToken: String + ) +} \ No newline at end of file diff --git a/app/src/main/java/com/notation/campusnote/ApiService/Results/SignUpResult.kt b/app/src/main/java/com/notation/campusnote/ApiService/Results/SignUpResult.kt new file mode 100644 index 00000000..aa6f3244 --- /dev/null +++ b/app/src/main/java/com/notation/campusnote/ApiService/Results/SignUpResult.kt @@ -0,0 +1,15 @@ +package com.notation.campusnote.ApiService.Results + + +data class SignUpResult( + val code: String, + val isSuccess: Boolean, + val message: String, + val result: Result +){ + data class Result( + val accesstoken: String, + val refreshtoken: String + ) +} + diff --git a/app/src/main/java/com/notation/campusnote/ApiService/Results/UserResult.kt b/app/src/main/java/com/notation/campusnote/ApiService/Results/UserResult.kt new file mode 100644 index 00000000..4448857c --- /dev/null +++ b/app/src/main/java/com/notation/campusnote/ApiService/Results/UserResult.kt @@ -0,0 +1,8 @@ +package com.notation.campusnote.ApiService.Results + +data class UserResult( + val code: String, + val isSuccess: Boolean, + val message: String, + val result: String +) \ No newline at end of file diff --git a/app/src/main/java/com/notation/campusnote/ApiService/RetrofitClient.kt b/app/src/main/java/com/notation/campusnote/ApiService/RetrofitClient.kt new file mode 100644 index 00000000..8ac485d5 --- /dev/null +++ b/app/src/main/java/com/notation/campusnote/ApiService/RetrofitClient.kt @@ -0,0 +1,19 @@ +import retrofit2.Retrofit +import retrofit2.converter.gson.GsonConverterFactory + +object RetrofitClient { + private const val BASE_URL = "http://3.38.225.49:8080/" + + val retrofit: Retrofit by lazy { + Retrofit.Builder() + .baseUrl(BASE_URL) + .addConverterFactory(GsonConverterFactory.create()) + .build() + } +} + +object ApiClient { + val apiService: ApiService by lazy { + RetrofitClient.retrofit.create(ApiService::class.java) + } +} \ No newline at end of file