Skip to content

Latest commit

History

History
59 lines (42 loc) · 2 KB

File metadata and controls

59 lines (42 loc) · 2 KB

Customizing Networking Backend

APIKit uses URLSession as networking backend by default. Since Session has abstraction layer of backend called SessionAdapter, you can change the backend of Session like below:

Demo implementation of Alamofire adapter is available here.

SessionAdapter

SessionAdapter provides an interface to get (Data?, URLResponse?, Error?) from URLRequest and returns SessionTask for cancellation.

publicprotocolSessionAdapter{func createTask(with URLRequest:URLRequest, handler:@escaping(Data?,URLResponse?,Error?)->Void)->SessionTaskfunc getTasks(with handler:@escaping([SessionTask])->Void)}publicprotocolSessionTask:class{func resume()func cancel()}

How Session works with SessionAdapter

Session takes an instance of type that conforms SessionAdapter as a parameter of initializer.

openclassSession{publicletadapter:SessionAdapterpublicletcallbackQueue:CallbackQueuepublicinit(adapter:SessionAdapter, callbackQueue:CallbackQueue=.main){self.adapter = adapter
self.callbackQueue = callbackQueue
}...}

Once it is initialized with a session adapter, it sends URLRequest and receives (Data?, URLResponse?, Error?) via the interfaces which are defined in SessionAdapter.

openfunc send<Request:APIKit.Request>(_ request:Request, callbackQueue:CallbackQueue?=nil, handler:@escaping(Result<Request.Response,SessionTaskError>)->Void={ _ in})->SessionTask?{leturlRequest:URLRequest=...lettask= adapter.createTask(with: urlRequest){ data, urlResponse, error in...}
task.resume()return task
}