Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AI 챗봇 앱 [STEP 2] 토미, 이지 #42

Open
wants to merge 15 commits into
base: d_Easy
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,7 @@ iOSInjectionProject/
!*.xcworkspace/contents.xcworkspacedata
**/xcshareddata/WorkspaceSettings.xcsettings

## config ##
*.xcconfig

Comment on lines +102 to +104
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

현재 빌드가 정상적으로 되지 않아요.
혹시 이 파일을 gitignore에 추가하신 이유가 있으실까요??
여러분들의 로컬에 있는 프로젝트를 삭제한 이후에 다시 클론을 받아서 빌드를 진행해보시면 좋을 것 같아요.
gitignore에 추가한 파일이 어떤파일인지 어떤 역할을 하는 파일인지 알아보시면 좋을 것 같아요.
그리고 이 문제를 어떻게 해결할 수 있을지도 확인해보세요!

Copy link
Author

@angryeon7 angryeon7 Apr 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

xcconfig란?

Xcode 프로젝트의 빌드 설정을 외부화하여 관리할 수 있게 해주는 파일입니다.

.xcconfig 확장자로 저장되며, 이를 사용하면 빌드 설정을 코드로 관리할 수 있어 복잡한 프로젝트에서 빌드 설정을 좀 더 쉽게 관리할 수 있습니다.

api key를 git에 노출시키지 않도록 하기 위해서 추가하였습니다.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

의도는 이해했습니다.
그렇지만 git을 통해서 pull을 받았을때 정상 빌드가 되지 않고 xcconfig를 사용하지 않고 빌드하는 경우 api key가 없어 API요청에 실패하게 되어있습니다.
데이터의 보안에 관련해서는 어떻게 처리하면 좋을지 추후에 조금 더 알아보시고 이번에는 추가해주시면 좋을 것 같네요 ㅎㅎ

