testFileUpload static method

Future<void> testFileUpload()

Test file upload to httpbin.org (test service)

Implementation

static Future<void> testFileUpload() async {
  try {
    // Create a temporary file to upload
    GtdLogger.i('Creating temporary file for upload...');
    final tempDir = Directory.systemTemp;
    final tempFile = File('${tempDir.path}/test_upload.txt');
    await tempFile.writeAsString('This is a test file for upload.');
    GtdLogger.i('Temporary file created at: ${tempFile.path}');

    // Setup environment for httpbin.org (public test API)
    final environment = BaseEnvironment(
      baseUrl: 'httpbin.org',
      platformPath: '',
      headers: {'Accept': 'application/json'},
    );

    // Create endpoint for file upload
    final endpoint = GtdEndpoint(env: environment, path: 'post');

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

    // Upload file with progress tracking
    GtdLogger.i('Starting file upload...');
    final response = await networkService.uploadFile(
      file: tempFile,
      fieldName: 'file',
      onSendProgress: (int sent, int total) {
        final progress = (sent / total * 100).toStringAsFixed(2);
        GtdLogger.i('Upload progress: $progress%');
      },
    );

    // Process response
    GtdLogger.i('Upload successful: ${response.statusCode}');
    GtdLogger.i('Response contains form data: ${response.data.containsKey('form')}');
    GtdLogger.i('Response contains files: ${response.data.containsKey('files')}');

    // Clean up the temporary file
    await tempFile.delete();
    GtdLogger.i('Temporary file deleted');

    return response.data;
  } catch (e) {
    // All errors from networkService are GtdError
    final gtdError = e as GtdError;
    GtdLogger.e('File upload failed: ${gtdError.message}');
    GtdLogger.e('Error code: ${gtdError.errorCode}');
    GtdLogger.e('Status code: ${gtdError.statusCode}');

    rethrow;
  }
}