Files
workavia-mail-front/ios/TwakeCore/Network/AlamofireService.swift
T
dab246 b1b2bb90a5 TF-2384 Handle refresh token when token expired in NSE
Signed-off-by: dab246 <tdvu@linagora.com>
(cherry picked from commit c8a39bb42792770c001ac90d6301d53126225d95)
2024-01-08 14:25:26 +01:00

32 lines
1.0 KiB
Swift

import Foundation
import Alamofire
class AlamofireService {
static let shared: AlamofireService = AlamofireService()
func post<T: Codable>(
url: String,
payloadData: Data?,
headers: HTTPHeaders? = nil,
interceptor: RequestInterceptor? = nil,
onSuccess: @escaping (T) -> Void,
onFailure: @escaping (Error) -> Void
) {
guard let baseUrl = URL(string: url),
var request = try? URLRequest(url: baseUrl, method: .post, headers: headers) else {
return onFailure(NetworkExceptions.requestUrlInvalid)
}
request.httpBody = payloadData
AF.request(request, interceptor: interceptor).validate().responseDecodable(of: T.self) { response in
switch(response.result) {
case .success(let data):
onSuccess(data)
case .failure(let error):
onFailure(NetworkExceptions(value: "\(error.localizedDescription)"))
}
}
}
}