VFNetwork

website·Indexed 29 days ago

https://vfnetwork.vercel.app/

A networking library designed for fast loading with a Cacheable Protocol and custom configurable headers for authentication. It provides tools for configuring HTTP methods (GET, POST, PUT, PATCH, DELETE) using APIBuilder, integrating DispatchGroup and DispatchSemaphore in execute methods, and performing unit tests with JSON files. Available versions include 1.4.1, 1.4.2, and 1.4.4.

Tokens
10.7K
Snippets
60
Records
73
Agent score
72%

What's inside VFNetwork

  1. Overview of VFNetwork

    VFNetwork is a networking library designed for ease of implementation across various software architectures, including MVVM-C, ViPER, and MVC. It allows developers to handle network requests by importing and configuring them directly without requiring additional abstraction layers from the network layer.
  2. Overview of VFNetwork networking library

    VFNetwork is a networking library designed for ease of implementation and compatibility with multiple architectural patterns, including MVVM-C, ViPER, and MVC. It allows developers to handle network requests by importing and configuring them directly without requiring additional abstraction layers from the network layer.
  3. Overview of VFNetwork networking library

    VFNetwork (v1.4.4) is a networking library designed for rapid implementation and compatibility with multiple architectural patterns, including MVVM-C, ViPER, and MVC. It provides a ready-to-use network layer that allows developers to perform requests by importing and configuring them without needing to build additional abstractions.
  4. Optimize request speed using VFNetwork caching

    VFNetwork includes a built-in caching mechanism designed to execute requests faster by reducing the need for repeated network calls for the same data.
  5. Configure a GET request using APIBuilder

    To implement a GET request, set the httpMethod to .get and the task to .request. The .request task performs a network call without a request body.
    enum HomeAPI {
        case home
    }
    
    extension HomeAPI: APIBuilder {
        var path: URLPath {
            switch self {
            case .home: return .plain("jokes/random")
            }
        }
        
        var httpMethod: HTTPMethods {
            switch self {
            case .home: return .get
            }
        }
        
        var headers: HTTPHeader {
            .empty
        }
        
        var task: HTTPTask {
            switch self {
            case .home: return .request
            }
        }
    }
  6. Define API endpoints using APIBuilder

    To organize network requests, create an enum that conforms to APIBuilder. This allows you to centrally define the path, HTTP method, headers, and task type for each endpoint.
    import VFNetwork
    
    enum HomeAPI {
        case joke
        case categories
    }
    
    extension HomeAPI: APIBuilder {
        
        var path: URLPath {
            switch self {
            case .joke: return .plain("jokes/random")
            case .categories: return .plain("jokes/categories")
            }
        }
        
        var httpMethod: HTTPMethods {
            switch self {
            case .joke, .categories: return .get
            }
        }
        
        var headers: HTTPHeader {
            .custom([
                .bearer("yourToken"),
                .basic("yourBase64"),
                .header("custom", "header")
            ])
        }
        
        var task: HTTPTask {
            switch self {
            case .joke, .categories: return .request
            }
        }
    }
  7. Configure a GET request using APIBuilder

    To implement a GET request in a route, set the httpMethod property to .get and the task property to .request. The .request task indicates that the call is performed without a request body, which is standard for GET operations.
    enum HomeAPI {
        case home
    }
    
    extension HomeAPI: APIBuilder {
        
        var path: URLPath {
            switch self {
            case .home:
                return .plain("jokes/random")
            }
        }
        
        var httpMethod: HTTPMethods {
            switch self {
            case .home:
                return .get // Define method GET
            }
        }
        
        var headers: HTTPHeader {
            .empty
        }
        
        var task: HTTPTask {
            switch self {
            case .home:
                return .request // This performs a request without a body
            }
        }
    }
  8. Execute network requests using RequestService

    Create a service class that inherits from RequestService<T> (where T is your APIBuilder enum). Use the execute method to trigger the request and handle the response with a specific model type.
    import VFNetwork
    
    class HomeService: RequestService<HomeAPI> {
        func getJoke(completion: @escaping (Result<JokeModel, Error>) -> Void) {
            execute(.joke, responseType: JokeModel.self, completion: completion)
        }
        
        func getCategories(completion: @escaping (Result<CategoryModel, Error>) -> Void) {
            execute(.categories, responseType: CategoryModel.self, completion: completion)
        }
    }
  9. Create and organize blog posts in Docusaurus

    Docusaurus blogging features are powered by the @docusaurus/plugin-content-blog plugin. To create blog posts, add Markdown files or folders to the blog directory. For posts with associated images, use a blog post folder to co-locate the images with the content. Blog post dates are automatically extracted from filenames using the format YYYY-MM-DD-title.md (e.g., 2019-05-30-welcome.md) or from an index.md file within a dated folder (e.g., 2019-05-30-welcome/index.md).
  10. Execute API requests using RequestService

    The RequestService class is used to execute requests defined by an APIBuilder implementation. Inherit from RequestService and call the execute method, specifying the endpoint and the expected response type.
    import VFNetwork
    
    class HomeService: RequestService<HomeAPI> {
        func getJoke(completion: @escaping (Result<JokeModel, Error>) -> Void) {
            execute(.home, responseType: JokeModel.self, completion: completion)
        }
        
        func getCategories(completion: @escaping (Result<CategoryModel, Error>) -> Void) {
            execute(.categories(someModel), responseType: CategoryModel.self, completion: completion)
        }
    }
  11. Configure POST, PUT, PATCH, and DELETE requests

    For methods that require a payload (POST, PUT, PATCH, DELETE), set the httpMethod to the corresponding value (.post, .put, .patch, or .delete) and use a task that supports a body, such as .requestEncoder with a VCodable model or .requestBody with a dictionary.
    struct ExampleModel: VCodable {
        var name: String
        var lastname: String
    }
    
    enum HomeAPI {
        case home(ExampleModel)
    }
    
    extension HomeAPI: APIBuilder {
        var path: URLPath {
            switch self {
            case .home: return .plain("jokes/random")
            }
        }
        
        var httpMethod: HTTPMethods {
            switch self {
            case .home: return .post
            }
        }
        
        var headers: HTTPHeader {
            .bearer("yourToken")
        }
        
        var task: HTTPTask {
            switch self {
            case let .home(model): return .requestEncoder(model)
            }
        }
    }
  12. Define API endpoints using APIBuilder

    Use an enum that conforms to APIBuilder to configure the details of your network requests, including the URL path, HTTP method, headers, and the task type.
    import VFNetwork
    
    enum HomeAPI {
        case joke
        case categories
    }
    
    extension HomeAPI: APIBuilder {
        
        var path: URLPath {
            switch self {
            case .joke:
                return .plain("jokes/random")
            case .categories:
                return .plain("jokes/categories")
            }
        }
        
        var httpMethod: HTTPMethods {
            switch self {
            case .joke, .categories:
                return .get
            }
        }
        
        var headers: HTTPHeader {
            .custom([
                .bearer("yourToken"),
                .basic("yourBase64"),
                .header("custom", "header")
            ])
        }
        
        var task: HTTPTask {
            switch self {
            case .joke, .categories:
                return .request
            }
        }
    }