All Projects → objcio → Tiny Networking

objcio / Tiny Networking

Licence: mit
Tiny Networking Library

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Tiny Networking

Spitfire
An easy to use WebRTC Datachannels library for .NET applications.
Stars: ✭ 164 (-10.38%)
Mutual labels:  networking
Quic.net
A .NET C# Implementation of QUIC protocol - Google's experimental transport layer.
Stars: ✭ 173 (-5.46%)
Mutual labels:  networking
Inlets Pro
Secure TCP and HTTP tunnels that work anywhere
Stars: ✭ 179 (-2.19%)
Mutual labels:  networking
Egress Operator
A Kubernetes operator to produce egress gateway Envoy pods and control access to them with network policies
Stars: ✭ 164 (-10.38%)
Mutual labels:  networking
Smol
A small and fast async runtime for Rust
Stars: ✭ 2,206 (+1105.46%)
Mutual labels:  networking
Ansible Role Wireguard
Ansible role for installing WireGuard VPN. Supports Ubuntu, Debian, Archlinx, Fedora and CentOS.
Stars: ✭ 176 (-3.83%)
Mutual labels:  networking
Netctl
Profile based systemd network management
Stars: ✭ 163 (-10.93%)
Mutual labels:  networking
Dlib
Allocators, I/O streams, math, geometry, image and audio processing for D
Stars: ✭ 182 (-0.55%)
Mutual labels:  networking
Netjson
NetJSON is a data interchange format for encoding the basic building blocks of networks.
Stars: ✭ 171 (-6.56%)
Mutual labels:  networking
Dpitunnel
DPITunnel is an android app made for censorship bypass
Stars: ✭ 179 (-2.19%)
Mutual labels:  networking
Lightio
LightIO is a userland implemented green thread library for ruby
Stars: ✭ 165 (-9.84%)
Mutual labels:  networking
Medium
Independent telecommunication environment
Stars: ✭ 171 (-6.56%)
Mutual labels:  networking
Tracepkt
Trace a ping packet journey across network interfaces and namespace on recent Linux. Supports IPv4 and IPv6.
Stars: ✭ 176 (-3.83%)
Mutual labels:  networking
Unity Fastpacedmultiplayer
Features a Networking Framework to be used on top of Unity Networking, in order to implement an Authoritative Server with Lag Compensation, Client-Side Prediction/Server Reconciliation and Entity Interpolation
Stars: ✭ 162 (-11.48%)
Mutual labels:  networking
Cidlib
The CIDLib general purpose C++ development environment
Stars: ✭ 179 (-2.19%)
Mutual labels:  networking
Simplenet
An easy-to-use, event-driven, asynchronous network application framework compiled with Java 11.
Stars: ✭ 164 (-10.38%)
Mutual labels:  networking
Dnxfirewall
dnxfirewall (dad's next-gen firewall), a pure Python next generation firewall built on top of Linux kernel/netfilter.
Stars: ✭ 174 (-4.92%)
Mutual labels:  networking
Zenoh
zenoh unifies data in motion, data in-use, data at rest and computations. It carefully blends traditional pub/sub with geo-distributed storages, queries and computations, while retaining a level of time and space efficiency that is well beyond any of the mainstream stacks.
Stars: ✭ 182 (-0.55%)
Mutual labels:  networking
Txeh
Go library and CLI utilty for /etc/hosts management.
Stars: ✭ 181 (-1.09%)
Mutual labels:  networking
Sam
System Architecture Mapper
Stars: ✭ 176 (-3.83%)
Mutual labels:  networking

TinyNetworking

This package contains a tiny networking library. It provides a struct Endpoint, which combines a URL request and a way to parse responses for that request. Because Endpoint is generic over the parse result, it provides a type-safe way to use HTTP endpoints.

Here are some examples:

A Simple Endpoint

This is an endpoint that represents a user's data (note that there are more fields in the JSON, left out for brevity):

struct User: Codable {
    var name: String
    var location: String?
}

func userInfo(login: String) -> Endpoint<User> {
    return Endpoint(json: .get, url: URL(string: "https://api.github.com/users/\(login)")!)
}

let sample = userInfo(login: "objcio")

The code above is just a description of an endpoint, it does not load anything. sample is a simple struct, which you can inspect (for example, in a unit test).

Here's how you can load an endpoint. The result is of type Result<User, Error>.

URLSession.shared.load(endpoint) { result in
   print(result)
}

Authenticated Endpoints

Here's an example of how you can have authenticated endpoints. You initialize the Mailchimp struct with an API key, and use that to compute an authHeader. You can then use the authHeader when you create endpoints.

struct Mailchimp {
    let base = URL(string: "https://us7.api.mailchimp.com/3.0/")!
    var apiKey = env.mailchimpApiKey
    var authHeader: [String: String] { 
        ["Authorization": "Basic " + "anystring:\(apiKey)".base64Encoded] 
    }

    func addContent(for episode: Episode, toCampaign campaignId: String) -> Endpoint<()> {
        struct Edit: Codable {
            var plain_text: String
            var html: String
        }
        let body = Edit(plain_text: plainText(episode), html: html(episode))
        let url = base.appendingPathComponent("campaigns/\(campaignId)/content")
        return Endpoint<()>(json: .put, url: url, body: body, headers: authHeader)
    }
}

Custom Parsing

The JSON encoding and decoding are added as conditional extensions on top of the Codable infrastructure. However, Endpoint itself is not at all tied to that. Here's the type of the parsing function:

var parse: (Data?, URLResponse?) -> Result<A, Error>

Having Data as the input means that you can write our own functionality on top. For example, here's a resource that parses images:

struct ImageError: Error {}

extension Endpoint where A == UIImage {
    init(imageURL: URL) {
        self = Endpoint(.get, url: imageURL) { data in
            Result {
                guard let d = data, let i = UIImage(data: d) else { throw ImageError() }
                return i
            }
        }
    }
}

You can also write extensions that do custom JSON serialization, or parse XML, or another format.

Testing Endpoints

Because an Endpoint is a plain struct, it's easy to test synchronously without a network connection. For example, you can test the image endpoint like this:

XCTAssertThrows(try Endpoint(imageURL: someURL).parse(nil, nil).get())
XCTAssertThrows(try Endpoint(imageURL: someURL).parse(invalidData, nil).get())
XCTAssertNoThrow(try Endpoint(imageURL: someURL).parse(validData, nil).get())

More Examples

More Documentation

The design and implementation of this library is covered extensively on Swift Talk. There's a collection with all the relevant episodes:

Networking

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].