From 6fd522230e149613787f2e24941896e5a3d42445 Mon Sep 17 00:00:00 2001 From: HELLOHIDI Date: Tue, 29 Oct 2024 12:03:51 +0900 Subject: [PATCH] =?UTF-8?q?[Feat/#92]=20=EC=9D=B8=ED=84=B0=EC=85=89?= =?UTF-8?q?=ED=8A=B8=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Foundation/Base/BaseService.swift | 33 ++++++++++--------- .../Request/Builder/RequestHandler.swift | 5 ++- .../Response/Error/ErrorHandler.swift | 31 +++++++++-------- .../xcshareddata/swiftpm/Package.resolved | 22 +------------ 4 files changed, 37 insertions(+), 54 deletions(-) diff --git a/HMH_Tuist_iOS/Projects/Modules/Networks/Sources/Refactor/Foundation/Base/BaseService.swift b/HMH_Tuist_iOS/Projects/Modules/Networks/Sources/Refactor/Foundation/Base/BaseService.swift index eef7f034..9425ae7a 100644 --- a/HMH_Tuist_iOS/Projects/Modules/Networks/Sources/Refactor/Foundation/Base/BaseService.swift +++ b/HMH_Tuist_iOS/Projects/Modules/Networks/Sources/Refactor/Foundation/Base/BaseService.swift @@ -15,6 +15,13 @@ final class BaseService { private let requestHandler = RequestHandler.shared + private lazy var session: URLSession = { + let configuration = URLSessionConfiguration.default + configuration.timeoutIntervalForRequest = 10 + configuration.timeoutIntervalForResource = 10 + return URLSession(configuration: configuration) + }() + func requestWithResult(_ target: API, _ responseType: T.Type) -> AnyPublisher { return fetchResponse(with: target) .flatMap { response in @@ -59,26 +66,16 @@ extension BaseService { /// 응답 유효성 검사 메서드 private func validate(response: NetworkResponse) -> AnyPublisher { guard response.response.isValidateStatus() else { - guard let data = response.data else { - return Fail(error: HMHNetworkError.invalidResponse(.invalidStatusCode(code: response.response.statusCode))) - .eraseToAnyPublisher() + if response.response.unAuthorized() { + RequestHandler.shared.tokenRequest() } - - return Just(data) - .decode(type: ErrorResponse.self, decoder: JSONDecoder()) - .mapError { _ in HMHNetworkError.invalidResponse(.invalidStatusCode(code: response.response.statusCode)) } - .flatMap { response in - Fail(error: HMHNetworkError.invalidResponse(.invalidStatusCode( - code: response.statusCode, - data: response.data - ) - ) - ).eraseToAnyPublisher() - } - .eraseToAnyPublisher() + let error = ErrorHandler.handleInvalidResponse(response: response) + return Fail(error: error).eraseToAnyPublisher() } + return Just(()).setFailureType(to: HMHNetworkError.self).eraseToAnyPublisher() } + /// 디코딩 메소드 private func decode(data: Data, target: API) -> AnyPublisher { @@ -96,5 +93,9 @@ extension HTTPURLResponse { func isValidateStatus() -> Bool { return (200...299).contains(self.statusCode) } + + func unAuthorized() -> Bool { + return self.statusCode == 401 + } } diff --git a/HMH_Tuist_iOS/Projects/Modules/Networks/Sources/Refactor/Foundation/Request/Builder/RequestHandler.swift b/HMH_Tuist_iOS/Projects/Modules/Networks/Sources/Refactor/Foundation/Request/Builder/RequestHandler.swift index f060b09d..a9b06b87 100644 --- a/HMH_Tuist_iOS/Projects/Modules/Networks/Sources/Refactor/Foundation/Request/Builder/RequestHandler.swift +++ b/HMH_Tuist_iOS/Projects/Modules/Networks/Sources/Refactor/Foundation/Request/Builder/RequestHandler.swift @@ -54,9 +54,12 @@ class RequestHandler { } .eraseToAnyPublisher() } - .eraseToAnyPublisher() } + + func tokenRequest() { + TokenInterceptor.shared.retry(for: session) + } } diff --git a/HMH_Tuist_iOS/Projects/Modules/Networks/Sources/Refactor/Foundation/Response/Error/ErrorHandler.swift b/HMH_Tuist_iOS/Projects/Modules/Networks/Sources/Refactor/Foundation/Response/Error/ErrorHandler.swift index 02b7f304..8a50242d 100644 --- a/HMH_Tuist_iOS/Projects/Modules/Networks/Sources/Refactor/Foundation/Response/Error/ErrorHandler.swift +++ b/HMH_Tuist_iOS/Projects/Modules/Networks/Sources/Refactor/Foundation/Response/Error/ErrorHandler.swift @@ -9,25 +9,24 @@ import Foundation import Combine -//class ErrorHandler { -// static let shared = ErrorHandler() -// private init () {} -// -// private let loggingHandler = NetworkLogHandler.shared -// -// func handleError(_ target: T, error: HMHNetworkError) -> HMHNetworkError { -//// guard let hmhNetworkError = error as? HMHNetworkError else { -//// return .unknown -//// } -// loggingHandler.responseError(target, result: error) -// -// return error -// } -//} - struct ErrorHandler { static func handleError(_ target: T, error: HMHNetworkError) -> HMHNetworkError { NetworkLogHandler.responseError(target, result: error) return error } + + // 유효하지 않은 응답인 경우 에러 처리 + static func handleInvalidResponse(response: NetworkResponse) -> HMHNetworkError { + if let data = response.data { + do { + // 에러 응답 모델로 디코딩 시도 + let errorResponse = try JSONDecoder().decode(ErrorResponse.self, from: data) + return .invalidResponse(.invalidStatusCode(code: response.response.statusCode, data: errorResponse.data)) + } catch { + return .invalidResponse(.invalidStatusCode(code: response.response.statusCode)) + } + } else { + return .invalidResponse(.invalidStatusCode(code: response.response.statusCode)) + } + } } diff --git a/HMH_iOS/HMH_iOS.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/HMH_iOS/HMH_iOS.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index 0f2a0462..47818748 100644 --- a/HMH_iOS/HMH_iOS.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/HMH_iOS/HMH_iOS.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -1,7 +1,5 @@ { - - "originHash" : "121817b8ddbac77685ab40e17f7a00d795dff609a9d68288c102f550e0f9779d", - + "originHash" : "511b5126f44782a39b3141b0c74c3caf5444c7925e20b25f53462ca6803f0925", "pins" : [ { "identity" : "alamofire", @@ -84,24 +82,6 @@ "version" : "6.7.0" } }, - { - "identity" : "realm-core", - "kind" : "remoteSourceControl", - "location" : "https://github.com/realm/realm-core.git", - "state" : { - "revision" : "9cf7ef4ad8e2f4c7a519c9a395ca3d253bb87aa8", - "version" : "14.6.2" - } - }, - { - "identity" : "realm-swift", - "kind" : "remoteSourceControl", - "location" : "https://github.com/realm/realm-swift", - "state" : { - "branch" : "master", - "revision" : "21ac01d5ae30dcc156e0e43e47c4afe39cc0367f" - } - }, { "identity" : "rxswift", "kind" : "remoteSourceControl",