Skip to content

Commit

Permalink
[Feat/#92] adapt 연결
Browse files Browse the repository at this point in the history
  • Loading branch information
HELLOHIDI committed Oct 28, 2024
1 parent bee585c commit a146532
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ enum AuthRouter {
}

extension AuthRouter: BaseTargetType {
var headers: Parameters? {


var headers: [String : String]? {
switch self {
case .socialLogin:
return APIHeaders.hasSocialTokenHeader
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ final class BaseService<Target: URLRequestTargetType> {
extension BaseService {
/// 네트워크 응답 처리 메소드
private func fetchResponse(with target: API) -> AnyPublisher<NetworkResponse, HMHNetworkError> {
return requestHandler.executeRequest(for: target)
return requestHandler.executeRequest(for: target, isWithInterceptor: target.isWithInterceptor)
.handleEvents(receiveSubscription: { _ in
NetworkLogHandler.requestLogging(target)
}, receiveOutput: { response in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct NetworkLogHandler {
let url = endpoint.url + (endpoint.path ?? "")
let method = endpoint.method.rawValue
let headers = endpoint.headers ?? [:]
let parameters = endpoint.parameters ?? [:]
let parameters = endpoint.task

print("""
================== 📤 Request ===================>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import Foundation
import Combine

struct TokenInterceptor {

Expand All @@ -21,16 +22,20 @@ struct TokenInterceptor {
}


func adapt(
_ request: URLRequest
) async throws -> URLRequest {
return request
}


func retry(
for session: URLSession
) async throws {

}
func adapt(_ request: URLRequest) -> AnyPublisher<URLRequest, HMHNetworkError> {
// 여기에 필요한 로직을 추가하여 request를 수정할 수 있습니다.
return Just(request)
.setFailureType(to: HMHNetworkError.self) // 성공 시 반환될 값의 타입 설정
.eraseToAnyPublisher() // AnyPublisher로 반환
}


func retry(for session: URLSession) -> AnyPublisher<Void, HMHNetworkError> {
// 여기에 retry 로직을 추가합니다.
return Future<Void, HMHNetworkError> { promise in
// retry 로직을 구현하여 promise를 성공 또는 실패로 완료합니다.
// 예: promise(.success(())) 또는 promise(.failure(error))
}
.eraseToAnyPublisher() // AnyPublisher로 반환
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,14 @@ class RequestHandler {
.mapError { ErrorHandler.handleError(target, error: .invalidRequest($0)) }
.flatMap { urlRequest in
if isWithInterceptor {
return TokenInterceptor
.catch { error in
// Adapt 실패 시 에러 처리
Just(urlRequest) // 원래 요청을 반환
.setFailureType(to: HMHNetworkError.RequestError.self)
}
return TokenInterceptor.shared.adapt(urlRequest)
} else {
return Just(urlRequest)
.setFailureType(to: HMHNetworkError.RequestError.self)
.setFailureType(to: HMHNetworkError.self)
.eraseToAnyPublisher()
}
}
.map { $0 }
.flatMap { urlRequest in
self.session.dataTaskPublisher(for: urlRequest)
.tryMap { data, response -> NetworkResponse in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ protocol URLRequestTargetType {
var method: HTTPMethod { get }
var headers : [String : String]? { get }
var task: Task { get }
var isWithInterceptor: Bool { get }

func asURLRequest() -> AnyPublisher<URLRequest, HMHNetworkError.RequestError>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public struct APIHeaders {
}

public extension APIHeaders {
static var noTokenHeader: Dictionary<String,String> {
[contentType: applicationJSON]
}

static var hasSocialTokenHeader: [String: String] {
return [contentType: applicationJSON,
auth: appleAccessToken]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ enum AuthAPI {
}

extension AuthAPI: BaseAPI {
var isWithInterceptor: Bool {
return false
}

var path: String? {
switch self {
case .signUp:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ enum ChallengeAPI {
}

extension ChallengeAPI: BaseAPI {
var isWithInterceptor: Bool {
return true
}

var path: String? {
switch self {
case .createChallenge:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ enum PointAPI {
}

extension PointAPI: BaseAPI {
var isWithInterceptor: Bool {
return false
}

var path: String? {
switch self {
case .getUsagePoint:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ enum UserAPI {
}

extension UserAPI: BaseAPI {
var isWithInterceptor: Bool {
return false
}

var path: String? {
switch self {
case .logout:
Expand Down

0 comments on commit a146532

Please sign in to comment.