Skip to content

Commit

Permalink
[Feat/#92] 인터셉트 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
HELLOHIDI committed Oct 29, 2024
1 parent a146532 commit 6fd5222
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ final class BaseService<Target: URLRequestTargetType> {

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<T: Decodable>(_ target: API, _ responseType: T.Type) -> AnyPublisher<T, HMHNetworkError> {
return fetchResponse(with: target)
.flatMap { response in
Expand Down Expand Up @@ -59,26 +66,16 @@ extension BaseService {
/// 응답 유효성 검사 메서드
private func validate(response: NetworkResponse) -> AnyPublisher<Void, HMHNetworkError> {
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<T: Decodable>(data: Data, target: API) -> AnyPublisher<T, HMHNetworkError> {
Expand All @@ -96,5 +93,9 @@ extension HTTPURLResponse {
func isValidateStatus() -> Bool {
return (200...299).contains(self.statusCode)
}

func unAuthorized() -> Bool {
return self.statusCode == 401
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,12 @@ class RequestHandler {
}
.eraseToAnyPublisher()
}

.eraseToAnyPublisher()
}

func tokenRequest() {
TokenInterceptor.shared.retry(for: session)
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,24 @@
import Foundation
import Combine

//class ErrorHandler {
// static let shared = ErrorHandler()
// private init () {}
//
// private let loggingHandler = NetworkLogHandler.shared
//
// func handleError<T: URLRequestTargetType>(_ 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<T: URLRequestTargetType>(_ 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))
}
}
}

Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{

"originHash" : "121817b8ddbac77685ab40e17f7a00d795dff609a9d68288c102f550e0f9779d",

"originHash" : "511b5126f44782a39b3141b0c74c3caf5444c7925e20b25f53462ca6803f0925",
"pins" : [
{
"identity" : "alamofire",
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 6fd5222

Please sign in to comment.