Skip to content

Instantly share code, notes, and snippets.

@samritchie
Created May 30, 2015 02:09
Show Gist options
  • Save samritchie/01d75d19639dc576a502 to your computer and use it in GitHub Desktop.
Save samritchie/01d75d19639dc576a502 to your computer and use it in GitHub Desktop.
Basic Alamofire RAC extension
/**
* Adapted from https://github.com/indragiek/AlamofireRACExtensions
*/
public func get<T>(url: String, f: (AnyObject -> Result<T, NSError>)) -> SignalProducer<T, NSError> {
return SignalProducer { sink, disposable in
let request = Alamofire.request(.GET, hostname + url)
.responseJSON { (request, response, responseObject, error) in
if let error = error {
sendError(sink, error)
} else if let response = response {
if let responseObject: AnyObject = responseObject {
let res = f(responseObject)
switch res {
case let .Success(b):
println("successful web response \(b.value))")
sendNext(sink, b.value)
sendCompleted(sink)
case let .Failure(e):
sendError(sink, e.value)
}
} else {
assertionFailure("Received no response object for successful response \(response) from request \(request)")
sendCompleted(sink)
}
} else {
fatalError("Invalid response -- no HTTP response or error")
}
}
request.resume()
disposable.addDisposable {
request.cancel()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment