From abe903f24e8b2e3fb17bfbadc9b07e6273c24103 Mon Sep 17 00:00:00 2001 From: Jin Xu Date: Tue, 17 Dec 2024 13:11:23 -0800 Subject: [PATCH] add method instead of add protocol --- Sources/Amplitude/Types.swift | 47 +++---------------- .../Supports/TestUtilities.swift | 28 +++++------ 2 files changed, 20 insertions(+), 55 deletions(-) diff --git a/Sources/Amplitude/Types.swift b/Sources/Amplitude/Types.swift index 0c217fa..4bb8ab1 100644 --- a/Sources/Amplitude/Types.swift +++ b/Sources/Amplitude/Types.swift @@ -149,7 +149,7 @@ extension Plugin { func onOptOutChanged(_ optOut: Bool) {} } -public protocol ResponseHandler: ResponseHandlerV2 { +public protocol ResponseHandler { func handle(result: Result) func handleSuccessResponse(code: Int) func handleBadRequestResponse(data: [String: Any]) @@ -157,17 +157,11 @@ public protocol ResponseHandler: ResponseHandlerV2 { func handleTooManyRequestsResponse(data: [String: Any]) func handleTimeoutResponse(data: [String: Any]) func handleFailedResponse(data: [String: Any]) -} -public protocol ResponseHandlerV2 { - // return true if some attempts to recover are implemented + // Added on v1.11.2. + // A replacement for handle(result: Result) -> Void + // Return true if some attempts to recover are implemented func handle(result: Result) -> Bool - func handleSuccessResponse(code: Int) -> Bool - func handleBadRequestResponse(data: [String: Any]) -> Bool - func handlePayloadTooLargeResponse(data: [String: Any]) -> Bool - func handleTooManyRequestsResponse(data: [String: Any]) -> Bool - func handleTimeoutResponse(data: [String: Any]) -> Bool - func handleFailedResponse(data: [String: Any]) -> Bool } extension ResponseHandler { @@ -182,39 +176,10 @@ extension ResponseHandler { } } +// Provide compatibility for new `handle` function added on v1.11.2. extension ResponseHandler { public func handle(result: Result) -> Bool { - handle(result: result) - return false - } - - public func handleSuccessResponse(code: Int) -> Bool { - handleSuccessResponse(code: code) - return false - } - - public func handleBadRequestResponse(data: [String: Any]) -> Bool { - handleBadRequestResponse(data: data) - return false - } - - public func handlePayloadTooLargeResponse(data: [String: Any]) -> Bool { - handlePayloadTooLargeResponse(data: data) - return false - } - - public func handleTooManyRequestsResponse(data: [String: Any]) -> Bool { - handleTooManyRequestsResponse(data: data) - return false - } - - public func handleTimeoutResponse(data: [String: Any]) -> Bool { - handleTimeoutResponse(data: data) - return false - } - - public func handleFailedResponse(data: [String: Any]) -> Bool { - handleFailedResponse(data: data) + let _: Void = handle(result: result) return false } } diff --git a/Tests/AmplitudeTests/Supports/TestUtilities.swift b/Tests/AmplitudeTests/Supports/TestUtilities.swift index 1b42a7c..abb5c1a 100644 --- a/Tests/AmplitudeTests/Supports/TestUtilities.swift +++ b/Tests/AmplitudeTests/Supports/TestUtilities.swift @@ -186,6 +186,20 @@ class FakeResponseHandler: ResponseHandler { self.eventsString = eventsString } + func handle(result: Result) -> Bool { + switch result { + case .success(let code): + return handleSuccessResponse(code: code) + default: + return false + } + } + + func handleSuccessResponse(code: Int) -> Bool { + storage.remove(eventBlock: eventBlock) + return true + } + func handle(result: Result) { let _: Bool = handle(result: result) } @@ -208,20 +222,6 @@ class FakeResponseHandler: ResponseHandler { func handleFailedResponse(data: [String: Any]) { } - - func handle(result: Result) -> Bool { - switch result { - case .success(let code): - return handleSuccessResponse(code: code) - default: - return false - } - } - - func handleSuccessResponse(code: Int) -> Bool { - storage.remove(eventBlock: eventBlock) - return true - } } class FakePersistentStorage: PersistentStorage {