getShortenedUrl function

Future<String> getShortenedUrl(
  1. String url
)

Implementation

Future<String> getShortenedUrl(String url) async {
  logger.info('Attempting to shorten URL: $url');
  try {
    validateURL(url, 'getShortenedUrl');
    final response = await http.post(
      Uri.parse('${Constants.BACKEND_BASE_URL}/api/sdk/shortener'),
      headers: {'Content-Type': 'application/json'},
      body: jsonEncode({'fullUrl': url}),
    );
    final res = jsonDecode(response.body);
    if (response.statusCode != 200) {
      logger.info('Failed to shorten URL: $url, Response: ${jsonEncode(res)}');
      return url;
    }
    final shortenedVerificationUrl = res['result']['shortUrl'];
    return shortenedVerificationUrl;
  } catch (err) {
    logger.info('Error shortening URL: $url, Error: $err');
    return url;
  }
}