Skip to content

Commit

Permalink
add method instead of add protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
sojingle committed Dec 17, 2024
1 parent 84201ff commit abe903f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 55 deletions.
47 changes: 6 additions & 41 deletions Sources/Amplitude/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,25 +149,19 @@ extension Plugin {
func onOptOutChanged(_ optOut: Bool) {}
}

public protocol ResponseHandler: ResponseHandlerV2 {
public protocol ResponseHandler {
func handle(result: Result<Int, Error>)
func handleSuccessResponse(code: Int)
func handleBadRequestResponse(data: [String: Any])
func handlePayloadTooLargeResponse(data: [String: Any])
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<Int, Error>) -> Void
// Return true if some attempts to recover are implemented
func handle(result: Result<Int, Error>) -> 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 {
Expand All @@ -182,39 +176,10 @@ extension ResponseHandler {
}
}

// Provide compatibility for new `handle` function added on v1.11.2.
extension ResponseHandler {
public func handle(result: Result<Int, any Error>) -> 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
}
}
28 changes: 14 additions & 14 deletions Tests/AmplitudeTests/Supports/TestUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,20 @@ class FakeResponseHandler: ResponseHandler {
self.eventsString = eventsString
}

func handle(result: Result<Int, Error>) -> 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<Int, any Error>) {
let _: Bool = handle(result: result)
}
Expand All @@ -208,20 +222,6 @@ class FakeResponseHandler: ResponseHandler {

func handleFailedResponse(data: [String: Any]) {
}

func handle(result: Result<Int, Error>) -> 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 {
Expand Down

0 comments on commit abe903f

Please sign in to comment.