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 :)
- Chainable Request / Response Methods
- Upload File / Data / MultipartFormData
- HTTP Basic Authorization
- TLS Certificate and Public Key Pinning
- Comprehensive Unit and Integration Test Coverage
- Synchronously/Asynchronously Request
- Timeouts
- Custom Cache Policy
- form (
x-www-form-encoded)/JSON HTTP body
- iOS 8.0+
- macOS 10.10+
- watchOS 2.0+
- tvOS 9.0+
- Xcode 8 with Swift 3
pod '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: Data)
let certData = NSData(contentsOfFile: NSBundle.mainBundle().pathForResource("FireDemo", ofType: "cer")!)!
f.setSSLPinning(localCertData: certData) {
print("Warning: Under Man-in-the-middle attack!!")
}
f.onError({ (resp, 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:
// cancel:
f.onCancel {
print("Canceled")
}
f.cancel()
// or:
f.cancel {
print("Canceled")
}Use Fire.Dispatch if you want to send requests synchronously:
let f = Fire.build(HTTPMethod: .GET, url: api.stringValue, timeout: timeout, dispatch: .synchronously)Fire.API:
open class func FireAPI1() {
Fire.API.baseURL = FireDemo.BASEURL
let api = Fire.API(appending: "get.php", HTTPMethod: .GET, successCode: .success)
Fire.request(api: api, params: [:], timeout: 0, callback: { (json, resp) in
if let status = resp?.statusCode {
if status == api.successCode.rawValue {
// ...
}
}
}) { (error) in
print(error.localizedDescription)
}
}
open class func FireAPI2() {
Fire.API.baseURL = FireDemo.BASEURL
let api = Fire.API(appending: "get.php", HTTPMethod: .GET, successCode: .success)
api.requestJSON(params: ["user": "Elias"], callback: { (json, resp) in
if let status = resp?.statusCode {
if status == api.successCode.rawValue {
// ...
}
}
}) { (error) in
print(error.localizedDescription)
}
}
open class func FireAPI3() {
Fire.API.baseURL = FireDemo.BASEURL
let api = Fire.API(appending: "get.php", HTTPMethod: .GET, headers: ["Content-Type": "text/json"], successCode: .success)
api.requestJSON(params: ["userid": "1232"], headers: ["Device": "iOS"], timeout: 60, dispatch: .asynchronously, callback: { (json, resp) in
if let status = resp?.statusCode {
if status == api.successCode.rawValue {
// ...
}
}
}) { (error) in
print(error.localizedDescription)
}
}Fire 一个使用 Swift 书写的轻�?级 iOS/macOS/watchOS/tvOS 平�?� HTTP/HTTPS 网络框架,深�?� Python-Requests: HTTP for Humans �?��?�。
Fire 为了更好的�?�读性而生,碰巧还�?�以�?行 :)
- 链�?调用的请求和�?应方法
- ä¸Šä¼ æ–‡ä»¶å’Œæ•°ï¿½?�
- 支�? HTTP Basic 认�?
- 支�? SSL Pinning
- 全�?�的�?�元和集�?测试覆盖
- �?�æ¥/异æ¥è¯·æ±‚
- è¶…æ—¶
- 自定义缓å˜ç–ç•¥
- 支�? form (
x-www-form-encoded)/JSON HTTP 请求体
- iOS 8.0+
- macOS 10.10+
- watchOS 2.0+
- tvOS 9.0+
- Xcode 8 �?� Swift 3
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: Data)
let certData = NSData(contentsOfFile: NSBundle.mainBundle().pathForResource("FireDemo", ofType: "cer")!)!
f.setSSLPinning(localCertData: certData) {
print("Warning: Under Man-in-the-middle attack!!")
}
f.onError({ (resp, 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")
}å¦‚æžœä½ éœ€ï¿½?�?�消请求:
// cancel:
f.onCancel {
print("Canceled")
}
f.cancel()
// or:
f.cancel {
print("Canceled")
}å¦‚æžœä½ æƒ³ï¿½?��?�?�æ¥è¯·æ±‚, 请使用 Fire.Dispatch:
let f = Fire.build(HTTPMethod: .GET, url: api.stringValue, timeout: timeout, dispatch: .synchronously)Fire.API:
open class func FireAPI1() {
Fire.API.baseURL = FireDemo.BASEURL
let api = Fire.API(appending: "get.php", HTTPMethod: .GET, successCode: .success)
Fire.request(api: api, params: [:], timeout: 0, callback: { (json, resp) in
if let status = resp?.statusCode {
if status == api.successCode.rawValue {
// ...
}
}
}) { (error) in
print(error.localizedDescription)
}
}
open class func FireAPI2() {
Fire.API.baseURL = FireDemo.BASEURL
let api = Fire.API(appending: "get.php", HTTPMethod: .GET, successCode: .success)
api.requestJSON(params: ["user": "Elias"], callback: { (json, resp) in
if let status = resp?.statusCode {
if status == api.successCode.rawValue {
// ...
}
}
}) { (error) in
print(error.localizedDescription)
}
}
open class func FireAPI3() {
Fire.API.baseURL = FireDemo.BASEURL
let api = Fire.API(appending: "get.php", HTTPMethod: .GET, headers: ["Content-Type": "text/json"], successCode: .success)
api.requestJSON(params: ["userid": "1232"], headers: ["Device": "iOS"], timeout: 60, dispatch: .asynchronously, callback: { (json, resp) in
if let status = resp?.statusCode {
if status == api.successCode.rawValue {
// ...
}
}
}) { (error) in
print(error.localizedDescription)
}
}Fire �?��?� Swift �?�作られる iOS/macOS/watchOS/tvOSiプラットホームHTTP/HTTPS �?�軽�?�?�データ�?�インタ�?ットフレームワーク(Internet-Framework)�?Python-Requests:HTTP for Humans �?�深�?�影響を与�?�られ�?�。
人を�?�解や�?��??�?�る�?��?�?��?作られ�?��?��?�。�?��?��?��?��?�プãƒã‚°ãƒ©ãƒ を実行�?��??る。
- 連鎖�?�れるリクエスト�?�レス�?ンス �?�メソッド
- Upload file/data �?�能
- HTTP Basic �?証をサ�?ート�?��?��?�る
- SSL Pinning をサ�?ート�?��?��?�る
- 全局�?�局部的�?�テスト�?�能
- �?�期通信/�?��?�期通信
- タイムアウト
- URLCachePolicy
- form (
x-www-form-encoded)/JSON HTTP 制�?をサ�?ート�?��?��?�る
- iOS 8.0 以上
- macOS 10.10 以上
- watchOS 2.0 以上
- tvOS 9.0 以上
- Xcode 8 / Swift 3 以上
CocoaPods �?�ライブラリをインストール�?��?��?�。
Podfile:
pod 'Fire'プãƒã‚¸ã‚§ã‚¯ãƒˆï¿½?�ディレクトリ内�?�以下を実行 pod install
関連�?�るリ�?ジトリ�?��?�?�?��?��?�MIT�?��?�。詳細�?��?�リ�?ジトリ内�?�LICENSEを�?�覧�??�?��?��?�。
@渋谷�?�猫
(※準備ä¸ï¿½?��?�)
Fire.build
let f = Fire.build(HTTPMethod: .GET, url: "https://yourdomain.com/get?l=zh")set.../add...
f.setParams(["key": "value"])
f.setFiles([file])
f.setHTTPHeaders(["Accept": "application/json"])
f.setBasicAuth("user", password: "pwd!@#")
f.setHTTPBody(raw: Data)
let certData = NSData(contentsOfFile: NSBundle.mainBundle().pathForResource("FireDemo", ofType: "cer")!)!
f.setSSLPinning(localCertData: certData) {
print("Warning: Under Man-in-the-middle attack!!")
}
f.onError({ (resp, error) -> Void in
print("Error: Network offline!")
})fire { (...) in }
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")
}// cancel:
f.onCancel {
print("Canceled")
}
f.cancel()
// or:
f.cancel {
print("Canceled")
}Fire.Dispatch:
let f = Fire.build(HTTPMethod: .GET, url: api.stringValue, timeout: timeout, dispatch: .synchronously)Fire.API:
open class func FireAPI1() {
Fire.API.baseURL = FireDemo.BASEURL
let api = Fire.API(appending: "get.php", HTTPMethod: .GET, successCode: .success)
Fire.request(api: api, params: [:], timeout: 0, callback: { (json, resp) in
if let status = resp?.statusCode {
if status == api.successCode.rawValue {
// ...
}
}
}) { (error) in
print(error.localizedDescription)
}
}
open class func FireAPI2() {
Fire.API.baseURL = FireDemo.BASEURL
let api = Fire.API(appending: "get.php", HTTPMethod: .GET, successCode: .success)
api.requestJSON(params: ["user": "Elias"], callback: { (json, resp) in
if let status = resp?.statusCode {
if status == api.successCode.rawValue {
// ...
}
}
}) { (error) in
print(error.localizedDescription)
}
}
open class func FireAPI3() {
Fire.API.baseURL = FireDemo.BASEURL
let api = Fire.API(appending: "get.php", HTTPMethod: .GET, headers: ["Content-Type": "text/json"], successCode: .success)
api.requestJSON(params: ["userid": "1232"], headers: ["Device": "iOS"], timeout: 60, dispatch: .asynchronously, callback: { (json, resp) in
if let status = resp?.statusCode {
if status == api.successCode.rawValue {
// ...
}
}
}) { (error) in
print(error.localizedDescription)
}
}

