-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkLayerAssembly.swift
More file actions
109 lines (96 loc) · 4.35 KB
/
Copy pathNetworkLayerAssembly.swift
File metadata and controls
109 lines (96 loc) · 4.35 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
//
// network-layer
// Copyright © 2023 Space Code. All rights reserved.
//
import Foundation
import NetworkLayerInterfaces
import Typhoon
/// A final class responsible for assembling the network layer components.
/// It configures the `IRequestProcessor` with necessary dependencies such as retry policies, interceptors, and encoders.
public final class NetworkLayerAssembly: INetworkLayerAssembly {
// MARK: Properties
/// The configuration settings for the network session and decoding.
private let configure: Configuration
/// The specific strategy used to handle request retries.
private let retryPolicyStrategy: RetryPolicyStrategy?
/// A thread-safe wrapper for the request processor delegate.
private var delegate: SafeRequestProcessorDelegate?
/// An optional interceptor for handling authentication (e.g., adding tokens).
private let interceptor: IAuthenticationInterceptor?
/// The encoder used to transform objects into JSON data for request bodies.
private let jsonEncoder: JSONEncoder
/// A global evaluator to determine if a retry should be attempted based on the error.
/// This applies to all requests processed by this instance.
private let retryEvaluator: (@Sendable (Error) -> RetryAction)?
// MARK: Initialization
/// Initializes a new instance of the assembly.
/// - Parameters:
/// - configure: The network configuration. Defaults to a standard session setup.
/// - retryStrategy: The strategy to determine how retries are handled. Defaults to `.none`.
/// - delegate: An optional delegate to observe request lifecycle events.
/// - interceptor: An optional interceptor for request/response modification.
/// - jsonEncoder: The encoder used for request bodies.
/// - retryEvaluator: A global evaluator to determine if a retry should be attempted based on the error.
public init(
configure: Configuration = .init(
sessionConfiguration: .default,
sessionDelegate: nil,
sessionDelegateQueue: nil,
jsonDecoder: JSONDecoder()
),
retryStrategy: RetryStrategy = .none,
delegate: RequestProcessorDelegate? = nil,
interceptor: IAuthenticationInterceptor? = nil,
jsonEncoder: JSONEncoder = JSONEncoder(),
retryEvaluator: (@Sendable (Error) -> RetryAction)? = nil
) {
self.configure = configure
self.delegate = SafeRequestProcessorDelegate(delegate: delegate)
self.interceptor = interceptor
self.jsonEncoder = jsonEncoder
self.retryEvaluator = retryEvaluator
switch retryStrategy {
case .none:
retryPolicyStrategy = nil
case .default:
retryPolicyStrategy = .constant(retry: 5, dispatchDuration: .seconds(1))
case let .custom(strategy):
retryPolicyStrategy = strategy
}
}
// MARK: INetworkLayerAssembly
/// Assembles and returns a fully configured request processor.
/// - Returns: An object conforming to `IRequestProcessor` ready to handle network calls.
public func assemble() -> IRequestProcessor {
RequestProcessor(
configuration: configure,
requestBuilder: requestBuilder,
dataRequestHandler: DataRequestHandler(),
retryPolicyService: retryPolicyStrategy.map { RetryPolicyService(strategy: $0) },
delegate: delegate,
interceptor: interceptor,
retryEvaluator: retryEvaluator
)
}
// MARK: - Private Computed Properties
/// Creates a request builder with the necessary encoders and formatters.
private var requestBuilder: IRequestBuilder {
RequestBuilder(
parametersEncoder: parametersEncoder,
requestBodyEncoder: requestBodyEncoder,
queryFormatter: queryFormatter
)
}
/// Provides the encoder for general request parameters.
private var parametersEncoder: IRequestParametersEncoder {
RequestParametersEncoder()
}
/// Provides the encoder for the request body, utilizing the assembly's JSON encoder.
private var requestBodyEncoder: IRequestBodyEncoder {
RequestBodyEncoder(jsonEncoder: jsonEncoder)
}
/// Provides the formatter for URL query parameters.
private var queryFormatter: IQueryParametersFormatter {
QueryParametersFormatter()
}
}