URLSession.sessionWithConfiguration constructor

URLSession.sessionWithConfiguration(
  1. URLSessionConfiguration config, {
  2. URLRequest? onRedirect(
    1. URLSession session,
    2. URLSessionTask task,
    3. HTTPURLResponse response,
    4. URLRequest newRequest,
    )?,
  3. URLSessionResponseDisposition onResponse(
    1. URLSession session,
    2. URLSessionTask task,
    3. URLResponse response
    )?,
  4. void onData(
    1. URLSession session,
    2. URLSessionTask task,
    3. Data error
    )?,
  5. void onFinishedDownloading(
    1. URLSession session,
    2. URLSessionDownloadTask task,
    3. Uri uri
    )?,
  6. void onComplete(
    1. URLSession session,
    2. URLSessionTask task,
    3. Error? error
    )?,
  7. void onWebSocketTaskOpened(
    1. URLSession session,
    2. URLSessionWebSocketTask task,
    3. String? protocol
    )?,
  8. void onWebSocketTaskClosed(
    1. URLSession session,
    2. URLSessionWebSocketTask task,
    3. int? closeCode,
    4. Data? reason,
    )?,
})

A client with a given configuration.

If onRedirect is set then it will be called whenever a HTTP request returns a redirect response (e.g. 302). The response parameter contains the response from the server. The newRequest parameter contains a follow-up request that would honor the server's redirect. If the return value of this function is null then the redirect will not occur. Otherwise, the returned URLRequest (usually newRequest) will be executed. onRedirect will not be called for background sessions, which automatically follow redirects. See URLSession:task:willPerformHTTPRedirection:newRequest:completionHandler:

If onResponse is set then it will be called whenever a valid response is received. The returned URLSessionResponseDisposition will decide how the content of the response is processed. See URLSession:dataTask:didReceiveResponse:completionHandler:

If onData is set then it will be called whenever response data is received. If the amount of received data is large, then it may be called more than once. See URLSession:dataTask:didReceiveData:

If onFinishedDownloading is set then it will be called whenever a URLSessionDownloadTask has finished downloading.

If onComplete is set then it will be called when a task completes. If error is null then the request completed successfully. See URLSession:task:didCompleteWithError:

See sessionWithConfiguration:delegate:delegateQueue:

If onWebSocketTaskOpened is set then it will be called when a URLSessionWebSocketTask successfully negotiated the handshake with the server.

If onWebSocketTaskClosed is set then it will be called if a URLSessionWebSocketTask receives a close control frame from the server. NOTE: A URLSessionWebSocketTask.receiveMessage must be in flight for onWebSocketTaskClosed to be called.

Implementation

factory URLSession.sessionWithConfiguration(
  URLSessionConfiguration config, {
  URLRequest? Function(URLSession session, URLSessionTask task,
          HTTPURLResponse response, URLRequest newRequest)?
      onRedirect,
  URLSessionResponseDisposition Function(
          URLSession session, URLSessionTask task, URLResponse response)?
      onResponse,
  void Function(URLSession session, URLSessionTask task, Data error)? onData,
  void Function(URLSession session, URLSessionDownloadTask task, Uri uri)?
      onFinishedDownloading,
  void Function(URLSession session, URLSessionTask task, Error? error)?
      onComplete,
  void Function(
          URLSession session, URLSessionWebSocketTask task, String? protocol)?
      onWebSocketTaskOpened,
  void Function(URLSession session, URLSessionWebSocketTask task,
          int? closeCode, Data? reason)?
      onWebSocketTaskClosed,
}) {
  // Avoid the complexity of simultaneous or out-of-order delegate callbacks
  // by only allowing callbacks to execute sequentially.
  // See https://developer.apple.com/forums/thread/47252
  // NOTE: this is likely to reduce throughput when there are multiple
  // requests in flight because each call to a delegate waits on a lock
  // that is unlocked by Dart code.
  final queue = ncb.NSOperationQueue.new1(linkedLibs)
    ..maxConcurrentOperationCount = 1
    ..name =
        'cupertino_http.NSURLSessionDelegateQueue'.toNSString(linkedLibs);

  return URLSession._(
      ncb.NSURLSession.sessionWithConfiguration_delegate_delegateQueue_(
          linkedLibs, config._nsObject, _delegate, queue),
      isBackground: config._isBackground,
      onRedirect: onRedirect,
      onResponse: onResponse,
      onData: onData,
      onFinishedDownloading: onFinishedDownloading,
      onComplete: onComplete,
      onWebSocketTaskOpened: onWebSocketTaskOpened,
      onWebSocketTaskClosed: onWebSocketTaskClosed);
}