import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';

BaseOptions dioOptions = BaseOptions(baseUrl: 'http://localhost:8080');

Future<String?> shortenUrl(String url) async {
  try {
    assert(url.isNotEmpty);
    final Response<String> res = await Dio(dioOptions).post('/', data: <String, String>{'url': url});
    final Map<String, dynamic> json = jsonDecode(res.data ?? '') as Map<String, dynamic>;
    print(json);
    return json['url'] as String?;
  } catch (err, st) {
    if (kDebugMode) {
      print('Encountered an error:');
      print(st);
      print(err);
    }
    return null;
  }
}
