Introduction
What's this?
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 :)
Features
- 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
Requirements
- iOS 8.0+
- macOS 10.10+
- watchOS 2.0+
- tvOS 9.0+
- Xcode 8 with Swift 3
Dependency
Installation
CocoaPods
pod 'Fire'Contribution
You are welcome to fork and submit pull requests.
License
Fire is open-sourced software, licensed under the MIT license.
Usage
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
ä¾?èµ–
安装
CocoaPods
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 ��何�?
Fire �� Swift �作られる iOS/macOS/watchOS/tvOSiプラットホームHTTP/HTTPS �軽��データ�インタ�ットフレームワーク(Internet-Framework)�Python-Requests:HTTP for Humans �深�影響を与�られ�。
人をç?†è§£ã‚„ã?™ã??ã?ªã‚‹ã?Ÿã‚?ã?«ã€?作られã?¾ã?—ã?Ÿã€‚ã?Ÿã?¾ã?Ÿã?¾ã?«ãƒ—ãƒã‚°ãƒ©ãƒ を実行ã?§ã??る。
フィー�ャー(Features)
- 連鎖�れるリクエスト�レス�ンス �メソッド
- 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
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)
}
}