Skip to content

h-suo/SwiftErickNetwork

Repository files navigation

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>
}

About

A package repository for networking assistance in Swift.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages