Skip to content

Commit

Permalink
Merge pull request #27 from icapps/feature/pass-data-to-interceptor
Browse files Browse the repository at this point in the history
Pass data to the interceptor.
  • Loading branch information
fousa authored Mar 3, 2019
2 parents 2922784 + 4390214 commit fd08a12
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Example/Tests/Mocks/MockedInterceptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Cara

class MockedInterceptor: Interceptor {
var interceptHandle: ((_ error: ResponseError, _ retry: @escaping () -> Void) -> Bool)?
func intercept(_ error: ResponseError, retry: @escaping () -> Void) -> Bool {
func intercept(_ error: ResponseError, data: Data?, retry: @escaping () -> Void) -> Bool {
return interceptHandle?(error, retry) ?? false
}
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ Once this is done you are good to go. For more information on what configuration

An intercept will _intercept_ the request when an error of type `ResponseError` occurs.

When this happens `intercept(_:retry:)` will be triggered and you should return `true` or `false` to indicate if you want the normal response flow to stop. When you stop the flow it will be possible to retry the request by calling the `retry()` block.
When this happens `intercept(_:data:retry:)` will be triggered and you should return `true` or `false` to indicate if you want the normal response flow to stop. When you stop the flow it will be possible to retry the request by calling the `retry()` block.

```swift
func intercept(_ error: ResponseError, retry: @escaping () -> Void) -> Bool {
func intercept(_ error: ResponseError, data: Data?, retry: @escaping () -> Void) -> Bool {
if error == .unauthorized {
// Execute the retry block after one second. The failed request will be retried.
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: retry)
Expand Down
3 changes: 2 additions & 1 deletion Sources/Configuration/Interceptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ public protocol Interceptor {
/// This function is triggered when a response error is triggered.
///
/// - parameter error: The error that triggers the interceptor.
/// - parameter data: The body's data object if available.
/// - parameter retry: The block you should trigger to retry the failed request.
///
/// - returns: Return if you want the response processing to be intercepted so that
/// the normal flow (with completion handler) is stopped.
func intercept(_ error: ResponseError, retry: @escaping () -> Void) -> Bool
func intercept(_ error: ResponseError, data: Data?, retry: @escaping () -> Void) -> Bool
}
2 changes: 1 addition & 1 deletion Sources/Service/NetworkService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class NetworkService: NSObject {
if
let responseError = urlResponse?.responseError,
let interceptor = self?.interceptor,
interceptor.intercept(responseError, retry: retry) {
interceptor.intercept(responseError, data: data, retry: retry) {
return
}

Expand Down

0 comments on commit fd08a12

Please sign in to comment.