Latest 0.0.2
Homepage https://github.com/SwiftReactive/HTTPCommander
License MIT
Platforms tvos 9.0, ios 8.0, osx 10.10, watchos 2.1, requires ARC
Dependencies RACCommander, Alamofire
Authors

image

Carthage compatible
Build Status
[CocoaPods]()
GitHub release

Requirements

Installation

CocoaPods

You can install ReactiveCommander using CocoaPods adding the following lines to your Podfile:

use_frameworks!
pod "RXHTTPCommander" // For RxSwift
pod "RACHTTPCommander" // For ReactiveCocoa

Wherever you want to use the framework import it with import RACHTTPCommander or import RXHTTPCommander.

Carthage

It’s also available to install using Carthage

github "SwiftReactive/HTTPCommander"

Depending on the framework you want to use ReactiveCocoa or RxSwift you’ll have to add to your project, RACHTTPCommander or RACHTTPCommander respectively. Then import them with these names.

Minimum Version supported

  • tvOS – 9.0
  • iOS – 8.0
  • OSX – 10.10
  • watchOS – 2.1

Contribute

  • Git clone the repository: git clone https://github.com/swiftreactive/HTTPCommander
  • Install Carthage: brew install carthage
  • Update Carthage dependencies: carthage update
  • Open the project HTTPCommander.xcodeproj

How to use HTTPCommander

HTTPCommander is built on top of ReactiveCommander whose core idea is:

  1. Operations are encapsulated in Commands
  2. Commands are enqueued in NSOperationQueues where they get executed.
  3. Execution response is returned as a Next event through Reactive Streams using RXSwift or ReactiveCocoa.

HTTPCommander offers a set of utils for creating commands that perform HTTP requests.

It uses Alamofire under the hood for Request and Response serializing.

1. Creating a requests repository
Responsible of creating requests. Define your requests repository that models your HTTP Server API. Thanks to protocol extensions, conform HTTPRequestFactory that includes pre-implemented functions for REST methods:

struct GithubAPI: HTTPRequestFactory {
  let baseURL: NSURL
  let headers: [String: String]
  init(baseURL: NSURL, headers: [String: String] = [:]) {
    self.baseURL = baseURL
    self.headers = headers
  }

  // Methods
  func getAccount() -> HTTPRequestable {
    return self.get("/user")
  }

  func deleteAccount() -> HTTPRequestable {
    return self.delete("/user")
  }
}

2. Creating the commands
Once you have your factory, creating Commands is pretty straightforward thanks to HTTPCommandFactory. Initialize the factory with your request and get the command for your given request and the type of response the API will return:

let githubAPI = GithubAPI()
let getAccountCommand = HTTPCommandFactory(githubAPI.getAccount()).jsonCommand()

3. Executing commands
Finally using NSOperationQueue we enqueue the command to be executed and subscribe to the response:

CommandQueue.backgroundQueue.addCommand(getAccountCommand).start { event in
  switch event {
    case .Next(let account):
      print(account)
    case .Failed(let error):
      print(error)
    default:
      break
  }
}

Typically in our apps we’re used to define what we call APIManager or APIClient. These big controllers are responsible of almost everything: building requests, executing them, parsing the response… With HTTPCommander the responsibilities are clearly defined and spread across multiple components:

  • HTTPRequestFactory ~> Initializing Requests
  • HTTPCommand ~> Executing the request serializing the response data.
  • CommandQueue ~> Enqueuing the commands to be executed.

License

HTTPCommander is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "RACHTTPCommander",
    "version": "0.0.2",
    "summary": "Reactive Commands for interacting with HTTP servers",
    "homepage": "https://github.com/SwiftReactive/HTTPCommander",
    "social_media_url": "https://twitter.com/pepibumur",
    "license": "MIT",
    "authors": {
        "@pepibumur": "[email protected]"
    },
    "source": {
        "git": "https://github.com/SwiftReactive/HTTPCommander.git",
        "tag": "0.0.2"
    },
    "platforms": {
        "tvos": "9.0",
        "ios": "8.0",
        "osx": "10.10",
        "watchos": "2.1"
    },
    "requires_arc": true,
    "source_files": [
        "HTTPCommander/Source/Core/**/*.{swift}",
        "HTTPCommander/Source/RAC/**/*.{swift}"
    ],
    "dependencies": {
        "RACCommander": [
            "~> 0.0"
        ],
        "Alamofire": [
            "~> 3.1"
        ]
    }
}

Pin It on Pinterest

Share This