- 열거형(enum)에서는 저장 프로퍼티를 사용할 수 없다.
static let user = UserDefaultService.shared.getUser()
var baseURL: URL {
return URL(string: "https://www.apple.com/")!
}
위에 처럼 계산 프로퍼티나 static을 사용해서 구현한다.
- alamofire 사용시 router 구현해 봤다.
import Foundation
import Alamofire
enum APIRouter: URLRequestConvertible {
// MARK: - Property
static let user = UserDefaultService.shared.getUser()
var baseURL: URL {
return URL(string: "http://localhost:8080/api")!
}
// MARK: - Cases
// auth
case emailSignIn(email: String, username: String, password: String)
case emailLogin(email: String, password: String)
// todo
case getTasks
case postTask(contents: String, date: String)
case updateTodo(contents: String, taskId: Int)
case deleteTodo(taskId: Int)
case checkTodo(taskId: Int)
// timer
case postTime(second: Int)
case getTodayTime
// board, group
case getClubs
case createClub(memberID: Int, clubName: String, numberOfMember: Int, clubInfo: String)
case singInClub(clubId: Int)
// MARK: - End Point
var endPoint: String {
guard let userId = APIRouter.user?.userInfo.id else { return ""}
switch self {
// auth
case .emailSignIn:
return "v1/members"
case .emailLogin:
return "v1/login"
// todo
case .getTasks, .postTask:
return "tasks/\(userId)"
case let .updateTodo(_, taskId), let .deleteTodo(taskId):
return "tasks/\(userId)/\(taskId)"
case let .checkTodo(taskId):
return "tasks/check/\(taskId)"
// timer
case .postTime, .getTodayTime:
return "timer/\(userId)"
// board, group
case .getClubs:
return "clubs"
case .createClub:
return "clubs"
case let .singInClub(clubId):
return "clubs/\(clubId)/\(userId)"
}
}
// MARK: - Method
var method: HTTPMethod {
switch self {
// auth
case .emailSignIn:
return .post
case .emailLogin:
return .post
case .deleteTodo:
return .delete
case .checkTodo:
return .get
// todo
case .getTasks:
return .get
case .postTask:
return .post
case .updateTodo:
return .put
// timer
case .postTime:
return .post
case .getTodayTime:
return .get
// board, group
case .getClubs:
return .get
case .createClub:
return .post
case .singInClub:
return .get
}
}
// MARK: - Parameters
var parameters: Parameters? {
var params = Parameters()
switch self {
// auth
case let .emailSignIn(email, username, password):
params["email"] = email
params["username"] = username
params["password"] = password
case let .emailLogin(email, password):
params["email"] = email
params["password"] = password
// todo
case .getTasks:
return nil
case let .postTask(contents, date):
params["contents"] = contents
params["date"] = date
case let .updateTodo(contents, _):
params["contents"] = contents
case .deleteTodo:
return nil
case .checkTodo:
return nil
// timer
case let .postTime(second):
params["second"] = second
case .getTodayTime:
return nil
// board, group
case .getClubs:
return nil
case let .createClub(memberID, clubName, numberOfMember, clubInfo):
params["memberID"] = memberID
params["clubName"] = clubName
params["numberOfMember"] = numberOfMember
params["clubInfo"] = clubInfo
case .singInClub:
return nil
}
return params
}
// MARK: - URL Request
func asURLRequest() throws -> URLRequest {
var urlRequest = URLRequest(url: baseURL.appendingPathComponent(endPoint))
urlRequest.method = self.method
switch self.method {
case .get:
return urlRequest
case .post:
if let parameters = parameters {
urlRequest.httpBody = try JSONSerialization.data(withJSONObject: parameters)
}
case .put:
urlRequest.httpBody = try JSONEncoding.default.encode(urlRequest, with: parameters).httpBody
default:
return urlRequest
}
return urlRequest
}
}
asURLRequest() 함수에서 method별로 구현해주기도 하고 case 별로 구현해주는 방법도 있다.
header를 넣어 주는 과정에서
urlRequest.headers = self.headers
처럼 시도해서 오류가 발생했었다ㅜ
urlRequest.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
setValue를 사용해서 구현해줘야한다.
Write a Networking Layer in Swift 4 using Alamofire 5 and Codable
alamofire git repository
stack overflow 참고
AlamofireURLRequestConfigurable 1