Skip to content

Commit

Permalink
add url shortner service
Browse files Browse the repository at this point in the history
  • Loading branch information
codenameakshay committed Nov 14, 2021
1 parent 2f7044b commit af9054d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/services/data_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import 'dart:io';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:github/github.dart';
import 'package:intl/intl.dart';
import 'package:odin/services/locator.dart';
import 'package:odin/services/shortner_service.dart';
import 'package:path/path.dart' as path;

class DataService {
final ShortnerService _shortnerService = locator<ShortnerService>();
final _env = dotenv.env;
final _gh =
GitHub(auth: Authentication.withToken(dotenv.env['GITHUB_TOKEN']));
Expand All @@ -22,6 +25,8 @@ class DataService {
path: path.basename(file.path),
),
);
return _ghFile.content?.downloadUrl ?? '';
final _downloadLink = await _shortnerService.shortUrl(
url: _ghFile.content?.downloadUrl ?? '');
return _downloadLink ?? '';
}
}
2 changes: 2 additions & 0 deletions lib/services/locator.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:get_it/get_it.dart';
import 'package:odin/services/data_service.dart';
import 'package:odin/services/shortner_service.dart';

import 'logger.dart';

Expand All @@ -9,6 +10,7 @@ Future<void> setupLocator() async {
Stopwatch stopwatch = Stopwatch()..start();
// locator.registerFactory<CurrentDataNotifier>(() => CurrentDataNotifier());
locator.registerLazySingleton<DataService>(() => DataService());
locator.registerLazySingleton<ShortnerService>(() => ShortnerService());
logger.d('Locator setup in ${stopwatch.elapsed}');
stopwatch.stop();
}
31 changes: 31 additions & 0 deletions lib/services/shortner_service.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:odin/services/logger.dart';

class ShortnerService {
final _dio = Dio(BaseOptions(
baseUrl: 'https://api.shrtco.de/v2/',
connectTimeout: 5000,
receiveTimeout: 3000,
));

Future<String?> shortUrl({required String url}) async {
final String time = DateTime.now().toString();
logger.d("GET:: $time : ${url.toString()}");
try {
final Stopwatch stopwatch = Stopwatch()..start();
final Response response = await _dio.post('shorten?url=$url');
stopwatch.stop();
logger.d("Last request took : ${stopwatch.elapsedMilliseconds} ms.");
logger.d("Request : ${response.realUri}");
if (response.statusCode != 201) {
throw HttpException(response.statusCode.toString());
}
return response.data["result"]["full_short_link"];
} catch (e, st) {
logger.e("Get Request Failed.", e, st);
return null;
}
}
}

0 comments on commit af9054d

Please sign in to comment.