Forest Client เป็นกรอบไคลเอนต์ API ที่ยืดหยุ่นและยืดหยุ่นได้และขยายได้ซึ่งสร้างขึ้นบน URLSession และ URLSessionTask มันรวมถึงการแมปวัตถุเครือข่ายจาก JSON ไปยังประเภทข้อมูลที่ใช้กันมากที่สุด เนื่องจากวิธีการเข้ารหัส/ถอดรหัสข้อมูลอย่างง่ายและสถาปัตยกรรมที่ขยายได้คุณสามารถเพิ่มตัวแมปวัตถุเครือข่ายที่กำหนดเองได้อย่างง่ายดาย Forest ให้คุณสมบัติทั้งหมดที่จำเป็นในการสร้างลูกค้าที่มีประสิทธิภาพสำหรับบริการแบ็กเอนด์ของคุณ
คุณสามารถถามได้ว่าทำไมไม่ได้รับเฟรมเวิร์กเครือข่ายที่พิสูจน์แล้วอื่น ๆ ? แน่นอนว่าคุณจะได้รับหนึ่งที่เหมาะสมที่สุดสำหรับความต้องการและความชอบสไตล์ของคุณ แต่มันก็ดีเสมอที่จะมีตัวเลือก ต่อไปนี้เป็นรายการของคุณสมบัติที่ฉันต้องการจากเลเยอร์เครือข่ายระดับสูงและนำไปใช้ใน Client Forest:
Decodable pod 'Forest'เพิ่ม Protobufs ที่สนับสนุนส่วนขยาย:
pod 'Forest/Protobuf'เพื่อใช้บริการการเข้าถึง:
pod 'Forest/Reachability'และอย่าลืมนำเข้ากรอบ:
import Forest เพียงแค่ใส่ไฟล์จากไดเรกทอรี Core และ Protobuf บางอย่างในโครงการของคุณ ในการใช้ส่วนขยาย Protobuf คุณจำเป็นต้องรวมเฟรมเวิร์ก SwiftProtobuf เข้ากับโครงการของคุณด้วย
คลาสหลักที่จัดการงานเครือข่ายคือ ServiceTask ServiceTask รวมถึงวิธีการจากโรงงานที่ช่วยกำหนดค่าการร้องขอและการตอบสนองพารามิเตอร์และตัวจัดการ หากคุณต้องการควบคุมกระบวนการร้องขอและจัดการการตอบกลับได้มากขึ้นคุณสามารถใช้การมอบหมายและใช้โปรโตคอล ServiceTaskRetrofitting และปรับเปลี่ยนพฤติกรรมงานผ่าน Retrofitter นอกจากนี้คุณยังสามารถ ServiceTask subclass มันถูกสร้างขึ้นเพื่อสิ่งนั้น
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 (GRPC ผ่าน HTTP):
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]
ฟอเรสต์มีอยู่ภายใต้ใบอนุญาต MIT ดูไฟล์ใบอนุญาตสำหรับข้อมูลเพิ่มเติม