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:
- Third party HTTP client like Alamofire
- Mock backend like
TestSessionAdapter URLSessionwith custom configuration and delegate
Demo implementation of Alamofire adapter is available here.
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()}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
}