Skip to content

Commit

Permalink
[MERGE] API 레이어 분리
Browse files Browse the repository at this point in the history
[REFACTOR] API 레이어 분리
  • Loading branch information
ffalswo2 authored Sep 12, 2023
2 parents bf41af3 + 9282a61 commit 4ebd495
Show file tree
Hide file tree
Showing 23 changed files with 341 additions and 275 deletions.
36 changes: 24 additions & 12 deletions LionHeart-iOS/LionHeart-iOS.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
self.window = UIWindow(windowScene: windowScene)
/// 폰트등록
Font.registerFonts()
let navigationController = UINavigationController(rootViewController: SplashViewController())
let navigationController = UINavigationController(rootViewController: SplashViewController(authService: AuthMyPageServiceWrapper(authAPIService: AuthAPI(apiService: APIService()), mypageAPIService: MyPageAPI(apiService: APIService()))))
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ extension Serviceable {
guard !NetworkErrorCode.serverErrorCode.contains(statusCode) else {
throw NetworkError.serverError
}
print("✅✅✅✅✅✅✅✅✅✅✅✅✅원래 API호출성공✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅")
print(model.data)
return model.data
}
}
96 changes: 96 additions & 0 deletions LionHeart-iOS/LionHeart-iOS/Network/API/AuthAPI.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//
// AuthAPI.swift
// LionHeart-iOS
//
// Created by uiskim on 2023/09/08.
//

import Foundation

protocol AuthAPIProtocol {
func reissueToken(token: Token) async throws -> Token?
func login(type: LoginType, kakaoToken: String) async throws
func signUp(type: LoginType, onboardingModel: UserOnboardingModel) async throws
@discardableResult func logout(token: UserDefaultToken) async throws -> String?
func resignUser() async throws
}

final class AuthAPI: AuthAPIProtocol {

private let apiService: Requestable

init(apiService: Requestable) {
self.apiService = apiService
}

func reissueToken(token: Token) async throws -> Token? {
let urlRequest = try makeReissueTokenUrlRequest(token: token)
return try await apiService.request(urlRequest)
}

func login(type: LoginType, kakaoToken: String) async throws {
let urlRequest = try makeLoginUrlRequest(type: type, kakaoToken: kakaoToken)
userdefaultsSettingWhenUserIn(model: try await apiService.request(urlRequest))
}

func signUp(type: LoginType, onboardingModel: UserOnboardingModel) async throws {
let urlRequest = try makeSignUpUrlRequest(type: type, onboardingModel: onboardingModel)
userdefaultsSettingWhenUserIn(model: try await apiService.request(urlRequest))
}

func logout(token: UserDefaultToken) async throws -> String? {
let urlRequest = try makeLogoutUrlRequest()
return try await apiService.request(urlRequest)
}

func resignUser() async throws {
let urlRequest = try makeResignUserUrlRequest()
_ = try await URLSession.shared.data(for: urlRequest)
UserDefaultsManager.tokenKey?.refreshToken = nil
}
}

extension AuthAPI {
private func userdefaultsSettingWhenUserIn(model: Token?) {
UserDefaultsManager.tokenKey?.accessToken = model?.accessToken
UserDefaultsManager.tokenKey?.refreshToken = model?.refreshToken
}
}