# End of https://www.toptal.com/developers/gitignore/api/swift
98 changes: 94 additions & 4 deletions ChatBot/ChatBot.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
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 */; };
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 */; };
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 */; };
B4B3E2BD2B42D1BB00818B3C /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B4B3E2BC2B42D1BB00818B3C /* AppDelegate.swift */; };
Expand All @@ -23,7 +32,17 @@
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 = "<group>"; };
84149A5A2BB285B1003595D0 /* ResponseDTO.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ResponseDTO.swift; sourceTree = "<group>"; };
84149A5C2BB2940B003595D0 /* RequestDTO.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RequestDTO.swift; sourceTree = "<group>"; };
843E18412BBD214C003EEF6D /* Configuration.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Configuration.xcconfig; sourceTree = "<group>"; };
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 = "<group>"; };
95280FB02BBD6C010071860A /* APIClient.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = APIClient.swift; sourceTree = "<group>"; };
95280FB22BBD6C440071860A /* APIRouter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = APIRouter.swift; sourceTree = "<group>"; };
95280FB42BBD6CA10071860A /* NetworkResult.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkResult.swift; sourceTree = "<group>"; };
95280FB62BBD6CF80071860A /* APIConstants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = APIConstants.swift; sourceTree = "<group>"; };
95280FBA2BBD70AE0071860A /* RequestDTO+DictionaryRepresentation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "RequestDTO+DictionaryRepresentation.swift"; sourceTree = "<group>"; };
95280FBC2BBD70E50071860A /* MessageDTO+DictionaryRepresentation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MessageDTO+DictionaryRepresentation.swift"; sourceTree = "<group>"; };
95280FBE2BBD71390071860A /* ChatService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatService.swift; sourceTree = "<group>"; };
95280FC02BBE0C880071860A /* AppConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppConfig.swift; sourceTree = "<group>"; };
95B4F31A2BB2C21E00AB4952 /* .swiftlint.yml */ = {isa = PBXFileReference; lastKnownFileType = text.yaml; path = .swiftlint.yml; sourceTree = "<group>"; };
95B4F3262BB42ED300AB4952 /* MessageDTO.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageDTO.swift; sourceTree = "<group>"; };
B4B3E2B92B42D1BB00818B3C /* ChatBot.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ChatBot.app; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -56,6 +75,44 @@
name = Frameworks;
sourceTree = "<group>";
};
95280FAA2BBD63A00071860A /* Network */ = {
isa = PBXGroup;
children = (
95280FAE2BBD68C90071860A /* NetworkRouter.swift */,
95280FB02BBD6C010071860A /* APIClient.swift */,
95280FB22BBD6C440071860A /* APIRouter.swift */,
95280FB42BBD6CA10071860A /* NetworkResult.swift */,
95280FB62BBD6CF80071860A /* APIConstants.swift */,
);
path = Network;
sourceTree = "<group>";
};
95280FB82BBD70870071860A /* Util */ = {
isa = PBXGroup;
children = (
95280FB92BBD708E0071860A /* Extensions */,
95280FC02BBE0C880071860A /* AppConfig.swift */,
);
path = Util;
sourceTree = "<group>";
};
95280FB92BBD708E0071860A /* Extensions */ = {
isa = PBXGroup;
children = (
95280FBA2BBD70AE0071860A /* RequestDTO+DictionaryRepresentation.swift */,
95280FBC2BBD70E50071860A /* MessageDTO+DictionaryRepresentation.swift */,
);
path = Extensions;
sourceTree = "<group>";
};
95280FC32BBE36590071860A /* Service */ = {
isa = PBXGroup;
children = (
95280FBE2BBD71390071860A /* ChatService.swift */,
);
path = Service;
sourceTree = "<group>";
};
95B4F3282BB43A8E00AB4952 /* App */ = {
isa = PBXGroup;
children = (
Expand All @@ -78,8 +135,9 @@
95B4F32A2BB43BDB00AB4952 /* Resources */ = {
isa = PBXGroup;
children = (
B4B3E2CA2B42D1BC00818B3C /* Info.plist */,
843E18412BBD214C003EEF6D /* Configuration.xcconfig */,
B4B3E2C52B42D1BC00818B3C /* Assets.xcassets */,
B4B3E2CA2B42D1BC00818B3C /* Info.plist */,
B4B3E2C72B42D1BC00818B3C /* LaunchScreen.storyboard */,
);
path = Resources;
Expand Down Expand Up @@ -126,8 +184,11 @@
children = (
95B4F3282BB43A8E00AB4952 /* App */,
95B4F3292BB43A9C00AB4952 /* Model */,
95280FAA2BBD63A00071860A /* Network */,
95280FC32BBE36590071860A /* Service */,
95B4F32B2BB4714200AB4952 /* Controller */,
95B4F32A2BB43BDB00AB4952 /* Resources */,
95280FB82BBD70870071860A /* Util */,
);
path = ChatBot;
sourceTree = "<group>";
Expand All @@ -139,11 +200,12 @@
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 */,
157BC9F75AEC0E5F7B580B83 /* [CP] Embed Pods Frameworks */,
);
buildRules = (
);
Expand Down Expand Up @@ -201,6 +263,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;
Expand Down Expand Up @@ -249,12 +328,21 @@
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 */,
95280FB32BBD6C440071860A /* APIRouter.swift in Sources */,
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 */,
95280FBF2BBD71390071860A /* ChatService.swift in Sources */,
84149A5D2BB2940B003595D0 /* RequestDTO.swift in Sources */,
95B4F3272BB42ED300AB4952 /* MessageDTO.swift in Sources */,
B4B3E2BF2B42D1BB00818B3C /* SceneDelegate.swift in Sources */,
95280FBD2BBD70E50071860A /* MessageDTO+DictionaryRepresentation.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -274,6 +362,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;
Expand Down Expand Up @@ -337,6 +426,7 @@
};
B4B3E2CC2B42D1BC00818B3C /* Release */ = {
isa = XCBuildConfiguration;
baseConfigurationReference = 843E18412BBD214C003EEF6D /* Configuration.xcconfig */;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES;
Expand Down Expand Up @@ -407,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",
Expand Down Expand Up @@ -442,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",
Expand Down
14 changes: 14 additions & 0 deletions ChatBot/ChatBot/Controller/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
}
}
}
}
2 changes: 1 addition & 1 deletion ChatBot/ChatBot/Model/MessageDTO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Created by nayeon on 3/27/24.
//

