mirrored_repositories/dotslash3.0/dartpad_gen_gui/lib
import 'dart:convert';

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

import 'package:dartpad_gen_gui/utils/authtoken.dart';

class DartpadGenerator {
  final String fileName;
  final String fileContents;

  DartpadGenerator({
    @required this.fileName,
    @required this.fileContents,
  });

  invokeGenerator() async {
    final response = await http.post(
      'https://api.github.com/gists',
      headers: {
        'Authorization': 'token $kAuthToken',
      },
      body: jsonEncode(
        {
          'public': true,
          'description':
              'Gist Generated by DartpadGenerator by GUI at ${DateTime.now().toString()}',
          'files': {
            fileName: {
              'content': fileContents,
            }
          }
        },
      ),
    );
    if (response.statusCode == 201) {
      final parsedResponse = jsonDecode(response.body);
      return 'https://dartpad.dev/${parsedResponse["id"]}';
    }
    return 'Something went wrong. Status code - ${response.statusCode}';
  }
}
