diff --git a/OpenMarket/JSONParsingTests/JSONParsingTests.swift b/OpenMarket/JSONParsingTests/JSONParsingTests.swift index 0223c7a..e89ea1d 100644 --- a/OpenMarket/JSONParsingTests/JSONParsingTests.swift +++ b/OpenMarket/JSONParsingTests/JSONParsingTests.swift @@ -9,7 +9,6 @@ import XCTest @testable import OpenMarket final class JSONParsingTests: XCTestCase { - let jsonParser = JsonParser() override func setUpWithError() throws { try super.setUpWithError() @@ -18,19 +17,13 @@ final class JSONParsingTests: XCTestCase { override func tearDownWithError() throws { try super.tearDownWithError() } - - func test_파싱_되는지_확인() { - let product = jsonParser.jsonParsingData() - - print(product) - } - func test_파싱한데이터_pageNo와_1이랑_비교() { - let product = jsonParser.jsonParsingData() + func test_파싱한데이터의_pageNo는_1이다() { + let pageNumber = 1 - let pageNo = product?.pageNo + let product = try? JSONParser().getMockData() - XCTAssertEqual(1, pageNo) + XCTAssertEqual(pageNumber, product?.pageNo) } } diff --git a/OpenMarket/OpenMarket.xcodeproj/project.pbxproj b/OpenMarket/OpenMarket.xcodeproj/project.pbxproj index be33246..0a6c2a5 100644 --- a/OpenMarket/OpenMarket.xcodeproj/project.pbxproj +++ b/OpenMarket/OpenMarket.xcodeproj/project.pbxproj @@ -10,14 +10,16 @@ 3258D0DB290F6E3900C25807 /* NetWork.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3258D0DA290F6E3900C25807 /* NetWork.swift */; }; 325B2A692914CF48009354D3 /* OpenMarketAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = 325B2A682914CF48009354D3 /* OpenMarketAPI.swift */; }; 325B2A6B2914CF50009354D3 /* OpenMarketURL.swift in Sources */ = {isa = PBXBuildFile; fileRef = 325B2A6A2914CF50009354D3 /* OpenMarketURL.swift */; }; + 325B2A7B291B3EE3009354D3 /* JSONParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = 325B2A7A291B3EE3009354D3 /* JSONParser.swift */; }; 325B7EBC29123E5000CEAF98 /* DetailPage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 325B7EBB29123E5000CEAF98 /* DetailPage.swift */; }; 325B7EBE2913630800CEAF98 /* NetWorkError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 325B7EBD2913630800CEAF98 /* NetWorkError.swift */; }; 32687C542907B36E005799CA /* Product.swift in Sources */ = {isa = PBXBuildFile; fileRef = 32687C532907B36E005799CA /* Product.swift */; }; - AA0CE0252908BD8E00A41C87 /* JsonParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA0CE0242908BD8E00A41C87 /* JsonParser.swift */; }; + AA0CE0252908BD8E00A41C87 /* JSONDecodable.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA0CE0242908BD8E00A41C87 /* JSONDecodable.swift */; }; AA0CE0282908CAEE00A41C87 /* ListPage.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA0CE0272908CAEE00A41C87 /* ListPage.swift */; }; AA0CE02A2908CB2100A41C87 /* Images.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA0CE0292908CB2100A41C87 /* Images.swift */; }; AA0CE02C2908CB2F00A41C87 /* Vendors.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA0CE02B2908CB2F00A41C87 /* Vendors.swift */; }; AA0CE0342908FE0700A41C87 /* JSONParsingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA0CE0332908FE0700A41C87 /* JSONParsingTests.swift */; }; + AA3D8160291A1B1600276292 /* JSONParsingError.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA3D815F291A1B1600276292 /* JSONParsingError.swift */; }; C70FB0FB25BEF61C00C9924E /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = C70FB0FA25BEF61C00C9924E /* AppDelegate.swift */; }; C70FB0FD25BEF61C00C9924E /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = C70FB0FC25BEF61C00C9924E /* SceneDelegate.swift */; }; C70FB0FF25BEF61C00C9924E /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C70FB0FE25BEF61C00C9924E /* ViewController.swift */; }; @@ -48,15 +50,17 @@ 325B2A682914CF48009354D3 /* OpenMarketAPI.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OpenMarketAPI.swift; sourceTree = ""; }; 325B2A6A2914CF50009354D3 /* OpenMarketURL.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OpenMarketURL.swift; sourceTree = ""; }; 325B2A702914D49A009354D3 /* OpenMarketNetWorkTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = OpenMarketNetWorkTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; + 325B2A7A291B3EE3009354D3 /* JSONParser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JSONParser.swift; sourceTree = ""; }; 325B7EBB29123E5000CEAF98 /* DetailPage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DetailPage.swift; sourceTree = ""; }; 325B7EBD2913630800CEAF98 /* NetWorkError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetWorkError.swift; sourceTree = ""; }; 32687C532907B36E005799CA /* Product.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Product.swift; sourceTree = ""; }; - AA0CE0242908BD8E00A41C87 /* JsonParser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JsonParser.swift; sourceTree = ""; }; + AA0CE0242908BD8E00A41C87 /* JSONDecodable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JSONDecodable.swift; sourceTree = ""; }; AA0CE0272908CAEE00A41C87 /* ListPage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ListPage.swift; sourceTree = ""; }; AA0CE0292908CB2100A41C87 /* Images.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Images.swift; sourceTree = ""; }; AA0CE02B2908CB2F00A41C87 /* Vendors.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Vendors.swift; sourceTree = ""; }; AA0CE0312908FE0700A41C87 /* JSONParsingTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = JSONParsingTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; AA0CE0332908FE0700A41C87 /* JSONParsingTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JSONParsingTests.swift; sourceTree = ""; }; + AA3D815F291A1B1600276292 /* JSONParsingError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JSONParsingError.swift; sourceTree = ""; }; C70FB0F725BEF61C00C9924E /* OpenMarket.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = OpenMarket.app; sourceTree = BUILT_PRODUCTS_DIR; }; C70FB0FA25BEF61C00C9924E /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; C70FB0FC25BEF61C00C9924E /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = ""; }; @@ -97,6 +101,7 @@ children = ( 325B2A682914CF48009354D3 /* OpenMarketAPI.swift */, 325B2A6A2914CF50009354D3 /* OpenMarketURL.swift */, + AA0CE0242908BD8E00A41C87 /* JSONDecodable.swift */, ); path = Protocol; sourceTree = ""; @@ -105,7 +110,7 @@ isa = PBXGroup; children = ( 3258D0DA290F6E3900C25807 /* NetWork.swift */, - AA0CE0242908BD8E00A41C87 /* JsonParser.swift */, + 325B2A7A291B3EE3009354D3 /* JSONParser.swift */, ); path = NetWork; sourceTree = ""; @@ -114,6 +119,7 @@ isa = PBXGroup; children = ( 325B7EBD2913630800CEAF98 /* NetWorkError.swift */, + AA3D815F291A1B1600276292 /* JSONParsingError.swift */, ); path = Error; sourceTree = ""; @@ -343,9 +349,11 @@ C70FB0FF25BEF61C00C9924E /* ViewController.swift in Sources */, 325B2A6B2914CF50009354D3 /* OpenMarketURL.swift in Sources */, 32687C542907B36E005799CA /* Product.swift in Sources */, + AA3D8160291A1B1600276292 /* JSONParsingError.swift in Sources */, 325B7EBE2913630800CEAF98 /* NetWorkError.swift in Sources */, - AA0CE0252908BD8E00A41C87 /* JsonParser.swift in Sources */, + AA0CE0252908BD8E00A41C87 /* JSONDecodable.swift in Sources */, C70FB0FB25BEF61C00C9924E /* AppDelegate.swift in Sources */, + 325B2A7B291B3EE3009354D3 /* JSONParser.swift in Sources */, 3258D0DB290F6E3900C25807 /* NetWork.swift in Sources */, 325B2A692914CF48009354D3 /* OpenMarketAPI.swift in Sources */, C70FB0FD25BEF61C00C9924E /* SceneDelegate.swift in Sources */, diff --git a/OpenMarket/OpenMarket/Controller/ViewController.swift b/OpenMarket/OpenMarket/Controller/ViewController.swift index 0ff5332..b4c451e 100644 --- a/OpenMarket/OpenMarket/Controller/ViewController.swift +++ b/OpenMarket/OpenMarket/Controller/ViewController.swift @@ -1,6 +1,6 @@ // // OpenMarket - ViewController.swift -// Created by yagom. +// Created by yagom. // Copyright © yagom. All rights reserved. // @@ -8,27 +8,10 @@ import UIKit import Foundation class ViewController: UIViewController { - let network = NetWork() - + let jsonParser = JSONParser() override func viewDidLoad() { super.viewDidLoad() - network.getDetailPageData { (result: Result) in - switch result { - case .success(let data): - print(data.id) - case .failure(let failure): - print(failure) - } - } - - network.getProductData { (result: Result) in - switch result { - case .success(let data): - print(data.pageNo) - case .failure(let failure): - print(failure) - } - } - + let a = try? jsonParser.getMockData() + print(a?.pageNo) } } diff --git a/OpenMarket/OpenMarket/Error/JSONParsingError.swift b/OpenMarket/OpenMarket/Error/JSONParsingError.swift new file mode 100644 index 0000000..77cc05d --- /dev/null +++ b/OpenMarket/OpenMarket/Error/JSONParsingError.swift @@ -0,0 +1,19 @@ +// +// JSONParsingError.swift +// OpenMarket +// +// Created by 박도원 on 2022/11/08. +// + +import Foundation + +enum JSONParsingError: Error, LocalizedError { + case jsonDecodingError + + var errorDescription: String { + switch self { + case .jsonDecodingError: + return "json 디코딩 오류" + } + } +} diff --git a/OpenMarket/OpenMarket/Error/NetWorkError.swift b/OpenMarket/OpenMarket/Error/NetWorkError.swift index 337aeab..fa6f08a 100644 --- a/OpenMarket/OpenMarket/Error/NetWorkError.swift +++ b/OpenMarket/OpenMarket/Error/NetWorkError.swift @@ -8,18 +8,18 @@ import Foundation enum NetWorkError: Error, LocalizedError { - case urlError, unknownError, statusError, decodingError + case urlError, unknownError, statusError, networkDecodingError var errorDescription: String { switch self { case .urlError: - return "URL을 불러오지 못했습니다." + return "url 오류" case .unknownError: - return "알 수 없는 에러입니다." + return "unknown" case .statusError: - return "status코드가 200에서 299사이가 아닙니다." - case .decodingError: - return "디코딩이 수행되지 않았습니다." + return "status코드 오류" + case .networkDecodingError: + return "네트워크 디코딩 오류" } } } diff --git a/OpenMarket/OpenMarket/NetWork/JSONParser.swift b/OpenMarket/OpenMarket/NetWork/JSONParser.swift new file mode 100644 index 0000000..b1f0093 --- /dev/null +++ b/OpenMarket/OpenMarket/NetWork/JSONParser.swift @@ -0,0 +1,18 @@ +// +// JSONParser.swift +// OpenMarket +// +// Created by 이은호 on 2022/11/09. +// + +import Foundation + +class JSONParser: JSONDecodable { + var product: Product? + + func getMockData() throws -> Product? { + let fileName = "products" + guard let mock = try? mockParsingData(name: fileName, data: product) else { throw NetWorkError.unknownError } + return mock + } +} diff --git a/OpenMarket/OpenMarket/NetWork/JsonParser.swift b/OpenMarket/OpenMarket/NetWork/JsonParser.swift deleted file mode 100644 index 3b17dee..0000000 --- a/OpenMarket/OpenMarket/NetWork/JsonParser.swift +++ /dev/null @@ -1,24 +0,0 @@ -// -// JsonPaser.swift -// OpenMarket -// -// Created by 박도원 on 2022/10/26. -// - -import UIKit - -class JsonParser { - enum FileName: String { - case fileName = "products" - } - - func mockParsingData() -> ListPage? { - guard let dataAsset = NSDataAsset(name: FileName.fileName.rawValue) else { - return nil - } - let jsonDecoder = JSONDecoder() - let product = try? jsonDecoder.decode(ListPage.self, from: dataAsset.data) - - return product - } -} diff --git a/OpenMarket/OpenMarket/NetWork/NetWork.swift b/OpenMarket/OpenMarket/NetWork/NetWork.swift index 3054f6c..f421e1b 100644 --- a/OpenMarket/OpenMarket/NetWork/NetWork.swift +++ b/OpenMarket/OpenMarket/NetWork/NetWork.swift @@ -10,34 +10,16 @@ import UIKit class NetWork: OpenMarketAPI { func getProductData(completion: @escaping (Result) -> Void) { - let url = host + listPath + queryParameter + let url = OpenMarketURL.host + OpenMarketURL.listPath + OpenMarketURL.queryParameter self.getOpenMarketData(url: url, completion: completion) } func getDetailPageData(completion: @escaping (Result) -> Void) { - let url = host + detailPath + let url = OpenMarketURL.host + OpenMarketURL.detailPath self.getOpenMarketData(url: url, completion: completion) } } -extension NetWork: OpenMarketURL { - var host: String { - return "https://openmarket.yagom-academy.kr/" - } - - var listPath: String { - return "api/products" - } - - var detailPath: String { - return "api/products/32" - } - - var queryParameter: String { - return "?page_no=1&items_per_page=100" - } -} - extension NetWork { func getOpenMarketData(url: String, completion: @escaping (Result) -> Void ) { guard let url = URL(string: url) else { @@ -67,7 +49,7 @@ extension NetWork { let receivedData = try JSONDecoder().decode(T.self, from: data) completion(.success(receivedData)) } catch { - completion(.failure(NetWorkError.decodingError)) + completion(.failure(NetWorkError.networkDecodingError)) } } } diff --git a/OpenMarket/OpenMarket/Protocol/JSONDecodable.swift b/OpenMarket/OpenMarket/Protocol/JSONDecodable.swift new file mode 100644 index 0000000..28fb4c9 --- /dev/null +++ b/OpenMarket/OpenMarket/Protocol/JSONDecodable.swift @@ -0,0 +1,23 @@ +// +// JsonPaser.swift +// OpenMarket +// +// Created by 박도원 on 2022/10/26. +// + +import UIKit + +protocol JSONDecodable { } + +extension JSONDecodable { + func mockParsingData(name: String, data: T) throws -> T? { + guard let dataAsset = NSDataAsset(name: name) else { + throw JSONParsingError.jsonDecodingError + } + let jsonDecoder = JSONDecoder() + guard let data = try? jsonDecoder.decode(T.self, from: dataAsset.data) else { + throw JSONParsingError.jsonDecodingError + } + return data + } +} diff --git a/OpenMarket/OpenMarket/Protocol/OpenMarketURL.swift b/OpenMarket/OpenMarket/Protocol/OpenMarketURL.swift index 25a4c54..44bab71 100644 --- a/OpenMarket/OpenMarket/Protocol/OpenMarketURL.swift +++ b/OpenMarket/OpenMarket/Protocol/OpenMarketURL.swift @@ -7,9 +7,9 @@ import Foundation -protocol OpenMarketURL { - var host: String { get } - var listPath: String { get } - var detailPath: String { get } - var queryParameter: String { get } +enum OpenMarketURL { + static let host = "https://openmarket.yagom-academy.kr/" + static let listPath = "api/products" + static let detailPath = "api/products/32" + static let queryParameter = "?page_no=1&items_per_page=100" } diff --git a/OpenMarket/OpenMarket/View/Assets.xcassets/.DS_Store b/OpenMarket/OpenMarket/View/Assets.xcassets/.DS_Store index 5dc4ad4..8706a3a 100644 Binary files a/OpenMarket/OpenMarket/View/Assets.xcassets/.DS_Store and b/OpenMarket/OpenMarket/View/Assets.xcassets/.DS_Store differ