Easy and lightweight network layer for creating different set of network requests like GET, POST, PUT, DELETE customizable with coders conforming to TopLevelDecoder, TopLevelEncoder

Overview

Network service layer Combine REST API CRUD

Easy and lightweight network layer for creating different set of network requests like GET, POST, PUT, DELETE customizable with coders conforming to TopLevelDecoder, TopLevelEncoder

Features

  • Stand alone package without any dependencies using just Apple's Combine facilities
  • Error handling from forming URLRequest object to gettting data on a subscription
  • Customizable for different environments development, production...
  • Customizable for different requests schemes from classic CRUD Rest to what suits to your own custom routes
  • Based on interfaces not implementations
  • Ability to log and customize logers for different environment
  • Ability to chain requests layering with predicate logic analyzing previous result
  • Do in parallel infinite amount of requests collecting their results in Array and waiting until they are all done

1. Environment

Define enum with interface IEnvironment

enum Environment: IEnvironment {

    case development

    case production

    var baseURL: String {
        switch self {
            case .development: return "http://localhost:3000"
            case .production: return "https://apple.com"
        }
    }
    
    var headers: [IRequestHeader]? {
        switch self {
            case .development: return [ContentType.applicationJSON]
            case .production: return [ContentType.textJSON]
        }
    }
    
    var logger : ILogger? {
        switch self {
            case .development: return ServiceLogger()
            case .production: return nil
        }
    }  
}

Request headers

All headers for a request have to conform to the interface IRequestHeader

The example implemetation for content type headers is here ContentType.swift

Logger

You can use out of the box standard logger ServiceLogger if you don't need some specific data from URLRequest and URLResponse or define your own with interface ILogger

2. API for endpoint

Define endpoint API enum

enum UserRestAPI {

    case index
    case read(id: Int)
    case create
    case update
    case delete(id: Int)
}

Extend the enum with interface IRequest

field type
route String
method RequestMethod
extension UserRestAPI: IRequest {
    
    var route: String {
        switch self {
            case .index: return "/user"
            case .read(let id): return "/user/\(id)"
            case .create: return "/user"
            case .update: return "/user"
            case .delete(let id): return "/user/\(id)"
        }
    }
    
    var method: RequestMethod {
        switch self {
            case .index: return .get
            case .read(_): return .get
            case .create: return .post
            case .update: return .put
            case .delete(_): return .delete
        }
    }
}

The example implemetation is here UserRestAPI.swift UserRestAPI.swift

3. Create network sevice

    let network = NetworkService(environment: Environment.development)
  • execute - Do request from the endpont configuration GET, POST, PUT, DELETE

There are four methods are available currently GET, POST, PUT, DELETE

Parameters

Pass a [String: CustomStringConvertible] dictionary to the parameter that avalable for GET, POST, PUT requests. It's an optinal parameter.

Read

   let cfg = UserRestAPI.read(id: 1)
   
   let publisher: Output = network.execute(with: cfg, ["token" : 65678])

Create

    let cfg = UserRestAPI.create
    let user = Model(id: 11, name: "Igor")

    let publisher: Output = network.execute(body: user, with: cfg)

Update

    let cfg = UserRestAPI.update
    let user = Model(id: 11, name: "Igor")    

    let publisher: Output = network.execute(body: user, with: cfg)

Delete

    let cfg = UserRestAPI.delete(id: 11)
    
    let publisher: Output = network.execute(with: cfg)

4. Managing requests

Chaining requests

1}, create)">
        let read: Output = network.execute(with: UserRestAPI.index)

        let user = Model(id: 11, name: "Igor")
        let create: Output = network.execute(body: user, with: UserRestAPI.create)
        
        read.then(create)
            
        // or chain it using predicate to analyze previous result
        
        read.then(ifTrue : {$0.count > 1}, create)

Do in parallel infinite amount of requests

Collecting their results in Array and waiting until they are all done

All requests are expected the same output and failure

       [read, create, delete, update, read, read ].doTogether

Mixing

First create and update requests as a group and then read

        [create, update]
                       .waitEverybody
                       .then(read)
                      
        [create, delete]
                        .waitEverybody
                        .then([delete, read].doTogether)               

Package installation

In Xcode - Select Xcode>File> Swift Packages>Add Package Dependency...
and add https://github.com/The-Igor/d3-network-service

Try it in the real environment

Simple server instalation (mocking with NodeJS Express)

To try it in the real environment. I suggest installing the basic NodeJS Express boilerplate. Take a look on the video snippet how easy it is to get it through Webstorm that is accessible for free for a trial period.

Server instalation (NodeJS Express)

Real SwiftUI example

d3-rest-combine-swift-example

  • Run server NodeJS Express
  • Run SwiftUI project

