Skip to content

Commit

Permalink
[Merge] #241 - conflict 해결 및 머지
Browse files Browse the repository at this point in the history
  • Loading branch information
jeongdung-eo committed Mar 14, 2024
2 parents 598f1af + 671d893 commit f9368ae
Show file tree
Hide file tree
Showing 23 changed files with 424 additions and 423 deletions.
89 changes: 45 additions & 44 deletions iOS-NOTTODO/iOS-NOTTODO.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,15 @@ extension ViewControllerFactoryImpl {
// achieve
extension ViewControllerFactoryImpl {
func makeAchieveViewController(coordinator: AchieveCoordinator) -> AchievementViewController {
let missionAPI = DefaultMissionAPI()
let missionAPI = DefaultMissionService()
let manager = AchieveManagerImpl(missionAPI: missionAPI)
let viewModel = AchievementViewModelImpl(coordinator: coordinator, manager: manager)
let viewController = AchievementViewController(viewModel: viewModel)
return viewController
}

func makeAchieveDetailViewController(coordinator: AchieveCoordinator, date: String) -> DetailAchievementViewController {
let missionAPI = DefaultMissionAPI()
let missionAPI = DefaultMissionService()
let manager = AchieveManagerImpl(missionAPI: missionAPI)
let viewModel = DetailAchievementViewModelImpl(coordinator: coordinator, manager: manager)
viewModel.selectedDate(date)
Expand Down
81 changes: 0 additions & 81 deletions iOS-NOTTODO/iOS-NOTTODO/Network/API/Auth/AuthAPI.swift

This file was deleted.

78 changes: 78 additions & 0 deletions iOS-NOTTODO/iOS-NOTTODO/Network/API/AuthAPI/AuthAPI.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//
// AuthAPI.swift
// iOS-NOTTODO
//
// Created by 김민서 on 2023/05/21.
//

import Foundation

import Moya

struct AuthRequest: Codable {
let socialToken: String
let fcmToken: String
var name: String?
}

enum AuthAPI {
case kakaoAuth(social: LoginType, request: AuthRequest)
case appleAuth(social: LoginType, request: AuthRequest)
case logout
case withdrawal
}

extension AuthAPI: BaseAPI {
var domain: BaseDomain {
return .auth
}

var urlPath: String {
switch self {
case .kakaoAuth(let social, _), .appleAuth(let social, _):
return URLConstant.auth + "/\(social.rawValue)"
case .logout:
return URLConstant.authLogout
case .withdrawal:
return URLConstant.authWithdrawal
}
}

var headerType: HeaderType {

switch self {
case .kakaoAuth, .appleAuth:
return .json
case .logout, .withdrawal:
return .jsonWithToken
}
}

var method: Moya.Method {
switch self {
case .kakaoAuth, .appleAuth:
return .post
case .logout, .withdrawal:
return .delete
}
}

var task: Moya.Task {
switch self {
case .kakaoAuth(_, let data):
return .requestJSONEncodable(data)
case .appleAuth(_, let data):
return .requestJSONEncodable(data)
case .logout, .withdrawal:
return .requestPlain
}
}
}

extension Encodable {
var toDictionary: [String: Any] {
guard let object = try? JSONEncoder().encode(self) else { fatalError() }
guard let dictionary = try? JSONSerialization.jsonObject(with: object, options: []) as? [String: Any] else { fatalError() }
return dictionary
}
}
120 changes: 47 additions & 73 deletions iOS-NOTTODO/iOS-NOTTODO/Network/API/BaseAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,94 +2,68 @@
// BaseAPI.swift
// iOS-NOTTODO
//
// Created by JEONGEUN KIM on 3/10/24.
// Created by JEONGEUN KIM on 3/14/24.
//

import Foundation
import Combine

import Alamofire
import Moya

final class BaseAPI<Target: TargetType> {

typealias API = Target

// MARK: - Properties

var cancelBag = Set<AnyCancellable>()

lazy var provider = self.defaultProvider

private lazy var defaultProvider: MoyaProvider<API> = {
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = 10
configuration.timeoutIntervalForResource = 10
configuration.requestCachePolicy = .reloadIgnoringLocalCacheData
let interceptor = AuthInterceptor.shared
let session = Session(configuration: configuration, interceptor: interceptor)
let provider = MoyaProvider<API>(
session: session,
plugins: [MoyaLoggingPlugin()]
)
return provider
}()

// MARK: - Initializers

public init() {}
enum BaseDomain {
case auth
case mission
case recommend
}

// MARK: - Providers

extension BaseAPI {
var `default`: BaseAPI {
self.provider = self.defaultProvider
return self
extension BaseDomain {

var url: String {
switch self {
case .auth:
return "/auth"
case .mission:
return "/mission"
case .recommend:
return "/recommend"
}
}
}

// MARK: - MakeRequests
protocol BaseAPI: TargetType {
var domain: BaseDomain { get }
var urlPath: String { get }
var headerType: HeaderType { get }
}

extension BaseAPI {
var baseURL: URL {
return URL(string: Bundle.main.baseURL)!
}

var path: String {
return domain.url + urlPath
}

func requestWithCombine<T: Decodable>(_ target: API) -> AnyPublisher<T, Error> {
return Future { promise in
self.provider.request(target) { response in
switch response {
case .success(let value):
do {
let decoder = JSONDecoder()
let body = try decoder.decode(T.self, from: value.data)
promise(.success(body))
} catch let error {
promise(.failure(error))
}
case .failure(let error):
if case MoyaError.underlying(let error, _) = error,
case AFError.requestRetryFailed(let retryError, _) = error,
let retryError = retryError as? APIError,
retryError == APIError.tokenReissuanceFailed {
promise(.failure(retryError))
} else {
promise(.failure(error))
}
}
}
}.eraseToAnyPublisher()
var validationType: ValidationType {
return .successCodes
}

// status codea만 사용하는 경우
func requestWithCombineNoResult(_ target: API) -> AnyPublisher<Int, Error> {
return Future { promise in
self.provider.request(target) { response in
switch response {
case .success(let value):
promise(.success(value.statusCode))
case .failure(let error):
promise(.failure(error))
}
}
}.eraseToAnyPublisher()
var headers: [String: String]? {
return headerType.value
}
}

public enum HeaderType {
case json
case jsonWithToken

public var value: [String: String] {
switch self {
case .json:
return ["Content-Type": "application/json"]
case .jsonWithToken:
return ["Content-Type": "application/json",
"Authorization": "\(KeychainUtil.getAccessToken())"]
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// MissionService.swift
// MissionAPI.swift
// iOS-NOTTODO
//
// Created by 강윤서 on 2023/06/07.
Expand All @@ -25,7 +25,7 @@ struct UpdateMissionRequest: Codable {
let goal: String?
}

enum MissionService {
enum MissionAPI {
case addMission(request: AddMissionRequest)
case updateMission(request: UpdateMissionRequest)
case recentMission
Expand All @@ -39,7 +39,7 @@ enum MissionService {
case achieveCalendar(month: String)
}

extension MissionService: BaseService {
extension MissionAPI: BaseAPI {
var domain: BaseDomain {
return .mission
}
Expand Down
Loading

0 comments on commit f9368ae

Please sign in to comment.