createBitLink method

Future<BitlyModel> createBitLink({
  1. required BitlyParams params,
})

Converts a long url to a Bitlink and sets additional parameters.

try {
      FShort.instance
          .createBitLink(
              params: BitlyParams(
        longUrl: "https://dev.bitly.com",
        domain: 'bit.ly',
        tags: ['ver1.1', 'ver1.2'],
        deeplinks: [
          DeeplinkParams(
            appId: 'com.hades.test',
            appUriPath: '/store?id=123456',
            installUrl:
                'https://play.google.com/store/apps/details?id=com.hades.test&hl=en_US',
            installType: 'promote_install',
          ),
        ],
      ))
          .then((value) {
        setState(() {
          _customURL = value.link;
        });
      });
    } on BitlyException catch (_) {
      //
    } on Exception catch (_) {
      //
    }

Implementation

Future<BitlyModel> createBitLink({required BitlyParams params}) async {
  final client = HttpClient();
  final endPoint = Uri.https('api-ssl.bitly.com', '/v4/bitlinks');

  final response = await client.postUrl(endPoint).then((request) {
    request.headers
      ..set(HttpHeaders.contentTypeHeader, 'application/json')
      ..set(HttpHeaders.authorizationHeader, 'Bearer $_token');

    request.add(utf8.encode(json.encode(params.toJson())));
    return request.close();
  });

  final responseBody = await response.transform(utf8.decoder).join();
  if (response.statusCode == StatusCode.SUCCESS ||
      response.statusCode == StatusCode.CREATED) {
    return BitlyModel.fromJson(json.decode(responseBody));
  } else {
    throw BitlyException.fromJson(json.decode(responseBody));
  }
}