pullServer method

Future<void> pullServer(
  1. PullInfoDto pullInfo, {
  2. required void onStart(
    1. PullStartDto pullStart
    ),
  3. required void onDownload(
    1. PullDownloadDto pullDownload
    ),
  4. required void onDone(
    1. PullInfoDto pullInfo
    ),
  5. void onError(
    1. Object error,
    2. StackTrace stackTrace
    )?,
})

POST /servers/pull?name=<target_name>&tag=<target_tag>

Implementation

Future<void> pullServer(
  PullInfoDto pullInfo, {
  required void Function(PullStartDto pullStart) onStart,
  required void Function(PullDownloadDto pullDownload) onDownload,
  required void Function(PullInfoDto pullInfo) onDone,
  void Function(Object error, StackTrace stackTrace)? onError,
}) async {
  try {
    Stream<String> sseStream = await serverSse.request(
      '/pull',
      queryParameters: pullInfo.toJson(),
    );
    void Function(String event, Map<String, dynamic> data) onEvent =
        (String event, Map<String, dynamic> data) {
          if (event == EventType.START) {
            onStart(PullStartDto.fromJson(data));
          } else if (event == EventType.DATA) {
            onDownload(PullDownloadDto.fromJson(data));
          } else if (event == EventType.DONE) {
            onDone(PullInfoDto.fromJson(data));
          }
        };
    sseStream.listen(
      (data) => SseClient.parse(data, onEvent),
      onError: (error, stackTrace) {
        onError?.call(error, stackTrace);
      },
      cancelOnError: true,
    );
  } catch (error, stackTrace) {
    if (onError != null) {
      onError(error, stackTrace);
      return;
    }
    rethrow;
  }
}