Swiftpack.co - Package - RxSwiftCommunity/RxAlamofire

RxAlamofire

RxAlamofire is a RxSwift wrapper around the elegant HTTP networking in Swift Alamofire.

Create release Version License Platform Carthage compatible

Getting Started

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):

let formatter = NSNumberFormatter()
formatter.numberStyle = .currencyStyle
formatter.currencyCode = "USD"
if let fromValue = NSNumberFormatter().numberFromString(self.fromTextField.text!) {

RxAlamofire.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)

} else {
    self.toTextField.text = "Invalid Input!"
}

Example Usages

Currently, the library features the following extensions:

let stringURL = ""

// MARK: URLSession simple and fast
let session = URLSession.shared()

_ = session.rx
    .response(.get, stringURL)
    .observeOn(MainScheduler.instance)
    .subscribe { print($0) }

_ = session.rx
    .json(.get, stringURL)
    .observeOn(MainScheduler.instance)
    .subscribe { print($0) }

_ = session.rx
    .data(.get, stringURL)
    .observeOn(MainScheduler.instance)
    .subscribe { print($0) }

// MARK: With Alamofire engine

_ = json(.get, stringURL)
    .observeOn(MainScheduler.instance)
    .subscribe { print($0) }

// validation
_ = request(.get, stringURL)
    .validate(statusCode: 200..<300)
    .validate(contentType: ["application/json"])
    .responseJSON()
    .observeOn(MainScheduler.instance)
    .subscribe { print($0) }

// progress
_ = request(.get, stringURL)
    .progress()
    .observeOn(MainScheduler.instance)
    .subscribe { print($0) }

// just fire upload and display progress
_ = upload(Data(), urlRequest: try! RxAlamofire.urlRequest(.get, stringURL))
    .progress()
    .observeOn(MainScheduler.instance)
    .subscribe { print($0) }

// progress and final result
// uploading files with progress showing is processing intensive operation anyway, so
// this 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) }


// MARK: Alamofire Session
// same methods with any Alamofire Session

let session = Session.default

// simple case
_ = session.rx.json(.get, stringURL)
    .observeOn(MainScheduler.instance)
    .subscribe { print($0) }

// URLHTTPResponse + JSON
_ = session.rx.responseJSON(.get, stringURL)
    .observeOn(MainScheduler.instance)
    .subscribe { print($0) }

// URLHTTPResponse + String
_ = session.rx.responseString(.get, stringURL)
    .observeOn(MainScheduler.instance)
    .subscribe { print($0) }

// URLHTTPResponse + Validation + JSON
_ = session.rx.request(.get, stringURL)
    .validate(statusCode: 200 ..< 300)
    .validate(contentType: ["text/json"])
    .json()
    .observeOn(MainScheduler.instance)
    .subscribe { print($0) }

// URLHTTPResponse + Validation + URLHTTPResponse + JSON
_ = session.rx.request(.get, stringURL)
    .validate(statusCode: 200 ..< 300)
    .validate(contentType: ["text/json"])
    .responseJSON()
    .observeOn(MainScheduler.instance)
    .subscribe { print($0) }

// URLHTTPResponse + Validation + URLHTTPResponse + String + Progress
_ = session.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) }

// Interceptor + URLHTTPResponse + Validation + JSON
let adapter = // Some RequestAdapter
let retrier = // Some RequestRetrier
let interceptor = Interceptor(adapter: adapter, retrier: retrier)
_ = session.rx.request(.get, stringURL)
    .validate()
    .validate(contentType: ["text/json"])
    .responseJSON()
    .observeOn(MainScheduler.instance)
    .subscribe { print($0) }

Installation

There are three ways to install RxAlamofire

CocoaPods

Just add to your project's Podfile:

pod 'RxAlamofire'

Carthage

Add following to Cartfile:

github "RxSwiftCommunity/RxAlamofire" ~> 5.3

Swift Package manager

Create a Package.swift file

// swift-tools-version:4.0

import PackageDescription

let package = Package(
        name: "TestRxAlamofire",

        dependencies: [
            .package(url: "https://github.com/RxSwiftCommunity/RxAlamofire.git",
                     from: "5.3.1"),
        ],

        targets: [
            .target(
                    name: "TestRxAlamofire",
                    dependencies: ["RxAlamofire"])
        ]
)

Manually

To manual install this extension you should get the RxAlamofire/Source/RxAlamofire.swift imported into your project, alongside RxSwift and Alamofire.

Requirements

RxAlamofire requires Swift 5.1 and dedicated versions of Alamofire (5.1.0) and RxSwift (5.1.0).

For the last Swift 5.0 support, please use RxAlamofire 5.1.0.

For the last Swift 4.2 support, please use RxAlamofire 4.5.0.

Github

link
Stars: 1395

Used By

Total: 0

Releases

RxAlamofire v5.6.1 - 2020-09-03 03:21:04

