-
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.
Browse files
Browse the repository at this point in the history
[Feat] 마이페이지 API 연결
- Loading branch information
Showing
13 changed files
with
194 additions
and
18 deletions.
There are no files selected for viewing
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,44 @@ | ||
// | ||
// UserService.swift | ||
// KkuMulKum | ||
// | ||
// Created by 이지훈 on 8/22/24. | ||
// | ||
|
||
import Foundation | ||
|
||
import Moya | ||
|
||
final class MyPageUserService: MyPageUserServiceType { | ||
private var provider = MoyaProvider<UserTargetType>() | ||
|
||
init(provider: MoyaProvider<UserTargetType> = MoyaProvider(plugins: [MoyaLoggingPlugin()])) { | ||
self.provider = provider | ||
} | ||
|
||
func getUserInfo() async throws -> LoginUserModel { | ||
return try await performRequest(.getUserInfo) | ||
} | ||
|
||
func performRequest<T: ResponseModelType>(_ target: UserTargetType) async throws -> T { | ||
return try await withCheckedThrowingContinuation { continuation in | ||
provider.request(target) { result in | ||
switch result { | ||
case .success(let response): | ||
do { | ||
let decodedResponse = try JSONDecoder().decode(ResponseBodyDTO<T>.self, from: response.data) | ||
guard decodedResponse.success, let data = decodedResponse.data else { | ||
throw decodedResponse.error.map(NetworkErrorMapper.mapErrorResponse) ?? | ||
NetworkError.unknownError("Unknown error occurred") | ||
} | ||
continuation.resume(returning: data) | ||
} catch { | ||
continuation.resume(throwing: error is NetworkError ? error : NetworkError.decodingError) | ||
} | ||
case .failure(let error): | ||
continuation.resume(throwing: NetworkError.networkError(error)) | ||
} | ||
} | ||
} | ||
} | ||
} |
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,56 @@ | ||
// | ||
// MypageTargetType.swift | ||
// KkuMulKum | ||
// | ||
// Created by 이지훈 on 8/22/24. | ||
// | ||
|
||
import Foundation | ||
|
||
import Moya | ||
|
||
enum UserTargetType { | ||
case getUserInfo | ||
} | ||
|
||
extension UserTargetType: TargetType { | ||
var baseURL: URL { | ||
guard let privacyInfo = Bundle.main.privacyInfo, | ||
let urlString = privacyInfo["BASE_URL"] as? String, | ||
let url = URL(string: urlString) else { | ||
fatalError("Invalid BASE_URL in PrivacyInfo.plist") | ||
} | ||
return url | ||
} | ||
|
||
var path: String { | ||
switch self { | ||
case .getUserInfo: | ||
return "/api/v1/users/me" | ||
} | ||
} | ||
|
||
var method: Moya.Method { | ||
switch self { | ||
case .getUserInfo: | ||
return .get | ||
} | ||
} | ||
|
||
var task: Task { | ||
switch self { | ||
case .getUserInfo: | ||
return .requestPlain | ||
} | ||
} | ||
|
||
var headers: [String : String]? { | ||
guard let token = DefaultKeychainService.shared.accessToken else { | ||
fatalError("No access token available") | ||
} | ||
return [ | ||
"Content-Type": "application/json", | ||
"Authorization": "Bearer \(token)" | ||
] | ||
} | ||
} |
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
13 changes: 13 additions & 0 deletions
13
KkuMulKum/Source/MyPage/MyPageServiceProtocol/MyPageUserServiceType.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,13 @@ | ||
// | ||
// MyPageUserServiceType.swift | ||
// KkuMulKum | ||
// | ||
// Created by 이지훈 on 8/22/24. | ||
// | ||
|
||
import Foundation | ||
|
||
protocol MyPageUserServiceType { | ||
func getUserInfo() async throws -> LoginUserModel | ||
func performRequest<T: ResponseModelType>(_ target: UserTargetType) async throws -> T | ||
} |
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
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.