Documentation(API)

  • You need to have Xcode 13 installed in order to have access to Documentation Compiler (DocC)
  • Go to Product > Build Documentation or ⌃⇧⌘ D
You might also like...
Blog post on medium about the creation of a marvel app from ground up
Blog post on medium about the creation of a marvel app from ground up

Marvel App Appearance • Motivation • Posts • Installation ------- Appearance Motivation This repository supports a series of posts that will show how

The Oakland Post iOS app, written in pure Swift
The Oakland Post iOS app, written in pure Swift

Now available on the App Store! Check it out! The Oakland Post App The mobile companion for The Oakland Post's website, written entirely in Swift! Scr

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

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

iOS 15, MVVM, Async Await, Core Data, Abstract Network Layer, Repository & DAO design patterns, SwiftUI and Combine
iOS 15, MVVM, Async Await, Core Data, Abstract Network Layer, Repository & DAO design patterns, SwiftUI and Combine

iOS 15, MVVM, Async Await, Core Data, Abstract Network Layer, Repository & DAO design patterns, SwiftUI and Combine

Dratini is a neat network abstraction layer.

Dratini Dratini is a neat network abstraction layer. If you are looking for a solution to make your network layer neat, Dratini is your choice. Dratin

Network abstraction layer written in Swift.
Network abstraction layer written in Swift.

Moya 14.0.0 A Chinese version of this document can be found here. You're a smart developer. You probably use Alamofire to abstract away access to URLS

Elegant network abstraction layer in Swift.
Elegant network abstraction layer in Swift.

Elegant network abstraction layer in Swift. 中文 Design Features Requirements Communication Installation Usage Base Usage - Target - Request - Download

A generic network layer written in swift

SwiftyNet 1.0.0 A generic network layer written in swift. you can use it as an abstraction layer above Alamofire with generic returned types. Installa

Releases(v1.0.3)
  • v1.0.3(Jun 6, 2022)

  • v1.0.2(Jun 5, 2022)

  • v1.0.1(May 28, 2022)

  • v1.0.0(May 27, 2022)

    Features

    • [x] Stand alone package without any dependencies using just Apple's Combine facilities
    • [x] Error handling from forming URLRequest object to gettting data on a subscription
    • [x] Customizable for different environments development, production...
    • [x] Customizable for different requests schemes from classic CRUD Rest to what suits to your own custom routes
    • [x] Based on interfaces not implementations
    • [x] Ability to log and customize logers for different environment
    Source code(tar.gz)
    Source code(zip)
Owner
Igor
software engineer
Igor
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
Access the native iOS / macOS reminders (get, update, delete) in TiDev / Titanium.

Titanium iOS Reminders API Access the native iOS reminders (get, update, delete) in TiDev / Titanium. Requirements The NSRemindersUsageDescription pri

Hans Knöchel 5 Nov 28, 2021
Another network wrapper for URLSession. Built to be simple, small and easy to create tests at the network layer of your application.

Another network wrapper for URLSession. Built to be simple, small and easy to create tests at the network layer of your application. Install Carthage

Ronan Rodrigo Nunes 89 Dec 26, 2022
Say goodbye to the Fat ugly singleton Network Manager with this Network Layer

MHNetwork Protocol Oriented Network Layer Aim to avoid having bloated singleton NetworkManager Philosophy the main philosophy behind MHNetwork is to h

Mohamed Emad Hegab 19 Nov 19, 2022
Lightweight network abstraction layer, written on top of Alamofire

TRON is a lightweight network abstraction layer, built on top of Alamofire. It can be used to dramatically simplify interacting with RESTful JSON web-

MLSDev 528 Dec 26, 2022
Conforming WebSocket (RFC 6455) client library for iOS and Mac OSX

SwiftWebSocket Conforming WebSocket (RFC 6455) client library for iOS and Mac OS

null 0 Dec 24, 2021
CoreNetwork module with the basic functionality of requests to the network

CoreNetwork module with the basic functionality of requests to the network

Moscow Metro 4 Apr 27, 2022
Menet is a TCP network middleware that can be dynamically modified through HTTP requests.

Menet Menet is a TCP network middleware that can be dynamically modified through HTTP requests. This is an experimental project, do NOT use it in prod

Nik 2 May 19, 2022
A conforming Objective-C WebSocket client library.

SocketRocket A conforming WebSocket (RFC 6455) client library for iOS, macOS and tvOS. Test results for SocketRocket here. You can compare to what mod

Facebook Incubator 9.4k Dec 27, 2022
MonkeyKing helps you to post messages to Chinese Social Networks.

MonkeyKing MonkeyKing helps you post SNS messages to Chinese Social Networks, without their buggy SDKs. MonkeyKing uses the same analysis process of o

null 2.7k Dec 29, 2022