1ea7f1c chore: Alamofire 5.2

Xcode 11.6

RxAlamofire v5.6.0 - 2020-07-07 12:57:37

c04ea5b feat(#186): Create Rx extensions mapping to Alamofire response method

Xcode 11.5

RxAlamofire v5.5.0 - 2020-06-22 13:57:37

4db992c feat(#183): Upload extensions

Xcode 11.5

RxAlamofire v5.4.0 - 2020-06-21 11:18:03

f059d88 feat(#185): Support Alamofire Interceptor

Xcode 11.5

RxAlamofire v5.3.2 - 2020-06-10 11:25:27

e475215 fix: indentation de1058d fix: To include tag version in buildflow 4e4f5c9 Adding unit tests for decodable request wrappers 0da090e chore(release): Cocoapods manual release [skip ci]

Xcode 11.5

RxAlamofire v5.3.1 - 2020-05-25 15:33:59

c63cf88 fix(#176) - Split release version splitting logic 4ad3a72 chore(release): 5.3.1 [skip ci] 3e22c2a fix(#176) - Fix cocoapods pipeline

Xcode 11.4.1

RxAlamofire v5.3.0 - 2020-05-25 14:42:12

e328582 feat(#176) - Release pipeline 35b2844 feat(#176) - Release pipes c75b0ef feat(#173): Implement danger and improve PR pipeline 78115aa feat(#174): Build pipeline to run on feature and PR ebbfdbe feat(#173): Regenerate project 87b9a7f fix(#172) - Update Cartfile.resolved eb7b483 Add extensions for Decodable (#170) 862093b Merge pull request #171 from hamedmohammadi/example-alamofire5.0-test 21cc24e test target added and OHHTTPStubs updated to 9.0.0 0151642 remoce test target from pod file it was error because still we dont have the according scheme in project d45e181 example schemes alamofier and minimum target updated

Xcode 11.4.1

RxAlamofire 5.2.0 - 2020-04-17 21:33:16

Support:

  • RxSwift 5.1
  • Alamofire 5.1

RxAlamofire 5.0.1 - 2019-09-13 09:37:12

Fixes Swift Package Manager support.

RxAlamofire 5.0.0 - 2019-05-03 10:13:28

Support RxSwift 5 and Xcode 10.2

RxAlamofire 4.5.0 - 2019-04-30 13:34:24

This would be the last version to support Swift 4.2

Fixed

  • Minimum Swift version is 4.2 (More details in https://github.com/RxSwiftCommunity/RxAlamofire/issues/144)

RxAlamofire 4.4.1 - 2019-04-03 13:14:14

Update RxSwift to 4.5

RxAlamofire 4.4.0 - 2019-03-28 09:45:28

Support Xcode 10.2. Thanks @rynecheow !

RxAlamofire 4.3.0 - 2018-09-24 21:23:20

Supports Xcode 10 and Swift 4.2. Thanks @rynecheow !

RxAlamofire 4.2.0 - 2018-04-03 13:14:44

See full release Change log at:

https://github.com/RxSwiftCommunity/RxAlamofire/blob/master/CHANGELOG.md#420

RxAlamofire 4.1.0 - 2018-02-26 22:16:19

See full release Change log at:

https://github.com/RxSwiftCommunity/RxAlamofire/blob/master/CHANGELOG.md#410

- 2017-10-18 11:00:16

  • Support for RxSwift 4.0.0

4.0.0-rc.0 - 2017-10-10 19:40:08

Added

  • Support for Xcode 9 and Swift 4.0 @gaplo917
  • Support for RxSwift 4.0.0-rc.0 @gaplo917

🍪 - 2017-06-20 19:38:45

Add

  • methods for URLRequestConvertible #69 by @5d
  • responseJSON to expose DataResponse method #79 by @freak4pc

Improve

  • API documentation improvements #70 #73 by @felipesabino and @gaplo917
  • Fix an issue when onCompleted could be called after 'onError' #80 by @vhbit

🍩 - 2016-11-24 17:05:10

Support for iOS 8 and macOS 10.10

🍫 - 2016-10-23 22:53:48

Adaptations for RxSwift 3.0.0

- 2016-10-17 18:33:24

  • Fixes Carthage integration

- 2016-10-17 01:14:31

  • Adaptations for RxSwift 3.0.0-rc.1

🔥🔥RxHotness🔥🔥 - 2016-10-17 01:14:58

Added

Changed

  • Replaces rx_* prefix with rx.* extensions. in order to be consistent with RxCocoa extensions
  • Renamed some methods and parameters names to be consistent with Alamofire 4.0 and Swift 3.0 e.g. :
//Before
    _ = manager.rx.JSON(.Get, stringURL)
        .observeOn(MainScheduler.instance)
        .subscribe { print($0) }

//After
_ = manager.rx.json(.get, stringURL)
        .observeOn(MainScheduler.instance)
        .subscribe { print($0) }