extension AuthAPI {
func makeResignUserUrlRequest() throws -> URLRequest {
return try NetworkRequest(path: "/v1/member", httpMethod: .delete).makeURLRequest(isLogined: true)
}

func makeLogoutUrlRequest() throws -> URLRequest {
return try NetworkRequest(path: "/v1/auth/logout", httpMethod: .post).makeURLRequest(isLogined: true)
}

func makeSignUpUrlRequest(type: LoginType, onboardingModel: UserOnboardingModel) throws -> URLRequest {
guard let fcmToken = UserDefaultsManager.tokenKey?.fcmToken,
let kakaoToken = onboardingModel.kakaoAccessToken,
let pregnantWeeks = onboardingModel.pregnacny,
let babyNickname = onboardingModel.fetalNickname else { throw NetworkError.badCasting }
let requestModel = SignUpRequest(socialType: type.raw, token: kakaoToken, fcmToken: fcmToken, pregnantWeeks: pregnantWeeks, babyNickname: babyNickname)
let param = requestModel.toDictionary()
let body = try JSONSerialization.data(withJSONObject: param)
return try NetworkRequest(path: "/v1/auth/signup", httpMethod: .post, body: body).makeURLRequest(isLogined: false)
}

func makeReissueTokenUrlRequest(token: Token) throws -> URLRequest {
let params = token.toDictionary()
let body = try JSONSerialization.data(withJSONObject: params, options: [])
return try NetworkRequest(path: "/v1/auth/reissue", httpMethod: .post, body: body)
.makeURLRequest(isLogined: false)
}

func makeLoginUrlRequest(type: LoginType, kakaoToken: String) throws -> URLRequest {
guard let fcmToken = UserDefaultsManager.tokenKey?.fcmToken else {
throw NetworkError.clientError(code: "", message: "\(String(describing: UserDefaultsManager.tokenKey))")
}
let loginRequest = LoginRequest(socialType: type.raw, token: kakaoToken, fcmToken: fcmToken)
let param = loginRequest.toDictionary()
let body = try JSONSerialization.data(withJSONObject: param)
return try NetworkRequest(path: "/v1/auth/login", httpMethod: .post, body: body).makeURLRequest(isLogined: false)
}
}
26 changes: 26 additions & 0 deletions LionHeart-iOS/LionHeart-iOS/Network/API/MyPageAPI.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// MyPageAPI.swift
// LionHeart-iOS
//
// Created by uiskim on 2023/09/10.
//

import Foundation

protocol MyPageAPIProtocol {
func getMyPage() async throws -> MyPageResponse?
}

final class MyPageAPI: MyPageAPIProtocol {

private let apiService: Requestable

init(apiService: Requestable) {
self.apiService = apiService
}

func getMyPage() async throws -> MyPageResponse? {
let urlRequest = try NetworkRequest(path: "/v1/member/profile", httpMethod: .get).makeURLRequest(isLogined: true)
return try await apiService.request(urlRequest)
}
}
34 changes: 34 additions & 0 deletions LionHeart-iOS/LionHeart-iOS/Network/Base/APIService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// APIService.swift
// LionHeart-iOS
//
// Created by uiskim on 2023/09/08.
//

import Foundation

protocol Requestable {
func request<T: Decodable>(_ request: URLRequest) async throws -> T?
}

