GithubHelp home page GithubHelp logo

allaboutapps / fetch Goto Github PK

View Code? Open in Web Editor NEW
30.0 10.0 7.0 371 KB

A resource based network abstraction based on Alamofire.

License: MIT License

Swift 100.00%
ios swift networking alamofire caching id-allaboutapps-ios

fetch's Issues

Better Stubbing Support

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 Resouces, 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.

I didn't catch FetchError at all

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

Can i Enable Console Logging For All Requests?

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.