When working with HTTP Networking, the first library developers think about could be Alamofire. But Alamofire comes with a lot of files and some of its APIs aren’t so much easier to use. Swinet is actually inspired a lot by Alamofire but so much more lightweight, easier to use with more straightforward and friendly APIs. Moreover Swinet supports closures-based, swift concurrency (async/await) and Combine which will be compatible with both old or modern iOS projects.
Installation
CocoaPods
CocoaPods is a dependency manager for Cocoa projects. To integrate Swinet into your Xcode project using CocoaPods, specify it in your Podfile:
pod 'Swinet', '~> 1.0.0'
Carthage
Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks. To integrate Swinet into your Xcode project using Carthage, specify it in your Cartfile:
Swinet.request("https://domain.com/api").responseJSON { json in
print(json)
}
Error handling
struct User: Decodable {
let username: String
let email: String
}
Swinet.request("https://domain.com/user", parameters: ["id": "123"])
.responseDecodable(User.self, success: { user in
print(user)
}, failure: { error in
print(error.errorDescription)
print(error.statusCode as Any)
print(error.data as Any)
})
Swift Concurrency
@MainActor
func fetchUser() async {
do {
let user = try await Swinet.request("https://domain.com/user").responseDecodable(User.self)
print(user)
} catch {
print(error)
}
}
Combine
var bag = Set<AnyCancellable>()
Swinet.request("https://domain.com/user")
.publishDecodable(User.self)
.sink { error in
print(error)
} receiveValue: { model in
print(model)
}
.store(in: &bag)
Post with JSON Body
let headers = [
"Content-Type": "application/json",
"Authorization": "Bearer 398u99fsh9sdhf9shf9sdhf9shdf"
]
let body = [
"username": "quan",
"password": "quan123"
]
Swinet.request("https://domain.com/login",
method: .post,
body: body,
headers: headers)
.responseDecodable(User.self, success: { user in
print(user)
}, failure: { error in
print(error)
})
Swinet
Lightweight and Powerful HTTP Networking in Swift
Motivation
When working with HTTP Networking, the first library developers think about could be Alamofire. But
Alamofirecomes with a lot of files and some of its APIs aren’t so much easier to use.Swinetis actually inspired a lot by Alamofire but so much more lightweight, easier to use with more straightforward and friendly APIs. MoreoverSwinetsupports closures-based, swift concurrency (async/await) and Combine which will be compatible with both old or modern iOS projects.Installation
CocoaPods
CocoaPods is a dependency manager for Cocoa projects. To integrate Swinet into your Xcode project using CocoaPods, specify it in your
Podfile:Carthage
Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks. To integrate Swinet into your Xcode project using Carthage, specify it in your
Cartfile:Swift Package Manager
The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the
swiftcompiler.Once you have your Swift package set up, adding Swinet as a dependency is as easy as adding it to the
dependenciesvalue of yourPackage.swift.Usage
Simple Requests
Error handling
Swift Concurrency
Combine
Post with JSON Body
Upload File
Download File
GraphQL
Configuration