Skip to content

Commit

Permalink
fix: tokenInterceptor 여부 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
Jihyun247 committed Aug 7, 2024
1 parent 483838e commit b95417e
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 16 deletions.
10 changes: 7 additions & 3 deletions Projects/Core/APIClient/Interface/APIClientInterface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ import DependenciesMacros

@DependencyClient
public struct APIClient {
public var apiRequest: @Sendable (_ request: APIBaseRequest) async throws -> (Data, URLResponse)
public var apiRequest: @Sendable (
_ request: APIBaseRequest,
_ isWithInterceptor: Bool
) async throws -> (Data, URLResponse)

public func apiRequest<T: Decodable>(
request: APIBaseRequest,
as: T.Type
as: T.Type,
isWithInterceptor: Bool = true
) async throws -> T {
let (data, _) = try await self.apiRequest(request)
let (data, _) = try await self.apiRequest(request, isWithInterceptor)

do {
let decodedData = try JSONDecoder().decode(T.self, from: data)
Expand Down
2 changes: 1 addition & 1 deletion Projects/Core/APIClient/Sources/APIBaseRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Shared

extension APIBaseRequest {
func asURLRequest() async throws -> URLRequest {
let baseURL = URL(string: self.baseURL)!
let baseURL = URL(string: "https://\(self.baseURL)")!

Check warning on line 15 in Projects/Core/APIClient/Sources/APIBaseRequest.swift

View workflow job for this annotation

GitHub Actions / Run Swiftlint

Force unwrapping should be avoided (force_unwrapping)
var urlRequest = URLRequest(url: baseURL.appendingPathComponent(path))
urlRequest.httpMethod = method.rawValue
urlRequest.setValue(
Expand Down
18 changes: 12 additions & 6 deletions Projects/Core/APIClient/Sources/APIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import APIClientInterface

import Dependencies

extension APIClient {
extension APIClient: DependencyKey {
public static let liveValue: APIClient = .live()

public static func live() -> Self {
Expand All @@ -32,11 +32,17 @@ extension APIClient {
self.tokenInterceptor = tokenInterceptor
}

func sendRequest(request: APIBaseRequest, retryCnt: Int = 0) async throws -> (Data, URLResponse) {
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()
urlRequest = try await tokenInterceptor.adapt(urlRequest)
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)
Expand All @@ -52,7 +58,7 @@ extension APIClient {
return (data, response)
case 401:
try await tokenInterceptor.retry(for: self.session)
return try await sendRequest(request: request, retryCnt: retryCnt + 1)
return try await sendRequest(request, isWithInterceptor: isWithInterceptor, retryCnt: retryCnt + 1)
case 400..<500:
throw throwNetworkErr(.requestError("bad request"))
case 500..<600:
Expand All @@ -72,8 +78,8 @@ extension APIClient {
let session = Session(tokenInterceptor: tokenInterceptor)

return .init(
apiRequest: { request in
return try await session.sendRequest(request: request)
apiRequest: { request, isWithInterceptor in
return try await session.sendRequest(request, isWithInterceptor: isWithInterceptor)
}
)
}
Expand Down
9 changes: 5 additions & 4 deletions Projects/Domain/AuthService/Sources/API/AuthAPIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ import Dependencies

extension AuthAPIClient: DependencyKey {
public static let liveValue: AuthAPIClient = .live()
private static func live() -> AuthAPIClient {
private static func live() -> Self {
return AuthAPIClient(
login: { deviceID, apiClient in
let service = AuthAPIService.login(deviceID)
var response = try await apiClient.apiRequest(
let response = try await apiClient.apiRequest(
request: service,
as: AuthDTO.Response.TokenResponseDTO.self
)
as: AuthDTO.Response.TokenResponseDTO.self,
isWithInterceptor: false
)
return response
}
)
Expand Down
7 changes: 6 additions & 1 deletion Projects/Feature/HomeFeature/Sources/HomeCore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import UserNotifications

import APIClientInterface
import AuthServiceInterface
import HomeFeatureInterface
import PushService
import UserNotificationClientInterface
Expand All @@ -17,12 +19,15 @@ import ComposableArchitecture
extension HomeCore {
public init() {
@Dependency(UserNotificationClient.self) var userNotificationClient

@Dependency(APIClient.self) var apiClient
@Dependency(AuthAPIClient.self) var authAPIClient

let reducer = Reduce<State, Action> { _, action in
switch action {
case .onAppear:
return .run { send in

Check warning on line 28 in Projects/Feature/HomeFeature/Sources/HomeCore.swift

View workflow job for this annotation

GitHub Actions / Run Swiftlint

Unused parameter in a closure should be replaced with _ (unused_closure_parameter)
_ = try await userNotificationClient.requestAuthorization([.alert, .badge, .sound])
_ = try await authAPIClient.login(deviceID: "deviceID", apiClient: apiClient)
}

case .localPushButtonTapped:
Expand Down
2 changes: 1 addition & 1 deletion XCConfig/Project/Mohanyang.xcconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

APP_NAME = 모하냥

BASE_URL_DEV = dev //dev
BASE_URL_DEV = dev.api.pomonyang.com //dev
BASE_URL_PROD = prod //prod
BASE_URL = $(BASE_URL_$(CONFIGURATION))
MARKETING_VERSION = 0.0.1
Expand Down

0 comments on commit b95417e

Please sign in to comment.