send<T> method

Future<T> send<T>(
  1. COSFetchConfig fetchConfig,
  2. COSConfig config
)

发送请求 fetchConfig 请求相关配置 config COS 相关配置

Implementation

Future<T> send<T>(COSFetchConfig fetchConfig, COSConfig config) async {
  try {
    HttpClient client = HttpClient();

    final fetchContext =
        COSFetchContext(fetchConfig: fetchConfig, config: config);

    final url = Uri.parse(fetchContext.url);
    final req = await client.openUrl(fetchConfig.method, url);

    COSLogger.t('${fetchConfig.method} ${url.toString()}');

    fetchContext.req = req;

    final reqHandles = [...globalReqHandlers, ...?fetchConfig.reqHandlers];

    try {
      for (final handler in reqHandles) {
        await handler(fetchContext);
      }
    } catch (error) {
      req.close();

      rethrow;
    }

    final res = await req.close();

    fetchContext.res = res;

    final resHandles = [...globalResHandlers, ...?fetchConfig.resHandlers];

    dynamic data;
    for (final handler in resHandles) {
      data = await handler(fetchContext, data);
    }

    return data;
  } catch (error) {
    if (error is SocketException && !fetchConfig.isRetryTimesExceed) {
      fetchConfig.increaseRetryTimes();
      return send(fetchConfig, config);
    } else {
      rethrow;
    }
  }
}