Forest Clientは、 URLSessionとURLSessionTaskの上に構築された柔軟で拡張可能なRestful APIクライアントフレームワークです。 JSONから最も一般的に使用されるデータ型までのネットワークオブジェクトマッパーが既に含まれています。単純なデータエンコード/デコードアプローチと拡張可能なアーキテクチャのため、カスタムネットワークオブジェクトマッパーを簡単に追加できます。 Forestは、バックエンドサービス用の堅牢なクライアントを構築するために必要なすべての機能を提供します。
他の実績のあるネットワークフレームワークを取得しないのはなぜですか?確かに、あなたはあなたのニーズとスタイルの好みに最適なものを手に入れることができますが、選択肢があるのは常に良いことです。以下は、より高いレベルのネットワーキングレイヤーから望んでいた機能のリストであり、フォレストクライアントに実装されています。
Decodableプロトコルに準拠しているオブジェクトを使用して pod 'Forest'拡張機能をサポートするプロトブフを追加します。
pod 'Forest/Protobuf'到達可能性サービスを使用するには:
pod 'Forest/Reachability'そして、フレームワークをインポートすることを忘れないでください:
import ForestプロジェクトにCoreおよびProtobuf Directories Somethereからファイルを配置するだけです。 Protobuf拡張機能を使用するには、SwiftProtobufフレームワークをプロジェクトにさらに統合する必要があります。
ネットワークタスクを処理するコアクラスはServiceTaskです。 ServiceTaskは、要求と応答のパラメーションとハンドラーの構成に役立つ工場の方法が含まれています。リクエストと応答の処理プロセスをさらに制御する必要がある場合は、委任を使用してServiceTaskRetrofittingプロトコルを実装し、レトロフィッターを介してタスク動作を変更できます。また、Subclass 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 NATAN.[email protected]
ForestはMITライセンスの下で利用できます。詳細については、ライセンスファイルを参照してください。