This repository was archived by the owner on Apr 17, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenAPITransport.swift
306 lines (263 loc) · 11.4 KB
/
OpenAPITransport.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// OpenAPITransport.swift
//
// Generated by openapi-generator
// https://openapi-generator.tech
import Foundation
import Combine
// MARK: - OpenAPITransport
public protocol OpenAPITransport: AnyObject {
var baseURL: URL? { get }
func send(request: URLRequest) -> AnyPublisher<OpenAPITransportResponse, OpenAPITransportError>
func cancelAll()
}
public struct OpenAPITransportResponse {
public let data: Data
public let statusCode: Int
public init(data: Data, statusCode: Int) {
self.data = data
self.statusCode = statusCode
}
}
public struct OpenAPITransportError: Error, CustomStringConvertible, LocalizedError {
public let statusCode: Int
public let description: String
public let errorDescription: String?
/// It might be source network error
public let nestedError: Error?
/// Data may contain additional reason info (like json payload)
public let data: Data
public init(
statusCode: Int,
description: String? = nil,
errorDescription: String? = nil,
nestedError: Error? = nil,
data: Data = Data()
) {
self.statusCode = statusCode
self.errorDescription = errorDescription
self.nestedError = nestedError
self.data = data
if let description = description {
self.description = description
} else {
var summary = "OpenAPITransportError with status \(statusCode)"
if let nestedError = nestedError {
summary.append(contentsOf: ", \(nestedError.localizedDescription)")
}
self.description = summary
}
}
}
// MARK: - Policy
/// Policy to define whether response is successful or requires authentication
public protocol ResponsePolicy {
func defineState(for request: URLRequest, output: URLSession.DataTaskPublisher.Output) -> AnyPublisher<ResponseState, Never>
}
public enum ResponseState {
/// Return success to client
case success
/// Return error to client
case failure
/// Repeat request
case retry
}
// MARK: - Interceptor
/// Define how to customize URL request before network call
public protocol Interceptor {
/// Customize request before performing. Add headers or encrypt body for example.
func intercept(request: URLRequest) -> AnyPublisher<URLRequest, OpenAPITransportError>
/// Customize response before handling. Decrypt body for example.
func intercept(output: URLSession.DataTaskPublisher.Output) -> AnyPublisher<URLSession.DataTaskPublisher.Output, OpenAPITransportError>
}
// MARK: - Transport delegate
public protocol OpenAPITransportDelegate: AnyObject {
func willStart(request: URLRequest)
func didFinish(request: URLRequest, response: HTTPURLResponse?, data: Data)
func didFinish(request: URLRequest, error: Error)
}
// MARK: - Implementation
open class URLSessionOpenAPITransport: OpenAPITransport {
public struct Config {
public var baseURL: URL?
public var session: URLSession
public var processor: Interceptor
public var policy: ResponsePolicy
public weak var delegate: OpenAPITransportDelegate?
public init(
baseURL: URL? = nil,
session: URLSession = .shared,
processor: Interceptor = DefaultInterceptor(),
policy: ResponsePolicy = DefaultResponsePolicy(),
delegate: OpenAPITransportDelegate? = nil
) {
self.baseURL = baseURL
self.session = session
self.processor = processor
self.policy = policy
self.delegate = delegate
}
}
private var cancellable = Set<AnyCancellable>()
public var config: Config
public var baseURL: URL? { config.baseURL }
public init(config: Config = .init()) {
self.config = config
}
open func send(request: URLRequest) -> AnyPublisher<OpenAPITransportResponse, OpenAPITransportError> {
config.processor
// Add custom headers or refresh token if needed
.intercept(request: request)
.flatMap { request -> AnyPublisher<OpenAPITransportResponse, OpenAPITransportError> in
self.config.delegate?.willStart(request: request)
// Perform network call
return self.config.session.dataTaskPublisher(for: request)
.mapError {
self.config.delegate?.didFinish(request: request, error: $0)
return OpenAPITransportError(statusCode: $0.code.rawValue, description: "Network call finished fails")
}
.flatMap { output in
self.config.processor.intercept(output: output)
}
.flatMap { output -> AnyPublisher<OpenAPITransportResponse, OpenAPITransportError> in
let response = output.response as? HTTPURLResponse
self.config.delegate?.didFinish(request: request, response: response, data: output.data)
return self.config.policy.defineState(for: request, output: output)
.setFailureType(to: OpenAPITransportError.self)
.flatMap { state -> AnyPublisher<OpenAPITransportResponse, OpenAPITransportError> in
switch state {
case .success:
let transportResponse = OpenAPITransportResponse(data: output.data, statusCode: 200)
return Result.success(transportResponse).publisher.eraseToAnyPublisher()
case .retry:
return Fail(error: OpenAPITransportError.retryError).eraseToAnyPublisher()
case .failure:
let code = response?.statusCode ?? OpenAPITransportError.noResponseCode
let transportError = OpenAPITransportError(statusCode: code, data: output.data)
return Fail(error: transportError).eraseToAnyPublisher()
}
}.eraseToAnyPublisher()
}
.eraseToAnyPublisher()
}
.retry(times: 2) { error -> Bool in
return error.statusCode == OpenAPITransportError.retryError.statusCode
}.eraseToAnyPublisher()
}
open func cancelAll() {
cancellable.removeAll()
}
}
public final class DefaultInterceptor: Interceptor {
public init() {}
public func intercept(request: URLRequest) -> AnyPublisher<URLRequest, OpenAPITransportError> {
Just(request)
.setFailureType(to: OpenAPITransportError.self)
.eraseToAnyPublisher()
}
public func intercept(output: URLSession.DataTaskPublisher.Output) -> AnyPublisher<URLSession.DataTaskPublisher.Output, OpenAPITransportError> {
Just(output)
.setFailureType(to: OpenAPITransportError.self)
.eraseToAnyPublisher()
}
}
public final class DefaultResponsePolicy: ResponsePolicy {
public init() {}
public func defineState(for request: URLRequest, output: URLSession.DataTaskPublisher.Output) -> AnyPublisher<ResponseState, Never> {
let state: ResponseState
switch (output.response as? HTTPURLResponse)?.statusCode {
case .some(200...299): state = .success
default: state = .failure
}
return Just(state).eraseToAnyPublisher()
}
}
/// Custom transport errors. It begins with 6.. not to conflict with HTTP codes
public extension OpenAPITransportError {
static let incorrectAuthenticationCode = 600
static func incorrectAuthenticationError(_ nestedError: Error? = nil) -> OpenAPITransportError {
OpenAPITransportError(
statusCode: OpenAPITransportError.incorrectAuthenticationCode,
description: "Impossible to add authentication headers to request",
errorDescription: NSLocalizedString(
"Impossible to add authentication headers to request",
comment: "Incorrect authentication"
),
nestedError: nestedError
)
}
static let failedAuthenticationRefreshCode = 601
static func failedAuthenticationRefreshError(_ nestedError: Error? = nil) -> OpenAPITransportError {
OpenAPITransportError(
statusCode: OpenAPITransportError.failedAuthenticationRefreshCode,
description: "Error while refreshing authentication",
errorDescription: NSLocalizedString(
"Error while refreshing authentication",
comment: "Failed authentication refresh"
),
nestedError: nestedError
)
}
static let noResponseCode = 603
static func noResponseError(_ nestedError: Error? = nil) -> OpenAPITransportError {
OpenAPITransportError(
statusCode: OpenAPITransportError.noResponseCode,
description: "There is no HTTP URL response",
errorDescription: NSLocalizedString(
"There is no HTTP URL response",
comment: "No response"
),
nestedError: nestedError
)
}
static let badURLCode = 604
static func badURLError(_ nestedError: Error? = nil) -> OpenAPITransportError {
OpenAPITransportError(
statusCode: OpenAPITransportError.badURLCode,
description: "Request URL cannot be created with given parameters",
errorDescription: NSLocalizedString(
"Request URL cannot be created with given parameters",
comment: "Bad URL"
),
nestedError: nestedError
)
}
static let invalidResponseMappingCode = 605
static func invalidResponseMappingError(data: Data) -> OpenAPITransportError {
OpenAPITransportError(
statusCode: OpenAPITransportError.invalidResponseMappingCode,
description: "Response data cannot be expected object scheme",
errorDescription: NSLocalizedString(
"Response data cannot be expected object scheme",
comment: "Invalid response mapping"
),
data: data
)
}
static let retryErrorCode = 606
static let retryError = OpenAPITransportError(statusCode: OpenAPITransportError.retryErrorCode)
}
// MARK: - Private
private extension Publishers {
struct RetryIf<P: Publisher>: Publisher {
typealias Output = P.Output
typealias Failure = P.Failure
let publisher: P
let times: Int
let condition: (P.Failure) -> Bool
func receive<S>(subscriber: S) where S : Subscriber, Failure == S.Failure, Output == S.Input {
guard times > 0 else { return publisher.receive(subscriber: subscriber) }
publisher.catch { (error: P.Failure) -> AnyPublisher<Output, Failure> in
if condition(error) {
return RetryIf(publisher: publisher, times: times - 1, condition: condition).eraseToAnyPublisher()
} else {
return Fail(error: error).eraseToAnyPublisher()
}
}.receive(subscriber: subscriber)
}
}
}
private extension Publisher {
func retry(times: Int, if condition: @escaping (Failure) -> Bool) -> Publishers.RetryIf<Self> {
Publishers.RetryIf(publisher: self, times: times, condition: condition)
}
}