Skip to content

Commit

Permalink
Merge pull request #4 from eunho8713/STEP1-2
Browse files Browse the repository at this point in the history
Step1 2
  • Loading branch information
dddowon authored Nov 9, 2022
2 parents 9419507 + b04433c commit 4ef21c0
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 92 deletions.
15 changes: 4 additions & 11 deletions OpenMarket/JSONParsingTests/JSONParsingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import XCTest
@testable import OpenMarket

final class JSONParsingTests: XCTestCase {
let jsonParser = JsonParser()

override func setUpWithError() throws {
try super.setUpWithError()
Expand All @@ -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)
}

}
16 changes: 12 additions & 4 deletions OpenMarket/OpenMarket.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -48,15 +50,17 @@
325B2A682914CF48009354D3 /* OpenMarketAPI.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OpenMarketAPI.swift; sourceTree = "<group>"; };
325B2A6A2914CF50009354D3 /* OpenMarketURL.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OpenMarketURL.swift; sourceTree = "<group>"; };
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 = "<group>"; };
325B7EBB29123E5000CEAF98 /* DetailPage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DetailPage.swift; sourceTree = "<group>"; };
325B7EBD2913630800CEAF98 /* NetWorkError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetWorkError.swift; sourceTree = "<group>"; };
32687C532907B36E005799CA /* Product.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Product.swift; sourceTree = "<group>"; };
AA0CE0242908BD8E00A41C87 /* JsonParser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JsonParser.swift; sourceTree = "<group>"; };
AA0CE0242908BD8E00A41C87 /* JSONDecodable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JSONDecodable.swift; sourceTree = "<group>"; };
AA0CE0272908CAEE00A41C87 /* ListPage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ListPage.swift; sourceTree = "<group>"; };
AA0CE0292908CB2100A41C87 /* Images.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Images.swift; sourceTree = "<group>"; };
AA0CE02B2908CB2F00A41C87 /* Vendors.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Vendors.swift; sourceTree = "<group>"; };
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 = "<group>"; };
AA3D815F291A1B1600276292 /* JSONParsingError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JSONParsingError.swift; sourceTree = "<group>"; };
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 = "<group>"; };
C70FB0FC25BEF61C00C9924E /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -97,6 +101,7 @@
children = (
325B2A682914CF48009354D3 /* OpenMarketAPI.swift */,
325B2A6A2914CF50009354D3 /* OpenMarketURL.swift */,
AA0CE0242908BD8E00A41C87 /* JSONDecodable.swift */,
);
path = Protocol;
sourceTree = "<group>";
Expand All @@ -105,7 +110,7 @@
isa = PBXGroup;
children = (
3258D0DA290F6E3900C25807 /* NetWork.swift */,
AA0CE0242908BD8E00A41C87 /* JsonParser.swift */,
325B2A7A291B3EE3009354D3 /* JSONParser.swift */,
);
path = NetWork;
sourceTree = "<group>";
Expand All @@ -114,6 +119,7 @@
isa = PBXGroup;
children = (
325B7EBD2913630800CEAF98 /* NetWorkError.swift */,
AA3D815F291A1B1600276292 /* JSONParsingError.swift */,
);
path = Error;
sourceTree = "<group>";
Expand Down Expand Up @@ -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 */,
Expand Down
25 changes: 4 additions & 21 deletions OpenMarket/OpenMarket/Controller/ViewController.swift
Original file line number Diff line number Diff line change
@@ -1,34 +1,17 @@
//
// OpenMarket - ViewController.swift
// Created by yagom.
// Created by yagom.
// Copyright © yagom. All rights reserved.
//

import UIKit
import Foundation

class ViewController: UIViewController {
let network = NetWork()

let jsonParser = JSONParser()
override func viewDidLoad() {
super.viewDidLoad()
network.getDetailPageData { (result: Result<DetailPage, Error>) in
switch result {
case .success(let data):
print(data.id)
case .failure(let failure):
print(failure)
}
}

network.getProductData { (result: Result<Product, Error>) in
switch result {
case .success(let data):
print(data.pageNo)
case .failure(let failure):
print(failure)
}
}

let a = try? jsonParser.getMockData()
print(a?.pageNo)
}
}
19 changes: 19 additions & 0 deletions OpenMarket/OpenMarket/Error/JSONParsingError.swift
Original file line number Diff line number Diff line change
@@ -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 디코딩 오류"
}
}
}
12 changes: 6 additions & 6 deletions OpenMarket/OpenMarket/Error/NetWorkError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 "네트워크 디코딩 오류"
}
}
}
18 changes: 18 additions & 0 deletions OpenMarket/OpenMarket/NetWork/JSONParser.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
24 changes: 0 additions & 24 deletions OpenMarket/OpenMarket/NetWork/JsonParser.swift

This file was deleted.

24 changes: 3 additions & 21 deletions OpenMarket/OpenMarket/NetWork/NetWork.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,16 @@ import UIKit

class NetWork: OpenMarketAPI {
func getProductData(completion: @escaping (Result<Product, Error>) -> 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<DetailPage, Error>) -> 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<T: Decodable>(url: String, completion: @escaping (Result<T, Error>) -> Void ) {
guard let url = URL(string: url) else {
Expand Down Expand Up @@ -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))
}
}
}
Expand Down
23 changes: 23 additions & 0 deletions OpenMarket/OpenMarket/Protocol/JSONDecodable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// JsonPaser.swift
// OpenMarket
//
// Created by 박도원 on 2022/10/26.
//

import UIKit

protocol JSONDecodable { }

extension JSONDecodable {
func mockParsingData<T: Decodable>(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
}
}
10 changes: 5 additions & 5 deletions OpenMarket/OpenMarket/Protocol/OpenMarketURL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Binary file modified OpenMarket/OpenMarket/View/Assets.xcassets/.DS_Store
Binary file not shown.

0 comments on commit 4ef21c0

Please sign in to comment.