edcc30808c
Signed-off-by: dab246 <tdvu@linagora.com> (cherry picked from commit d8a330b987860f133b7e56b4aa67669c9d3c8f20)
52 lines
1.7 KiB
Swift
52 lines
1.7 KiB
Swift
import Foundation
|
|
import Alamofire
|
|
|
|
class JmapClient {
|
|
static let shared: JmapClient = JmapClient()
|
|
|
|
private let jmapHeader = HTTPHeader(name: "Accept", value: "application/json;jmapVersion=rfc-8621")
|
|
|
|
private func getBasicAuthorization(basicAuth: String) -> String {
|
|
return "Basic \(basicAuth)"
|
|
}
|
|
|
|
func getNewEmails(
|
|
apiUrl: String,
|
|
accountId: String,
|
|
sinceState: String,
|
|
authenticationType: AuthenticationType,
|
|
tokenOidc: TokenOidc?,
|
|
basicAuth: String?,
|
|
onSuccess: @escaping ([Email]) -> Void,
|
|
onFailure: @escaping (Error) -> Void
|
|
) {
|
|
let authenticationValue = authenticationType == AuthenticationType.basic
|
|
? getBasicAuthorization(basicAuth: basicAuth ?? "")
|
|
: tokenOidc?.getAuthorization() ?? ""
|
|
|
|
let headers = HTTPHeaders([
|
|
jmapHeader,
|
|
HTTPHeader(name: "Authorization", value: authenticationValue)
|
|
])
|
|
|
|
let jmapRequestObject = JmapRequestGenerator.shared.createEmailChangesRequest(
|
|
accountId: accountId,
|
|
sinceState: sinceState
|
|
)
|
|
|
|
AlamofireService.shared.post(
|
|
url: apiUrl,
|
|
payloadData: jmapRequestObject?.toData(),
|
|
headers: headers,
|
|
onSuccess: { (data: JmapResponseObject<Email>) in
|
|
if let listEmail = data.parsing(methodName: JmapConstants.EMAIL_GET_METHOD_NAME, methodCallId: "c1"), !listEmail.isEmpty {
|
|
onSuccess(listEmail)
|
|
} else {
|
|
onFailure(JmapExceptions.notFoundNewEmails)
|
|
}
|
|
},
|
|
onFailure: onFailure
|
|
)
|
|
}
|
|
}
|