Skip to content

Commit

Permalink
feat: 인터셉터 구현(#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
sozohoy committed Feb 29, 2024
1 parent 6c7adf0 commit b49b0e2
Showing 1 changed file with 31 additions and 64 deletions.
95 changes: 31 additions & 64 deletions SUWIKI/SUWIKI/Core/Network/Interceptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,15 @@ import Foundation

import Alamofire

//final class tempInterceptor: RequestInterceptor {
// func retry(_ request: Request, for session: Session, dueTo error: Error, completion: @escaping (RetryResult) -> Void) {
// <#code#>
// }
//}

final class BaseInterceptor: RequestInterceptor {

let keychainManager = KeychainManager.shared

func adapt(_ urlRequest: URLRequest,
for session: Session,
completion: @escaping (Result<URLRequest, Error>) -> Void) {
func adapt(
_ urlRequest: URLRequest,
for session: Session,
completion: @escaping (Result<URLRequest, Error>) -> Void
) {
var request = urlRequest
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
if let accessToken = keychainManager.read(token: .AccessToken) {
Expand All @@ -30,60 +26,31 @@ final class BaseInterceptor: RequestInterceptor {
completion(.success(request))
}

// func adapt(
// _ urlRequest: URLRequest,
// for session: Session,
// completion: @escaping (Result<URLRequest, Error>) -> Void) {
// var request = urlRequest
// request.setValue("application/json", forHTTPHeaderField: "Content-Type")
// if let accessToken = keychainManager.read(token: .AccessToken) {
// request.setValue(accessToken, forHTTPHeaderField: "Authorization")
// }
// completion(.success(request))
// }

// func retry(_ request: Request, for session: Session, dueTo error: Error) async throws -> RetryResult {
// print("@LOG RETRY")
// guard let accessToken = keychainManager.read(token: .AccessToken) else {
// return .doNotRetry
// }
// let target = APITarget.User.refresh(DTO.RefreshRequest(authorization: accessToken))
// do {
//
// let tokens = try await APIProvider.request(DTO.RefreshResponse.self, target: target)
// keychainManager.create(token: .AccessToken, value: tokens.accessToken)
// keychainManager.create(token: .RefreshToken, value: tokens.refreshToken)
// return .retry
// } catch {
// return .doNotRetryWithError(error)
// }
// }
// func retry(
// _ request: Request,
// for session: Session,
// dueTo error: Error,
// completion: @escaping (RetryResult) -> Void) {
// print("@LOG RETRY 2")
// guard let refreshToken = keychainManager.read(token: .RefreshToken)
// else {
// completion(.doNotRetry)
// return
// }
// let target = APITarget.User.refresh(DTO.RefreshRequest(authorization: refreshToken))
// Task {
// do {
// let tokens = try await APIProvider.request(DTO.RefreshResponse.self, target: target)
// keychainManager.create(token: .AccessToken, value: tokens.accessToken)
// keychainManager.create(token: .RefreshToken, value: tokens.refreshToken)
// await MainActor.run {
// completion(.doNotRetry)
// }
// } catch {
// await MainActor.run {
// completion(.doNotRetry)
// }
// }
// }
// }
func retry(
_ request: Request,
for session: Session,
dueTo error: Error,
completion: @escaping (RetryResult) -> Void
) {
guard let refreshToken = keychainManager.read(token: .RefreshToken), request.response?.statusCode == 401 else {
return completion(.doNotRetryWithError(error))
}
let target = APITarget.User.refresh(DTO.RefreshRequest(authorization: refreshToken))
APIProvider.requestRefreshToken(DTO.RefreshResponse.self, target: target) { [weak self] response in
switch response.result {
case .success:
guard let tokens = response.value,
let self = self else {
completion(.doNotRetry)
return
}
self.keychainManager.create(token: .AccessToken, value: tokens.accessToken)
self.keychainManager.create(token: .RefreshToken, value: tokens.refreshToken)
completion(.retry)
case .failure:
completion(.doNotRetryWithError(error))
}
}
}
}

0 comments on commit b49b0e2

Please sign in to comment.