Netw is a lightweight iOS wrapper around URLSession that allows you to define clean HTTP requests
Netw provides a clean way to define and make your networking calls. It supports standard HTTP methods as well as multipart/formdata media upload.
- Define clean and isolated HTTP requests
- Support for multipart/formdata requests
- Built on top of URLSession
- No external libraries imported
- iOS 10.0+
You can use CocoaPods to install Netw
by adding it to your Podfile
:
pod 'Netw'
Create a Cartfile
that lists the framework and run carthage update
. Follow the instructions to add $(SRCROOT)/Carthage/Build/iOS/Netw.framework
to an iOS project.
github "franckclement/Netw"
import Netw
struct GithubRepositorySearchRequest: Request {
// Response type must conforms to Decodable
typealias Response = GithubSearchResults<GithubRepository>
// Setup your requests parameters as properties
let queryRequest: String
var data: DataType {
let data = RequestData(path: "https://api.github.com/search/repositories",
method: .get,
queryParams: ["q": queryRequest])
return DataType.standard(data: data)
}
// Initialize your custom request passing your custom parameters
init(queryRequest: String) {
self.queryRequest = queryRequest
}
}
GithubSearchResults<>
and GithubRepository
types can be seen here
import Netw
struct ImgurMediaPostRequest: Request {
typealias Response = ImgurImageUploadResponse
var data: DataType {
// We get an in-bundle image for test purpose
let bundle = Bundle.init(for: YouBundleClass.self)
let image = UIImage(named: "mockImage", in: bundle, compatibleWith: nil)!
// Use the built in Media type to create your medias to ulpload
let media = Media(key: "image",
fileName: "mockImage",
data: image.jpegData(compressionQuality: 0.5)!,
mimeType: "image/png")
let data = RequestMediaData(path: "https://api.imgur.com/3/upload",
medias: [media])
return DataType.media(data: data)
}
}
ImgurImageUploadResponse
type can be seen here
let repositoryRequest = GithubRepositorySearchRequest(queryRequest: "RxSwift")
_ = repositoryRequest.execute(completion: { result in
switch result {
case .success(let response):
print(response)
case .failure(let error):
print(error)
break
}
})
Submit a pull request 🚀
Franck CLEMENT – @Twitter
Distributed under the MIT license. See LICENSE
for more information.