create method

Future<ShareLink> create({
  1. required ShareSession session,
  2. String? destinationPath,
  3. String? destinationWeb,
  4. Map<String, String>? params,
  5. String? ogTitle,
  6. String? ogDescription,
  7. String? ogImage,
})

Implementation

Future<ShareLink> create({
  required ShareSession session,
  String? destinationPath,
  String? destinationWeb,
  Map<String, String>? params,
  String? ogTitle,
  String? ogDescription,
  String? ogImage,
}) async {
  final path = destinationPath?.trim() ?? '';
  final web = destinationWeb?.trim() ?? '';
  if (path.isEmpty && web.isEmpty) {
    throw ArgumentError('destinationPath or destinationWeb required');
  }

  final uri = Uri.parse('$_kApiOrigin/v1/sdk/short-links');
  final request = await _http.postUrl(uri);
  request.headers.set(HttpHeaders.acceptHeader, 'application/json');
  request.headers.set(HttpHeaders.contentTypeHeader, 'application/json');
  request.headers.set('X-Taqlyn-Client-Id', session.clientId);
  request.headers.set('X-Taqlyn-Public-Key-Id', session.publicKeyId);
  request.write(
    jsonEncode({
      'clientId': session.clientId,
      'publicKeyId': session.publicKeyId,
      if (path.isNotEmpty) 'destinationPath': path,
      if (web.isNotEmpty) 'destinationWeb': web,
      'params': ?params,
      'ogTitle': ?ogTitle,
      'ogDescription': ?ogDescription,
      'ogImage': ?ogImage,
      'env': ?session.env,
    }),
  );
  final response = await request.close();
  final text = await response.transform(utf8.decoder).join();
  if (response.statusCode < 200 || response.statusCode >= 300) {
    throw HttpException(
      'createShareLink failed: ${response.statusCode}',
      uri: uri,
    );
  }
  final decoded = jsonDecode(text);
  if (decoded is! Map) {
    throw const FormatException('createShareLink: unexpected body');
  }
  return ShareLink.fromMap(Map<String, Object?>.from(decoded));
}