SwiftErickNetwork

1.0.6

A package repository for networking assistance in Swift.
h-suo/SwiftErickNetwork

What's New

SPM Release

2024-04-12T07:12:34Z

SPM 1.0.6 Release

  • Add async request function
  • Add async request test

SwiftErickNetwork

You can easily network using the SwiftErickNetwork package.

iOSVersion macOSVersion Platforms SwiftSPM


Overview

SwiftErickNetwork provides a blueprint called NetworkConfigurable to easily create EndPoints and offers a NetworkManager to facilitate networking and decoding tasks seamlessly.

NetworkConfigurable

NetworkConfigurable provides a blueprint for EndPoints, assisting in the creation and management of various API EndPoints used within the app.

Additionally, it offers support for url() and urlRequest() functions, making it convenient when simply working with URL or URLRequest instances.

public protocol NetworkConfigurable {
    
    associatedtype Response
    
    var baseURL: String { get }
    var path: String { get }
    var queryParameters: [URLQueryItem]? { get }
    var httpMethod: HttpMethod { get }
    var httpHeaderFields: [String: String]? { get }
    var httpBody: Encodable? { get }
}

NetworkManager

NetworkManager facilitates networking and decoding effortlessly using either a URL or an EndPoint. When performing networking with an EndPoint, it returns decoded data using the Response type specified by the EndPoint.

Moreover, it provides the requestPublisher(with:) function and request(with:) async function, empowering asynchronous processing with both Combine and concurrency.

public protocol NetworkManageable {
    
    var urlSession: URLSessionProtocol { get }
    
    func request<DTO: Decodable, EndPoint: NetworkConfigurable>(
        with endpoint: EndPoint,
        completion: @escaping (Result<DTO, NetworkError>) -> Void
    ) where EndPoint.Response == DTO
    
    func request(
        with url: URL,
        completion: @escaping (Result<Data, NetworkError>) -> Void
    )
    
    func requestPublisher<DTO: Decodable, EndPoint: NetworkConfigurable>(
        with endpoint: EndPoint
    ) -> AnyPublisher<DTO, NetworkError> where EndPoint.Response == DTO
    
    func requestPublisher(with url: URL) -> AnyPublisher<Data, NetworkError>

    func request<DTO: Decodable, EndPoint: NetworkConfigurable>(
        with endpoint: EndPoint
    ) async -> Result<DTO, NetworkError> where EndPoint.Response == DTO
    
    func request(with url: URL) async  -> Result<Data, NetworkError>
}

Description

  • Swift Tools 5.8.0
View More Packages from this Author

Dependencies

  • None
Last updated: Fri Oct 18 2024 16:49:04 GMT-0900 (Hawaii-Aleutian Daylight Time)