-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathNetworkSession.swift
More file actions
149 lines (120 loc) · 5.14 KB
/
NetworkSession.swift
File metadata and controls
149 lines (120 loc) · 5.14 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//
// Copyright © 2022 Iterable. All rights reserved.
//
import Foundation
protocol DataTaskProtocol {
var state: URLSessionDataTask.State { get }
func resume()
func cancel()
}
extension URLSessionDataTask: DataTaskProtocol {}
protocol NetworkSessionProtocol {
typealias CompletionHandler = (Data?, URLResponse?, Error?) -> Void
var timeout: TimeInterval { get set }
func makeRequest(_ request: URLRequest, completionHandler: @escaping CompletionHandler)
func makeDataRequest(with url: URL, completionHandler: @escaping CompletionHandler)
func createDataTask(with url: URL, completionHandler: @escaping CompletionHandler) -> DataTaskProtocol
}
extension NetworkSessionProtocol {
var timeout: TimeInterval {
get { 60.0 }
set {}
}
}
extension URLSession: NetworkSessionProtocol {
var timeout: TimeInterval {
get {
configuration.timeoutIntervalForRequest
}
set {
configuration.timeoutIntervalForRequest = newValue
}
}
func makeRequest(_ request: URLRequest, completionHandler: @escaping CompletionHandler) {
let task = dataTask(with: request) { data, response, error in
completionHandler(data, response, error)
}
task.resume()
}
func makeDataRequest(with url: URL, completionHandler: @escaping CompletionHandler) {
let task = dataTask(with: url) { data, response, error in
completionHandler(data, response, error)
}
task.resume()
}
func createDataTask(with url: URL, completionHandler: @escaping CompletionHandler) -> DataTaskProtocol {
dataTask(with: url, completionHandler: completionHandler)
}
}
protocol RedirectNetworkSessionDelegate: AnyObject {
func onRedirect(deepLinkLocation: URL?, campaignId: NSNumber?, templateId: NSNumber?, messageId: String?)
}
protocol RedirectNetworkSessionProvider {
func createRedirectNetworkSession(delegate: RedirectNetworkSessionDelegate) -> NetworkSessionProtocol
}
class RedirectNetworkSession: NSObject, NetworkSessionProtocol {
var timeout: TimeInterval {
get {
networkSession.timeout
}
set {
networkSession.timeout = newValue
}
}
func makeRequest(_ request: URLRequest, completionHandler: @escaping CompletionHandler) {
networkSession.makeRequest(request, completionHandler: completionHandler)
}
func makeDataRequest(with url: URL, completionHandler: @escaping CompletionHandler) {
networkSession.makeDataRequest(with: url, completionHandler: completionHandler)
}
func createDataTask(with url: URL, completionHandler: @escaping CompletionHandler) -> DataTaskProtocol {
networkSession.createDataTask(with: url, completionHandler: completionHandler)
}
private lazy var networkSession: NetworkSessionProtocol = {
URLSession(configuration: .default, delegate: self, delegateQueue: OperationQueue.main)
} ()
init(delegate: RedirectNetworkSessionDelegate?) {
self.delegate = delegate
}
private weak var delegate: RedirectNetworkSessionDelegate?
}
extension RedirectNetworkSession: URLSessionDelegate, URLSessionTaskDelegate {
internal func urlSession(_: URLSession,
task _: URLSessionTask,
willPerformHTTPRedirection response: HTTPURLResponse,
newRequest request: URLRequest,
completionHandler: @escaping (URLRequest?) -> Void) {
var deepLinkLocation: URL? = nil
var campaignId: NSNumber? = nil
var templateId: NSNumber? = nil
var messageId: String? = nil
deepLinkLocation = request.url
guard let headerFields = response.allHeaderFields as? [String: String] else {
delegate?.onRedirect(deepLinkLocation: deepLinkLocation, campaignId: campaignId, templateId: templateId, messageId: messageId)
completionHandler(nil)
return
}
guard let url = response.url else {
delegate?.onRedirect(deepLinkLocation: deepLinkLocation, campaignId: campaignId, templateId: templateId, messageId: messageId)
completionHandler(nil)
return
}
for cookie in HTTPCookie.cookies(withResponseHeaderFields: headerFields, for: url) {
if cookie.name == Const.CookieName.campaignId {
campaignId = number(fromString: cookie.value)
} else if cookie.name == Const.CookieName.templateId {
templateId = number(fromString: cookie.value)
} else if cookie.name == Const.CookieName.messageId {
messageId = cookie.value
}
}
delegate?.onRedirect(deepLinkLocation: deepLinkLocation, campaignId: campaignId, templateId: templateId, messageId: messageId)
completionHandler(nil)
}
private func number(fromString str: String) -> NSNumber {
if let intValue = Int(str) {
return NSNumber(value: intValue)
}
return NSNumber(value: 0)
}
}