testFileUpload static method

Future testFileUpload(
  1. File file
)

Test file upload to a public test API

Implementation

static Future<dynamic> testFileUpload(File file) async {
  // Use httpbin.org as it accepts file uploads for testing
  final environment = BaseEnvironment(
    baseUrl: 'httpbin.org',
    platformPath: '',
    headers: {
      'Accept': 'application/json',
    },
  );

  final endpoint = GtdEndpoint(env: environment, path: 'post');

  // Configure the network service
  final networkService = GtdNetworkService.shared;
  networkService.request = GTDNetworkRequest(
    type: GtdMethod.post,
    enpoint: endpoint,
    data: {
      'description': 'Test file upload',
      'timestamp': DateTime.now().toIso8601String(),
    },
  );

  try {
    final response = await networkService.uploadFile(
      file: file,
      fieldName: 'file',
      onSendProgress: (int sent, int total) {
        final progress = (sent / total * 100).toStringAsFixed(2);
        print('Upload progress: $progress%');
      },
    );

    print('Upload test successful: ${response.statusCode}');
    print('Response data: ${response.data}');
    return response.data;
  } on DioException catch (e) {
    print('Upload test failed: ${e.message}');
    rethrow;
  }
}