-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Endpoint 추가 및 네트워크 통신 설정및 오류메세지 추가
- Loading branch information
1 parent
63d2844
commit b0313b5
Showing
10 changed files
with
176 additions
and
23 deletions.
There are no files selected for viewing
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
Binary file modified
BIN
+19.5 KB
(150%)
...proj/project.xcworkspace/xcuserdata/hoonhachoi.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
This file was deleted.
Oops, something went wrong.
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,13 @@ | ||
// | ||
// Category.swift | ||
// Side-dish | ||
// | ||
// Created by 조중윤 on 2021/04/19. | ||
// | ||
|
||
import Foundation | ||
|
||
struct SideDishes: Codable { | ||
let statusCode: Int | ||
let body: [Item] | ||
} |
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,28 @@ | ||
// | ||
// EndPoint.swift | ||
// Side-dish | ||
// | ||
// Created by HOONHA CHOI on 2021/04/19. | ||
// | ||
|
||
import Foundation | ||
|
||
enum Endpoint { | ||
private static let scheme = "https" | ||
private static let host = "h3rb9c0ugl.execute-api.ap-northeast-2.amazonaws.com" | ||
private static let basicPath = "/develop/baminchan/" | ||
|
||
static func url(path : Path) -> URL? { | ||
var components = URLComponents() | ||
components.scheme = Endpoint.scheme | ||
components.host = Endpoint.host | ||
components.path = basicPath + "\(path)" | ||
return components.url | ||
} | ||
} | ||
|
||
enum Path: String { | ||
case main = "1main1" | ||
case soup = "soup" | ||
case side = "side" | ||
} |
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,13 @@ | ||
// | ||
// HTTPMethod.swift | ||
// Side-dish | ||
// | ||
// Created by HOONHA CHOI on 2021/04/19. | ||
// | ||
|
||
import Foundation | ||
|
||
enum HTTPMethod: String { | ||
case get = "GET" | ||
case post = "POST" | ||
} |
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,34 @@ | ||
// | ||
// NetworkError.swift | ||
// Side-dish | ||
// | ||
// Created by HOONHA CHOI on 2021/04/19. | ||
// | ||
|
||
import Foundation | ||
|
||
enum NetworkError : Error { | ||
case invalidURL | ||
case invalidRequest | ||
case invalidResponse | ||
case invalidStatusCode(Int) | ||
case emptyData | ||
case failParsing | ||
|
||
var message: String { | ||
switch self { | ||
case .invalidURL: | ||
return "올바르지 않은 URL입니다" | ||
case .invalidRequest: | ||
return "올바르지 않은 요청입니다" | ||
case .invalidResponse: | ||
return "올바르지 않은 응답입니다" | ||
case .invalidStatusCode(let code): | ||
return "올바르지 않은 응답 코드입니다 \(code)" | ||
case .emptyData: | ||
return "올바르지 않은 데이터입니다" | ||
case .failParsing: | ||
return "올바르지 않은 데이터모델입니다" | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
Side-dish/Side-dish/DataLayer/Network/NetworkManager.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,48 @@ | ||
// | ||
// NetworkManager.swift | ||
// Side-dish | ||
// | ||
// Created by HOONHA CHOI on 2021/04/19. | ||
// | ||
|
||
import Foundation | ||
import Combine | ||
|
||
class NetworkManager { | ||
|
||
func getResource(path: Path, method: HTTPMethod) -> AnyPublisher<SideDishes, NetworkError> { | ||
guard let urlRequest = makeURLRequest(path: path, method: method) else { | ||
return Fail(error: NetworkError.invalidURL).eraseToAnyPublisher() | ||
} | ||
return URLSession.shared.dataTaskPublisher(for: urlRequest) | ||
.mapError { _ in | ||
NetworkError.invalidRequest | ||
} | ||
.tryMap{ data , response -> Data in | ||
guard let httpResponse = response as? HTTPURLResponse else { | ||
throw NetworkError.invalidResponse | ||
} | ||
guard 200..<300 ~= httpResponse.statusCode else { | ||
throw NetworkError.invalidStatusCode(httpResponse.statusCode) | ||
} | ||
guard !data.isEmpty else { | ||
throw NetworkError.emptyData | ||
} | ||
return data | ||
} | ||
.decode(type: SideDishes.self, decoder: JSONDecoder()) | ||
.mapError { _ in | ||
NetworkError.failParsing | ||
} | ||
.eraseToAnyPublisher() | ||
} | ||
|
||
private func makeURLRequest(path: Path, method: HTTPMethod) -> URLRequest? { | ||
guard let url = Endpoint.url(path: path) else { | ||
return nil | ||
} | ||
var request = URLRequest(url: url) | ||
request.httpMethod = method.rawValue | ||
return request | ||
} | ||
} |
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
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