This package is meant to make http request of an easy way inspiren in the architecture of Moya package

Overview

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 = .init(plugins: [])
    
    func login(email: String, password: String) -> 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) -> Error> {
        return provider.request(.book(id: id))
    }
}

LoginViewModel.swift

= .init() private var repository = Repository.shared func login(completion: @escaping Callback) { self.isLoading = true repository.login(email: email, password: password) .sink(onSuccess: completion) .store(in: &cancellable) } } ">
import Foundation
import Combine

class LoginViewModel: ObservableObject {
    
    @Published var email: String = ""
    @Published var password: String = ""
    private var cancellable: Set = .init()
    private var repository = Repository.shared
    
    func login(completion: @escaping Callback) {
        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)
}
You might also like...
Type-safe networking abstraction layer that associates request type with response type.

APIKit APIKit is a type-safe networking abstraction layer that associates request type with response type. // SearchRepositoriesRequest conforms to Re

Automatically sets the network activity indicator for any performed request.

BigBrother BIG BROTHER IS WATCHING YOU. BigBrother is a Swift library made for iOS that automatically watches for any performed request and sets the n

NSURLSession network abstraction layer, using Codable and Decodable for response and Encodable for request. ⚙ī¸đŸš€
NSURLSession network abstraction layer, using Codable and Decodable for response and Encodable for request. ⚙ī¸đŸš€

SONetworking NSURLSession network abstraction layer, using Codable and Decodable for response and Encodable for request. Project Folder and File Struc

YTKNetwork is a high level request util based on AFNetworking.

YTKNetwork What YTKNetwork is a high level request util based on AFNetworking. It's developed by the iOS Team of YuanTiKu. It provides a High Level AP

DBNetworkStack is a network abstraction for fetching request and mapping them to model objects

DBNetworkStack Main Features 🛡 Typed network resources 🏠 Value oriented architecture 🔀 Exchangeable implementations 🚄 Extendable API 🎹 Composable

An iOS library to route API paths to objects on client side with request, mapping, routing and auth layers

WANetworkRouting Developed and Maintained by ipodishima Founder & CTO at Wasappli Inc. Sponsored by Wisembly A routing library to fetch objects from a

Request adapter for URL requests from "MovieLister" demo app (Swift for Good book, a chapter by Ben Scheirman)

RequestAdapter Request adapter for URL requests from "MovieLister" demo app (Swift for Good book, a chapter by Ben Scheirman) The code is taken from:

Easy to use CFNetwork wrapper for HTTP requests, Objective-C, Mac OS X and iPhone

ASIHTTPRequest is an easy to use wrapper around the CFNetwork API that makes some of the more tedious aspects of communicating with web servers easier

Easy HTTP Networking in Swift a NSURLSession wrapper with image caching support
Easy HTTP Networking in Swift a NSURLSession wrapper with image caching support

Networking was born out of the necessity of having a simple networking library that doesn't have crazy programming abstractions or uses the latest rea

Owner
Angel Rada
Angel Rada
Minimalistic Swift HTTP request agent for iOS and OS X

Agent Table of Contents Introduction Usage HTTP Verbs Overloading Method Chaining Response Closure Verbs Methods NSMutableURLRequest Contributing Lice

null 589 Jun 29, 2022
Http Request wrapper written in Swift

Net Net is a HttpRequest wrapper written in Swift Features GET, POST, PUT, DELETE method Powerful request params: nested params, number, string, dic,

Le Van Nghia 304 Jun 29, 2022
An Generic HTTP Request Library For Swift

GRequest An HTTP request library written in Swift. ##Basic Usage Be simple, as it should be: Request("https://api.github.com/repos/lingoer/SwiftyJSON/

Ruoyu Fu 114 Oct 18, 2022
Minimalist HTTP request library via async / await

Making HTTP API requests in Swift using Foundation can be verbose and use a mix of types like URL, URLComponents and URLRequest to form a request and then handling all the encoding and decoding steps later

Nicholas Maccharoli 13 Nov 5, 2022
iONess is HTTP Request Helper for iOS platform used by HCI iOS App

iONess iONess (iOS Network Session) is HTTP Request Helper for the iOS platform used by Home Credit Indonesia iOS App. It uses Ergo as a concurrent he

OSFE Homecredit Indonesia 1 Mar 28, 2022
A network extension app to block a user input URI. Meant as a network extension filter proof of concept.

URIBlockNE A network extension app to block a user input URI. Meant as a network extension filter proof of concept. This is just a research effort to

Charles Edge 5 Oct 17, 2022
Swish is a networking library that is particularly meant for requesting and decoding JSON via Decodable

Swish Nothing but net(working). Swish is a networking library that is particularly meant for requesting and decoding JSON via Decodable. It is protoco

thoughtbot, inc. 369 Nov 14, 2022
🧚 RxSwift + Moya + HandyJSON + Plugins.

RxNetworks ?? . RxSwift + Moya + HandyJSON + Plugins. ?? ?? ?? English | įŽ€äŊ“中文 This is a set of infrastructure based on RxSwift + Moya MoyaNetwork This

77。 140 Jan 3, 2023
GitTime is GitHub Tracking App. Using ReactorKit, RxSwift, Moya.

GitTime Feature Activity: GitHub Contributions graph & Event lists Trending: Trending Repositories & Developers Buddys: Show your buddy's contribution

Kanz 58 Dec 7, 2022
Http - Demo for Http Layer

http Example To run the example project, clone the repo, and run pod install fro

null 0 Jan 24, 2022