-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[REFACTOR] API 레이어 분리
- Loading branch information
Showing
23 changed files
with
341 additions
and
275 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,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) | ||
} | ||
} |
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,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) | ||
} | ||
} |
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,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 | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
LionHeart-iOS/LionHeart-iOS/Network/Services/AuthMyPageServiceWrapper.swift
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,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
94
LionHeart-iOS/LionHeart-iOS/Network/Services/AuthService.swift
This file was deleted.
Oops, something went wrong.
24 changes: 0 additions & 24 deletions
24
LionHeart-iOS/LionHeart-iOS/Network/Services/MyPageService.swift
This file was deleted.
Oops, something went wrong.
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
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
Oops, something went wrong.