Universal AppleOS Apps Networking kit
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'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"))]If you prefer not to use any of the aforementioned dependency managers, you can integrate CatalystNet into your project manually.
import CatalystNetstructPhoto:Decodable{letalbumId:Intletid:Intlettitle:String?leturl:String?letthumbnailUrl:String?}structCustomError:Decodable{letmessage:String?letcode:Int?}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)}}@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)}}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)}}}}@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)}}letphotosApi=PhotosApi()letphotoId:Int=42self.photosApi.photo(with: photoId){(photo, error)inprint(photo)}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
