A resource based, protocol oriented networking library designed for pure-SwiftUI applications.

Overview

Monarch 👑 - WIP

A resource based, protocol oriented networking library designed for pure-SwiftUI applications.

Features:

  • Async/Await
  • Resource Based
  • Protocol Oriented
  • Caching Support
  • Preview Support
  • Extensible
  • All the buzzwords!

Description

Monarch is a network library designed to harness the SwiftUI View Hierarchy to simplify dependency injection. It's called Monarch because it sits at the top of your hierarchy.

To use Monarch, you register one or multiple request providers at a very high level of your view hierarchy.

struct MonarchApp: App {
  var body: some Scene {
    WindowGroup {
      ContentView()
        .registerProvider(NetworkClient())
    }
  }
}

Any views lower in the view hierarchy can read the requestProvider environment value and use it to execute a request. Because requests contain a previewData value, views can be easily previewed in the Preview Canvas.

struct ContentView: View {
  @Environment(\.requestProvider) var provider
  @State var user: User?
  
  var body: some View {
    Text(user?.name ?? "Loading...")
      .task {
        do {
          user = try await provider.perform(CurrentUserRequest())
        } catch {
          print("Whoops")
        }
      }
  }
}

Creating a Request

Requests are one of the basic building blocks of Monarch. They describe a resource, and can be defined with as few as two values.

struct CurrentUserRequest: Request {
  var path: String { "users/me" }
  var previewData: User { .example }
}

The RequestProvider Protocol

RequestProvider is the protocol that powers most of the other protocols and classes in the library; it defines a single function:

func perform<R: Request>(_ request: R) async throws -> R.ResponseType

Network providers use this function to fetch data from the network, while cache providers use it to read values from memory or the disk.

Chaining Providers

By specifying the domain property of a Request you can determine which provider should receive that request.

When multiple providers are registered, Monarch will attempt to call them in the order they were registered.

extension RequestDomain {
  static let images = RequestDomain(rawValue: 1 << 0)
}

struct MonarchApp: App {
  var body: some Scene {
    WindowGroup {
      ContentView()
        .registerProvider(ImageCache(), domain: .images)
        .registerProvider(ImageClient(), domain: .images)
        .registerProvider(NetworkClient(), domain: .any)
    }
  }
}

In this example, any requests in the images domain will be received by the ImageCache, if no cache value is found, ImageClient will fetch the image. Any other requests will be sent directly to NetworkClient.

You might also like...
RSNetworking is a networking library written entirly for the Swift programming language.

RSNetworking is a networking library written entirly for the Swift programming language.

Asynchronous socket networking library for Mac and iOS

CocoaAsyncSocket CocoaAsyncSocket provides easy-to-use and powerful asynchronous socket libraries for macOS, iOS, and tvOS. The classes are described

ServiceData is an HTTP networking library written in Swift which can download different types of data.

ServiceData Package Description : ServiceData is an HTTP networking library written in Swift which can download different types of data. Features List

foursquare iOS networking library

FSNetworking foursquare's iOS networking library FSN is a small library for HTTP networking on iOS. It comprises a single class, FSNConnection, and se

Swish is a networking library that is particularly meant for requesting and decoding JSON via Decodable
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

Malibu is a networking library built on promises
Malibu is a networking library built on promises

Description Palm trees, coral reefs and breaking waves. Welcome to the surf club Malibu, a networking library built on promises. It's more than just a

A networking library for Swift
A networking library for Swift

Nikka Nikka is a super simple Swift HTTP networking library that comes with many extensions to make it modular and really powerful. Installation Usage

QwikHttp is a robust, yet lightweight and simple to use HTTP networking library for iOS, tvOS and watchOS

QwikHttp is a robust, yet lightweight and simple to use HTTP networking library. It allows you to customize every aspect of your http requests within a single line of code, using a Builder style syntax to keep your code super clean.

A networking library for iOS, macOS, watchOS and tvOS

Thunder Request Thunder Request is a Framework used to simplify making http and https web requests. Installation Setting up your app to use ThunderBas

Owner
Emilio Pelaez Romero
iOS Developer since 2009, sherlocked in 2011
Emilio Pelaez Romero
Alamofire Network Layer written in swift 5 using the protocol oriented, combine, UIKit, MVVM.

CoreAPI-iOS This project Contains Alamofire Network layer Based on Protocol Oriented Concept and Combine Framework. It is created with UIKit, Alamofir

Mehran Kamalifard 27 Nov 11, 2022
VFNetwork is a protocol-oriented network layer that will help you assemble your requests in just a few steps.

Simple, Fast and Easy. Introduction VFNetwork is a protocol-oriented network layer that will help you assemble your requests in just a few steps. How

Victor Freitas 4 Aug 22, 2022
Swift implementation of WalletConnect v.2 protocol for native iOS applications

Wallet Connect v.2 - Swift Swift implementation of WalletConnect v.2 protocol for native iOS applications. Requirements iOS 13 XCode 13 Swift 5 Usage

WalletConnect 142 Jan 4, 2023
🌏 A zero-dependency networking solution for building modern and secure iOS, watchOS, macOS and tvOS applications.

A zero-dependency networking solution for building modern and secure iOS, watchOS, macOS and tvOS applications. ?? TermiNetwork was tested in a produc

Bill Panagiotopoulos 90 Dec 17, 2022
C-Xpress is a modern c-like object oriented programming language running on top of the .NET framework

C-Xpress Programming Language The cxpress programming language is a High Level object oriented programming language running on the .NET Framework C-Xp

null 2 Jan 12, 2022
FreeRDP is a free remote desktop protocol library and clients

FreeRDP: A Remote Desktop Protocol Implementation FreeRDP is a free implementation of the Remote Desktop Protocol (RDP), released under the Apache lic

null 7.8k Jan 8, 2023
A simple GCD based HTTP client and server, written in 'pure' Swift

SwiftyHTTP Note: I'm probably not going to update this any further - If you need a Swift networking toolset for the server side, consider: Macro.swift

Always Right Institute 116 Aug 6, 2022
DispatchSource based socket framework written in pure Swift

SwiftDSSocket Overview SwiftDSSocket is a purely swift based asynchronous socket framework built atop DispatchSource. Function signatures are pretty m

Yi Huang 65 Nov 15, 2022
🏇 A Swift HTTP / HTTPS networking library just incidentally execute on machines

Thus, programs must be written for people to read, and only incidentally for machines to execute. Harold Abelson, "Structure and Interpretation of Com

John Lui 845 Oct 30, 2022
Malibu is a networking library built on promises

Description Palm trees, coral reefs and breaking waves. Welcome to the surf club Malibu, a networking library built on promises. It's more than just a

Vadym Markov 410 Dec 30, 2022