@@ -27,122 +27,6 @@ extension NetworkError: LocalizedError {
27
27
}
28
28
}
29
29
30
- protocol DataTaskProtocol {
31
- var state : URLSessionDataTask . State { get }
32
- func resume( )
33
- func cancel( )
34
- }
35
-
36
- extension URLSessionDataTask : DataTaskProtocol { }
37
-
38
- protocol NetworkSessionProtocol {
39
- typealias CompletionHandler = ( Data ? , URLResponse ? , Error ? ) -> Void
40
- func makeRequest( _ request: URLRequest , completionHandler: @escaping CompletionHandler )
41
- func makeDataRequest( with url: URL , completionHandler: @escaping CompletionHandler )
42
- func createDataTask( with url: URL , completionHandler: @escaping CompletionHandler ) -> DataTaskProtocol
43
- }
44
-
45
- extension URLSession : NetworkSessionProtocol {
46
- func makeRequest( _ request: URLRequest , completionHandler: @escaping CompletionHandler ) {
47
- let task = dataTask ( with: request) { data, response, error in
48
- completionHandler ( data, response, error)
49
- }
50
-
51
- task. resume ( )
52
- }
53
-
54
- func makeDataRequest( with url: URL , completionHandler: @escaping CompletionHandler ) {
55
- let task = dataTask ( with: url) { data, response, error in
56
- completionHandler ( data, response, error)
57
- }
58
-
59
- task. resume ( )
60
- }
61
-
62
- func createDataTask( with url: URL , completionHandler: @escaping CompletionHandler ) -> DataTaskProtocol {
63
- dataTask ( with: url, completionHandler: completionHandler)
64
- }
65
- }
66
-
67
- protocol RedirectNetworkSessionDelegate : AnyObject {
68
- func onRedirect( deeplinkLocation: URL ? , campaignId: NSNumber ? , templateId: NSNumber ? , messageId: String ? )
69
- }
70
-
71
- protocol RedirectNetworkSessionProvider {
72
- func createRedirectNetworkSession( delegate: RedirectNetworkSessionDelegate ) -> NetworkSessionProtocol
73
- }
74
-
75
- class RedirectNetworkSession : NSObject , NetworkSessionProtocol {
76
- func makeRequest( _ request: URLRequest , completionHandler: @escaping CompletionHandler ) {
77
- networkSession. makeRequest ( request, completionHandler: completionHandler)
78
- }
79
-
80
- func makeDataRequest( with url: URL , completionHandler: @escaping CompletionHandler ) {
81
- networkSession. makeDataRequest ( with: url, completionHandler: completionHandler)
82
- }
83
-
84
- func createDataTask( with url: URL , completionHandler: @escaping CompletionHandler ) -> DataTaskProtocol {
85
- networkSession. createDataTask ( with: url, completionHandler: completionHandler)
86
- }
87
-
88
- private lazy var networkSession : NetworkSessionProtocol = {
89
- URLSession ( configuration: . default, delegate: self , delegateQueue: OperationQueue . main)
90
- } ( )
91
-
92
- init ( delegate: RedirectNetworkSessionDelegate ? ) {
93
- self . delegate = delegate
94
- }
95
-
96
- private weak var delegate : RedirectNetworkSessionDelegate ?
97
- }
98
-
99
- extension RedirectNetworkSession : URLSessionDelegate , URLSessionTaskDelegate {
100
- internal func urlSession( _: URLSession ,
101
- task _: URLSessionTask ,
102
- willPerformHTTPRedirection response: HTTPURLResponse ,
103
- newRequest request: URLRequest ,
104
- completionHandler: @escaping ( URLRequest ? ) -> Void ) {
105
- var deepLinkLocation : URL ? = nil
106
- var campaignId : NSNumber ? = nil
107
- var templateId : NSNumber ? = nil
108
- var messageId : String ? = nil
109
-
110
- deepLinkLocation = request. url
111
-
112
- guard let headerFields = response. allHeaderFields as? [ String : String ] else {
113
- delegate? . onRedirect ( deeplinkLocation: deepLinkLocation, campaignId: campaignId, templateId: templateId, messageId: messageId)
114
- return
115
- }
116
-
117
- guard let url = response. url else {
118
- delegate? . onRedirect ( deeplinkLocation: deepLinkLocation, campaignId: campaignId, templateId: templateId, messageId: messageId)
119
- return
120
- }
121
-
122
- for cookie in HTTPCookie . cookies ( withResponseHeaderFields: headerFields, for: url) {
123
- if cookie. name == Const . CookieName. campaignId {
124
- campaignId = number ( fromString: cookie. value)
125
- } else if cookie. name == Const . CookieName. templateId {
126
- templateId = number ( fromString: cookie. value)
127
- } else if cookie. name == Const . CookieName. messageId {
128
- messageId = cookie. value
129
- }
130
- }
131
-
132
- delegate? . onRedirect ( deeplinkLocation: deepLinkLocation, campaignId: campaignId, templateId: templateId, messageId: messageId)
133
- completionHandler ( nil )
134
- }
135
-
136
- private func number( fromString str: String ) -> NSNumber {
137
- if let intValue = Int ( str) {
138
- return NSNumber ( value: intValue)
139
- }
140
-
141
- return NSNumber ( value: 0 )
142
- }
143
-
144
- }
145
-
146
30
struct NetworkHelper {
147
31
static func getData( fromUrl url: URL , usingSession networkSession: NetworkSessionProtocol ) -> Pending < Data , Error > {
148
32
let fulfill = Fulfill < Data , Error > ( )
0 commit comments