Benji是用Swift编写的轻量级HTTP网络库,用于简单的HTTP请求和文件的上传/下载。
理念
简单的接口,用于制作RESTful HTTP请求。
要求
- iOS 8.0+ / macOS 10.10+ / tvOS 9.0+ / watchOS 2.0+
- Xcode 11+
- Swift 4.2+
安装
CocoaPods
CocoaPods 是用于Cocoa项目的依赖管理器。您可以使用以下命令安装它
$ gem install cocoapods
构建Benji 1.0+ 需要 CocoaPods 1.1+。
要使用CocoaPods将Benji集成到您的Xcode项目中,请在您的 Podfile
中指定它
platform :ios, '10.0'
use_frameworks!
target '<Your Target Name>' do
pod 'Benji', '~> 1.0'
end
然后,运行以下命令
$ pod install
手动
如果您不希望使用上述任何依赖管理器,则可以手动将Benji集成到项目中。
嵌入式框架
- 克隆 Benji 仓库。
- 打开 Benji.xcodeproj 并构建 Benji 框架
- 将 Benji 框架导入你的 .xcodeproj
API 文档
创建实例
将框架导入你要使用的文件中。
import Benji
支持全局使用时的常规初始化或单例引用。
let benji = Benji() // init
// or
let benji = Benji.shared // singleton
基本参数
基本参数可以针对每个实例设置,用于 Benji 所做的所有请求。
可选的 baseURL 参数接受一个字符串 URL,作为所有带有拼接 URI 的调用引用。
self.benji.baseURL : String = "https://api.<domain>.com/v2"
可选的 baseHeaders 参数接受一个字典,其中包含应用于所有 HTTP 请求的头部参数。
self.benji.baseHeaders : [String : String] = [
"<header Key>" : "<header value>"
]
HTTP 请求
所有 HTTP 请求方法如下。每个方法都包含一个带有可选参数的回调,如果它们可用,则返回。以下参数返回如下
error: Error? // returns when requests errors out
response: HTTPURLResponse? // returns with http response details
data: Any? // returns data when request is successful
如果设置了 baseURL,则应用于以下方法的 URL 字符串将追加到 baseURL。
baseURL + 应用 URL
如果设置了 baseHeaders,则应用于以下方法的任何头部将被追加到请求的 baseHeaders。
// GET
self.benji.GET(url: String,
headers: [String : String]?,
completion: (Error?, HTTPURLResponse?, Any?) -> Void)
// POST
self.benji.POST(url: String,
headers: [String : String]?,
body: Any,
completion: (Error?, HTTPURLResponse?, Any?) -> Void)
// PUT
self.benji.PUT(url: String,
headers: [String : String]?,
body: Any,
completion: (Error?, HTTPURLResponse?, Any?) -> Void)
// PATCH
self.benji.PATCH(url: String,
headers: [String : String]?,
body: Any,
completion: (Error?, HTTPURLResponse?, Any?) -> Void)
// DELETE
self.benji.DELETE(url: String,
headers: [String : String]?,
completion: (Error?, HTTPURLResponse?, Any?) -> Void)
FETCH 是一个超级函数,是执行上述所有请求的替代方式。只需应用 BenjiRequestType 即可。
self.benji.FETCH(type: BenjiRequestType,
url: String,
headers: [String : String]?,
body: Any?,
completion: (Error?, HTTPURLResponse?, Any?) -> Void)
Benji Fetch Delegate
如果需要通过RESTful fetch请求监视错误或日志,请设置以下内容:
BenjiFetchDelegate // Set BenjiFetchDelegate to the delegate class
self.benji.fetchDelegate = self // Assign the fetchDelegate on the Benji instance
// Set the following optional delegate methods
func benjiDidGetError(_ error: Error) // returns when error occurs during fetch requests
func benjiLogRequest(_ log: [String : Any]) // returns log details of the fetch requests
HTTP 文件下载器
当使用与Fetch请求类似的Benji文件下载器时,如果设置了所有baseURLs和baseHeaders,则将应用这些值。
self.benji.DOWNLOAD(url:String,
headers: [String : String]?,
completion: (Error?, HTTPURLResponse?, Any?) -> Void)
Benji 下载数 据 委 托
可以使用BenjiDownloadDelegate来监视下载数据的进度,并在下载完成后检索文件位置。
BenjiDownloadDelegate // Set BenjiDownloadDelegate to the delegate class
self.benji.downloadDelegate = self // Assign the downloadDelegate on the Benji instance
// Set the following optional delegate methods
func benjiDidGetDownloadProgress(_ progress:Float, percentage:Int) // returns download progress values.
func benjiDownloadComplete(_ location: URL) // returns location url on device for the completed file downloaded.
HTTP 文件上传器
当使用Benji文件上传器类似于Fetch请求时,如果设置了所有baseURLs和baseHeaders,则将应用这些值。必需的参数是文件名和文件路径。
self.benji.UPLOAD(url:String,
type: BenjiRequestType,
headers: [String : String]?,
body:[String : Any]?,
fileName:String, // Name of file to upload
filePath: String, // location path of the file to upload
completion: (Error?, HTTPURLResponse?, Any?) -> Void)
Benji 上传数据 委 托
可以使用BenjiUploadDelegate来监视上传进度。
BenjiUploadDelegate // Set BenjiUploadDelegate to the delegate class
self.benji.uploadDelegate = self // Assign the uploadDelegate on the Benji instance
// Set the following optional delegate methods
func benjiDidGetUploadProgress(_ progress:Float, percentage:Int) // returns upload progress values.
func benjiUploadComplete() // executed when file upload completes.
静态下载方法
静态下载程序不支持baseURL或baseHeaders。在请求以下函数中的数据时,务必提供完整URL。
// SYNCHRONOUS DOWNLOAD REQUEST
Benji.syncFileDownload(url: String) -> Data?
// ASYNCHRONOUS DOWNLOAD REQUEST
Benji.asyncFileDownload(url: String, completion: (Error?, Data?) -> Void)
静态Benji分析器
以下函数是用于JSON解析的辅助函数。使用dataFromObject将Swift对象解析为Data,使用objectFromData从有效的JSON数据解析对象。
// parse object into JSON data
BenjiParser.dataFromObject(object: Any,
completion: (Error?, Data?) -> Void)
// parse object from JSON data
BenjiParser.objectFromData(data: Data,
completion: (Error?, Any?) -> Void)