Skip to content

Commit

Permalink
Merge pull request #30 from icapps/feature/bypass-interception
Browse files Browse the repository at this point in the history
Bypass interception
  • Loading branch information
fousa authored Mar 12, 2019
2 parents e595108 + b0f292e commit dc6e33c
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 5 deletions.
48 changes: 44 additions & 4 deletions Example/Tests/Specs/Service/NetworkServiceSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ class NetworkServiceSpec: QuickSpec {
self.stub(http(.get, uri: "https://relative.com/request"), delay: 0.1, http(200))
let request = URLRequest(url: URL(string: "https://relative.com/request")!)

service.execute(request, with: MockedSerializer(), retryCount: 0, retry: {}, completion: { _ in })
service.execute(request,
with: MockedSerializer(),
isInterceptable: true,
retryCount: 0,
retry: {},
completion: { _ in })
expect(loggerOne.didTriggerStartRequest).toNot(beNil())
expect(loggerTwo.didTriggerStartRequest).toNot(beNil())
}
Expand All @@ -48,7 +53,12 @@ class NetworkServiceSpec: QuickSpec {
let request = URLRequest(url: URL(string: "https://relative.com/request")!)

waitUntil { done in
service.execute(request, with: MockedSerializer(), retryCount: 0, retry: {}, completion: { _ in
service.execute(request,
with: MockedSerializer(),
isInterceptable: true,
retryCount: 0,
retry: {},
completion: { _ in
done()
})
}
Expand All @@ -70,7 +80,12 @@ class NetworkServiceSpec: QuickSpec {
service.interceptor = interceptor

waitUntil { done in
service.execute(one, with: MockedSerializer(), retryCount: 0, retry: {}, completion: { _ in
service.execute(one,
with: MockedSerializer(),
isInterceptable: true,
retryCount: 0,
retry: {},
completion: { _ in
done()
})
}
Expand All @@ -88,11 +103,34 @@ class NetworkServiceSpec: QuickSpec {
service.interceptor = interceptor

waitUntil { done in
service.execute(one, with: MockedSerializer(), retryCount: 0, retry: {
service.execute(one, with: MockedSerializer(), isInterceptable: true, retryCount: 0, retry: {
done()
}, completion: { _ in })
}
}

it("should not retry a request when not interceptable") {
self.stub(http(.get, uri: "https://relative.com/one"), http(401))
let one = URLRequest(url: URL(string: "https://relative.com/one")!)

let interceptor = MockedInterceptor()
interceptor.interceptHandle = { error, retry in
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2, execute: retry)
return true
}
service.interceptor = interceptor

waitUntil { done in
service.execute(one,
with: MockedSerializer(),
isInterceptable: false,
retryCount: 0,
retry: {},
completion: { _ in
done()
})
}
}
}

context("threading") {
Expand All @@ -104,6 +142,7 @@ class NetworkServiceSpec: QuickSpec {
DispatchQueue.main.async {
service.execute(request,
with: MockedSerializer(),
isInterceptable: true,
retryCount: 0,
retry: {}, completion: { _ in
expect(Thread.isMainThread) == true
Expand All @@ -121,6 +160,7 @@ class NetworkServiceSpec: QuickSpec {
DispatchQueue.global(qos: .utility).async {
service.execute(request,
with: MockedSerializer(),
isInterceptable: true,
retryCount: 0,
retry: {},
completion: { _ in
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ func intercept(_ error: ResponseError, data: Data?, retry: @escaping () -> Void)
}
```

⚠️ The `Request` can define it it can be intercepted with the `isInterceptable` property. This should always be true in most cases. But, for example, the refresh tokens request should not be intercepted.

### Trigger a Request

In order to trigger a request you have to do 2 things:
Expand Down
4 changes: 4 additions & 0 deletions Sources/Request/Request+Defaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ extension Request {
public var networkServiceType: NSURLRequest.NetworkServiceType {
return .default
}

public var isInterceptable: Bool {
return true
}
}
3 changes: 3 additions & 0 deletions Sources/Request/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@ public protocol Request {
var cachePolicy: URLRequest.CachePolicy { get }
/// Set the network service type to prioritize the request.
var networkServiceType: NSURLRequest.NetworkServiceType { get }
/// Define if the request can be intercepted. Make sure this is set to false when you are for example
/// refreshing the tokens. The refresh request should not be interceptable.
var isInterceptable: Bool { get }
}
3 changes: 3 additions & 0 deletions Sources/Service/NetworkService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ class NetworkService: NSObject {
// MARK: - Execute

@discardableResult
// swiftlint:disable function_parameter_count
func execute<S: Serializer>(_ urlRequest: URLRequest,
with serializer: S,
isInterceptable: Bool,
retryCount: UInt,
retry: @escaping () -> Void,
completion: @escaping (_ response: S.Response) -> Void) -> URLSessionDataTask {
Expand All @@ -42,6 +44,7 @@ class NetworkService: NSObject {
if
let responseError = urlResponse?.responseError,
let interceptor = self?.interceptor,
isInterceptable,
interceptor.intercept(responseError, data: data, retryCount: retryCount, retry: retry) {
return
}
Expand Down
6 changes: 5 additions & 1 deletion Sources/Service/Service.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ open class Service {
do {
// Create the request.
let urlRequest = try request.makeURLRequest(with: configuration)
return networkService.execute(urlRequest, with: serializer, retryCount: retryCount, retry: { [weak self] in
return networkService.execute(urlRequest,
with: serializer,
isInterceptable: request.isInterceptable,
retryCount: retryCount,
retry: { [weak self] in
self?.execute(request, with: serializer, retryCount: retryCount + 1, completion: completion)
}, completion: completion)
} catch {
Expand Down

0 comments on commit dc6e33c

Please sign in to comment.