struct MessageDTO: Codable {
struct MessageDTO: Codable, DictionaryRepresentable {
let role: Role
let content: String
}
Expand Down
6 changes: 5 additions & 1 deletion ChatBot/ChatBot/Model/RequestDTO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
40 changes: 40 additions & 0 deletions ChatBot/ChatBot/Network/APIClient.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// APIClient.swift
// ChatBot
//
// Created by nayeon on 4/3/24.
//

import Alamofire

enum APIClient {

static func request<T: Decodable>(_ object: T.Type,
router: URLRequestConvertible,
completion: @escaping (NetworkResult<T>) -> 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(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(message: errorMessage)))
case 500...599:
completion(.failure(.serverError(message: errorMessage ?? "server error")))
default:
completion(.failure(.networkFail))
}
}
}
}
}
}
21 changes: 21 additions & 0 deletions ChatBot/ChatBot/Network/APIConstants.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// APIConstants.swift
// ChatBot
//
// Created by nayeon on 4/3/24.
//

import Foundation

enum 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"
}
Comment on lines +14 to +21
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HTTPHeaderField, ContentType도 사용하는 방법을 보니 API요청시 필요한 기본 세팅 문자열을 다루고 있네요.
APIConstants에 포함되어도 될 것 같은데 별도로 분리해서 선언한 이유가 궁금합니다.
그리고 여러분들의 class, struct, enum 선택 기준도 궁금합니다!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

API 요청에서 반복적으로 사용되는 HTTP 헤더 필드와 콘텐츠 타입을 정의함으로써 모듈화와 코드의 가독성을 높였습니다. 하드코딩하는 대신 의미 있는 이름을 사용할 수 있고, API 요청에 필요한 기본 세팅이 변경될 경우, 해당 세팅을 열거형의 값만 수정하여 유지보수를 쉽게 할 수 있습니다.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

애플에서는 기본적으로 특별한 이유가 없다면 struct를 사용하는것을 권장하는 것으로 알고 있습니다.
그 이유라고 하면 다음과 같은 기준이 있을 것입니다.

  1. 해당 객체가 값으로 쓰일 것인지 래퍼런스 타입으로 쓰일것인지 (불변성과 가변성)
  2. 대입 되는 경우가 생성 되는 경우보다 많을 시 class
  3. 하지만 비교적 많은 데이터, 메모리 용량이 큰 타입일 경우 class 유리할 때도 있다

열거형도 값 타입으로서 struct를 사용 할 때와 같은 기준으로 사용하지만 연관된 값들을 그룹화하여 표현하는 데 유리한 점이 있기 때문에 선택지를 제한하거나 관련된 값들을 묶어서 사용할 때 선택합니다.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

먼저 공통으로 사용되는 값들을 이렇게 분리해서 별도로 선언하여 재사용할 수 있게 구현하신 것은 좋은 방법이라고 생각합니다.
다만 제가 코멘트를 남겨놓았던 이유가 몇가지 있는데요.

  1. APIConstants, HTTPHeaderField, ContentType 사용 방법이 동일한 것 같은데 APIConstants는 struct에 static으로 프로퍼티를 선언해서 사용하고 있고 나머지 두개는 enum으로 구현해서 rawValue를 사용하고 있어 같은 목적과 사용 방법이 같은데 구현이 달라서 코멘트 남겼습니다.
  2. HTTPHeaderField, ContentType에 있는 값들을 포괄적으로 보면 APIConstants의 내용에도 포함될 수 있는 것 같아 별도로 분리하신 이유를 여쭤봤습니다.
  3. baseURL를 static으로 선언해두었는데 APIConstants 인스턴스를 생성하는 경우가 있을지?도 고민해보시면 좋을것 같아 질문드렸습니다.

51 changes: 51 additions & 0 deletions ChatBot/ChatBot/Network/APIRouter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//
// APIRouter.swift
// ChatBot
//
// Created by nayeon on 4/3/24.
//

import Alamofire

enum APIRouter: NetworkRouter, URLRequestConvertible {
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
}
}
}
17 changes: 17 additions & 0 deletions ChatBot/ChatBot/Network/NetworkResult.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// NetworkResult.swift
// ChatBot
//
// Created by nayeon on 4/3/24.
//

enum NetworkResult<T> {
case success(T)
case failure(NetworkError)
}

enum NetworkError: Error {
case pathError(message: String?)
case serverError(message: String?)
case networkFail
}
Loading