Skip to content

Commit

Permalink
feat: #4 - 코드리뷰 반영 - 불필요한 네트워크 모듈 파일 삭제
Browse files Browse the repository at this point in the history
  • Loading branch information
hryeong66 committed Jun 13, 2024
1 parent 86ad29b commit b1e342a
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 128 deletions.
34 changes: 0 additions & 34 deletions Projects/Core/PPACNetwork/Sources/APIService.swift

This file was deleted.

14 changes: 0 additions & 14 deletions Projects/Core/PPACNetwork/Sources/APIServiceError.swift

This file was deleted.

21 changes: 21 additions & 0 deletions Projects/Core/PPACNetwork/Sources/NetworkError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// APIServiceError.swift
// PPACNetwork
//
// Created by 장혜령 on 6/8/24.
//

import Foundation

enum NetworkError: Error {
/// encoding Error
case urlEncodingError
/// response Error
case invalidResponse
/// statucCode 400 ~ 499 error
case clientError(statusCode: Int, message: String?)
/// statucCode 500 ~ error
case serverError(statusCode: Int, message: String?)
/// 알 수 없는 에러
case unknown
}
33 changes: 0 additions & 33 deletions Projects/Core/PPACNetwork/Sources/NetworkRequest.swift

This file was deleted.

44 changes: 44 additions & 0 deletions Projects/Core/PPACNetwork/Sources/NetworkService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// NetworkService.swift
// PPACNetwork
//
// Created by 장혜령 on 6/8/24.
//

import Foundation

final class NetworkService: NetworkServiceable {

func request<T: Decodable>(_ request: Requestable) async -> (data: T?, error: Error?) {
guard let encodedUrl = request.url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed),
let url = URL(string: encodedUrl) else {
return (nil, NetworkError.urlEncodingError)
}

let urlRequest = request.buildURLRequest(with: url)

let (data, response): (Data, URLResponse)
do {
(data, response) = try await URLSession.shared.data(for: urlRequest)
} catch {
return (nil, NetworkError.invalidResponse)
}

guard let httpResponse = response as? HTTPURLResponse else {
return (nil, NetworkError.invalidResponse)
}

switch httpResponse.statusCode {
case 200..<300:
let decoder = JSONDecoder()
let decodedData = try? decoder.decode(T.self, from: data)
return (decodedData, nil)
case 400..<500:
return (nil, NetworkError.clientError(statusCode: httpResponse.statusCode, message: String(data: data, encoding: .utf8)))
case 500..<600:
return (nil, NetworkError.serverError(statusCode: httpResponse.statusCode, message: String(data: data, encoding: .utf8)))
default:
return (nil, NetworkError.unknown)
}
}
}
14 changes: 0 additions & 14 deletions Projects/Features/Home/Sources/Service/ExampleServiceable.swift

This file was deleted.

33 changes: 0 additions & 33 deletions Projects/Features/Home/Sources/Service/ExempleAPIClient.swift

This file was deleted.

0 comments on commit b1e342a

Please sign in to comment.