bfernandesbfs/Interceptor
Request and Response Interceptor
How does this work?
At first, after setup was applied in your code, we need to create interceptor objects based on two protocol ResquestInterceptor and ResponseInterceptor
ResquestInterceptor
struct TokenInterceptor: RequestInterceptor {
func intercept(_ request: inout URLRequest) throws {
request.addValue("Bearer HASH-test-1234", forHTTPHeaderField: "Authorization")
}
}
ResponseInterceptor
class BlockInterceptorSpy: ResponseInterceptor {
func intercept(_ data: Data?, response: URLResponse?, error: Error?) {
if let value = response as? HTTPURLResponse, value.statusCode == 401 {
...
}
}
}
Adding on linked list
After this, we can add these objects to linked list (Look implementation here) based on Responder package, we are going to add each object through the function Interceptor.add(_ :)
var interceptor = Interceptor()
interceptor.add(TokenInterceptor()).add(BlockInterceptorSpy())
Applying in your code
It's very simple, look at the code bellow:
Here, we apply request interceptor after creating URLRequest, the function applyRequest will go through each object added and correlated with ResquestInterceptor and will change or apply some data to it.
let url = URL(string: "http://test.com")
var request = URLRequest(url: url)
try interceptor.applyRequest(&request)
next step, add the interceptor after URLSession's callback in your code. This point is really easy, just put function applyResponse on property completionhandler, its types are equals.
let task = session.dataTask(with: url, completionHandler: interceptor.applyResponse { data, response, error in
...
})
task.resume()
that's it!
How to install this?
Swift Package Manager
To use Interceptor as a Swift Package Manager package just add the following in your Package.swift file.
.package(url: "https://github.com/bfernandesbfs/Interceptor.git", .upToNextMajor(from: "0.0.1"))
Interceptor has dependency of Responder Package
License
Package Metadata
Repository: bfernandesbfs/Interceptor
Stars: 7
Forks: 0
Open issues: 0
Default branch: main
Primary language: swift
License: MIT
README: README.md