From d526d024d49517cd1a168ad96242ffe97ac67488 Mon Sep 17 00:00:00 2001 From: yoruck2 Date: Wed, 3 Apr 2024 16:29:53 +0900 Subject: [PATCH 01/14] =?UTF-8?q?add:=20=20api=20key=20config=EC=97=90=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ChatBot/ChatBot/Resources/Configuration.xcconfig | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 ChatBot/ChatBot/Resources/Configuration.xcconfig diff --git a/ChatBot/ChatBot/Resources/Configuration.xcconfig b/ChatBot/ChatBot/Resources/Configuration.xcconfig new file mode 100644 index 00000000..820e8957 --- /dev/null +++ b/ChatBot/ChatBot/Resources/Configuration.xcconfig @@ -0,0 +1,12 @@ +// +// Configuration.xcconfig +// ChatBot +// +// Created by dopamint on 4/3/24. +// + +// Configuration settings file format documentation can be found at: +// https://help.apple.com/xcode/#/dev745c5c974 + +API_KEY = CDWbrcIx9jelvj8bocniT3BlbkFJ9woqJgyL9zREct3iyIPd + From 70c7a819b3357d11d4a49d7b1f30cb4675712401 Mon Sep 17 00:00:00 2001 From: yoruck2 Date: Wed, 3 Apr 2024 16:30:51 +0900 Subject: [PATCH 02/14] =?UTF-8?q?add:=20plist=EC=97=90=20api=20key=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +++ ChatBot/ChatBot.xcodeproj/project.pbxproj | 12 ++++++++---- ChatBot/ChatBot/Resources/Info.plist | 2 ++ ChatBot/Podfile.lock | 2 +- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 8ee8cb7d..9c13ae32 100644 --- a/.gitignore +++ b/.gitignore @@ -99,4 +99,7 @@ iOSInjectionProject/ !*.xcworkspace/contents.xcworkspacedata **/xcshareddata/WorkspaceSettings.xcsettings +## config ## +*.xcconfig + # End of https://www.toptal.com/developers/gitignore/api/swift diff --git a/ChatBot/ChatBot.xcodeproj/project.pbxproj b/ChatBot/ChatBot.xcodeproj/project.pbxproj index 6e350285..2fc374b4 100644 --- a/ChatBot/ChatBot.xcodeproj/project.pbxproj +++ b/ChatBot/ChatBot.xcodeproj/project.pbxproj @@ -23,6 +23,7 @@ 548DCC0B40D1F4BC4766D293 /* Pods-ChatBot.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-ChatBot.release.xcconfig"; path = "Target Support Files/Pods-ChatBot/Pods-ChatBot.release.xcconfig"; sourceTree = ""; }; 84149A5A2BB285B1003595D0 /* ResponseDTO.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ResponseDTO.swift; sourceTree = ""; }; 84149A5C2BB2940B003595D0 /* RequestDTO.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RequestDTO.swift; sourceTree = ""; }; + 843E18412BBD214C003EEF6D /* Configuration.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Configuration.xcconfig; sourceTree = ""; }; 86F2885AC45A38F47FDA4AD5 /* Pods_ChatBot.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_ChatBot.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 95B4F31A2BB2C21E00AB4952 /* .swiftlint.yml */ = {isa = PBXFileReference; lastKnownFileType = text.yaml; path = .swiftlint.yml; sourceTree = ""; }; 95B4F3262BB42ED300AB4952 /* MessageDTO.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageDTO.swift; sourceTree = ""; }; @@ -78,8 +79,9 @@ 95B4F32A2BB43BDB00AB4952 /* Resources */ = { isa = PBXGroup; children = ( - B4B3E2CA2B42D1BC00818B3C /* Info.plist */, + 843E18412BBD214C003EEF6D /* Configuration.xcconfig */, B4B3E2C52B42D1BC00818B3C /* Assets.xcassets */, + B4B3E2CA2B42D1BC00818B3C /* Info.plist */, B4B3E2C72B42D1BC00818B3C /* LaunchScreen.storyboard */, ); path = Resources; @@ -139,8 +141,8 @@ isa = PBXNativeTarget; buildConfigurationList = B4B3E2CD2B42D1BC00818B3C /* Build configuration list for PBXNativeTarget "ChatBot" */; buildPhases = ( - 95B4F3192BB2C1A100AB4952 /* SwiftLint */, CF48DBE895D16B153F73C922 /* [CP] Check Pods Manifest.lock */, + 95B4F3192BB2C1A100AB4952 /* SwiftLint */, B4B3E2B52B42D1BB00818B3C /* Sources */, B4B3E2B62B42D1BB00818B3C /* Frameworks */, B4B3E2B72B42D1BB00818B3C /* Resources */, @@ -274,6 +276,7 @@ /* Begin XCBuildConfiguration section */ B4B3E2CB2B42D1BC00818B3C /* Debug */ = { isa = XCBuildConfiguration; + baseConfigurationReference = 843E18412BBD214C003EEF6D /* Configuration.xcconfig */; buildSettings = { ALWAYS_SEARCH_USER_PATHS = NO; ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES; @@ -324,7 +327,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 15.0; + IPHONEOS_DEPLOYMENT_TARGET = 14.0; LOCALIZATION_PREFERS_STRING_CATALOGS = YES; MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; MTL_FAST_MATH = YES; @@ -337,6 +340,7 @@ }; B4B3E2CC2B42D1BC00818B3C /* Release */ = { isa = XCBuildConfiguration; + baseConfigurationReference = 843E18412BBD214C003EEF6D /* Configuration.xcconfig */; buildSettings = { ALWAYS_SEARCH_USER_PATHS = NO; ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES; @@ -381,7 +385,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 15.0; + IPHONEOS_DEPLOYMENT_TARGET = 14.0; LOCALIZATION_PREFERS_STRING_CATALOGS = YES; MTL_ENABLE_DEBUG_INFO = NO; MTL_FAST_MATH = YES; diff --git a/ChatBot/ChatBot/Resources/Info.plist b/ChatBot/ChatBot/Resources/Info.plist index 0eb786dc..a1c5c6b6 100644 --- a/ChatBot/ChatBot/Resources/Info.plist +++ b/ChatBot/ChatBot/Resources/Info.plist @@ -2,6 +2,8 @@ + API_KEY + $(API_KEY) UIApplicationSceneManifest UIApplicationSupportsMultipleScenes diff --git a/ChatBot/Podfile.lock b/ChatBot/Podfile.lock index 907e2985..a5561b61 100644 --- a/ChatBot/Podfile.lock +++ b/ChatBot/Podfile.lock @@ -13,4 +13,4 @@ SPEC CHECKSUMS: PODFILE CHECKSUM: 5d1036dd232630979c2ecd98143c4c994fa049db -COCOAPODS: 1.12.0 +COCOAPODS: 1.15.2 From 7a1f1ad7410712dc8416ccfecbb3bd8141c671c1 Mon Sep 17 00:00:00 2001 From: angryeon7 Date: Thu, 4 Apr 2024 10:21:11 +0900 Subject: [PATCH 03/14] =?UTF-8?q?feat:=20=EB=84=A4=ED=8A=B8=EC=9B=8C?= =?UTF-8?q?=ED=81=AC=20=EB=A0=88=EC=9D=B4=EC=96=B4=20=EC=9E=91=EC=97=85=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ChatBot/ChatBot.xcodeproj/project.pbxproj | 68 +++++++++++++++++++++ ChatBot/ChatBot/Network/APIConstants.swift | 21 +++++++ ChatBot/ChatBot/Network/APIRouter.swift | 64 +++++++++++++++++++ ChatBot/ChatBot/Network/NetworkResult.swift | 19 ++++++ ChatBot/ChatBot/Network/NetworkRouter.swift | 17 ++++++ ChatBot/ChatBot/Util/AppConfig.swift | 18 ++++++ ChatBot/Podfile | 1 + ChatBot/Podfile.lock | 8 ++- 8 files changed, 214 insertions(+), 2 deletions(-) create mode 100644 ChatBot/ChatBot/Network/APIConstants.swift create mode 100644 ChatBot/ChatBot/Network/APIRouter.swift create mode 100644 ChatBot/ChatBot/Network/NetworkResult.swift create mode 100644 ChatBot/ChatBot/Network/NetworkRouter.swift create mode 100644 ChatBot/ChatBot/Util/AppConfig.swift diff --git a/ChatBot/ChatBot.xcodeproj/project.pbxproj b/ChatBot/ChatBot.xcodeproj/project.pbxproj index 2fc374b4..834c078b 100644 --- a/ChatBot/ChatBot.xcodeproj/project.pbxproj +++ b/ChatBot/ChatBot.xcodeproj/project.pbxproj @@ -10,6 +10,11 @@ 3756D96839324F046B039CE8 /* Pods_ChatBot.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 86F2885AC45A38F47FDA4AD5 /* Pods_ChatBot.framework */; }; 84149A5B2BB285B1003595D0 /* ResponseDTO.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84149A5A2BB285B1003595D0 /* ResponseDTO.swift */; }; 84149A5D2BB2940B003595D0 /* RequestDTO.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84149A5C2BB2940B003595D0 /* RequestDTO.swift */; }; + 95280FAF2BBD68C90071860A /* NetworkRouter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95280FAE2BBD68C90071860A /* NetworkRouter.swift */; }; + 95280FB32BBD6C440071860A /* APIRouter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95280FB22BBD6C440071860A /* APIRouter.swift */; }; + 95280FB52BBD6CA10071860A /* NetworkResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95280FB42BBD6CA10071860A /* NetworkResult.swift */; }; + 95280FB72BBD6CF80071860A /* APIConstants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95280FB62BBD6CF80071860A /* APIConstants.swift */; }; + 95280FC12BBE0C880071860A /* AppConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95280FC02BBE0C880071860A /* AppConfig.swift */; }; 95B4F31B2BB2C21E00AB4952 /* .swiftlint.yml in Resources */ = {isa = PBXBuildFile; fileRef = 95B4F31A2BB2C21E00AB4952 /* .swiftlint.yml */; }; 95B4F3272BB42ED300AB4952 /* MessageDTO.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95B4F3262BB42ED300AB4952 /* MessageDTO.swift */; }; B4B3E2BD2B42D1BB00818B3C /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B4B3E2BC2B42D1BB00818B3C /* AppDelegate.swift */; }; @@ -25,6 +30,11 @@ 84149A5C2BB2940B003595D0 /* RequestDTO.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RequestDTO.swift; sourceTree = ""; }; 843E18412BBD214C003EEF6D /* Configuration.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Configuration.xcconfig; sourceTree = ""; }; 86F2885AC45A38F47FDA4AD5 /* Pods_ChatBot.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_ChatBot.framework; sourceTree = BUILT_PRODUCTS_DIR; }; + 95280FAE2BBD68C90071860A /* NetworkRouter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkRouter.swift; sourceTree = ""; }; + 95280FB22BBD6C440071860A /* APIRouter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = APIRouter.swift; sourceTree = ""; }; + 95280FB42BBD6CA10071860A /* NetworkResult.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkResult.swift; sourceTree = ""; }; + 95280FB62BBD6CF80071860A /* APIConstants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = APIConstants.swift; sourceTree = ""; }; + 95280FC02BBE0C880071860A /* AppConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppConfig.swift; sourceTree = ""; }; 95B4F31A2BB2C21E00AB4952 /* .swiftlint.yml */ = {isa = PBXFileReference; lastKnownFileType = text.yaml; path = .swiftlint.yml; sourceTree = ""; }; 95B4F3262BB42ED300AB4952 /* MessageDTO.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageDTO.swift; sourceTree = ""; }; B4B3E2B92B42D1BB00818B3C /* ChatBot.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ChatBot.app; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -57,6 +67,39 @@ name = Frameworks; sourceTree = ""; }; + 95280FAA2BBD63A00071860A /* Network */ = { + isa = PBXGroup; + children = ( + 95280FAE2BBD68C90071860A /* NetworkRouter.swift */, + 95280FB22BBD6C440071860A /* APIRouter.swift */, + 95280FB42BBD6CA10071860A /* NetworkResult.swift */, + 95280FB62BBD6CF80071860A /* APIConstants.swift */, + ); + path = Network; + sourceTree = ""; + }; + 95280FB82BBD70870071860A /* Util */ = { + isa = PBXGroup; + children = ( + 95280FC02BBE0C880071860A /* AppConfig.swift */, + ); + path = Util; + sourceTree = ""; + }; + 95280FB92BBD708E0071860A /* Extensions */ = { + isa = PBXGroup; + children = ( + ); + path = Extensions; + sourceTree = ""; + }; + 95280FC32BBE36590071860A /* Service */ = { + isa = PBXGroup; + children = ( + ); + path = Service; + sourceTree = ""; + }; 95B4F3282BB43A8E00AB4952 /* App */ = { isa = PBXGroup; children = ( @@ -128,8 +171,10 @@ children = ( 95B4F3282BB43A8E00AB4952 /* App */, 95B4F3292BB43A9C00AB4952 /* Model */, + 95280FAA2BBD63A00071860A /* Network */, 95B4F32B2BB4714200AB4952 /* Controller */, 95B4F32A2BB43BDB00AB4952 /* Resources */, + 95280FB82BBD70870071860A /* Util */, ); path = ChatBot; sourceTree = ""; @@ -146,6 +191,7 @@ B4B3E2B52B42D1BB00818B3C /* Sources */, B4B3E2B62B42D1BB00818B3C /* Frameworks */, B4B3E2B72B42D1BB00818B3C /* Resources */, + 157BC9F75AEC0E5F7B580B83 /* [CP] Embed Pods Frameworks */, ); buildRules = ( ); @@ -203,6 +249,23 @@ /* End PBXResourcesBuildPhase section */ /* Begin PBXShellScriptBuildPhase section */ + 157BC9F75AEC0E5F7B580B83 /* [CP] Embed Pods Frameworks */ = { + isa = PBXShellScriptBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + inputFileListPaths = ( + "${PODS_ROOT}/Target Support Files/Pods-ChatBot/Pods-ChatBot-frameworks-${CONFIGURATION}-input-files.xcfilelist", + ); + name = "[CP] Embed Pods Frameworks"; + outputFileListPaths = ( + "${PODS_ROOT}/Target Support Files/Pods-ChatBot/Pods-ChatBot-frameworks-${CONFIGURATION}-output-files.xcfilelist", + ); + runOnlyForDeploymentPostprocessing = 0; + shellPath = /bin/sh; + shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-ChatBot/Pods-ChatBot-frameworks.sh\"\n"; + showEnvVarsInLog = 0; + }; 95B4F3192BB2C1A100AB4952 /* SwiftLint */ = { isa = PBXShellScriptBuildPhase; alwaysOutOfDate = 1; @@ -252,8 +315,13 @@ buildActionMask = 2147483647; files = ( B4B3E2C12B42D1BB00818B3C /* ViewController.swift in Sources */, + 95280FB72BBD6CF80071860A /* APIConstants.swift in Sources */, + 95280FB52BBD6CA10071860A /* NetworkResult.swift in Sources */, + 95280FB32BBD6C440071860A /* APIRouter.swift in Sources */, B4B3E2BD2B42D1BB00818B3C /* AppDelegate.swift in Sources */, + 95280FC12BBE0C880071860A /* AppConfig.swift in Sources */, 84149A5B2BB285B1003595D0 /* ResponseDTO.swift in Sources */, + 95280FAF2BBD68C90071860A /* NetworkRouter.swift in Sources */, 84149A5D2BB2940B003595D0 /* RequestDTO.swift in Sources */, 95B4F3272BB42ED300AB4952 /* MessageDTO.swift in Sources */, B4B3E2BF2B42D1BB00818B3C /* SceneDelegate.swift in Sources */, diff --git a/ChatBot/ChatBot/Network/APIConstants.swift b/ChatBot/ChatBot/Network/APIConstants.swift new file mode 100644 index 00000000..37a0a59e --- /dev/null +++ b/ChatBot/ChatBot/Network/APIConstants.swift @@ -0,0 +1,21 @@ +// +// APIConstants.swift +// ChatBot +// +// Created by nayeon on 4/3/24. +// + +import Foundation + +struct APIConstants { + static let baseURL = "https://api.openai.com" +} + +enum HTTPHeaderField: String { + case authentication = "Authorization" + case contentType = "Content-Type" +} + +enum ContentType: String { + case json = "application/json" +} diff --git a/ChatBot/ChatBot/Network/APIRouter.swift b/ChatBot/ChatBot/Network/APIRouter.swift new file mode 100644 index 00000000..cd2e7f84 --- /dev/null +++ b/ChatBot/ChatBot/Network/APIRouter.swift @@ -0,0 +1,64 @@ +// +// APIRouter.swift +// ChatBot +// +// Created by nayeon on 4/3/24. +// + +import Alamofire + +enum APIRouter: NetworkRouter { + case chatCompletion(requestDTO: RequestDTO) + + var baseURL: String { + return APIConstants.baseURL + } + + var path: String { + switch self { + case .chatCompletion: + return "/v1/chat/completions" + } + } + + var method: HTTPMethod { + switch self { + case .chatCompletion: + return .post + } + } + + var headers: [String: String] { + return [ + HTTPHeaderField.contentType.rawValue: ContentType.json.rawValue, + HTTPHeaderField.authentication.rawValue: "Bearer \(AppConfig.openAIAPIKey)" + ] + } + + var parameters: [String: Any]? { + switch self { + case .chatCompletion(let requestDTO): + return requestDTO.dictionaryRepresentation() + } + } + + var encoding: ParameterEncoding { + switch self { + case .chatCompletion: + return JSONEncoding.default + } + } +} + +extension APIRouter: URLRequestConvertible { + func asURLRequest() throws -> URLRequest { + let url = try baseURL.asURL() + var urlRequest = URLRequest(url: url.appendingPathComponent(path)) + urlRequest.httpMethod = method.rawValue + urlRequest.allHTTPHeaderFields = headers + if let parameters = parameters { + urlRequest = try encoding.encode(urlRequest, with: parameters) + } + return urlRequest + } +} diff --git a/ChatBot/ChatBot/Network/NetworkResult.swift b/ChatBot/ChatBot/Network/NetworkResult.swift new file mode 100644 index 00000000..16cc011a --- /dev/null +++ b/ChatBot/ChatBot/Network/NetworkResult.swift @@ -0,0 +1,19 @@ +// +// NetworkResult.swift +// ChatBot +// +// Created by nayeon on 4/3/24. +// + +enum NetworkResult { + case success(T) + case failure(NetworkError) +} + +enum NetworkError: Error { + case requestError + case pathError + case parsingError + case serverError + case networkFail +} diff --git a/ChatBot/ChatBot/Network/NetworkRouter.swift b/ChatBot/ChatBot/Network/NetworkRouter.swift new file mode 100644 index 00000000..1e94441e --- /dev/null +++ b/ChatBot/ChatBot/Network/NetworkRouter.swift @@ -0,0 +1,17 @@ +// +// NetworkRouter.swift +// ChatBot +// +// Created by nayeon on 4/3/24. +// + +import Alamofire + +protocol NetworkRouter { + var baseURL: String { get } + var path: String { get } + var method: HTTPMethod { get } + var headers: [String: String] { get } + var parameters: [String: Any]? { get } + var encoding: ParameterEncoding { get } +} diff --git a/ChatBot/ChatBot/Util/AppConfig.swift b/ChatBot/ChatBot/Util/AppConfig.swift new file mode 100644 index 00000000..11782e8c --- /dev/null +++ b/ChatBot/ChatBot/Util/AppConfig.swift @@ -0,0 +1,18 @@ +// +// AppConfig.swift +// ChatBot +// +// Created by nayeon on 4/4/24. +// + +import Foundation + +enum AppConfig { + static var openAIAPIKey: String { + guard let key = Bundle.main.infoDictionary?["API_KEY"] as? String else { + assertionFailure("API_KEY를 찾을 수 없음") + return "" + } + return key + } +} diff --git a/ChatBot/Podfile b/ChatBot/Podfile index dc8abb8c..56f8f602 100644 --- a/ChatBot/Podfile +++ b/ChatBot/Podfile @@ -7,4 +7,5 @@ target 'ChatBot' do # Pods for ChatBot pod "SwiftLint" + pod 'Alamofire' end diff --git a/ChatBot/Podfile.lock b/ChatBot/Podfile.lock index a5561b61..dc6f1af0 100644 --- a/ChatBot/Podfile.lock +++ b/ChatBot/Podfile.lock @@ -1,16 +1,20 @@ PODS: + - Alamofire (5.6.4) - SwiftLint (0.54.0) DEPENDENCIES: + - Alamofire - SwiftLint SPEC REPOS: trunk: + - Alamofire - SwiftLint SPEC CHECKSUMS: + Alamofire: 4e95d97098eacb88856099c4fc79b526a299e48c SwiftLint: c1de071d9d08c8aba837545f6254315bc900e211 -PODFILE CHECKSUM: 5d1036dd232630979c2ecd98143c4c994fa049db +PODFILE CHECKSUM: f2c861e2b4b4d583b1a645c37ba0896b5d68f903 -COCOAPODS: 1.15.2 +COCOAPODS: 1.12.0 From da4153d877b3fdf5c32481686a4705a9db9ed248 Mon Sep 17 00:00:00 2001 From: angryeon7 Date: Thu, 4 Apr 2024 10:23:24 +0900 Subject: [PATCH 04/14] =?UTF-8?q?feat:=20dictionary=20=ED=83=80=EC=9E=85?= =?UTF-8?q?=20=EB=B3=80=ED=99=98=20=EB=A9=94=EC=84=9C=EB=93=9C=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ChatBot/ChatBot.xcodeproj/project.pbxproj | 9 +++++++++ .../MessageDTO+DictionaryRepresentation.swift | 15 +++++++++++++++ .../RequestDTO+DictionaryRepresentation.swift | 18 ++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 ChatBot/ChatBot/Util/Extensions/MessageDTO+DictionaryRepresentation.swift create mode 100644 ChatBot/ChatBot/Util/Extensions/RequestDTO+DictionaryRepresentation.swift diff --git a/ChatBot/ChatBot.xcodeproj/project.pbxproj b/ChatBot/ChatBot.xcodeproj/project.pbxproj index 834c078b..58fb0303 100644 --- a/ChatBot/ChatBot.xcodeproj/project.pbxproj +++ b/ChatBot/ChatBot.xcodeproj/project.pbxproj @@ -14,6 +14,8 @@ 95280FB32BBD6C440071860A /* APIRouter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95280FB22BBD6C440071860A /* APIRouter.swift */; }; 95280FB52BBD6CA10071860A /* NetworkResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95280FB42BBD6CA10071860A /* NetworkResult.swift */; }; 95280FB72BBD6CF80071860A /* APIConstants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95280FB62BBD6CF80071860A /* APIConstants.swift */; }; + 95280FBB2BBD70AE0071860A /* RequestDTO+DictionaryRepresentation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95280FBA2BBD70AE0071860A /* RequestDTO+DictionaryRepresentation.swift */; }; + 95280FBD2BBD70E50071860A /* MessageDTO+DictionaryRepresentation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95280FBC2BBD70E50071860A /* MessageDTO+DictionaryRepresentation.swift */; }; 95280FC12BBE0C880071860A /* AppConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95280FC02BBE0C880071860A /* AppConfig.swift */; }; 95B4F31B2BB2C21E00AB4952 /* .swiftlint.yml in Resources */ = {isa = PBXBuildFile; fileRef = 95B4F31A2BB2C21E00AB4952 /* .swiftlint.yml */; }; 95B4F3272BB42ED300AB4952 /* MessageDTO.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95B4F3262BB42ED300AB4952 /* MessageDTO.swift */; }; @@ -34,6 +36,8 @@ 95280FB22BBD6C440071860A /* APIRouter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = APIRouter.swift; sourceTree = ""; }; 95280FB42BBD6CA10071860A /* NetworkResult.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkResult.swift; sourceTree = ""; }; 95280FB62BBD6CF80071860A /* APIConstants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = APIConstants.swift; sourceTree = ""; }; + 95280FBA2BBD70AE0071860A /* RequestDTO+DictionaryRepresentation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "RequestDTO+DictionaryRepresentation.swift"; sourceTree = ""; }; + 95280FBC2BBD70E50071860A /* MessageDTO+DictionaryRepresentation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MessageDTO+DictionaryRepresentation.swift"; sourceTree = ""; }; 95280FC02BBE0C880071860A /* AppConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppConfig.swift; sourceTree = ""; }; 95B4F31A2BB2C21E00AB4952 /* .swiftlint.yml */ = {isa = PBXFileReference; lastKnownFileType = text.yaml; path = .swiftlint.yml; sourceTree = ""; }; 95B4F3262BB42ED300AB4952 /* MessageDTO.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageDTO.swift; sourceTree = ""; }; @@ -81,6 +85,7 @@ 95280FB82BBD70870071860A /* Util */ = { isa = PBXGroup; children = ( + 95280FB92BBD708E0071860A /* Extensions */, 95280FC02BBE0C880071860A /* AppConfig.swift */, ); path = Util; @@ -89,6 +94,8 @@ 95280FB92BBD708E0071860A /* Extensions */ = { isa = PBXGroup; children = ( + 95280FBA2BBD70AE0071860A /* RequestDTO+DictionaryRepresentation.swift */, + 95280FBC2BBD70E50071860A /* MessageDTO+DictionaryRepresentation.swift */, ); path = Extensions; sourceTree = ""; @@ -314,6 +321,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 95280FBB2BBD70AE0071860A /* RequestDTO+DictionaryRepresentation.swift in Sources */, B4B3E2C12B42D1BB00818B3C /* ViewController.swift in Sources */, 95280FB72BBD6CF80071860A /* APIConstants.swift in Sources */, 95280FB52BBD6CA10071860A /* NetworkResult.swift in Sources */, @@ -325,6 +333,7 @@ 84149A5D2BB2940B003595D0 /* RequestDTO.swift in Sources */, 95B4F3272BB42ED300AB4952 /* MessageDTO.swift in Sources */, B4B3E2BF2B42D1BB00818B3C /* SceneDelegate.swift in Sources */, + 95280FBD2BBD70E50071860A /* MessageDTO+DictionaryRepresentation.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/ChatBot/ChatBot/Util/Extensions/MessageDTO+DictionaryRepresentation.swift b/ChatBot/ChatBot/Util/Extensions/MessageDTO+DictionaryRepresentation.swift new file mode 100644 index 00000000..7bc4ed70 --- /dev/null +++ b/ChatBot/ChatBot/Util/Extensions/MessageDTO+DictionaryRepresentation.swift @@ -0,0 +1,15 @@ +// +// MessageDTO+DictionaryRepresentation.swift +// ChatBot +// +// Created by nayeon on 4/3/24. +// + +extension MessageDTO { + func dictionaryRepresentation() -> [String: Any] { + return [ + "role": role.rawValue, + "content": content + ] + } +} diff --git a/ChatBot/ChatBot/Util/Extensions/RequestDTO+DictionaryRepresentation.swift b/ChatBot/ChatBot/Util/Extensions/RequestDTO+DictionaryRepresentation.swift new file mode 100644 index 00000000..9abb3a53 --- /dev/null +++ b/ChatBot/ChatBot/Util/Extensions/RequestDTO+DictionaryRepresentation.swift @@ -0,0 +1,18 @@ +// +// RequestDTO+DictionaryRepresentation.swift +// ChatBot +// +// Created by nayeon on 4/3/24. +// + +extension RequestDTO { + func dictionaryRepresentation() -> [String: Any] { + var parameters: [String: Any] = [:] + + parameters["model"] = model.rawValue + parameters["stream"] = stream + parameters["messages"] = messages.map { $0.dictionaryRepresentation() } + + return parameters + } +} From 1fcfe8509ccb57ea44a3112c267a2070f17572a0 Mon Sep 17 00:00:00 2001 From: angryeon7 Date: Thu, 4 Apr 2024 10:25:01 +0900 Subject: [PATCH 05/14] =?UTF-8?q?feat:=20=EB=84=A4=ED=8A=B8=EC=9B=8C?= =?UTF-8?q?=ED=81=AC=20=EC=9A=94=EC=B2=AD=20APIClient=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ChatBot/ChatBot.xcodeproj/project.pbxproj | 4 +++ ChatBot/ChatBot/Network/APIClient.swift | 39 +++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 ChatBot/ChatBot/Network/APIClient.swift diff --git a/ChatBot/ChatBot.xcodeproj/project.pbxproj b/ChatBot/ChatBot.xcodeproj/project.pbxproj index 58fb0303..38c0c527 100644 --- a/ChatBot/ChatBot.xcodeproj/project.pbxproj +++ b/ChatBot/ChatBot.xcodeproj/project.pbxproj @@ -11,6 +11,7 @@ 84149A5B2BB285B1003595D0 /* ResponseDTO.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84149A5A2BB285B1003595D0 /* ResponseDTO.swift */; }; 84149A5D2BB2940B003595D0 /* RequestDTO.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84149A5C2BB2940B003595D0 /* RequestDTO.swift */; }; 95280FAF2BBD68C90071860A /* NetworkRouter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95280FAE2BBD68C90071860A /* NetworkRouter.swift */; }; + 95280FB12BBD6C010071860A /* APIClient.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95280FB02BBD6C010071860A /* APIClient.swift */; }; 95280FB32BBD6C440071860A /* APIRouter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95280FB22BBD6C440071860A /* APIRouter.swift */; }; 95280FB52BBD6CA10071860A /* NetworkResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95280FB42BBD6CA10071860A /* NetworkResult.swift */; }; 95280FB72BBD6CF80071860A /* APIConstants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95280FB62BBD6CF80071860A /* APIConstants.swift */; }; @@ -33,6 +34,7 @@ 843E18412BBD214C003EEF6D /* Configuration.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Configuration.xcconfig; sourceTree = ""; }; 86F2885AC45A38F47FDA4AD5 /* Pods_ChatBot.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_ChatBot.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 95280FAE2BBD68C90071860A /* NetworkRouter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkRouter.swift; sourceTree = ""; }; + 95280FB02BBD6C010071860A /* APIClient.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = APIClient.swift; sourceTree = ""; }; 95280FB22BBD6C440071860A /* APIRouter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = APIRouter.swift; sourceTree = ""; }; 95280FB42BBD6CA10071860A /* NetworkResult.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkResult.swift; sourceTree = ""; }; 95280FB62BBD6CF80071860A /* APIConstants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = APIConstants.swift; sourceTree = ""; }; @@ -75,6 +77,7 @@ isa = PBXGroup; children = ( 95280FAE2BBD68C90071860A /* NetworkRouter.swift */, + 95280FB02BBD6C010071860A /* APIClient.swift */, 95280FB22BBD6C440071860A /* APIRouter.swift */, 95280FB42BBD6CA10071860A /* NetworkResult.swift */, 95280FB62BBD6CF80071860A /* APIConstants.swift */, @@ -329,6 +332,7 @@ B4B3E2BD2B42D1BB00818B3C /* AppDelegate.swift in Sources */, 95280FC12BBE0C880071860A /* AppConfig.swift in Sources */, 84149A5B2BB285B1003595D0 /* ResponseDTO.swift in Sources */, + 95280FB12BBD6C010071860A /* APIClient.swift in Sources */, 95280FAF2BBD68C90071860A /* NetworkRouter.swift in Sources */, 84149A5D2BB2940B003595D0 /* RequestDTO.swift in Sources */, 95B4F3272BB42ED300AB4952 /* MessageDTO.swift in Sources */, diff --git a/ChatBot/ChatBot/Network/APIClient.swift b/ChatBot/ChatBot/Network/APIClient.swift new file mode 100644 index 00000000..850c2bc8 --- /dev/null +++ b/ChatBot/ChatBot/Network/APIClient.swift @@ -0,0 +1,39 @@ +// +// APIClient.swift +// ChatBot +// +// Created by nayeon on 4/3/24. +// + +import Alamofire + +final class APIClient { + static let shared = APIClient() + + func request(_ object: T.Type, + router: URLRequestConvertible, + completion: @escaping (NetworkResult) -> Void) { + AF.request(router) + .validate(statusCode: 200..<500) + .responseDecodable(of: object) { response in + switch response.result { + case .success(let decodedData): + completion(.success(decodedData)) + case .failure(_): + if let statusCode = response.response?.statusCode { + switch statusCode { + case 400...499: + completion(.failure(.pathError)) + case 500...599: + completion(.failure(.serverError)) + default: + completion(.failure(.networkFail)) + } + } else { + completion(.failure(.networkFail)) + + } + } + } + } +} From c48cf43d964d4854be493fc65139acdd604d6228 Mon Sep 17 00:00:00 2001 From: angryeon7 Date: Thu, 4 Apr 2024 10:25:51 +0900 Subject: [PATCH 06/14] =?UTF-8?q?feat:=20ChatService=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ChatBot/ChatBot.xcodeproj/project.pbxproj | 5 ++++ ChatBot/ChatBot/Service/ChatService.swift | 32 +++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 ChatBot/ChatBot/Service/ChatService.swift diff --git a/ChatBot/ChatBot.xcodeproj/project.pbxproj b/ChatBot/ChatBot.xcodeproj/project.pbxproj index 38c0c527..f4e11e95 100644 --- a/ChatBot/ChatBot.xcodeproj/project.pbxproj +++ b/ChatBot/ChatBot.xcodeproj/project.pbxproj @@ -17,6 +17,7 @@ 95280FB72BBD6CF80071860A /* APIConstants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95280FB62BBD6CF80071860A /* APIConstants.swift */; }; 95280FBB2BBD70AE0071860A /* RequestDTO+DictionaryRepresentation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95280FBA2BBD70AE0071860A /* RequestDTO+DictionaryRepresentation.swift */; }; 95280FBD2BBD70E50071860A /* MessageDTO+DictionaryRepresentation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95280FBC2BBD70E50071860A /* MessageDTO+DictionaryRepresentation.swift */; }; + 95280FBF2BBD71390071860A /* ChatService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95280FBE2BBD71390071860A /* ChatService.swift */; }; 95280FC12BBE0C880071860A /* AppConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95280FC02BBE0C880071860A /* AppConfig.swift */; }; 95B4F31B2BB2C21E00AB4952 /* .swiftlint.yml in Resources */ = {isa = PBXBuildFile; fileRef = 95B4F31A2BB2C21E00AB4952 /* .swiftlint.yml */; }; 95B4F3272BB42ED300AB4952 /* MessageDTO.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95B4F3262BB42ED300AB4952 /* MessageDTO.swift */; }; @@ -40,6 +41,7 @@ 95280FB62BBD6CF80071860A /* APIConstants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = APIConstants.swift; sourceTree = ""; }; 95280FBA2BBD70AE0071860A /* RequestDTO+DictionaryRepresentation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "RequestDTO+DictionaryRepresentation.swift"; sourceTree = ""; }; 95280FBC2BBD70E50071860A /* MessageDTO+DictionaryRepresentation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MessageDTO+DictionaryRepresentation.swift"; sourceTree = ""; }; + 95280FBE2BBD71390071860A /* ChatService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatService.swift; sourceTree = ""; }; 95280FC02BBE0C880071860A /* AppConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppConfig.swift; sourceTree = ""; }; 95B4F31A2BB2C21E00AB4952 /* .swiftlint.yml */ = {isa = PBXFileReference; lastKnownFileType = text.yaml; path = .swiftlint.yml; sourceTree = ""; }; 95B4F3262BB42ED300AB4952 /* MessageDTO.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageDTO.swift; sourceTree = ""; }; @@ -106,6 +108,7 @@ 95280FC32BBE36590071860A /* Service */ = { isa = PBXGroup; children = ( + 95280FBE2BBD71390071860A /* ChatService.swift */, ); path = Service; sourceTree = ""; @@ -182,6 +185,7 @@ 95B4F3282BB43A8E00AB4952 /* App */, 95B4F3292BB43A9C00AB4952 /* Model */, 95280FAA2BBD63A00071860A /* Network */, + 95280FC32BBE36590071860A /* Service */, 95B4F32B2BB4714200AB4952 /* Controller */, 95B4F32A2BB43BDB00AB4952 /* Resources */, 95280FB82BBD70870071860A /* Util */, @@ -334,6 +338,7 @@ 84149A5B2BB285B1003595D0 /* ResponseDTO.swift in Sources */, 95280FB12BBD6C010071860A /* APIClient.swift in Sources */, 95280FAF2BBD68C90071860A /* NetworkRouter.swift in Sources */, + 95280FBF2BBD71390071860A /* ChatService.swift in Sources */, 84149A5D2BB2940B003595D0 /* RequestDTO.swift in Sources */, 95B4F3272BB42ED300AB4952 /* MessageDTO.swift in Sources */, B4B3E2BF2B42D1BB00818B3C /* SceneDelegate.swift in Sources */, diff --git a/ChatBot/ChatBot/Service/ChatService.swift b/ChatBot/ChatBot/Service/ChatService.swift new file mode 100644 index 00000000..17a9f31a --- /dev/null +++ b/ChatBot/ChatBot/Service/ChatService.swift @@ -0,0 +1,32 @@ +// +// ChatService.swift +// ChatBot +// +// Created by nayeon on 4/3/24. +// + +protocol ChatServiceProtocol { + func sendChatRequest(message: String, completion: @escaping (NetworkResult) -> Void) +} + +final class ChatService: ChatServiceProtocol { + let apiClient: APIClient + + init(apiClient: APIClient = .shared) { + self.apiClient = apiClient + } + + func sendChatRequest(message: String, completion: @escaping (NetworkResult) -> Void) { + let requestDTO = RequestDTO(model: .basic, stream: false, messages: [MessageDTO(role: .user, content: message)]) + let router = APIRouter.chatCompletion(requestDTO: requestDTO) + + apiClient.request(ResponseDTO.self, router: router) { result in + switch result { + case .success(let response): + completion(.success(response)) + case .failure(let error): + completion(.failure(error)) + } + } + } +} From 4b7736bf035bcc61a490c63b787b521cd92ae2cc Mon Sep 17 00:00:00 2001 From: angryeon7 Date: Thu, 4 Apr 2024 10:52:08 +0900 Subject: [PATCH 07/14] =?UTF-8?q?feat:=20=EC=9A=94=EC=B2=AD=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ChatBot/ChatBot/Controller/ViewController.swift | 14 ++++++++++++++ ChatBot/ChatBot/Util/AppConfig.swift | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/ChatBot/ChatBot/Controller/ViewController.swift b/ChatBot/ChatBot/Controller/ViewController.swift index 2ed5f125..597099f0 100644 --- a/ChatBot/ChatBot/Controller/ViewController.swift +++ b/ChatBot/ChatBot/Controller/ViewController.swift @@ -8,7 +8,21 @@ import UIKit class ViewController: UIViewController { + let chatService = ChatService() + override func viewDidLoad() { super.viewDidLoad() + sendTestMessage("안녕 만나서 반가워") + } + + func sendTestMessage(_ message: String) { + chatService.sendChatRequest(message: message) { result in + switch result { + case .success(let response): + print("Response: \(response)") + case .failure(let error): + print("Error: \(error)") + } + } } } diff --git a/ChatBot/ChatBot/Util/AppConfig.swift b/ChatBot/ChatBot/Util/AppConfig.swift index 11782e8c..66d9deb9 100644 --- a/ChatBot/ChatBot/Util/AppConfig.swift +++ b/ChatBot/ChatBot/Util/AppConfig.swift @@ -9,7 +9,7 @@ import Foundation enum AppConfig { static var openAIAPIKey: String { - guard let key = Bundle.main.infoDictionary?["API_KEY"] as? String else { + guard let key = Bundle.main.object(forInfoDictionaryKey: "API_KEY") as? String else { assertionFailure("API_KEY를 찾을 수 없음") return "" } From 1ab9a9c0a7bf4ff721a7794d20c05474a12e1c03 Mon Sep 17 00:00:00 2001 From: nayeon Date: Thu, 4 Apr 2024 11:01:24 +0900 Subject: [PATCH 08/14] =?UTF-8?q?refactor:=20.gitignore=20=EC=84=A4?= =?UTF-8?q?=EC=A0=95=20=EC=9E=AC=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ChatBot/ChatBot/Resources/Configuration.xcconfig | 12 ------------ 1 file changed, 12 deletions(-) delete mode 100644 ChatBot/ChatBot/Resources/Configuration.xcconfig diff --git a/ChatBot/ChatBot/Resources/Configuration.xcconfig b/ChatBot/ChatBot/Resources/Configuration.xcconfig deleted file mode 100644 index 820e8957..00000000 --- a/ChatBot/ChatBot/Resources/Configuration.xcconfig +++ /dev/null @@ -1,12 +0,0 @@ -// -// Configuration.xcconfig -// ChatBot -// -// Created by dopamint on 4/3/24. -// - -// Configuration settings file format documentation can be found at: -// https://help.apple.com/xcode/#/dev745c5c974 - -API_KEY = CDWbrcIx9jelvj8bocniT3BlbkFJ9woqJgyL9zREct3iyIPd - From e703a3d4fe275fd089b8280eb56c700c023c1ca4 Mon Sep 17 00:00:00 2001 From: nayeon Date: Tue, 9 Apr 2024 19:56:18 +0900 Subject: [PATCH 09/14] =?UTF-8?q?chore:=20=EC=B5=9C=EC=86=8C=EC=A7=80?= =?UTF-8?q?=EC=9B=90=20=EB=B2=84=EC=A0=84=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ChatBot/ChatBot.xcodeproj/project.pbxproj | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ChatBot/ChatBot.xcodeproj/project.pbxproj b/ChatBot/ChatBot.xcodeproj/project.pbxproj index f4e11e95..bf464667 100644 --- a/ChatBot/ChatBot.xcodeproj/project.pbxproj +++ b/ChatBot/ChatBot.xcodeproj/project.pbxproj @@ -413,7 +413,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 14.0; + IPHONEOS_DEPLOYMENT_TARGET = 15.0; LOCALIZATION_PREFERS_STRING_CATALOGS = YES; MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; MTL_FAST_MATH = YES; @@ -471,7 +471,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 14.0; + IPHONEOS_DEPLOYMENT_TARGET = 15.0; LOCALIZATION_PREFERS_STRING_CATALOGS = YES; MTL_ENABLE_DEBUG_INFO = NO; MTL_FAST_MATH = YES; @@ -497,7 +497,7 @@ INFOPLIST_KEY_UILaunchStoryboardName = LaunchScreen; INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; - IPHONEOS_DEPLOYMENT_TARGET = 13.0; + IPHONEOS_DEPLOYMENT_TARGET = 15.0; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", @@ -532,7 +532,7 @@ INFOPLIST_KEY_UIMainStoryboardFile = Main; INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; - IPHONEOS_DEPLOYMENT_TARGET = 13.0; + IPHONEOS_DEPLOYMENT_TARGET = 15.0; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", From cdd02f723d97b298826396e08cff3e4d78560d46 Mon Sep 17 00:00:00 2001 From: nayeon Date: Tue, 9 Apr 2024 19:58:40 +0900 Subject: [PATCH 10/14] =?UTF-8?q?refactor:=20APIRouter=20=EA=B3=B5?= =?UTF-8?q?=ED=86=B5=20=EB=A1=9C=EC=A7=81=EC=9D=84=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ChatBot/ChatBot/Network/APIClient.swift | 3 +-- ChatBot/ChatBot/Network/APIRouter.swift | 15 +-------------- ChatBot/ChatBot/Network/NetworkRouter.swift | 13 +++++++++++++ 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/ChatBot/ChatBot/Network/APIClient.swift b/ChatBot/ChatBot/Network/APIClient.swift index 850c2bc8..67363ba2 100644 --- a/ChatBot/ChatBot/Network/APIClient.swift +++ b/ChatBot/ChatBot/Network/APIClient.swift @@ -30,8 +30,7 @@ final class APIClient { completion(.failure(.networkFail)) } } else { - completion(.failure(.networkFail)) - + completion(.failure(.networkFail)) } } } diff --git a/ChatBot/ChatBot/Network/APIRouter.swift b/ChatBot/ChatBot/Network/APIRouter.swift index cd2e7f84..9dd8583f 100644 --- a/ChatBot/ChatBot/Network/APIRouter.swift +++ b/ChatBot/ChatBot/Network/APIRouter.swift @@ -7,7 +7,7 @@ import Alamofire -enum APIRouter: NetworkRouter { +enum APIRouter: NetworkRouter, URLRequestConvertible { case chatCompletion(requestDTO: RequestDTO) var baseURL: String { @@ -49,16 +49,3 @@ enum APIRouter: NetworkRouter { } } } - -extension APIRouter: URLRequestConvertible { - func asURLRequest() throws -> URLRequest { - let url = try baseURL.asURL() - var urlRequest = URLRequest(url: url.appendingPathComponent(path)) - urlRequest.httpMethod = method.rawValue - urlRequest.allHTTPHeaderFields = headers - if let parameters = parameters { - urlRequest = try encoding.encode(urlRequest, with: parameters) - } - return urlRequest - } -} diff --git a/ChatBot/ChatBot/Network/NetworkRouter.swift b/ChatBot/ChatBot/Network/NetworkRouter.swift index 1e94441e..5248b289 100644 --- a/ChatBot/ChatBot/Network/NetworkRouter.swift +++ b/ChatBot/ChatBot/Network/NetworkRouter.swift @@ -15,3 +15,16 @@ protocol NetworkRouter { var parameters: [String: Any]? { get } var encoding: ParameterEncoding { get } } + +extension NetworkRouter { + func asURLRequest() throws -> URLRequest { + let url = try baseURL.asURL() + var urlRequest = URLRequest(url: url.appendingPathComponent(path)) + urlRequest.httpMethod = method.rawValue + urlRequest.allHTTPHeaderFields = headers + if let parameters = parameters { + urlRequest = try encoding.encode(urlRequest, with: parameters) + } + return urlRequest + } +} From f7391500cd3f1377d6d46268fdc569aa0ab517b8 Mon Sep 17 00:00:00 2001 From: nayeon Date: Tue, 9 Apr 2024 21:52:33 +0900 Subject: [PATCH 11/14] =?UTF-8?q?refactor:=20=EA=B5=AC=EC=B2=B4=EC=A0=81?= =?UTF-8?q?=EC=9D=B8=20=EC=97=90=EB=9F=AC=EB=A9=94=EC=84=B8=EC=A7=80=20?= =?UTF-8?q?=EC=B6=9C=EB=A0=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ChatBot/ChatBot/Network/APIClient.swift | 13 ++++++++----- ChatBot/ChatBot/Network/NetworkResult.swift | 8 ++++---- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/ChatBot/ChatBot/Network/APIClient.swift b/ChatBot/ChatBot/Network/APIClient.swift index 67363ba2..5b474638 100644 --- a/ChatBot/ChatBot/Network/APIClient.swift +++ b/ChatBot/ChatBot/Network/APIClient.swift @@ -19,18 +19,21 @@ final class APIClient { switch response.result { case .success(let decodedData): completion(.success(decodedData)) - case .failure(_): + case .failure(let error): + var errorMessage: String? + if let data = response.data, let message = String(data: data, encoding: .utf8) { + errorMessage = message + } + if let statusCode = response.response?.statusCode { switch statusCode { case 400...499: - completion(.failure(.pathError)) + completion(.failure(.pathError(message: errorMessage))) case 500...599: - completion(.failure(.serverError)) + completion(.failure(.serverError(message: errorMessage ?? "server error"))) default: completion(.failure(.networkFail)) } - } else { - completion(.failure(.networkFail)) } } } diff --git a/ChatBot/ChatBot/Network/NetworkResult.swift b/ChatBot/ChatBot/Network/NetworkResult.swift index 16cc011a..a913ea4c 100644 --- a/ChatBot/ChatBot/Network/NetworkResult.swift +++ b/ChatBot/ChatBot/Network/NetworkResult.swift @@ -11,9 +11,9 @@ enum NetworkResult { } enum NetworkError: Error { - case requestError - case pathError - case parsingError - case serverError + case requestError(message: String?) + case pathError(message: String?) + case parsingError(message: String?) + case serverError(message: String?) case networkFail } From ac01d9e7b59d9764c1261774012f19ec2249fcc4 Mon Sep 17 00:00:00 2001 From: nayeon Date: Wed, 10 Apr 2024 15:10:52 +0900 Subject: [PATCH 12/14] =?UTF-8?q?feat:=20Dictionary=ED=83=80=EC=9E=85=20?= =?UTF-8?q?=EB=B3=80=ED=99=98=20=ED=94=84=EB=A1=9C=ED=86=A0=EC=BD=9C=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ChatBot/ChatBot/Model/MessageDTO.swift | 2 +- ChatBot/ChatBot/Model/RequestDTO.swift | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/ChatBot/ChatBot/Model/MessageDTO.swift b/ChatBot/ChatBot/Model/MessageDTO.swift index 6aa95b7d..f3ac7314 100644 --- a/ChatBot/ChatBot/Model/MessageDTO.swift +++ b/ChatBot/ChatBot/Model/MessageDTO.swift @@ -5,7 +5,7 @@ // Created by nayeon on 3/27/24. // -struct MessageDTO: Codable { +struct MessageDTO: Codable, DictionaryRepresentable { let role: Role let content: String } diff --git a/ChatBot/ChatBot/Model/RequestDTO.swift b/ChatBot/ChatBot/Model/RequestDTO.swift index 9cc251f1..6e2191ae 100644 --- a/ChatBot/ChatBot/Model/RequestDTO.swift +++ b/ChatBot/ChatBot/Model/RequestDTO.swift @@ -5,7 +5,11 @@ // Created by dopamint on 3/26/24. // -struct RequestDTO: Encodable { +protocol DictionaryRepresentable { + func dictionaryRepresentation() -> [String: Any] +} + +struct RequestDTO: Encodable, DictionaryRepresentable { let model: GPTModel let stream: Bool let messages: [MessageDTO] From b4a5bdc40547eeabf6806ce812b0412457e822a1 Mon Sep 17 00:00:00 2001 From: nayeon Date: Wed, 10 Apr 2024 15:30:01 +0900 Subject: [PATCH 13/14] =?UTF-8?q?chore:=20=EB=B6=88=ED=95=84=EC=9A=94?= =?UTF-8?q?=ED=95=9C=20=EC=BD=94=EB=93=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ChatBot/ChatBot/Network/APIConstants.swift | 2 +- ChatBot/ChatBot/Network/NetworkResult.swift | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/ChatBot/ChatBot/Network/APIConstants.swift b/ChatBot/ChatBot/Network/APIConstants.swift index 37a0a59e..68ab88f6 100644 --- a/ChatBot/ChatBot/Network/APIConstants.swift +++ b/ChatBot/ChatBot/Network/APIConstants.swift @@ -7,7 +7,7 @@ import Foundation -struct APIConstants { +enum APIConstants { static let baseURL = "https://api.openai.com" } diff --git a/ChatBot/ChatBot/Network/NetworkResult.swift b/ChatBot/ChatBot/Network/NetworkResult.swift index a913ea4c..adcfc656 100644 --- a/ChatBot/ChatBot/Network/NetworkResult.swift +++ b/ChatBot/ChatBot/Network/NetworkResult.swift @@ -11,9 +11,7 @@ enum NetworkResult { } enum NetworkError: Error { - case requestError(message: String?) case pathError(message: String?) - case parsingError(message: String?) case serverError(message: String?) case networkFail } From d58b3f2632457e95800888065cbe5d37592fdb46 Mon Sep 17 00:00:00 2001 From: nayeon Date: Thu, 11 Apr 2024 18:58:52 +0900 Subject: [PATCH 14/14] =?UTF-8?q?refactor:=20APIClient=20enum=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ChatBot/ChatBot/Network/APIClient.swift | 9 ++++----- ChatBot/ChatBot/Service/ChatService.swift | 7 +------ 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/ChatBot/ChatBot/Network/APIClient.swift b/ChatBot/ChatBot/Network/APIClient.swift index 5b474638..5e42eda6 100644 --- a/ChatBot/ChatBot/Network/APIClient.swift +++ b/ChatBot/ChatBot/Network/APIClient.swift @@ -7,12 +7,11 @@ import Alamofire -final class APIClient { - static let shared = APIClient() +enum APIClient { - func request(_ object: T.Type, - router: URLRequestConvertible, - completion: @escaping (NetworkResult) -> Void) { + static func request(_ object: T.Type, + router: URLRequestConvertible, + completion: @escaping (NetworkResult) -> Void) { AF.request(router) .validate(statusCode: 200..<500) .responseDecodable(of: object) { response in diff --git a/ChatBot/ChatBot/Service/ChatService.swift b/ChatBot/ChatBot/Service/ChatService.swift index 17a9f31a..09a23a10 100644 --- a/ChatBot/ChatBot/Service/ChatService.swift +++ b/ChatBot/ChatBot/Service/ChatService.swift @@ -10,17 +10,12 @@ protocol ChatServiceProtocol { } final class ChatService: ChatServiceProtocol { - let apiClient: APIClient - - init(apiClient: APIClient = .shared) { - self.apiClient = apiClient - } func sendChatRequest(message: String, completion: @escaping (NetworkResult) -> Void) { let requestDTO = RequestDTO(model: .basic, stream: false, messages: [MessageDTO(role: .user, content: message)]) let router = APIRouter.chatCompletion(requestDTO: requestDTO) - apiClient.request(ResponseDTO.self, router: router) { result in + APIClient.request(ResponseDTO.self, router: router) { result in switch result { case .success(let response): completion(.success(response))