pod 'Alamofire', '~> 5.5'
pod 'SwiftyJSON', '~> 4.0'
GET
private func requestGet<T:Codable>(url:String, completion:@escaping (APIResult<T>)->Void) {
self.request = AF.request(url, method:.get, encoding: JSONEncoding.default)
self.request?.validate(statusCode: 200..<500)
self.request?.responseDecodable(of: T.self) { response in
switch response.result {
case .success(let data):
completion(.success(data))
case .failure(let error):
completion(.failure(error))
}
}
}
POST
private func requestPost<T:Codable>(url:String, params:[String:Any], completion:@escaping (APIResult<T>)->Void) {
print("\n\n")
print("REQUEST POST")
print(url)
self.request = AF.request(url, method:.post, parameters: params, encoding: JSONEncoding.default)
self.request?.validate(statusCode: 200..<500)
self.request?.responseDecodable(of: T.self) { response in
switch response.result {
case .success(let data):
completion(.success(data))
case .failure(let error):
completion(.failure(error))
}
}
}
PUT
private func requestPut<T:Codable>(url:String, params:[String:Any], completion:@escaping (APIResult<T>)->Void) {
self.request = AF.request(url, method:.put, parameters: params, encoding: JSONEncoding.default)
self.request?.validate(statusCode: 200..<500)
self.request?.responseDecodable(of: T.self) { response in
switch response.result {
case .success(let data):
completion(.success(data))
case .failure(let error):
completion(.failure(error))
}
}
}
DELETE
private func requestDelete<T:Codable>(url:String, params:[String:Any], completion:@escaping (APIResult<T>)->Void) {
self.request = AF.request(url, method:.delete, parameters: params, encoding: JSONEncoding.default)
self.request?.validate(statusCode: 200..<500)
self.request?.responseDecodable(of: T.self) { response in
switch response.result {
case .success(let data):
completion(.success(data))
case .failure(let error):
completion(.failure(error))
}
}
}
'개발 > IOS' 카테고리의 다른 글
키보드 올라올때 화면 대응하기 (0) | 2022.01.24 |
---|---|
Swift IOS 앨범 권한 (0) | 2021.06.01 |
ios swift get app version (0) | 2020.11.10 |
[IOS] Swift 서버 이미지 업로드 (0) | 2020.04.19 |