πŸ€΅πŸ½β€β™€οΈ Janet β€” A thin HTTP networking layer built on URLSession for simple, declarative endpoint specification leveraging the power of async/await.

Related tags

Networking Janet
Overview

πŸ€΅πŸ½β€β™€οΈ Janet

β€” Just another networking kit β€”

A thin HTTP networking layer built on URLSession for simple, declarative endpoint specification leveraging the power of async/await.

GetUsersResponse { // Create an instance of the request. let request = GetUsersRequest() // Create a network manager. You may use an shared instance. let networkManager = NetworkManager() // Send the request. return try await networkManager.send(request: request) }">
    // Defines what the service's endpoint looks like.
    struct GetUsersRequest: NetworkRequestWithResponse {
        // The decodable response type we expect.
        typealias ResponseType = GetUsersResponse

        // The HTTP method used.
        let method: HTTPMethod = .get
        
        // The URL.
        let endpoint: URL = .init(string: "https://reqres.in/api/users")!
    }
    
    func getUsers(page: Int) throws async -> GetUsersResponse {
        // Create an instance of the request.
        let request = GetUsersRequest()
        
        // Create a network manager. You may use an shared instance.
        let networkManager = NetworkManager()
        
        // Send the request.
        return try await networkManager.send(request: request)
    }

Features

The following features have been implemented or are planned for implementation.

If you would like to raise a feature request, please refer to the contribution section first.

Installation

Swift Package Manager

Please add the following line to the dependencies array in your Package.swift file:

    .package(url: "https://github.com/nholloh/Janet", from: "0.1.0")

Supported Platforms

  • iOS 13+
  • macOS 10.15+
  • watchOS 6+
  • tvOS 13+

Prior to Xcode 13.2, concurrency features will only work for iOS 15, macOS 11, watchOS 8 and tvOS 15. Linux and Windows are not supported. Please upvote the respective issue, if you'd like to see support.

Usage

First, find out what the traits of the endpoint you want to connect to are. Then, use any combination of the protocols below to define your service configuration struct:

Trait Description Protocol
Has query parameters Used when your request needs to append query parameters to the URL. NetworkRequestWithQuery
Has request body Used when your request has a body. NetworkRequestWithBody
Expects response body Used when you expect a decodable response. NetworkRequestWithResponse

If none of the above applies, you may use the default NetworkRequest protocol.

Then, create your request configuration. Here are some examples:

Service with response, which has no request body or request parameters:

    struct GetUsersRequest: NetworkRequestWithResponse {
        // The decodable response type we expect.
        typealias ResponseType = GetUsersResponse

        // The HTTP method used.
        let method: HTTPMethod = .get
        
        // The URL.
        let endpoint: URL = .init(string: "https://reqres.in/api/users")!
    }

Service with query parameters and response:

    struct GetUsersRequestPaged: NetworkRequestWithQuery, NetworkRequestWithResponse {
        // The query type to be encoded into the URL.
        struct Query: Encodable {
            let page: Int
        }

        struct ResponseType: Decodable {
            let page: Int
            let total: Int
        }

        let method: HTTPMethod = .get
        
        // An instance of above query type
        let query: Encodable
        let endpoint: URL = .init(string: "https://reqres.in/api/users")!
    }

Service with request body, without response:

    struct CreateUserRequest: NetworkRequestWithBody {
        
        // The body to be sent with the HTTP request.
        struct BodyType: Encodable {
            let name: String
            let job: String
        }

        let method: HTTPMethod = .post
        let body: BodyType
        let endpoint: URL = .init(string: "https://reqres.in/api/users")!
    }

Then, create an instance of your new request configuration and use it with an instance of NetworkManager to send your request:

    func getUsers(page: Int) throws async -> GetUsersResponse {
        // Create an instance of the request.
        let request = GetUsersRequest()
        
        // Create a network manager. You may use an shared instance.
        let networkManager = NetworkManager()
        
        // Send the request.
        return try await networkManager.send(request: request)
    }

For detailed documentation, please check the Features.

What does Janet stand for?

Its an acronym, which stands for:

J Just

A another

NET Network (library)

Also, its a reference to an incredibly helpful and empathetic AI from the series The Good Place. If you haven't seen it, you should definitely check it out!

Contributing

For contributions, please consider the following scenarios:

You find a bug

Please raise an issue and describe the bug you found. Feel free to fork and fix the bug, then raise a pull request with us. A collaborator of Janet will review it shortly and provide feedback.

You have a feature request

In case you have a feature request, please raise an issue with the feature request. We will assess importance of features based on the number of upvotes of said issue. If you already have a concrete idea how this feature may work, you may of course fork and make the necessary changes. Please remember to update the documentation!

IDE setup

There's no special setup required (as of now)! Just check out the code and start tinkering.

You might also like...
Declarative and Reactive Networking for Swift.

Squid Squid is a declarative and reactive networking library for Swift. Developed for Swift 5, it aims to make use of the latest language features. Th

Async network layer with Combine
Async network layer with Combine

Version 1.0.10 Currently Available Platform Version iOS 12.0 tvOS 10.0 macOS 10.15 watchOS 3.0 macCatalyst 13.0 Hover is a Network layer which uses Ap

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

Sherlock Holmes of the networking layer. :male_detective:
Sherlock Holmes of the networking layer. :male_detective:

ResponseDetective is a non-intrusive framework for intercepting any outgoing requests and incoming responses between your app and your server for debu

Advanced Networking Layer Using Alamofire with Unit Testing

Advanced Networking Layer Using Alamofire with Unit Testing

An elegant yet powerful iOS networking layer inspired by ActiveRecord.
An elegant yet powerful iOS networking layer inspired by ActiveRecord.

Written in Swift 5 AlamoRecord is a powerful yet simple framework that eliminates the often complex networking layer that exists between your networki

GXBaseAPI - GARPIX Networking Layer

GXBaseAPI GARPIX Networking Layer URLSession + Combine + Codable + Generics ВсС

Deal with query items, HTTP headers, request body and more in an easy, declarative way

Reusable system for complex URL requests with Swift. Deal with query items, HTTP headers, request body and more in an easy, declarative way. Check out our engineering blog to learn more!

Super lightweight async HTTP server library in pure Swift runs in iOS / MacOS / Linux

Embassy Super lightweight async HTTP server in pure Swift. Please read: Embedded web server for iOS UI testing. See also: Our lightweight web framewor

Comments
  • Unit & Integration Tests

    Unit & Integration Tests

    Motivation

    We'd like to have comprehensive unit and integration tests so we can rely on automated testing when making changes.

    Acceptance Criteria

    • Unit tests are written for units providing function and logic.
    • Integration tests are written for all scenarios/use cases that Janet covers.

    Out of scope

    • CI, as it does not exist (yet)
    enhancement 
    opened by nholloh 1
Owner
Niklas Holloh
Niklas Holloh
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
Thin wrapper around NSURLSession in swift. Simplifies HTTP requests.

SwiftHTTP SwiftHTTP is a thin wrapper around NSURLSession in Swift to simplify HTTP requests. Features Convenient Closure APIs Simple Queue Support Pa

Dalton 1.9k Dec 7, 2022
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

Conrado Mateu Gisbert 18 Dec 23, 2022
AsyncHTTP - Generic networking library written using Swift async/await

Generic networking library written using Swift async/await

Laszlo Teveli 7 Aug 3, 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
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
A web API client in Swift built using Async/Await

Get A modern web API client in Swift built using Async/Await and Actors. let cli

Alexander Grebenyuk 745 Jan 3, 2023
Swift async/await based socket

Socket Swift async/await based socket library Introduction This library exposes an idiomatic Swift API for interacting with POSIX sockets via an async

null 89 Dec 29, 2022
Simple asynchronous HTTP networking class for Swift

YYHRequest YYHRequest is a simple and lightweight class for loading asynchronous HTTP requests in Swift. Built on NSURLConnection and NSOperationQueue

yayuhh 77 May 18, 2022
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.

Logan Sease 2 Mar 20, 2022