final class APIService: Requestable {
func request<T: Decodable>(_ request: URLRequest) async throws -> T? {
let (data, _) = try await URLSession.shared.data(for: request)
let decoder = JSONDecoder()
guard let decodedData = try? decoder.decode(BaseResponse<T>.self, from: data) else {
throw NetworkError.jsonDecodingError
}

let statusCode = decodedData.code
guard !NetworkErrorCode.clientErrorCode.contains(statusCode) else {
throw NetworkError.clientError(code: decodedData.code, message: decodedData.message)
}

guard !NetworkErrorCode.serverErrorCode.contains(statusCode) else {
throw NetworkError.serverError
}

print("✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅APIService성공✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅")
return decodedData.data
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// AuthMyPageServiceWrapper.swift
// LionHeart-iOS
//
// Created by 김민재 on 2023/09/07.
//

import Foundation

final class AuthMyPageServiceWrapper: AuthServiceProtocol, MyPageServiceProtocol {

private let authAPIService: AuthAPIProtocol
private let mypageAPIService: MyPageAPIProtocol

init(authAPIService: AuthAPIProtocol, mypageAPIService: MyPageAPIProtocol) {
self.authAPIService = authAPIService
self.mypageAPIService = mypageAPIService
}

func getMyPage() async throws -> MyPageAppData {
guard let model = try await mypageAPIService.getMyPage()
else { return MyPageAppData.empty }
return MyPageAppData(badgeImage: model.level, nickname: model.babyNickname, isAlarm: model.notificationStatus)
}

func reissueToken(token: Token) async throws -> Token? {
let model = try await authAPIService.reissueToken(token: token)
return try await authAPIService.reissueToken(token: token)
}

func login(type: LoginType, kakaoToken: String) async throws {
try await authAPIService.login(type: type, kakaoToken: kakaoToken)
}

func signUp(type: LoginType, onboardingModel: UserOnboardingModel) async throws {
try await authAPIService.signUp(type: type, onboardingModel: onboardingModel)
}

func resignUser() async throws {
try await authAPIService.resignUser()
}

func logout(token: UserDefaultToken) async throws {
try await authAPIService.logout(token: token)
}

}
94 changes: 0 additions & 94 deletions LionHeart-iOS/LionHeart-iOS/Network/Services/AuthService.swift

This file was deleted.

24 changes: 0 additions & 24 deletions LionHeart-iOS/LionHeart-iOS/Network/Services/MyPageService.swift

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ private extension ArticleCategoryViewController {
}

navigationBar.rightSecondBarItemAction {
let myPageViewController = MyPageViewController()
let wrapper = AuthMyPageServiceWrapper(authAPIService: AuthAPI(apiService: APIService()), mypageAPIService: MyPageAPI(apiService: APIService()))
let myPageViewController = MyPageViewController(service: wrapper)
self.navigationController?.pushViewController(myPageViewController, animated: true)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ extension ArticleDetailViewController: ViewControllerServiceable {
switch error {
case .unAuthorizedError:
guard let window = self.view.window else { return }
ViewControllerUtil.setRootViewController(window: window, viewController: SplashViewController(), withAnimation: false)
ViewControllerUtil.setRootViewController(window: window, viewController: SplashViewController(authService: AuthMyPageServiceWrapper(authAPIService: AuthAPI(apiService: APIService()), mypageAPIService: MyPageAPI(apiService: APIService()))), withAnimation: false)
case .clientError(_, let message):
LHToast.show(message: "\(message)")
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@ extension ArticleListByCategoryViewController: ViewControllerServiceable {
LHToast.show(message: "Image Error")
case .unAuthorizedError:
guard let window = self.view.window else { return }
ViewControllerUtil.setRootViewController(window: window, viewController: SplashViewController(), withAnimation: false)
case .clientError(_, _):
print("뜨면 위험함")
ViewControllerUtil.setRootViewController(window: window, viewController: SplashViewController(authService: AuthMyPageServiceWrapper(authAPIService: AuthAPI(apiService: APIService()), mypageAPIService: MyPageAPI(apiService: APIService()))), withAnimation: false)
case .clientError(_, let message):
LHToast.show(message: message)
case .serverError:
LHToast.show(message: "승준이 빠따")
LHToast.show(message: error.description)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ extension BookmarkViewController: ViewControllerServiceable {
LHToast.show(message: "Image Error")
case .unAuthorizedError:
guard let window = self.view.window else { return }
ViewControllerUtil.setRootViewController(window: window, viewController: SplashViewController(), withAnimation: false)
case .clientError(_, _):
print("뜨면 위험함")
ViewControllerUtil.setRootViewController(window: window, viewController: SplashViewController(authService: AuthMyPageServiceWrapper(authAPIService: AuthAPI(apiService: APIService()), mypageAPIService: MyPageAPI(apiService: APIService()))), withAnimation: false)
case .clientError(_, let message):
LHToast.show(message: message)
case .serverError:
LHToast.show(message: "승준이 빠따")
LHToast.show(message: "server error")
}
}
}
Expand Down
Loading

0 comments on commit 4ebd495

Please sign in to comment.