-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2f7044b
commit af9054d
Showing
3 changed files
with
39 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |