Skip to content

Commit

Permalink
[Feat] sopt-makers#360 - 파트 랭킹 네트워크 레이어
Browse files Browse the repository at this point in the history
  • Loading branch information
lsj8706 committed Apr 5, 2024
1 parent 24a8d46 commit 4e31c7d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 65 deletions.
115 changes: 59 additions & 56 deletions SOPT-iOS/Projects/Modules/Networks/Sources/API/RankAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,69 +12,72 @@ import Alamofire
import Moya

public enum RankAPI {
case rank
case currentRank
case rankDetail(userName: String)
case rank
case currentRank
case rankDetail(userName: String)
case rankPart
}

extension RankAPI: BaseAPI {

public static var apiType: APIType = .rank

// MARK: - Header
public var headers: [String: String]? {
switch self {
case .rank, .rankDetail:
return HeaderType.jsonWithToken.value
default: return HeaderType.json.value
}

public static var apiType: APIType = .rank

// MARK: - Header
public var headers: [String: String]? {
switch self {
case .rank, .rankDetail:
return HeaderType.jsonWithToken.value
default: return HeaderType.json.value
}

// MARK: - Path
public var path: String {
switch self {
case .rank:
return ""
case .currentRank:
return "/current"
case .rankDetail:
return "/detail"
}
}

// MARK: - Path
public var path: String {
switch self {
case .rank:
return ""
case .currentRank:
return "/current"
case .rankDetail:
return "/detail"
case .rankPart:
return "/part"
}
// MARK: - Method
public var method: Moya.Method {
switch self {
default: return .get
}
}

// MARK: - Method
public var method: Moya.Method {
switch self {
default: return .get
}

// MARK: - Parameters
private var bodyParameters: Parameters? {
var params: Parameters = [:]
switch self {
case .rankDetail(let userName):
params["nickname"] = userName
default: break
}
return params
}

// MARK: - Parameters
private var bodyParameters: Parameters? {
var params: Parameters = [:]
switch self {
case .rankDetail(let userName):
params["nickname"] = userName
default: break
}

private var parameterEncoding: ParameterEncoding {
switch self {
case .rankDetail:
return URLEncoding.default
default:
return JSONEncoding.default
}
return params
}

private var parameterEncoding: ParameterEncoding {
switch self {
case .rankDetail:
return URLEncoding.default
default:
return JSONEncoding.default
}
public var task: Task {
switch self {
case .rankDetail:
return .requestParameters(parameters: bodyParameters ?? [:], encoding: parameterEncoding)
default:
return .requestPlain
}
}

public var task: Task {
switch self {
case .rankDetail:
return .requestParameters(parameters: bodyParameters ?? [:], encoding: parameterEncoding)
default:
return .requestPlain
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,21 @@ import Moya
public typealias DefaultRankService = BaseService<RankAPI>

public protocol RankService {
func fetchRankingList(isCurrentGeneration: Bool) -> AnyPublisher<[RankingEntity], Error>
func fetchRankDetail(userName: String) -> AnyPublisher<RankDetailEntity, Error>
func fetchRankingList(isCurrentGeneration: Bool) -> AnyPublisher<[RankingEntity], Error>
func fetchRankDetail(userName: String) -> AnyPublisher<RankDetailEntity, Error>
func fetchPartRanking() -> AnyPublisher<[PartRankingEntity], Error>
}

extension DefaultRankService: RankService {
public func fetchRankingList(isCurrentGeneration: Bool) -> AnyPublisher<[RankingEntity], Error> {
requestObjectInCombine(isCurrentGeneration ? RankAPI.currentRank : RankAPI.rank)
}

public func fetchRankDetail(userName: String) -> AnyPublisher<RankDetailEntity, Error> {
requestObjectInCombine(RankAPI.rankDetail(userName: userName))
}
public func fetchRankingList(isCurrentGeneration: Bool) -> AnyPublisher<[RankingEntity], Error> {
requestObjectInCombine(isCurrentGeneration ? RankAPI.currentRank : RankAPI.rank)
}

public func fetchRankDetail(userName: String) -> AnyPublisher<RankDetailEntity, Error> {
requestObjectInCombine(RankAPI.rankDetail(userName: userName))
}

public func fetchPartRanking() -> AnyPublisher<[PartRankingEntity], Error> {
requestObjectInCombine(.rankPart)
}
}

0 comments on commit 4e31c7d

Please sign in to comment.