-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathRequestProcessorUtil.swift
117 lines (108 loc) · 5.09 KB
/
RequestProcessorUtil.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//
// Copyright © 2020 Iterable. All rights reserved.
//
import Foundation
struct RequestProcessorUtil {
@discardableResult
static func sendRequest(requestProvider: @escaping () -> Pending<SendRequestValue, SendRequestError>,
successHandler onSuccess: OnSuccessHandler? = nil,
failureHandler onFailure: OnFailureHandler? = nil,
authManager: IterableAuthManagerProtocol? = nil,
requestIdentifier identifier: String) -> Pending<SendRequestValue, SendRequestError> {
let result = Fulfill<SendRequestValue, SendRequestError>()
requestProvider().onSuccess { json in
reportSuccess(result: result, value: json, successHandler: onSuccess, identifier: identifier)
}
.onError { error in
if error.httpStatusCode == 401, error.iterableCode == JsonValue.Code.invalidJwtPayload {
ITBError("invalid JWT token, trying again: \(error.reason ?? "")")
authManager?.requestNewAuthToken(hasFailedPriorAuth: true) { _ in
requestProvider().onSuccess { json in
reportSuccess(result: result, value: json, successHandler: onSuccess, identifier: identifier)
}.onError { error in
reportFailure(result: result, error: error, failureHandler: onFailure, identifier: identifier)
}
} onFailure: { _ in
reportFailure(result: result, error: error, failureHandler: onFailure, identifier: identifier)
}
} else if error.httpStatusCode == 401, error.iterableCode == JsonValue.Code.badApiKey {
ITBError(error.reason)
reportFailure(result: result, error: error, failureHandler: onFailure, identifier: identifier)
} else {
ITBError(error.reason)
reportFailure(result: result, error: error, failureHandler: onFailure, identifier: identifier)
}
}
return result
}
@discardableResult
static func apply(successHandler onSuccess: OnSuccessHandler? = nil,
andFailureHandler onFailure: OnFailureHandler? = nil,
andAuthManager authManager: IterableAuthManagerProtocol? = nil,
toResult result: Pending<SendRequestValue, SendRequestError>,
withIdentifier identifier: String) -> Pending<SendRequestValue, SendRequestError> {
result.onSuccess { json in
if let onSuccess = onSuccess {
onSuccess(json)
} else {
defaultOnSuccess(identifier)(json)
}
}.onError { error in
if error.httpStatusCode == 401, error.iterableCode == JsonValue.Code.invalidJwtPayload {
ITBError(error.reason)
authManager?.requestNewAuthToken(hasFailedPriorAuth: true, onSuccess: nil, onFailure: nil)
} else if error.httpStatusCode == 401, error.iterableCode == JsonValue.Code.badApiKey {
ITBError(error.reason)
}
if let onFailure = onFailure {
onFailure(error.reason, error.data)
} else {
defaultOnFailure(identifier)(error.reason, error.data)
}
}
return result
}
private static func reportSuccess(result: Fulfill<SendRequestValue, SendRequestError>,
value: SendRequestValue,
successHandler onSuccess: OnSuccessHandler?,
identifier: String) {
if let onSuccess = onSuccess {
onSuccess(value)
} else {
Self.defaultOnSuccess(identifier)(value)
}
result.resolve(with: value)
}
private static func reportFailure(result: Fulfill<SendRequestValue, SendRequestError>,
error: SendRequestError,
failureHandler onFailure: OnFailureHandler?,
identifier: String) {
if let onFailure = onFailure {
onFailure(error.reason, error.data)
} else {
defaultOnFailure(identifier)(error.reason, error.data)
}
result.reject(with: error)
}
private static func defaultOnSuccess(_ identifier: String) -> OnSuccessHandler {
{ data in
if let data = data {
ITBInfo("\(identifier) succeeded, got response: \(data)")
} else {
ITBInfo("\(identifier) succeeded.")
}
}
}
private static func defaultOnFailure(_ identifier: String) -> OnFailureHandler {
{ reason, data in
var toLog = "\(identifier) failed:"
if let reason = reason {
toLog += ", \(reason)"
}
if let data = data {
toLog += ", got response \(String(data: data, encoding: .utf8) ?? "nil")"
}
ITBError(toLog)
}
}
}