Skip to content

Repository files navigation

CatalystNet

Universal AppleOS Apps Networking kit

badgeCocoaPods CompatibleiOSiOSmacOSmacOSMIT

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.1.0'

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.1.0"))]

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

Defining model:

structPhoto:Decodable{letalbumId:Intletid:Intlettitle:String?leturl:String?letthumbnailUrl:String?}structCustomError:Decodable{letmessage:String?letcode:Int?}

Defining base Api class:

classExampleApi:Api{privateletclient:HttpClient!init(baseUrl:String="https://jsonplaceholder.typicode.com"){self.client =HttpClient(baseUrl: baseUrl)}func load<T, E>(_ resource:Resource<T,E>,
multitasking:Bool=false,
completion:@escaping(Result<T,E>)->Void){
// Setup auth policy if needed
// Also you could extend "Resource" model with authentication method for simply calling "resource.authenticate()" when needed
super.load(resource,self.client, multitasking: multitasking, completion: completion)}}
Defining Asynchronous Functions (iOS >= 13.0.0)
@available(iOS 13.0.0,*)@available(macOS 10.15.0,*)extensionExampleApi{func load<T, E>(_ resource:Resource<T,E>)asyncthrows->T{
// Setup auth policy here if needed
returntryawait super.load(resource,self.client)}}

Defining Api class methods:

classPhotosApi:ExampleApi{privatestructEndpoints{staticletphotos:String="/photos"}func photo(with id:Int, completion:@escaping(Photo?,CatalystError<CustomError>?)->Void){varresource=Resource<Photo,CustomError>(path:Api.resource(Endpoints.photos, with: id))
resource.method =.get
// resource.authenticate() // if needed
self.load(resource){ response inswitch response {case.success(let photo):completion(photo,nil)case.failure(let error):completion(nil, error)}}}}
Defining Asynchronous Functions (iOS >= 13.0.0)
@available(iOS 13.0.0,*)@available(macOS 10.15.0,*)extensionPhotosApi{func photo(with id:Int)asyncthrows->Photo{varresource=Resource<Photo,CustomError>(path:Api.resource(Endpoints.photos, with: id))
resource.method =.get
// resource.authenticate() // if needed
returntryawaitself.load(resource)}}

Try it:

letphotosApi=PhotosApi()letphotoId:Int=42self.photosApi.photo(with: photoId){(photo, error)inprint(photo)}
Try it asynchronously:
letphotoId:Int=42do{letphoto=tryawaitself.photosApi.photo(with: photoId)print(photo)}catch{print(error)
// TODO: Proceed "error" object
}

Output:

Photo(
albumId: 1,
id: 42,
title: Optional("voluptatibus a autem molestias voluptas architecto culpa"),
url: Optional("https://via.placeholder.com/600/ca50ac"),
thumbnailUrl: Optional("https://via.placeholder.com/150/ca50ac")
)

Please follow Tests/ExampleTests.swift file for more usage examples <3

About

Universal Networking kit

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages