عميل Forest هو إطار عمل عميل API مرن وقابل للتمديد تم إنشاؤه على قمة URLSession و URLSessionTask . ويتضمن بالفعل محوطة كائن الشبكة من JSON إلى أنواع البيانات الأكثر استخدامًا. نظرًا لنهج ترميز/فك تشفير البيانات البسيط والهندسة المعمارية القابلة للتمديد ، يمكنك بسهولة إضافة محاصر كائن الشبكة المخصص الخاص بك. توفر Forest جميع الميزات اللازمة لبناء عميل قوي لخدمات الواجهة الخلفية الخاصة بك.
هل يمكنك أن تسأل لماذا لا تحصل على أي إطار عمل مثبت على التواصل؟ بالتأكيد ، ستحصل على أفضل مناسبة لاحتياجاتك وتفضيلات الأسلوب ، ولكن من الجيد دائمًا أن يكون لديك خيارات. فيما يلي قائمة الميزات التي أردتها من طبقة الشبكات ذات المستوى الأعلى وتنفيذها في عميل الغابات:
Decodable pod 'Forest'أضف protobufs الداعمة الإضافات:
pod 'Forest/Protobuf'لاستخدام خدمة الوصول:
pod 'Forest/Reachability'ولا تنس استيراد الإطار:
import Forest فقط ضع الملفات من دلائل Core و Protobuf Somethere في مشروعك. لاستخدام امتدادات Protobuf ، تحتاج بالإضافة إلى دمج إطار SwiftProtobuf في مشروعك.
الفئة الأساسية التي تتعامل مع مهمة الشبكة هي ServiceTask . يتضمن ServiceTask أساليب المصنع تساعد على تكوين معاملات الطلب والاستجابة. إذا كنت بحاجة إلى مزيد من التحكم في عملية تقديم الطلب والتعامل مع الاستجابة ، فيمكنك استخدام التفويض وتنفيذ بروتوكول ServiceTaskRetrofitting وتعديل سلوك المهمة عبر التحديثية. كما يمكنك مقاس 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 (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]
الغابة متوفرة تحت رخصة معهد ماساتشوستس للتكنولوجيا. انظر ملف الترخيص لمزيد من المعلومات.