Last active
August 29, 2015 14:27
-
-
Save GE-N/0d0d4cb15f45075b2425 to your computer and use it in GitHub Desktop.
smsapi for bdd dev
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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