-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2fb947f
commit bb220ae
Showing
1 changed file
with
81 additions
and
0 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
Tving-Clone/Tving-Clone/Data/DataSources/ContentDataSource.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// | ||
// ContentDataSource.swift | ||
// Tving-Clone | ||
// | ||
// Created by Seonwoo Kim on 6/7/24. | ||
// | ||
|
||
import Foundation | ||
|
||
import Moya | ||
|
||
class ContentDataSource { | ||
private var movieProvider = MoyaProvider<MovieTargetType>(plugins: [MoyaLoggingPlugin()]) | ||
} | ||
|
||
extension ContentDataSource { | ||
func getMovieInfo(completion: @escaping (Result<[Content], Error>) -> Void) { | ||
movieProvider.request(.getMovieData) { result in | ||
switch result { | ||
case .success(let response): | ||
let statusCode = response.statusCode | ||
let data = response.data | ||
do { | ||
let networkResult = try self.judgeStatus(by: statusCode, data, MovieResponseDTO.self).toAppData() | ||
completion(.success(networkResult)) | ||
} catch { | ||
completion(.failure(error)) | ||
} | ||
case .failure(let error): | ||
completion(.failure(error)) | ||
} | ||
} | ||
} | ||
|
||
func getDetailInfo(code: String, completion: @escaping (Result<ContentDetail, Error>) -> Void) { | ||
movieProvider.request(.getDetailData(code: code)) { result in | ||
switch result { | ||
case .success(let response): | ||
let statusCode = response.statusCode | ||
let data = response.data | ||
do { | ||
let networkResult = try self.judgeStatus(by: statusCode, data, DetailResponseDTO.self).toDomain() | ||
completion(.success(networkResult)) | ||
} catch { | ||
completion(.failure(error)) | ||
} | ||
case .failure(let error): | ||
completion(.failure(error)) | ||
} | ||
} | ||
} | ||
|
||
private func judgeStatus<T: Codable>(by statusCode: Int, _ data: Data, _ object: T.Type) throws -> T { | ||
switch statusCode { | ||
case 200..<205: | ||
return try isValidData(data: data, T.self) | ||
case 400..<500: | ||
throw NetworkError.requestError | ||
case 500: | ||
throw NetworkError.serverError | ||
default: | ||
throw NetworkError.networkFailure | ||
} | ||
} | ||
|
||
private func isValidData<T: Codable>(data: Data, _ object: T.Type) throws -> T { | ||
let decoder = JSONDecoder() | ||
guard let decodedData = try? decoder.decode(T.self, from: data) else { | ||
print("⛔️ 디코딩 오류가 발생했습니다 ⛔️") | ||
throw NetworkError.decodingError | ||
} | ||
return decodedData | ||
} | ||
} | ||
|
||
enum NetworkError: Error { | ||
case requestError | ||
case serverError | ||
case networkFailure | ||
case decodingError | ||
} |