Name: RxAlamofire
Owner: RxSwift Community
Description: RxSwift wrapper around the elegant HTTP networking in Swift Alamofire
Created: 2015-08-23 11:37:23.0
Updated: 2018-05-24 09:42:18.0
Pushed: 2018-04-20 11:29:10.0
Homepage: null
Size: 1349
Language: Swift
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
RxAlamofire is a RxSwift wrapper around the elegant HTTP networking in Swift Alamofire.
Wrapping RxSwift around Alamofire makes working with network requests a smoother and nicer task. Alamofire is a very powerful framework and RxSwift add the ability to compose responses in a simple and effective way.
A basic usage is (considering a simple currency converter):
formatter = NSNumberFormatter()
atter.numberStyle = .CurrencyStyle
atter.currencyCode = "USD"
et fromValue = NSNumberFormatter().numberFromString(self.fromTextField.text!) {
amofire.requestJSON(.get, sourceStringURL)
.debug()
.subscribe(onNext: { [weak self] (r, json) in
if let dict = json as? [String: AnyObject] {
let valDict = dict["rates"] as! Dictionary<String, AnyObject>
if let conversionRate = valDict["USD"] as? Float {
self?.toTextField.text = formatter
.string(from: NSNumber(value: conversionRate * fromValue))
}
}
}, onError: { [weak self] (error) in
self?.displayError(error as NSError)
})
.disposed(by: disposeBag)
se {
self.toTextField.text = "Invalid Input!"
Currently, the library features the following extensions:
stringURL = ""
ARK: URLSession simple and fast
session = URLSession.shared()
session.rx
.json(.get, stringURL)
.observeOn(MainScheduler.instance)
.subscribe { print($0) }
session.rx
.data(.get, stringURL)
.observeOn(MainScheduler.instance)
.subscribe { print($0) }
ARK: With Alamofire engine
json(.get, stringURL)
.observeOn(MainScheduler.instance)
.subscribe { print($0) }
alidation
request(.get, stringURL)
.validate(statusCode: 200..<300)
.validate(contentType: ["application/json"])
.responseJSON()
.observeOn(MainScheduler.instance)
.subscribe { print($0) }
rogress
request(.get, stringURL)
.progress()
.observeOn(MainScheduler.instance)
.subscribe { print($0) }
ust fire upload and display progress
upload(Data(), urlRequest: try! RxAlamofire.urlRequest(.get, stringURL))
.progress()
.observeOn(MainScheduler.instance)
.subscribe { print($0) }
rogress and final result
ploading files with progress showing is processing intensive operation anyway, so
his doesn't add much overhead
request(.get, stringURL)
.flatMap { request -> Observable<(Data?, RxProgress)> in
let dataPart = request.rx
.data()
.map { d -> Data? in d }
.startWith(nil as Data?)
let progressPart = request.rx.progress()
return Observable.combineLatest(dataPart, progressPart) { ($0, $1) }
}
.observeOn(MainScheduler.instance)
.subscribe { print($0) }
ARK: Alamofire manager
ame methods with any alamofire manager
manager = SessionManager.default
imple case
manager.rx.json(.get, stringURL)
.observeOn(MainScheduler.instance)
.subscribe { print($0) }
RLHTTPResponse + JSON
manager.rx.responseJSON(.get, stringURL)
.observeOn(MainScheduler.instance)
.subscribe { print($0) }
RLHTTPResponse + String
manager.rx.responseString(.get, stringURL)
.observeOn(MainScheduler.instance)
.subscribe { print($0) }
RLHTTPResponse + Validation + JSON
manager.rx.request(.get, stringURL)
.validate(statusCode: 200 ..< 300)
.validate(contentType: ["text/json"])
.json()
.observeOn(MainScheduler.instance)
.subscribe { print($0) }
RLHTTPResponse + Validation + URLHTTPResponse + JSON
manager.rx.request(.get, stringURL)
.validate(statusCode: 200 ..< 300)
.validate(contentType: ["text/json"])
.responseJSON()
.observeOn(MainScheduler.instance)
.subscribe { print($0) }
RLHTTPResponse + Validation + URLHTTPResponse + String + Progress
manager.rx.request(.get, stringURL)
.validate(statusCode: 200 ..< 300)
.validate(contentType: ["text/something"])
.flatMap { request -> Observable<(String?, RxProgress)> in
let stringPart = request.rx
.string()
.map { d -> String? in d }
.startWith(nil as String?)
let progressPart = request.rx.progress()
return Observable.combineLatest(stringPart, progressPart) { ($0, $1) }
}
.observeOn(MainScheduler.instance)
.subscribe { print($0) }
There are three ways to install RxAlamofire
Just add to your project's Podfile
:
'RxAlamofire'
Add following to Cartfile
:
ub "RxSwiftCommunity/RxAlamofire" "master"
Create a Package.swift
file
wift-tools-version:4.0
rt PackageDescription
package = Package(
name: "TestRxAlamofire",
dependencies: [
.package(url: "https://github.com/RxSwiftCommunity/RxAlamofire.git",
from: "4.0.0"),
],
targets: [
.target(
name: "TestRxAlamofire",
dependencies: ["RxAlamofire"])
]
To manual install this extension you should get the RxAlamofire/Source/RxAlamofire.swift
imported into your project, alongside RxSwift and Alamofire.
RxAlamofire requires Swift 4.0 and dedicated versions of Alamofire (4.5.1) and RxSwift (4.0.0-beta.0).