반응형
Alamofire 호출 형식 기록
Get
##Result<Success, Error>
static func getCommon(url: String, paramDic: [String: Any], completionHandler: @escaping (Result<AppCheck, Error>) -> Void) {
let header: HTTPHeaders = [
"Content-Type":"application/json",
"Accept":"application/json"
]
AF.request(url, method: .get, parameters: paramDic, encoding: URLEncoding.default, headers: header)
.validate(statusCode: 200..<500)
.responseDecodable(of: AppCheck.self) { response in
switch response.result {
case .success(let data):
completionHandler(.success(data))
case .failure(let error):
completionHandler(.failure(error))
}
}
}
Post
##[String: Any]
static func login(url: String, paramDic: [String: Any], completionHandler: @escaping ([String: Any]) -> Void) {
let header: HTTPHeaders = [
"Content-Type":"application/json",
"Accept":"application/json"
]
AF.request(url, method: .post, parameters: paramDic, encoding: JSONEncoding.default, headers: header)
.validate(statusCode: 200..<500)
.responseDecodable(of: LoginModel.self) { response in
var resultParam = [String: Any]()
switch response.result {
case .success(let data):
resultParam["result"] = data
completionHandler(resultParam)
case .failure(let error):
if response.response?.statusCode == 409 {
resultParam["result"] = 409
completionHandler(resultParam)
} else {
resultParam["result"] = 400
completionHandler(resultParam)
}
}
}
}
Model
Get
struct AppCheck: Codable {
let transactionTime, status, message: String
let data: DataClass
}
// MARK: - DataClass
struct DataClass: Codable {
let appVer, appHash, appName: String
let canUseHash, needUpdate: Bool
let binaryLink: String
let plistURL: String
let appPlatformType: String
enum CodingKeys: String, CodingKey {
case appVer, appHash, appName, canUseHash, needUpdate, binaryLink
case plistURL = "plistUrl"
case appPlatformType
}
}
Post
import Foundation
// MARK: - LoginModel
struct LoginModel: Codable {
let data: DataClass?
let message: String?
}
// MARK: - DataClass
struct DataClass: Codable {
let accessToken: String?
let userInfo: UserInfo?
}
// MARK: - UserInfo
struct UserInfo: Codable {
let userID: Int?
let nickName, gender, age, height: JSONNull?
let weight: JSONNull?
let thumbNailURL: String?
let usingPlatform, benchPressWeight, deadLiftWeight, squatWeight: Int?
let emailAddr: JSONNull?
let createdAt, updatedAt: String?
let role: Role?
enum CodingKeys: String, CodingKey {
case userID = "userId"
case nickName, gender, age, height, weight
case thumbNailURL = "thumbNailUrl"
case usingPlatform, benchPressWeight, deadLiftWeight, squatWeight, emailAddr, createdAt, updatedAt, role
}
}
// MARK: - Role
struct Role: Codable {
let roleID: Int?
let roleName: String?
enum CodingKeys: String, CodingKey {
case roleID = "roleId"
case roleName
}
}
// MARK: - Encode/decode helpers
class JSONNull: Codable, Hashable {
public static func == (lhs: JSONNull, rhs: JSONNull) -> Bool {
return true
}
public var hashValue: Int {
return 0
}
public init() {}
public required init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if !container.decodeNil() {
throw DecodingError.typeMismatch(JSONNull.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for JSONNull"))
}
}
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encodeNil()
}
}
반응형
'Swift' 카테고리의 다른 글
notRunnging 상태에서 링크 및 푸시에서 접근시 (0) | 2023.01.17 |
---|---|
웹뷰 전체 내역 캡쳐 및 앨범 저장 (0) | 2023.01.17 |
RxSwift + Alamofire (0) | 2022.07.14 |
iOS ScrollView 설정 (0) | 2022.06.05 |
iOS RxSwift GPS (0) | 2022.05.12 |