Swiftpack.co - hellc/CatalystNet as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
hellc/CatalystNet
Universal AppleOS Apps Networking kit
.package(url: "https://github.com/hellc/CatalystNet.git", from: "1.0.2")

CatalystNet

Universal AppleOS Apps Networking kit

badge CocoaPods Compatible iOS iOS macOS macOS MIT

Installation:

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate CatalystNet into your Xcode project using CocoaPods, specify it in your Podfile:

pod 'CatalystNet', '~> 1.0.1'

Swift Package Manager

The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler.

Once you have your Swift package set up, adding CatalystNet as a dependency is as easy as adding it to the dependencies value of your Package.swift.

dependencies: [
    .package(url: "https://github.com/hellc/CatalystNet.git", .upToNextMajor(from: "1.0.1"))
]

Manually

If you prefer not to use any of the aforementioned dependency managers, you can integrate CatalystNet into your project manually.

Usage:

Http client:

Import package

import CatalystNet

Define a requested resource:

struct Post: Decodable {
    let id: UInt
    let userId: UInt
    let title: String?
    let body: String?
}

Define Api class base:

class TestApi: Api {
    private let client: HttpClient!
    
    init(baseUrl: String) {
        self.client = HttpClient(baseUrl: baseUrl)
    }
    
    func load<T, E>(_ resource: Resource<T, E>,
                    multitasking: Bool = false,
                    completion: @escaping (Result<Any, E>) -> Void) {
        super.load(resource, self.client, multitasking: multitasking, completion: completion)
    }
}

Define Api class methods:

extension TestApi {
    private struct Endpoints {
        static let posts = "/posts"
    }
    
    func post(with id: String, completion: @escaping (Post?, HttpError<String>?) -> Void) {
        var resource = Resource<Post, String>(path: Api.resource(Endpoints.posts, with: id))
        
        resource.method = .get
        
        self.load(resource) { response in
            if let post = response.value as? Post {
                completion(post, nil)
            } else if let error = response.error {
                completion(nil, error)
            }
        }
    }
}

Try it:

let testApi = TestApi(baseUrl: "https://jsonplaceholder.typicode.com")
let id = "42"

self.testApi.post(with: id) { (post, error) in
    if let post = post, error == nil {
        print(post)
    }
}

Output:

Post(id: 42, userId: 5, title: "commodi ullam sint et excepturi error explicabo praesentium voluptas", body: "odio fugit voluptatum ducimus earum autem est incidunt voluptatem\nodit reiciendis aliquam sunt sequi nulla dolorem\nnon facere repellendus voluptates quia\nratione harum vitae ut")

GitHub

link
Stars: 1
Last commit: 1 week ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.

Release Notes

Release v1.0.2
1 week ago

Files features

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics