generateBitLyLink method

Future<BitLyLinkData> generateBitLyLink({
  1. required BitLinkParameters parameters,
  2. required String longUrl,
  3. String domain = "bit.ly",
  4. String? groupGuid,
})

Official Documentation :- Converts a long url to a Bitlink and sets additional parameters.

It will Return BitLyLinkData after successful operation. And throws BitLyException on unsuccessful operation. Example:--

try{
  final shortener = BitLyShortener(
    accessToken: "YOUR_TOKEN",
  );
  // check official documentation for info regarding BitLinks
  final _parameters = BitLinkParameters(
    title: "title",
    deepLinkAppId: "com.bitly.app",
    deepLinkAppUriPath: "/store?id=123456",
    deepLinkInstallUrl: "link",
    deepLinkInstallType: "promote_install",
  );
  BitLyLinkData linkData=await shortener.generateBitLyLink(
    longUrl: 'ANY_URL',
    parameters:_parameters,
  );
  print(linkData.link);
}
on BitLyException catch(e){ //For handling BitLyException
  print(e);
}
on Exception catch(e){ // For handling other Exceptions related to http package
  print(e);
}

Check out official documentation for more info.

Implementation

Future<BitLyLinkData> generateBitLyLink({
  ///Additional deepLinks parameters for creating a bitlink link
  required BitLinkParameters parameters,

  ///link you want to generate shortLink of
  /// Must not be empty or null
  required String longUrl,

  ///Custom Domain Name.
  ///by default set to bit.ly
  String domain = "bit.ly",

  ///Group GUID
  String? groupGuid,
}) async {
  final _body = parameters._generateBody(longUrl, groupGuid, domain);

  final response = await post(
    Uri.parse(_bitlinkUri),
    body: _body,
    headers: {
      _contentTypeHeader: _jsonContentType,
      _authHeader: "Bearer $accessToken",
    },
  );
  if (response.statusCode == 200 || response.statusCode == 201) {
    final _linkData = jsonDecode(response.body);
    return BitLyLinkData.fromMap(_linkData);
  } else {
    final _errorData = jsonDecode(response.body);
    throw BitLyException(_errorData);
  }
}