Skip to content

Commit

Permalink
Merge branch 'feature/Onboarding/10' of https://github.com/PyeonHaeng…
Browse files Browse the repository at this point in the history
…/PyeonHaeng-iOS into feature/Onboarding/10
  • Loading branch information
isGeekCode committed Feb 4, 2024
2 parents 02ad554 + d961952 commit b5e3816
Show file tree
Hide file tree
Showing 239 changed files with 4,535 additions and 282 deletions.
5 changes: 3 additions & 2 deletions APIService/Package.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// swift-tools-version: 5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

Expand All @@ -18,20 +17,22 @@ let package = Package(
],
dependencies: [
.package(path: "../Core"),
.package(path: "../Entity"),
],
targets: [
.target(
name: "HomeAPI",
dependencies: [
.product(name: "Network", package: "Core"),
.product(name: "Entity", package: "Entity"),
]
),
.target(
name: "HomeAPISupport",
dependencies: [
"HomeAPI",
],
resources: [.process("HomeProductResponse.json")]
resources: [.process("Mocks")]
),
]
)
37 changes: 32 additions & 5 deletions APIService/Sources/HomeAPI/HomeEndPoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,39 @@
import Foundation
import Network

struct HomeEndPoint: EndPoint {
var method: HTTPMethod
// MARK: - HomeEndPoint

var path: String
public enum HomeEndPoint {
case fetchProducts(ProductRequest)
case fetchCount(ProductCountRequest)
}

// MARK: EndPoint

extension HomeEndPoint: EndPoint {
public var method: HTTPMethod {
.get
}

public var path: String {
switch self {
case .fetchProducts:
"/v2/products"
case .fetchCount:
"/v2/products/count"
}
}

var parameters: Network.HTTPParameter
public var parameters: HTTPParameter {
switch self {
case let .fetchProducts(requestModel):
.query(requestModel)
case let .fetchCount(requestModel):
.query(requestModel)
}
}

var headers: [String: String]
public var headers: [String: String] {
["Content-Type": "application/json"]
}
}
54 changes: 54 additions & 0 deletions APIService/Sources/HomeAPI/HomeService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// HomeService.swift
//
//
// Created by 홍승현 on 1/31/24.
//

import Entity
import Foundation
import Network

// MARK: - HomeServiceRepresentable

public protocol HomeServiceRepresentable {
func fetchProductList(request: ProductRequest) async throws -> [Product]
func fetchProductCount(request: ProductCountRequest) async throws -> Int
}

// MARK: - HomeService

public struct HomeService {
private let network: Networking

public init(network: Networking) {
self.network = network
}
}

// MARK: HomeServiceRepresentable

extension HomeService: HomeServiceRepresentable {
public func fetchProductList(request: ProductRequest) async throws -> [Product] {
let products: [ProductResponse] = try await network.request(with: HomeEndPoint.fetchProducts(request))
return products.map(Product.init)
}

public func fetchProductCount(request: ProductCountRequest) async throws -> Int {
let countResponse: ProductCountResponse = try await network.request(with: HomeEndPoint.fetchCount(request))
return countResponse.count
}
}

private extension Product {
init(dto: ProductResponse) {
self.init(
id: dto.id,
imageURL: dto.imageURL,
price: dto.price,
name: dto.name,
promotion: dto.promotion,
convenienceStore: dto.convenienceStore
)
}
}
17 changes: 17 additions & 0 deletions APIService/Sources/HomeAPI/Requests/ProductCountRequest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// ProductCountRequest.swift
//
//
// Created by 홍승현 on 2/2/24.
//

import Entity
import Foundation

public struct ProductCountRequest: Encodable {
public let convenienceStore: ConvenienceStore

public init(convenienceStore: ConvenienceStore) {
self.convenienceStore = convenienceStore
}
}
25 changes: 25 additions & 0 deletions APIService/Sources/HomeAPI/Requests/ProductRequest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// ProductRequest.swift
//
//
// Created by 홍승현 on 1/31/24.
//

import Entity
import Foundation

public struct ProductRequest: Encodable {
public let store: ConvenienceStore
public let promotion: Promotion
public let order: Order
public let pageSize: Int
public let offset: Int

public init(store: ConvenienceStore, promotion: Promotion, order: Order, pageSize: Int, offset: Int) {
self.store = store
self.promotion = promotion
self.order = order
self.pageSize = pageSize
self.offset = offset
}
}
12 changes: 12 additions & 0 deletions APIService/Sources/HomeAPI/Responses/ProductCountResponse.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// ProductCountResponse.swift
//
//
// Created by 홍승현 on 2/2/24.
//

import Foundation

struct ProductCountResponse: Decodable {
let count: Int
}
27 changes: 27 additions & 0 deletions APIService/Sources/HomeAPI/Responses/ProductResponse.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// ProductResponse.swift
//
//
// Created by 홍승현 on 1/31/24.
//

import Entity
import Foundation

struct ProductResponse: Decodable {
let id: Int
let imageURL: URL
let price: Int
let name: String
let promotion: Promotion
let convenienceStore: ConvenienceStore

enum CodingKeys: String, CodingKey {
case id
case imageURL = "image_url"
case price
case name
case promotion
case convenienceStore
}
}
85 changes: 0 additions & 85 deletions APIService/Sources/HomeAPISupport/HomeProductResponse.json

This file was deleted.

15 changes: 12 additions & 3 deletions APIService/Sources/HomeAPISupport/HomeURLProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@
//

import Foundation
import HomeAPI
import Network

public final class HomeURLProtocol: URLProtocol {
private let fileName = "HomeProductResponse"
private lazy var mockData: [String: Data?] = [
HomeEndPoint.fetchProducts(
.init(store: .gs25, promotion: .allItems, order: .normal, pageSize: 0, offset: 0)
).path: loadMockData(fileName: "HomeProductResponse"),
HomeEndPoint.fetchCount(
.init(convenienceStore: .gs25)
).path: loadMockData(fileName: "HomeProductCountResponse"),
]

override public class func canInit(with _: URLRequest) -> Bool {
true
Expand All @@ -21,8 +29,9 @@ public final class HomeURLProtocol: URLProtocol {

override public func startLoading() {
defer { client?.urlProtocolDidFinishLoading(self) }
if let data = loadMockData(fileName: fileName),
let url = request.url,
if let url = request.url,
let mockData = mockData[url.path(percentEncoded: true)],
let data = mockData,
let response = HTTPURLResponse(url: url, statusCode: 200, httpVersion: nil, headerFields: nil) {
client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed)
client?.urlProtocol(self, didLoad: data)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"count": 24
}
Loading

0 comments on commit b5e3816

Please sign in to comment.