Meniny / Fire-in-Swift
- понедельник, 24 апреля 2017 г. в 03:11:41
Swift
A delightful HTTP/HTTPS networking framework for iOS/macOS platform written in Swift.
Fire is a delightful HTTP/HTTPS networking framework for iOS/macOS/watchOS/tvOS platform written in Swift and inspired by Python-Requests: HTTP for Humans.
Fire was written for humans to read, and incidentally, for machines to execute :)
x-www-form-encoded
)/JSON HTTP bodypod 'Fire'
You are welcome to fork and submit pull requests.
Fire is open-sourced software, licensed under the MIT
license.
To send a request with Fire, you need to do 3 steps.
First, build up a Fire object:
let f = Fire.build(HTTPMethod: .GET, url: "https://yourdomain.com/get?l=zh")
Then, config the Fire object:
f.setParams(["key": "value"])
f.setFiles([file])
f.setHTTPHeaders(["Accept": "application/json"])
f.setBasicAuth("user", password: "pwd!@#")
f.setHTTPBody(raw: json.rawValue)
let certData = NSData(contentsOfFile: NSBundle.mainBundle().pathForResource("FireDemo", ofType: "cer")!)!
f.setSSLPinning(localCertData: certData) {
print("Warning: Under Man-in-the-middle attack!!")
}
f.onError({ (error) -> Void in
print("Error: Network offline!")
})
Finally, fire up:
f.fire { (json, resp) -> Void in
print(json["arg"]["key"].stringValue)
}
// or
f.fireForJSON { (json, resp) -> Void in
print(json["arg"]["key"].stringValue)
}
// or
f.fireForString { (str, resp) -> Void in
print(str)
}
// or
f.fireForData { (data, resp) -> Void in
print("Success")
}
If you want to cancel it:
f.cancel {
print("Canceled")
}
Fire 一个使用 Swift 书写的轻量级 iOS/macOS/watchOS/tvOS 平台 HTTP/HTTPS 网络框架,深受 Python-Requests: HTTP for Humans 启发。
Fire 为了更好的可读性而生,碰巧还可以运行 :)
x-www-form-encoded
)/JSON HTTP 请求体pod 'Fire'
欢迎任何人提交代码和问题。
Fire 是一个开源软体,遵循 MIT
协议。
要使用 Fire 发送请求,你只需要三个步骤。
首先,构建一个 Fire 实例:
let f = Fire.build(HTTPMethod: .GET, url: "https://yourdomain.com/get?l=zh")
然后,进行一些配置:
f.setParams(["key": "value"])
f.setFiles([file])
f.setHTTPHeaders(["Accept": "application/json"])
f.setBasicAuth("user", password: "pwd!@#")
f.setHTTPBody(raw: json.rawValue)
let certData = NSData(contentsOfFile: NSBundle.mainBundle().pathForResource("FireDemo", ofType: "cer")!)!
f.setSSLPinning(localCertData: certData) {
print("Warning: Under Man-in-the-middle attack!!")
}
f.onError({ (error) -> Void in
print("Error: Network offline!")
})
最后,发起请求:
f.fire { (json, resp) -> Void in
print(json["arg"]["key"].stringValue)
}
// or
f.fireForJSON { (json, resp) -> Void in
print(json["arg"]["key"].stringValue)
}
// or
f.fireForString { (str, resp) -> Void in
print(str)
}
// or
f.fireForData { (data, resp) -> Void in
print("Success")
}
如果你需要取消请求:
f.cancel {
print("Canceled")
}