allaboutapps / fetch Goto Github PK
View Code? Open in Web Editor NEWA resource based network abstraction based on Alamofire.
License: MIT License
A resource based network abstraction based on Alamofire.
License: MIT License
As a user of Fetch I would like to register custom subclasses of URLProtocol to participate in the URL loading process.
It should be possible to register custom subclasses via URLProtocol.registerClass(_:)
For a project that I am working on, I am trying to test my token refresh integration with Fetch. However, I am running into some limitations with the stubbing support of Fetch.
The Stub
protocol is defined as follows:
/// A `Stub` represents a network response, which can be used to imitate an API endpoint
public protocol Stub {
/// HTTP status code
typealias StatusCode = Int
/// The result of the stubbed network call
/// It can be used to return a HTTP status code with a HTTP body and HTTP headers or an error
var result: Result<(StatusCode, Data, HTTPHeaders), Error> { get }
/// The id to identify a `Stub`. It is used to match the stub to the response
var id: UUID { get }
/// The `TimeInterval` after which the stub is returned to simulate a network delay
var delay: TimeInterval { get }
}
This is good, but does not support "dynamic" stubbing. To be more precise, I would like to adapt a stub, based on the request parameters. Here is what I would imagine a better (or more dynamic) stub to look like:
public struct StubResponse {
let statusCode: Int
let headers: HTTPHeaders
let body: Data
}
public protocol Stub {
var delay: TimeInterval { get }
func response(for request: URLRequest) -> StubResponse
}
Using this approach, more complex stubs could be constructed. For my personal use-case, I could define a stub that returns a 401 (triggering the token refresh logic) and for the second request a regular 200.
Another idea that I had when I played with the stubs is a better way to inject stubs. Right now, when defining stubs, I have to define them directly in the Resource
. While this makes it great to read, it limits me in defining different stubs per test. Let's say, for one test I want to return a list with 4 entries, while for another test I want to return a list with zero entries.
Right now I am solving this by writing my own StubbingController
where I register my stubs for routes. This, however, makes it annoying to define my Resouce
s, because I have to repeat myself a lot. A current Resource
in my project looks like this:
public struct API {
@Injected(\.apiClient) private static var apiClient: APIClient
@Injected(\.stubbingController) private static var stubbingController: StubbingController
public static func login(username: String, password: String) -> Resource<Credentials> {
return Resource(
apiClient: apiClient,
method: .post,
path: "/auth/login",
body: .encodable([
"username": username,
"password": password
]),
stub: stubbingController.resolveStub(
for: .post,
path: "/auth/login"
)
)
}
}
As you can see, I can now dynamically resolve my stubs, however, I have to repeat this code for all my endpoints. If a mechanism like this was pre-built into Fetch, I would save myself a lot of work and the project would gain more flexibility.
I would love more opinions on these two ideas, especially since one of them is potentially API-breaking.
please update pod file version 2.3.0. thinks
Hello i have url error with code 403 Forbidden but it is not received on .failure case for original fetch function and also on my customFetch function as below
extension Resource {
@discardableResult func customFetch(cachePolicy: CachePolicy? = nil, queue: DispatchQueue = .main, onResponse: @escaping (Swift.Result<FetchResponse<T>, FetchError>, Bool) -> Void) -> RequestToken where T: Equatable & Cacheable {
fetch { result, _ in
switch result {
case .success(let response):
onResponse(.success(response), true)
case .failure(let error):
onResponse(.failure(error), false)
}
}
}
}
so what i miss to make it working fine?
all i want is to handle not 200 response code, did i miss some config i must do to enable it?
please i want help on this
thanks
Hello,
After i upgrade Fetch for latest version i miss the logging, but on old version it is showing console logs.
So shall i do some configuration to enable it again, or it is disabled on new version?
Looking forward for your help.
Thanks
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.