Skip to content

Commit

Permalink
feat #144: getServerURL RxMoya 방식 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
minsangKang committed May 22, 2024
1 parent f554462 commit 8c90a07
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 21 deletions.
14 changes: 9 additions & 5 deletions Project_Timer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
8706C3302AEF881500F7C842 /* TTSignupTextFieldWarning.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8706C32F2AEF881500F7C842 /* TTSignupTextFieldWarning.swift */; };
8706C3322AEF8A0300F7C842 /* TTSignupNextButtonForMac.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8706C3312AEF8A0300F7C842 /* TTSignupNextButtonForMac.swift */; };
870800502B2C38F900830B39 /* ServerURLNetwork.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8708004F2B2C38F900830B39 /* ServerURLNetwork.swift */; };
870800522B2C3A0800830B39 /* ServerURLDTO.swift in Sources */ = {isa = PBXBuildFile; fileRef = 870800512B2C3A0800830B39 /* ServerURLDTO.swift */; };
870800522B2C3A0800830B39 /* ServerURLResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 870800512B2C3A0800830B39 /* ServerURLResponse.swift */; };
870800542B2C3A5400830B39 /* ServerURLRepository.swift in Sources */ = {isa = PBXBuildFile; fileRef = 870800532B2C3A5400830B39 /* ServerURLRepository.swift */; };
870800562B2C3AC600830B39 /* ServerURLRepositoryInterface.swift in Sources */ = {isa = PBXBuildFile; fileRef = 870800552B2C3AC600830B39 /* ServerURLRepositoryInterface.swift */; };
870800582B2C3B0C00830B39 /* GetServerURLUseCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 870800572B2C3B0C00830B39 /* GetServerURLUseCase.swift */; };
Expand Down Expand Up @@ -318,6 +318,7 @@
87B90B3A2BF4AEF600D6886E /* FirebaseRepository.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87B90B392BF4AEF600D6886E /* FirebaseRepository.swift */; };
87B90B3C2BF4B13D00D6886E /* AppVersionResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87B90B3B2BF4B13D00D6886E /* AppVersionResponse.swift */; };
87B90B3F2BF4BC6D00D6886E /* ErrorResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87B90B3E2BF4BC6D00D6886E /* ErrorResponse.swift */; };
87B90B412BF4BFEE00D6886E /* TTProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87B90B402BF4BFEE00D6886E /* TTProvider.swift */; };
87BE88E72AD425BE0010A84A /* SigninTextFieldView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87BE88E62AD425BE0010A84A /* SigninTextFieldView.swift */; };
87BEBEE9281C17000095CD29 /* RecordsManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87BEBEE8281C17000095CD29 /* RecordsManager.swift */; };
87BEBEEB281C19CA0095CD29 /* StopwatchVM.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87BEBEEA281C19CA0095CD29 /* StopwatchVM.swift */; };
Expand Down Expand Up @@ -482,7 +483,7 @@
8706C32F2AEF881500F7C842 /* TTSignupTextFieldWarning.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TTSignupTextFieldWarning.swift; sourceTree = "<group>"; };
8706C3312AEF8A0300F7C842 /* TTSignupNextButtonForMac.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TTSignupNextButtonForMac.swift; sourceTree = "<group>"; };
8708004F2B2C38F900830B39 /* ServerURLNetwork.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ServerURLNetwork.swift; sourceTree = "<group>"; };
870800512B2C3A0800830B39 /* ServerURLDTO.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ServerURLDTO.swift; sourceTree = "<group>"; };
870800512B2C3A0800830B39 /* ServerURLResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ServerURLResponse.swift; sourceTree = "<group>"; };
870800532B2C3A5400830B39 /* ServerURLRepository.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ServerURLRepository.swift; sourceTree = "<group>"; };
870800552B2C3AC600830B39 /* ServerURLRepositoryInterface.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ServerURLRepositoryInterface.swift; sourceTree = "<group>"; };
870800572B2C3B0C00830B39 /* GetServerURLUseCase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GetServerURLUseCase.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -736,6 +737,7 @@
87B90B392BF4AEF600D6886E /* FirebaseRepository.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FirebaseRepository.swift; sourceTree = "<group>"; };
87B90B3B2BF4B13D00D6886E /* AppVersionResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppVersionResponse.swift; sourceTree = "<group>"; };
87B90B3E2BF4BC6D00D6886E /* ErrorResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ErrorResponse.swift; sourceTree = "<group>"; };
87B90B402BF4BFEE00D6886E /* TTProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TTProvider.swift; sourceTree = "<group>"; };
87BE88E62AD425BE0010A84A /* SigninTextFieldView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SigninTextFieldView.swift; sourceTree = "<group>"; };
87BEBEE8281C17000095CD29 /* RecordsManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RecordsManager.swift; sourceTree = "<group>"; };
87BEBEEA281C19CA0095CD29 /* StopwatchVM.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StopwatchVM.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1151,7 +1153,6 @@
875FAE222B1C653A008F19D2 /* DTO */ = {
isa = PBXGroup;
children = (
870800512B2C3A0800830B39 /* ServerURLDTO.swift */,
8708005E2B2C5F5F00830B39 /* AuthDTO.swift */,
8708006E2B2CA92C00830B39 /* DailyDTO.swift */,
87F10938284C589A002E31EA /* FunctionInfo.swift */,
Expand Down Expand Up @@ -1728,8 +1729,9 @@
children = (
8708005D2B2C5DF400830B39 /* FirebaseValue */,
87D4DCCE2BA5492700BB5AAB /* SimpleResponse.swift */,
87B90B3B2BF4B13D00D6886E /* AppVersionResponse.swift */,
87B90B3E2BF4BC6D00D6886E /* ErrorResponse.swift */,
87B90B3B2BF4B13D00D6886E /* AppVersionResponse.swift */,
870800512B2C3A0800830B39 /* ServerURLResponse.swift */,
);
path = Response;
sourceTree = "<group>";
Expand Down Expand Up @@ -1936,6 +1938,7 @@
87F778DC2B196E8F00909511 /* Data */ = {
isa = PBXGroup;
children = (
87B90B402BF4BFEE00D6886E /* TTProvider.swift */,
87B90B3D2BF4BC0100D6886E /* Error */,
87D4DCCD2BA548E300BB5AAB /* Response */,
87D4DCCA2BA5476900BB5AAB /* Request */,
Expand Down Expand Up @@ -2437,6 +2440,7 @@
87A8CD292AF0C5CB00D4C1D7 /* OverlayRemoveButtonForTextFieldView.swift in Sources */,
8751C6F628472133007FAFC4 /* SettingCellInfo.swift in Sources */,
87D4DCC72BA52F3800BB5AAB /* ResetPasswordInfo.swift in Sources */,
87B90B412BF4BFEE00D6886E /* TTProvider.swift in Sources */,
8750082D2905998200E27B4A /* TargetTimeSettingPopupVC.swift in Sources */,
8760FCB929541BE3000BCCD1 /* KeyChain.swift in Sources */,
87D5E65328C7477100D53F8D /* MonthTime.swift in Sources */,
Expand All @@ -2460,7 +2464,7 @@
8706C32E2AEF878D00F7C842 /* TTSignupTextFieldUnderlineView.swift in Sources */,
87199F442882BC430017D01A /* StandardDailyGraphView.swift in Sources */,
93B08521248DDF2F00E215BD /* AppDelegate.swift in Sources */,
870800522B2C3A0800830B39 /* ServerURLDTO.swift in Sources */,
870800522B2C3A0800830B39 /* ServerURLResponse.swift in Sources */,
8791C1F227DCD050000D6BA9 /* Notification+Extension.swift in Sources */,
87F771BF2984101700BF1907 /* Todolist.swift in Sources */,
878BF7EF2955E73C0044C079 /* SyncLog.swift in Sources */,
Expand Down
22 changes: 11 additions & 11 deletions Project_Timer/Data/Repository/FirebaseRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import RxSwift
import RxMoya

final class FirebaseRepository {
private let api: MoyaProvider<FirebaseAPI>
private let api: TTProvider<FirebaseAPI>

init(api: MoyaProvider<FirebaseAPI>) {
init(api: TTProvider<FirebaseAPI>) {
self.api = api
}

Expand All @@ -23,15 +23,15 @@ final class FirebaseRepository {
.map(AppVersionResponse.self)
.map { $0.toDomain() }
.catch { error in
if let moyaError = error as? MoyaError {
switch moyaError {
case .statusCode(let response):
return Single.error(NetworkError.serverError(statusCode: response.statusCode))
default:
return Single.error(NetworkError.FAIL)
}
}
return Single.error(NetworkError.DECODEERROR)
}
}

func getServerURL() -> Single<String> {
return self.api.rx.request(.getServerURL)
.map(ServerURLResponse.self)
.map { $0.base.value }
.catch { error in
return Single.error(NetworkError.DECODEERROR)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class ServerURLRepository: ServerURLRepositoryInterface {
switch result.status {
case .SUCCESS:
guard let data = result.data,
let dto = try? JSONDecoder().decode(ServerURLDTO.self, from: data) else {
let dto = try? JSONDecoder().decode(ServerURLResponse.self, from: data) else {
completion(.failure(.DECODEERROR))
return
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
//
// ServerURLDTO.swift
// ServerURLResponse.swift
// Project_Timer
//
// Created by Kang Minsang on 2023/12/15.
// Copyright © 2023 FDEE. All rights reserved.
// Created by Kang Minsang on 2024/05/15.
// Copyright © 2024 FDEE. All rights reserved.
//

import Foundation

struct ServerURLDTO: Decodable {
/// TiTi 서버 URL 정보
struct ServerURLResponse: Decodable {
let base: FirebaseStringValue

private enum RootKey: String, CodingKey {
Expand Down
34 changes: 34 additions & 0 deletions Project_Timer/Data/TTProvider.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// TTProvider.swift
// Project_Timer
//
// Created by Kang Minsang on 2024/05/15.
// Copyright © 2024 FDEE. All rights reserved.
//

import Foundation
import RxSwift
import Moya
import RxMoya

/// 공통적인 에러를 반환하는 Provider
final class TTProvider<T: TargetType>: MoyaProvider<T> {
func request(_ token: T) -> Single<Response> {
return super.rx.request(token)
.catch { error in
self.handleError(error)
}
}

private func handleError(_ error: Error) -> Single<Response> {
if let moyaError = error as? MoyaError {
switch moyaError {
case .statusCode(let response):
return Single.error(NetworkError.serverError(statusCode: response.statusCode))
default:
return Single.error(NetworkError.FAIL)
}
}
return Single.error(NetworkError.FAIL)
}
}

0 comments on commit 8c90a07

Please sign in to comment.