Skip to content

Commit

Permalink
[feat] #13 AnyEncodable 타입 적용
Browse files Browse the repository at this point in the history
- 코드 리뷰 적용
  • Loading branch information
soletree committed Nov 14, 2024
1 parent 76a1dbb commit a52d5bb
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
4 changes: 4 additions & 0 deletions SniffMeet/SniffMeet.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
FD0634282CE596BD003C9D6B /* SNMRequestConvertible.swift in Sources */ = {isa = PBXBuildFile; fileRef = FD0634272CE596BA003C9D6B /* SNMRequestConvertible.swift */; };
FD06342A2CE596D0003C9D6B /* NetworkProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = FD0634292CE596C2003C9D6B /* NetworkProvider.swift */; };
FD06342C2CE5984D003C9D6B /* SNMNetworkResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = FD06342B2CE59843003C9D6B /* SNMNetworkResponse.swift */; };
FD0634312CE62578003C9D6B /* AnyEncodable.swift in Sources */ = {isa = PBXBuildFile; fileRef = FD0634302CE62573003C9D6B /* AnyEncodable.swift */; };
FD3A033F2CD8CF460047B7ED /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = FD3A03392CD8CF460047B7ED /* Assets.xcassets */; };
FD3A03412CD8CF460047B7ED /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = FD3A033C2CD8CF460047B7ED /* LaunchScreen.storyboard */; };
FD3A03422CD8CF460047B7ED /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = FD3A03382CD8CF460047B7ED /* AppDelegate.swift */; };
Expand Down Expand Up @@ -87,6 +88,7 @@
FD0634272CE596BA003C9D6B /* SNMRequestConvertible.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SNMRequestConvertible.swift; sourceTree = "<group>"; };
FD0634292CE596C2003C9D6B /* NetworkProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkProvider.swift; sourceTree = "<group>"; };
FD06342B2CE59843003C9D6B /* SNMNetworkResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SNMNetworkResponse.swift; sourceTree = "<group>"; };
FD0634302CE62573003C9D6B /* AnyEncodable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnyEncodable.swift; sourceTree = "<group>"; };
FD3A03202CD8CDE50047B7ED /* SniffMeet.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SniffMeet.app; sourceTree = BUILT_PRODUCTS_DIR; };
FD3A03382CD8CF460047B7ED /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
FD3A03392CD8CF460047B7ED /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
Expand Down Expand Up @@ -188,6 +190,7 @@
FD0634242CE596A7003C9D6B /* SNMNetwork */ = {
isa = PBXGroup;
children = (
FD0634302CE62573003C9D6B /* AnyEncodable.swift */,
FD0634292CE596C2003C9D6B /* NetworkProvider.swift */,
FD0634272CE596BA003C9D6B /* SNMRequestConvertible.swift */,
FD0634252CE596B2003C9D6B /* Endpoint.swift */,
Expand Down Expand Up @@ -1127,6 +1130,7 @@
FD69B1B52CE3BC7E009D71DC /* UserDefaultsManager.swift in Sources */,
320043722CDC9E5F00D08B6D /* (null) in Sources */,
3200441E2CE48D3500D08B6D /* MPConnectionManager.swift in Sources */,
FD0634312CE62578003C9D6B /* AnyEncodable.swift in Sources */,
3200441A2CE48A3D00D08B6D /* MPCBroswer.swift in Sources */,
3200437C2CDCA6F300D08B6D /* (null) in Sources */,
A2C328922CE3BEA000D255AB /* AppRouter.swift in Sources */,
Expand Down
26 changes: 26 additions & 0 deletions SniffMeet/SniffMeet/Source/SNMNetwork/AnyEncodable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// AnyEncodable.swift
// SniffMeet
//
// Created by sole on 11/14/24.
//

import Foundation

struct AnyEncodable {
private let jsonEncoder: JSONEncoder
private let value: any Encodable

init(_ value: any Encodable, jsonEncoder: JSONEncoder = AnyEncodable.defaultJSONEncoder) {
self.value = value
self.jsonEncoder = jsonEncoder
}

func encode() throws -> Data {
try jsonEncoder.encode(value)
}
}

extension AnyEncodable {
static let defaultJSONEncoder: JSONEncoder = JSONEncoder()
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public extension SNMRequestConvertible {
else { throw SNMNetworkError.invalidRequest(request: self) }
urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
do {
let jsonEncoder: JSONEncoder = JSONEncoder()
let encodedData = try jsonEncoder.encode(body)
let anyEncodableBody = AnyEncodable(body)
let encodedData = try anyEncodableBody.encode()
urlRequest.httpBody = encodedData
} catch {
throw SNMNetworkError.encodingError(with: body)
Expand Down

0 comments on commit a52d5bb

Please sign in to comment.