Skip to content

Commit

Permalink
Feat: Endpoint 추가 및 네트워크 통신 설정및 오류메세지 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
HoonHaChoi committed Apr 19, 2021
1 parent 63d2844 commit b0313b5
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 23 deletions.
32 changes: 28 additions & 4 deletions Side-dish/Side-dish.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,24 @@
8844B7BF262D6C3000FA49E9 /* FoodCardCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8844B7BE262D6C3000FA49E9 /* FoodCardCell.swift */; };
8844B7C4262D6ED500FA49E9 /* FoodCardCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 8844B7C3262D6ED500FA49E9 /* FoodCardCell.xib */; };
8844B7D0262DB6A500FA49E9 /* Item.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8844B7CF262DB6A500FA49E9 /* Item.swift */; };
8844B7D4262DBA3F00FA49E9 /* Category.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8844B7D3262DBA3F00FA49E9 /* Category.swift */; };
8844B7D4262DBA3F00FA49E9 /* SideDishes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8844B7D3262DBA3F00FA49E9 /* SideDishes.swift */; };
BFCE4953262D4891006C0882 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = BFCE4952262D4891006C0882 /* AppDelegate.swift */; };
BFCE4955262D4891006C0882 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = BFCE4954262D4891006C0882 /* SceneDelegate.swift */; };
BFCE4957262D4891006C0882 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BFCE4956262D4891006C0882 /* ViewController.swift */; };
BFCE495C262D4895006C0882 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = BFCE495B262D4895006C0882 /* Assets.xcassets */; };
BFCE495F262D4895006C0882 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = BFCE495D262D4895006C0882 /* LaunchScreen.storyboard */; };
BFCE496F262D883F006C0882 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = BFCE496E262D883F006C0882 /* Main.storyboard */; };
BFCE4976262DBFE2006C0882 /* EndPoint.swift in Sources */ = {isa = PBXBuildFile; fileRef = BFCE4975262DBFE2006C0882 /* EndPoint.swift */; };
BFCE497A262DC205006C0882 /* HTTPMethod.swift in Sources */ = {isa = PBXBuildFile; fileRef = BFCE4979262DC205006C0882 /* HTTPMethod.swift */; };
BFCE497D262DC22A006C0882 /* NetworkError.swift in Sources */ = {isa = PBXBuildFile; fileRef = BFCE497C262DC22A006C0882 /* NetworkError.swift */; };
BFCE4980262DC3F2006C0882 /* NetworkManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = BFCE497F262DC3F2006C0882 /* NetworkManager.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
8844B7BE262D6C3000FA49E9 /* FoodCardCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FoodCardCell.swift; sourceTree = "<group>"; };
8844B7C3262D6ED500FA49E9 /* FoodCardCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = FoodCardCell.xib; sourceTree = "<group>"; };
8844B7CF262DB6A500FA49E9 /* Item.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Item.swift; sourceTree = "<group>"; };
8844B7D3262DBA3F00FA49E9 /* Category.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Category.swift; sourceTree = "<group>"; };
8844B7D3262DBA3F00FA49E9 /* SideDishes.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SideDishes.swift; sourceTree = "<group>"; };
BFCE494F262D4891006C0882 /* Side-dish.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Side-dish.app"; sourceTree = BUILT_PRODUCTS_DIR; };
BFCE4952262D4891006C0882 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
BFCE4954262D4891006C0882 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
Expand All @@ -32,6 +36,10 @@
BFCE495E262D4895006C0882 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
BFCE4960262D4895006C0882 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
BFCE496E262D883F006C0882 /* Main.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = Main.storyboard; sourceTree = "<group>"; };
BFCE4975262DBFE2006C0882 /* EndPoint.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EndPoint.swift; sourceTree = "<group>"; };
BFCE4979262DC205006C0882 /* HTTPMethod.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HTTPMethod.swift; sourceTree = "<group>"; };
BFCE497C262DC22A006C0882 /* NetworkError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkError.swift; sourceTree = "<group>"; };
BFCE497F262DC3F2006C0882 /* NetworkManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkManager.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand All @@ -57,6 +65,7 @@
8844B7CD262DB62C00FA49E9 /* DataLayer */ = {
isa = PBXGroup;
children = (
BFCE4974262DBFCC006C0882 /* Network */,
8844B7CE262DB63B00FA49E9 /* Model */,
);
path = DataLayer;
Expand All @@ -66,7 +75,7 @@
isa = PBXGroup;
children = (
8844B7CF262DB6A500FA49E9 /* Item.swift */,
8844B7D3262DBA3F00FA49E9 /* Category.swift */,
8844B7D3262DBA3F00FA49E9 /* SideDishes.swift */,
);
path = Model;
sourceTree = "<group>";
Expand Down Expand Up @@ -103,6 +112,17 @@
path = "Side-dish";
sourceTree = "<group>";
};
BFCE4974262DBFCC006C0882 /* Network */ = {
isa = PBXGroup;
children = (
BFCE4975262DBFE2006C0882 /* EndPoint.swift */,
BFCE4979262DC205006C0882 /* HTTPMethod.swift */,
BFCE497C262DC22A006C0882 /* NetworkError.swift */,
BFCE497F262DC3F2006C0882 /* NetworkManager.swift */,
);
path = Network;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -176,10 +196,14 @@
files = (
8844B7D0262DB6A500FA49E9 /* Item.swift in Sources */,
BFCE4957262D4891006C0882 /* ViewController.swift in Sources */,
BFCE4980262DC3F2006C0882 /* NetworkManager.swift in Sources */,
BFCE4953262D4891006C0882 /* AppDelegate.swift in Sources */,
BFCE4955262D4891006C0882 /* SceneDelegate.swift in Sources */,
BFCE497A262DC205006C0882 /* HTTPMethod.swift in Sources */,
8844B7BF262D6C3000FA49E9 /* FoodCardCell.swift in Sources */,
8844B7D4262DBA3F00FA49E9 /* Category.swift in Sources */,
BFCE497D262DC22A006C0882 /* NetworkError.swift in Sources */,
BFCE4976262DBFE2006C0882 /* EndPoint.swift in Sources */,
8844B7D4262DBA3F00FA49E9 /* SideDishes.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Binary file not shown.
18 changes: 0 additions & 18 deletions Side-dish/Side-dish/DataLayer/Model/Category.swift

This file was deleted.

13 changes: 13 additions & 0 deletions Side-dish/Side-dish/DataLayer/Model/SideDishes.swift
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]
}
28 changes: 28 additions & 0 deletions Side-dish/Side-dish/DataLayer/Network/EndPoint.swift
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"
}
13 changes: 13 additions & 0 deletions Side-dish/Side-dish/DataLayer/Network/HTTPMethod.swift
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"
}
34 changes: 34 additions & 0 deletions Side-dish/Side-dish/DataLayer/Network/NetworkError.swift
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 Side-dish/Side-dish/DataLayer/Network/NetworkManager.swift
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
}
}
2 changes: 1 addition & 1 deletion Side-dish/Side-dish/View/FoodCardCell.xib
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
</constraints>
</collectionViewCellContentView>
<size key="customSize" width="342" height="130"/>
<point key="canvasLocation" x="193" y="169"/>
<point key="canvasLocation" x="192.75362318840581" y="168.75"/>
</collectionViewCell>
</objects>
<resources>
Expand Down
11 changes: 11 additions & 0 deletions Side-dish/Side-dish/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,27 @@
//

import UIKit
import Combine

class ViewController: UIViewController {

@IBOutlet weak var SideDishCollectionView: UICollectionView!
private var cancellable = Set<AnyCancellable>()

override func viewDidLoad() {
super.viewDidLoad()
SideDishCollectionView.register(FoodCardCell.nib, forCellWithReuseIdentifier: FoodCardCell.identifier)
SideDishCollectionView.dataSource = self
SideDishCollectionView.delegate = self

NetworkManager().getResource(path: .main, method: .get).sink { (complete) in
if case .failure(let error) = complete {
print(error)
}
} receiveValue: { (category) in
print(category)
}.store(in: &cancellable)

}

}
Expand Down

0 comments on commit b0313b5

Please sign in to comment.