Skip to content

Commit 63f0e78

Browse files
committed
fix listener
1 parent 173a5d3 commit 63f0e78

File tree

4 files changed

+21
-13
lines changed

4 files changed

+21
-13
lines changed

Sources/SwiftAPIClient/APIClientCaller.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ public extension APIClient {
238238
configs = configs.with(\.reportMetrics, false)
239239
configs.logRequestStarted(request, uuid: uuid)
240240
let result = try caller.mockResult(for: mock)
241-
configs.logRequestCompleted(request, response: nil, data: nil, uuid: uuid, start: start, result: result)
241+
configs.logRequestCompleted(request, response: nil, data: nil, uuid: uuid, start: start)
242242
return result
243243
}
244244

@@ -257,9 +257,11 @@ public extension APIClient {
257257
response: (response as? (Data, HTTPResponse))?.1,
258258
data: data,
259259
uuid: uuid,
260-
start: start,
261-
result: result
260+
start: start
262261
)
262+
if !caller.logRequestByItSelf {
263+
configs.listener.onRequestCompleted(id: uuid, configs: configs)
264+
}
263265
return result
264266
} catch {
265267
var error = error

Sources/SwiftAPIClient/Clients/HTTPClient.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ extension APIClientCaller where Result == AsyncThrowingValue<(Value, HTTPRespons
142142
let result = try serialize((value, response)) {
143143
try validate(value, response, configs)
144144
}
145+
configs.listener.onRequestCompleted(id: uuid, configs: configs)
145146
return (result, response)
146147
}
147148
} mockResult: { value in

Sources/SwiftAPIClient/Modifiers/ListenerModifiers.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@ public protocol APIClientListener {
55
func onRequestStarted(id: UUID, request: HTTPRequestComponents, configs: APIClient.Configs)
66
func onResponseReceived<R>(id: UUID, response: R, configs: APIClient.Configs)
77
func onResponseSerialized<T>(id: UUID, response: T, configs: APIClient.Configs)
8-
func onError(id: UUID, error: Error, configs: APIClient.Configs)
8+
func onRequestFailed(id: UUID, error: Error, configs: APIClient.Configs)
9+
func onRequestCompleted(id: UUID, configs: APIClient.Configs)
910
}
1011

11-
extension APIClientListener {
12+
public extension APIClientListener {
1213

1314
func onRequestStarted(id: UUID, request: HTTPRequestComponents, configs: APIClient.Configs) {}
1415
func onResponseReceived<R>(id: UUID, response: R, configs: APIClient.Configs) {}
1516
func onResponseSerialized<T>(id: UUID, response: T, configs: APIClient.Configs) {}
16-
func onError(id: UUID, error: Error, configs: APIClient.Configs) {}
17+
func onRequestFailed(id: UUID, error: Error, configs: APIClient.Configs) {}
18+
func onRequestCompleted(id: UUID, configs: APIClient.Configs) {}
1719
}
1820

1921
public struct MultiplexAPIClientListener: APIClientListener {
@@ -36,8 +38,12 @@ public struct MultiplexAPIClientListener: APIClientListener {
3638
listeners.forEach { $0.onResponseSerialized(id: id, response: response, configs: configs) }
3739
}
3840

39-
public func onError(id: UUID, error: Error, configs: APIClient.Configs) {
40-
listeners.forEach { $0.onError(id: id, error: error, configs: configs) }
41+
public func onRequestFailed(id: UUID, error: Error, configs: APIClient.Configs) {
42+
listeners.forEach { $0.onRequestFailed(id: id, error: error, configs: configs) }
43+
}
44+
45+
public func onRequestCompleted(id: UUID, configs: APIClient.Configs) {
46+
listeners.forEach { $0.onRequestCompleted(id: id, configs: configs) }
4147
}
4248
}
4349

Sources/SwiftAPIClient/Modifiers/LoggingModifier.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,17 +151,16 @@ public extension APIClient.Configs {
151151
} catch {
152152
finalError = error
153153
}
154-
listener.onError(id: uuid, error: finalError, configs: self)
154+
listener.onRequestFailed(id: uuid, error: finalError, configs: self)
155155
return finalError
156156
}
157157

158-
func logRequestCompleted<T>(
158+
func logRequestCompleted(
159159
_ request: HTTPRequestComponents,
160160
response: HTTPResponse?,
161161
data: Data?,
162162
uuid: UUID,
163-
start: Date,
164-
result: T
163+
start: Date
165164
) {
166165
let duration = Date().timeIntervalSince(start)
167166
if !loggingComponents.isEmpty {
@@ -179,7 +178,7 @@ public extension APIClient.Configs {
179178
if reportMetrics {
180179
updateHTTPMetrics(for: request, status: response?.status, duration: duration, successful: true)
181180
}
182-
listener.onResponseSerialized(id: uuid, response: result, configs: self)
181+
listener.onRequestCompleted(id: uuid, configs: self)
183182
}
184183
}
185184

0 commit comments

Comments
 (0)