shortenUrl method

Future<String> shortenUrl(
  1. String url, {
  2. String? code,
})

Shortens the provided url. An optional code can be provided to create your own vanity URL.

Implementation

Future<String> shortenUrl(String url, {String? code}) {
  final params = <String, dynamic>{};

  params['url'] = url;

  if (code != null) {
    params['code'] = code;
  }

  return github
      .request('POST', 'http://git.io/', params: params)
      .then((response) {
    if (response.statusCode != StatusCodes.CREATED) {
      throw GitHubError(github, 'Failed to create shortened url!');
    }

    return response.headers['Location']!.split('/').last;
  });
}