Skip to content

Commit

Permalink
feat: APIProvider(#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
sozohoy committed May 21, 2024
1 parent 32f7dd4 commit 2eae03c
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions GsHometown-iOS/GsHometown-iOS/Network/APIProvider.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//
// APIProvider.swift
// GsHometown-iOS
//
// Created by 한지석 on 5/21/24.
//

import Foundation

import Moya

final class APIProvider<T: TargetType>: MoyaProvider<T> {
init() {
let plugin: [PluginType] = [GsPlugin()]
super.init(plugins: plugin)
}

func request<U: Codable>(
_ object: U.Type,
target: T,
completion: @escaping (NetworkResult<U>) -> ()
) {
request(target) { [weak self] result in
guard let self = self else {
completion(.networkFail)
return
}
switch result {
case .success(let response):
let statusCode = response.statusCode
let data = response.data
let networkResult = self.judgeStatus(statusCode,
data,
object.self)
completion(networkResult)
case .failure:
completion(.networkFail)
}
}
}

func judgeStatus<U: Codable>(
_ statusCode: Int,
_ data: Data,
_ object: U.Type
) -> NetworkResult<U> {
switch statusCode {
case 200..<205:
return isVaildData(data, object)
case 400..<500:
return .requestErr
case 500:
return .serverErr
default:
return .networkFail
}
}

private func isVaildData<U: Codable>(
_ data: Data,
_ object: U.Type
) -> NetworkResult<U> {
let decoder = JSONDecoder()
guard let decodedData = try? decoder.decode(
U.self,
from: data
) else {
print("⛔️ \(self)애서 디코딩 오류가 발생했습니다 ⛔️")
return .pathErr
}
return .success(decodedData)
}
}

0 comments on commit 2eae03c

Please sign in to comment.