All Projects → Gengart → NetworkAgent

Gengart / NetworkAgent

Licence: MIT License
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

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to NetworkAgent

SwiftUI-MVVM-C
An iOS template project using SwiftUI, Combine and MVVM-C software architecture
Stars: ✭ 85 (+431.25%)
Mutual labels:  combine, swiftui
Notflix
📱Netflix like application using SwiftUI and Combine
Stars: ✭ 76 (+375%)
Mutual labels:  combine, swiftui
Francis
Bonjour browser for macOS and iOS
Stars: ✭ 25 (+56.25%)
Mutual labels:  combine, swiftui
Project01-C-User-Event-Collector
💜🎷 네이버 VIBE 사용자 이벤트 수집기 🎷💜
Stars: ✭ 21 (+31.25%)
Mutual labels:  combine, swiftui
Luna
Tracking the moon phase using SwiftUI and Combine
Stars: ✭ 19 (+18.75%)
Mutual labels:  combine, swiftui
Chat
A basic SwiftUI chat app that leverages the new URLSessionWebSocketTask.
Stars: ✭ 22 (+37.5%)
Mutual labels:  combine, swiftui
SuperCodable
Codable, but with Super power made custom Codable behavior easy.
Stars: ✭ 23 (+43.75%)
Mutual labels:  easy-to-use, codable
NetworkImage
Asynchronous image loading in SwiftUI
Stars: ✭ 39 (+143.75%)
Mutual labels:  combine, swiftui
SwiftUIKit
A collection of missing SwiftUI components
Stars: ✭ 198 (+1137.5%)
Mutual labels:  spm, swiftui
TagField
🏷 Simple Tag Field for SwiftUI 🏷
Stars: ✭ 19 (+18.75%)
Mutual labels:  spm, swiftui
Columbus
A feature-rich country picker for iOS, tvOS and watchOS.
Stars: ✭ 23 (+43.75%)
Mutual labels:  spm, swiftui
GITGET
GitHub의 Contributions를 iOS의 Widget으로 보여주는 App
Stars: ✭ 101 (+531.25%)
Mutual labels:  combine, swiftui
SwiftUIKit
📱 UIKit code that is fun to write
Stars: ✭ 71 (+343.75%)
Mutual labels:  spm, swiftui
Shift
Light-weight EventKit wrapper.
Stars: ✭ 31 (+93.75%)
Mutual labels:  combine, swiftui
SwiftUI-App
This swiftUI Demo is very simple & easy to understand. This swiftUI demo includes On-boarding screens, login screen, forgot password screen, sign up screen, home & logout.
Stars: ✭ 175 (+993.75%)
Mutual labels:  easy-to-use, swiftui
NYTimes-iOS
🗽 NY Times is an Minimal News 🗞 iOS app 📱 built to describe the use of SwiftSoup and CoreData with SwiftUI🔥
Stars: ✭ 152 (+850%)
Mutual labels:  combine, swiftui
swiftui-mapkit
SwiftUI meets MapKit
Stars: ✭ 17 (+6.25%)
Mutual labels:  combine, swiftui
CoordinatorSwiftUI
A simple project to test the implementation of Coordinator Pattern using SwiftUI.
Stars: ✭ 28 (+75%)
Mutual labels:  combine, swiftui
column-text-view-ui
📄 Column Text View is an adaptive UI component that renders text in columns, horizontally [iOS 12, UIKit, TextKit, SwiftUI].
Stars: ✭ 11 (-31.25%)
Mutual labels:  spm, swiftui
mocka
Mocka — A Mock Server Made for Developers by Developers, made in Swift ❤️
Stars: ✭ 56 (+250%)
Mutual labels:  combine, swiftui

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)
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].