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]
森林可根據麻省理工學院許可獲得。有關更多信息,請參見許可證文件。