-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CAT-161] Feature: token interceptor를 포함한 API 모듈 수정 (#18)
- Loading branch information
Showing
17 changed files
with
271 additions
and
226 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
42 changes: 42 additions & 0 deletions
42
Projects/Core/APIClient/Interface/APIClientInterface.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,42 @@ | ||
// | ||
// APIClientInterface.swift | ||
// APIClient | ||
// | ||
// Created by 김지현 on 8/6/24. | ||
// Copyright © 2024 PomoNyang. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
import Logger | ||
|
||
import Dependencies | ||
import DependenciesMacros | ||
|
||
@DependencyClient | ||
public struct APIClient { | ||
public var apiRequest: @Sendable ( | ||
_ request: APIBaseRequest, | ||
_ isWithInterceptor: Bool | ||
) async throws -> (Data, URLResponse) | ||
|
||
public func apiRequest<T: Decodable>( | ||
request: APIBaseRequest, | ||
as: T.Type, | ||
isWithInterceptor: Bool = true | ||
) async throws -> T { | ||
let (data, _) = try await self.apiRequest(request, isWithInterceptor) | ||
|
||
do { | ||
let decodedData = try JSONDecoder().decode(T.self, from: data) | ||
return decodedData | ||
} catch { | ||
throw NetworkError.decodingError | ||
} | ||
} | ||
} | ||
|
||
extension APIClient: TestDependencyKey { | ||
public static let previewValue = Self() | ||
public static let testValue = Self() | ||
} |
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
17 changes: 0 additions & 17 deletions
17
Projects/Core/APIClient/Interface/TokenInterceptorInterface.swift
This file was deleted.
Oops, something went wrong.
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,86 @@ | ||
// | ||
// APIClient.swift | ||
// APIClient | ||
// | ||
// Created by 김지현 on 8/6/24. | ||
// Copyright © 2024 PomoNyang. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
import Logger | ||
import APIClientInterface | ||
|
||
import Dependencies | ||
|
||
extension APIClient: DependencyKey { | ||
public static let liveValue: APIClient = .live() | ||
|
||
public static func live() -> Self { | ||
|
||
actor Session { | ||
nonisolated let tokenInterceptor: TokenInterceptor | ||
|
||
let session: URLSession = { | ||
let config = URLSessionConfiguration.default | ||
return URLSession(configuration: config) | ||
}() | ||
|
||
let decoder: JSONDecoder = JSONDecoder() | ||
|
||
init(tokenInterceptor: TokenInterceptor) { | ||
self.tokenInterceptor = tokenInterceptor | ||
} | ||
|
||
func sendRequest( | ||
_ request: APIBaseRequest, | ||
isWithInterceptor: Bool, | ||
retryCnt: Int = 0 | ||
) async throws -> (Data, URLResponse) { | ||
guard retryCnt < 3 else { throw throwNetworkErr(.timeOutError) } | ||
|
||
var urlRequest = try await request.asURLRequest() | ||
if isWithInterceptor { | ||
urlRequest = try await tokenInterceptor.adapt(urlRequest) | ||
} | ||
Logger.shared.log(category: .network, "API Request:\n\(dump(urlRequest))") | ||
|
||
let (data, response) = try await session.data(for: urlRequest) | ||
|
||
guard let httpResponse = response as? HTTPURLResponse else { | ||
let error = NetworkError.noResponseError | ||
Logger.shared.log(level: .error, category: .network, "API Error:\n\(dump(error))") | ||
throw error | ||
} | ||
|
||
switch httpResponse.statusCode { | ||
case 200..<300: | ||
return (data, response) | ||
case 401: | ||
try await tokenInterceptor.retry(for: self.session) | ||
return try await sendRequest(request, isWithInterceptor: isWithInterceptor, retryCnt: retryCnt + 1) | ||
case 400..<500: | ||
throw throwNetworkErr(.requestError("bad request")) | ||
case 500..<600: | ||
throw throwNetworkErr(.serverError) | ||
default: | ||
throw throwNetworkErr(.unknownError) | ||
} | ||
|
||
func throwNetworkErr(_ error: NetworkError) -> NetworkError { | ||
Logger.shared.log(level: .error, category: .network, "\(error.localizedDescription):\n\(dump(error))") | ||
return error | ||
} | ||
} | ||
} | ||
|
||
let tokenInterceptor = TokenInterceptor() | ||
let session = Session(tokenInterceptor: tokenInterceptor) | ||
|
||
return .init( | ||
apiRequest: { request, isWithInterceptor in | ||
return try await session.sendRequest(request, isWithInterceptor: isWithInterceptor) | ||
} | ||
) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
34 changes: 0 additions & 34 deletions
34
Projects/Core/APIClient/Sources/Plugin/EventLoggerDelegate.swift
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.