resolve method

Future<ResolveOutcome> resolve(
  1. String shortUrl, {
  2. String? apiKey,
})

Resolve a short URL via GET /sdk/resolve, surfacing the outcome explicitly so callers can tell a genuine 404 from a transport error.

Implementation

Future<ResolveOutcome> resolve(String shortUrl, {String? apiKey}) async {
  final url = RegExp(r'^https?://', caseSensitive: false).hasMatch(shortUrl)
      ? shortUrl
      : 'https://$shortUrl';
  http.Response res;
  try {
    res = await _http.get(
      Uri.parse('$apiBase/sdk/resolve?url=${Uri.encodeComponent(url)}'),
      headers: apiKey != null ? {'x-app-key': apiKey} : {},
    );
  } catch (e) {
    return ResolveOutcome(ok: false, status: 0, body: null, error: '$e');
  }
  final text = res.body;
  Map<String, dynamic>? body;
  try {
    body = text.isNotEmpty ? (jsonDecode(text) as Map<String, dynamic>) : null;
  } catch (_) {
    body = {'raw': text};
  }
  if (res.statusCode < 200 || res.statusCode >= 300) {
    final msg = body?['message'] ??
        body?['error'] ??
        'HTTP ${res.statusCode}';
    return ResolveOutcome(
        ok: false, status: res.statusCode, body: body, error: '$msg');
  }
  return ResolveOutcome(ok: true, status: res.statusCode, body: body);
}