Forest
1.0.0
森林客户是建立在URLSession和URLSessionTask之上的灵活且可扩展的REST -API客户框架。它已经包括从JSON到最常用的数据类型的网络对象映射器。由于其简单的数据编码/解码方法和可扩展的体系结构,您可以轻松添加自定义网络对象映射器。 Forest提供了为您的后端服务建立强大客户端所需的所有功能。
您可能会要求为什么不获得其他验证的网络框架?当然,您将获得最适合您的需求和样式偏好的最佳选择,但是有选择总是很好。以下是我想要从更高级别网络层中的功能列表,并在森林客户端实现:
Decodable协议的对象 pod 'Forest'添加支持扩展的Protobufs:
pod 'Forest/Protobuf'使用可及性服务:
pod 'Forest/Reachability'而且不要忘记导入该框架:
import Forest只需将文件从Core和Protobuf目录中放入您的项目中即可。要使用Protobuf扩展,您需要另外将Swift Protobuf Framework集成到您的项目中。
处理网络任务的核心类是ServiceTask 。 ServiceTask包括有助于配置请求和响应参数和处理程序的工厂方法。如果您需要对提出请求和处理响应的过程进行更多的控制,则可以使用委托并实现ServiceTaskRetrofitting协议,并通过Retrofitter修改任务行为。另外,您可以亚类ServiceTask ,它是为此而构建的。
ServiceTask ( )
. url ( " https://host.com/path/to/endpoint " )
. method ( . GET )
. query ( [ " param " : value ] )
// Expecting valid JSON response
. json { ( object , response ) in
print ( " JSON response received: ( object ) " )
}
. error { ( error , response ) in
print ( " Error occurred: ( error ) " )
}
. perform ( )使用符合代码协议的对象发送和接收数据:
struct NameRequest : Encodable {
let name : String
}
struct NameResponse : Decodable {
let isValid : Bool
}
ServiceTask ( )
// Set base url and HTTP method
. endpoint ( . POST , " https://host.com " )
// Add path to resource
. path ( " /path/to/resource " )
// Serialize our Codable struct and set body
. body ( codable : NameRequest ( " some " ) )
// Expect response with the object of 'NameResponse' type
. codable { ( object : NameResponse , response ) in
print ( " Name valid: ( object . isValid ) " )
}
// Otherwise will fail with error
. error { ( error , response ) in
print ( " Error occured: ( error ) " )
}
. perform ( )只需下载一些文件:
ServiceTask ( )
. headers ( [ " Authorization " : " Bearer ( token ) " ] )
. method ( . PUT )
. url ( " https://host.com/file/12345 " )
. body ( text : " 123456789 " )
. file { ( url , response ) in
print ( " Downloaded: ( url ) " )
// Remove temp file
try ? FileManager . default . removeItem ( at : url )
}
. error { ( error , response ) in
print ( " Error occured: ( error ) " )
}
// When download destination not provided, content will be downloaded and saved to temp file
. download ( )上传多部分形式数据编码的内容:
do {
// Create new form data builder
var formDataBuilder = FormDataBuilder ( )
// Filename and MIME type will be obtained automatically from URL. It can be provided explicitly too
formDataBuilder . append ( . file ( name : " image " , url : * url * ) )
// Generate form data in memory. It also can be written directly to disk or stream using encode(to:) method
let formData = try formDataBuilder . encode ( )
ServiceTask ( )
. endpoint ( . POST , " https://host.com/upload " )
. body ( data : formData , contentType : formDataBuilder . contentType )
. response ( content : { ( response ) in
switch response {
case . success :
print ( " Done! " )
case . failure ( let error ) :
print ( " Failed to upload: ( error ) " )
}
} )
. perform ( )
}
catch {
print ( " ( error ) )
return
}
发送和接收Protobuf消息(http上的GRPC):
ServiceTask ( )
. endpoint ( . POST , " https://host.com " )
// Create and configure request message in place
. body { ( message : inout Google_Protobuf_StringValue ) in
message . value = " something "
}
// Expecting Google_Protobuf_Empty message response
. proto { ( message : Google_Protobuf_Empty , response ) in
print ( " Done! " )
}
. error { ( error , response ) in
print ( " Error occured: ( error ) " )
}
. perform ( )
// Or another version of the code above with explicitly provided types
ServiceTask ( )
. endpoint ( . POST , " https://host.com " )
// Create and configure request message in place
. body ( proto : Google_Protobuf_SourceContext . self ) { ( message ) in
message . fileName = " file.name "
}
// Expecting Google_Protobuf_Empty message response
. response ( proto : Google_Protobuf_Empty . self ) { ( response ) in
switch response {
case . success ( let message ) :
print ( " Done! " )
case . failure ( let error ) :
print ( " Error occured: ( error ) " )
}
}
. perform ( ) natan zalkin [email protected]
森林可根据麻省理工学院许可获得。有关更多信息,请参见许可证文件。