Skip to content

Instantly share code, notes, and snippets.

@GE-N
Last active August 29, 2015 14:27
Show Gist options
  • Save GE-N/0d0d4cb15f45075b2425 to your computer and use it in GitHub Desktop.
Save GE-N/0d0d4cb15f45075b2425 to your computer and use it in GitHub Desktop.
smsapi for bdd dev
import Alamofire
enum APIResult {
case Success
case Fail(NSError)
func isSuccess() -> Bool {
switch self {
case .Success: return true
case .Fail: return false
}
}
}
typealias APICallback = APIResult -> ()
class SMSAPI {
class func sendSMS(number: String, text: String, callback: APICallback?) {
let url = "http://my.api.host/api/sendsms"
var body = ["tel" : number, "sms" : text]
Alamofire.request(.POST, url, parameters: body, encoding: .JSON)
.responseJSON(options: .MutableContainers) { (request, response, JSON, error) -> Void in
let code = response.statusCode
if code == 200 {
callback?(.Success)
}
else {
var userInfo: [String : String!]? = nil
if let errorDesc = JSON?["message"] as? String {
userInfo = [NSLocalizedDescriptionKey : errorDesc]
}
let error = NSError(domain: "SMSME", code: code!, userInfo: userInfo)
callback?(.Fail(error))
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment