cosRequest<T> function

Future<Response<T>> cosRequest<T>(
  1. String method,
  2. String action, {
  3. required ObjectStoragePutObjectRequest putObjectRequest,
  4. Map<String, String?> params = const {},
  5. Map<String, String?> headers = const {},
  6. String? token,
  7. String scheme = "https",
  8. Stream? stream,
  9. Object? data,
})

Implementation

Future<Response<T>> cosRequest<T>(
  String method,
  String action, {
  required ObjectStoragePutObjectRequest putObjectRequest,
  Map<String, String?> params = const {},
  Map<String, String?> headers = const {},
  String? token,
  String scheme = "https",
  Stream? stream,
  Object? data,
}) async {
  String urlParams =
      params.keys.toList().map((e) => "$e=${params[e] ?? ""}").join("&");
  if (urlParams.isNotEmpty) {
    urlParams = "?$urlParams";
  }
  final dio = Dio();

  if (!action.startsWith("/")) {
    action = "/$action";
  }

  // "$scheme://$bucketName.cos.$region.myqcloud.com"
  final uri =
      "$scheme://${putObjectRequest.bucketName}.cos.${putObjectRequest.region}.myqcloud.com";
  var sighn = getSign(
    method,
    action,
    secretId: putObjectRequest.accessKeyId,
    secretKey: putObjectRequest.accessKeySecret,
    params: params,
    headers: headers,
  );
  final reqHeaders = headers.map((key, value) => MapEntry(key, value ?? ""));
  reqHeaders["Authorization"] = sighn;
  if (token != null) {
    reqHeaders["x-cos-security-token"] = token;
  }
  try {
    final resp = await dio.request<T>(
      "$uri$action",
      queryParameters: params,
      data: data ?? stream,
      options: Options(
        method: method,
        headers: reqHeaders,
        validateStatus: (status) => true,
      ),
    );
    return resp;
  } catch (e) {
    cosLog("request error: $e");
    rethrow;
  }
}