mirrored_repositories/dotslash3.0/dartpad_gen_cli
import 'dart:io';
import 'dart:convert';

import 'package:meta/meta.dart';
import 'package:http/http.dart' as http;

import 'package:dartpad_gen_cli/authtoken.dart';

class DartpadGenerator {
  final List<String> fileNames;

  DartpadGenerator({
    @required this.fileNames,
  });

  void invokeGenerator() async {
    fileNames.toList().forEach((fileName) async {
      final filePath = Directory.current.path + '/' + fileName;
      final fileContent = await File(filePath).readAsString();
      final response = await http.post('https://api.github.com/gists',
          headers: {
            'Authorization': 'token $kAuthToken',
          },
          body: jsonEncode({
            'public': true,
            'description': 'Gist Generated by DartpadGenerator at ${DateTime.now().toString()}',
            'files': {
              fileName: {'content': fileContent}
            }
          }));
          if(response.statusCode == 201) {
            final parsedResponse = jsonDecode(response.body);
            print('https://dartpad.dev/${parsedResponse["id"]}');
          } else {
            print('Something went wrong. Status code - ${response.statusCode}');
          }
    });
  }
}
