Swiftpack.co -  Gengart/NetworkAgent as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
Gengart/NetworkAgent
This package is meant to make http request of an easy way inspiren in the architecture of Moya package. This package is 100% free of dependencies and works with Combine api + Codable
.package(url: "https://github.com/Gengart/NetworkAgent.git", from: "1.0.1")

NetworkAgent

Swift Package Manager compatible

This package is meant to make http request of an easy way inspiren in the architecture of Moya package. This package is 100% free of dependencies and works with Combine api + Codable

Example

Api.swift

import NetworkAgent

enum Api {
    case login(email: String, password: String)
    case books(query: [String: Any])
    case book(id: Int)
}

extension Api: NetworkAgentEndpoint {
    var baseURL: URL {
        return URL(string: "https://some_url.com/api")!
    }
    
    var path: String {
        switch self {
        case .login: return "/login"
        case .books: return "/books"
        case let .book(id): return "/books/\(id)"
        }
    }
    
    var method: HTTPMethod {
        return .get
    }
    
    var task: HTTPTask {
        switch self {
            case let .login(email, password): return .requestAttributes(attributes: ["email:" email, "password": password], encoding: .json)
            case let .books(query): return .requestAttributes(attributes: query, encoding: .url)
            case .book: return .requestPlain
        }
    }
}

Repository.swift

import NetworkAgent

class Repository {

    typealias Callback<T> = (T) -> ()
    static let shared: Repository = Repository()
    private let provider: NetworkAgentProvider<Api> = .init(plugins: [])
    
    func login(email: String, password: String) -> <Session, Error> {
        return provider.request(.login(email: email, password: password))
    }
    
    func books(query: [String: Any]) -> <[Book], Error> {
        return provider.request(.books(query: query))
    }
    
    func book(id: int) -> <Book, Error> {
        return provider.request(.book(id: id))
    }
}

LoginViewModel.swift

import Foundation
import Combine

class LoginViewModel: ObservableObject {
    
    @Published var email: String = ""
    @Published var password: String = ""
    private var cancellable: Set<AnyCancellable> = .init()
    private var repository = Repository.shared
    
    func login(completion: @escaping Callback<Session>) {
        self.isLoading = true
        repository.login(email: email, password: password)
            .sink(onSuccess: completion)
            .store(in: &cancellable)
    }
}

Plugins

To make a custom plugin is as easy to implement the protocol NetworkAgentPlugin every function of the protocol is optional.

public protocol NetworkAgentPlugin {
    func onRequest(_ request: URLRequest, with configuration: RequestConfiguration)
    func onResponse(_ response: HTTPURLResponse, with payload: Data)
    func onResponse(_ response: HTTPURLResponse?, with payload: Data?, receiving error: NetworkAgent.NetworkError, from endpoint: NetworkAgentEndpoint)
}

GitHub

link
Stars: 13
Last commit: 3 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API