diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..81525b4 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,25 @@ +{ + // Utilisez IntelliSense pour en savoir plus sur les attributs possibles. + // Pointez pour afficher la description des attributs existants. + // Pour plus d'informations, visitez : https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "desktopapp", + "request": "launch", + "type": "dart" + }, + { + "name": "desktopapp (profile mode)", + "request": "launch", + "type": "dart", + "flutterMode": "profile" + }, + { + "name": "desktopapp (release mode)", + "request": "launch", + "type": "dart", + "flutterMode": "release" + } + ] +} \ No newline at end of file diff --git a/lib/utils/requests.dart b/lib/utils/requests.dart new file mode 100644 index 0000000..98a0960 --- /dev/null +++ b/lib/utils/requests.dart @@ -0,0 +1,109 @@ +import 'dart:convert'; +import 'dart:io'; + +import 'package:desktopapp/utils/logger.dart'; +import 'package:dio/dio.dart'; +import 'package:flutter/material.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +enum AuthentificationType { code, refreshToken } + +enum AlertType { follow, subscription, donation, host } + +extension AlertTypeExtension on AlertType { + String get value { + switch (this) { + case AlertType.follow: + return 'follow'; + case AlertType.subscription: + return 'subscription'; + case AlertType.donation: + return 'donation'; + case AlertType.host: + return 'host'; + } + } +} + +class Requests { + static Future authorize(String token, AuthentificationType type) async { + SharedPreferences prefs = await SharedPreferences.getInstance(); + Dio dio = Dio(BaseOptions( + contentType: Headers.jsonContentType, + responseType: ResponseType.json, + validateStatus: (_) => true)); + + var objectData = { + 'grant_type': 'authorization_code', + 'client_id': prefs.getString('client_id'), + 'client_secret': prefs.getString('client_secret'), + 'redirect_uri': 'http://localhost:1234/', + type == AuthentificationType.code ? 'code' : 'refresh_token': token, + }; + + var data = jsonEncode(objectData); + + var response = + await dio.post('https://streamlabs.com/api/v1.0/token', data: data); + + if (response.statusCode == HttpStatus.ok) { + jsonDecode(response.data).forEach((key, value) => + prefs.setString('credentials.$key', value.toString())); + Logger.log(LoggerType.info, Requests, + 'Authorization success : ${response.data}'); + return true; + } else { + Logger.log(LoggerType.error, Requests, + 'Authorization failed : ${response.data}'); + return false; + } + } + + static Future sendAlert(String message, + {AlertType type = AlertType.donation, + String? imageHref, + String? soundHref, + Duration? duration, + Color? specialTextColor}) async { + SharedPreferences prefs = await SharedPreferences.getInstance(); + Dio dio = Dio(BaseOptions( + contentType: Headers.jsonContentType, + responseType: ResponseType.json, + validateStatus: (_) => true)); + + var accessToken = prefs.getString('credentials.access_token'); + + var objectMap = { + 'type': type.value, + 'message': message, + 'access_token': accessToken, + }; + + if (imageHref != null) objectMap.putIfAbsent('image_href', () => imageHref); + if (soundHref != null) objectMap.putIfAbsent('sound_href', () => soundHref); + if (duration != null) { + objectMap.putIfAbsent('duration', () => duration.inSeconds.toString()); + } + if (specialTextColor != null) { + objectMap.putIfAbsent( + 'special_text_color', + () => + "#${specialTextColor.red.toRadixString(16).padLeft(2, '0')}${specialTextColor.green.toRadixString(16).padLeft(2, '0')}${specialTextColor.blue.toRadixString(16).padLeft(2, '0')}${specialTextColor.alpha.toRadixString(16).padLeft(2, '0')}"); + } + + var data = jsonEncode(objectMap); + + var response = + await dio.post('https://streamlabs.com/api/v1.0/alerts', data: data); + + if (response.statusCode == HttpStatus.ok) { + Logger.log(LoggerType.info, Requests, + 'Authorization success : ${response.data}'); + } else { + Logger.log(LoggerType.error, Requests, + 'Authorization error : ${response.data} with $data and ${response.requestOptions.headers}'); + } + + return response.statusCode == HttpStatus.ok; + } +} diff --git a/lib/widgets/components/tutorials/step3.dart b/lib/widgets/components/tutorials/step3.dart index 3c5e1ca..81dc140 100644 --- a/lib/widgets/components/tutorials/step3.dart +++ b/lib/widgets/components/tutorials/step3.dart @@ -1,9 +1,8 @@ -import 'dart:convert'; import 'dart:io'; import 'package:desktopapp/classes/routes.dart'; import 'package:desktopapp/utils/logger.dart'; -import 'package:dio/dio.dart'; +import 'package:desktopapp/utils/requests.dart'; import 'package:flutter/material.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:url_launcher/url_launcher.dart'; @@ -86,27 +85,7 @@ class Step3 extends StatelessWidget { ], )); } else { - Dio dio = Dio(BaseOptions( - contentType: Headers.jsonContentType, - responseType: ResponseType.json, - validateStatus: (_) => true)); - - var data = jsonEncode({ - 'grant_type': 'authorization_code', - 'client_id': prefs.getString('client_id'), - 'client_secret': prefs.getString('client_secret'), - 'redirect_uri': 'http://localhost:1234/', - 'code': code, - }); - - var response = - await dio.post('https://streamlabs.com/api/v1.0/token', data: data); - - if (response.statusCode == HttpStatus.ok) { - jsonDecode(response.data).forEach((key, value) => - prefs.setString('credentials.$key', value.toString())); - Logger.log(LoggerType.info, this, - 'Authorization success : ${response.data}'); + if (await Requests.authorize(code, AuthentificationType.code)) { showDialog( context: context, builder: (_) => AlertDialog( @@ -124,8 +103,6 @@ class Step3 extends StatelessWidget { ], )); } else { - Logger.log(LoggerType.error, this, - 'Authorization error : ${response.data} with $data and ${response.requestOptions.headers}'); showDialog( context: context, builder: (_) => AlertDialog( diff --git a/lib/widgets/pages/home.dart b/lib/widgets/pages/home.dart index f70086a..719c9be 100644 --- a/lib/widgets/pages/home.dart +++ b/lib/widgets/pages/home.dart @@ -1,9 +1,5 @@ -import 'dart:convert'; -import 'dart:io'; - import 'package:desktopapp/classes/routes.dart'; -import 'package:desktopapp/utils/logger.dart'; -import 'package:dio/dio.dart'; +import 'package:desktopapp/utils/requests.dart'; import 'package:flutter/material.dart'; import 'package:shared_preferences/shared_preferences.dart'; @@ -19,29 +15,7 @@ class HomePage extends StatelessWidget { ), TextButton( onPressed: () async { - Dio dio = Dio(BaseOptions( - contentType: Headers.jsonContentType, - responseType: ResponseType.json, - validateStatus: (_) => true)); - - var accessToken = prefs.getString('credentials.access_token'); - - var data = jsonEncode({ - 'type': 'donation', - 'message': '*MrDev023* *Break* Test !', - 'access_token': accessToken, - 'special_text_color': '#ff00ff', - 'image_href': 'https://postimgs.org/img/logo.png', - 'sound_href': '' - }); - - var response = await dio - .post('https://streamlabs.com/api/v1.0/alerts', data: data); - - if (response.statusCode == HttpStatus.ok) { - prefs.setString('credentials', response.data.toString()); - Logger.log(LoggerType.info, this, - 'Authorization success : ${response.data}'); + if (await Requests.sendAlert('*MrDev023* *Break* Test !')) { showDialog( context: context, builder: (_) => AlertDialog( @@ -56,8 +30,6 @@ class HomePage extends StatelessWidget { ], )); } else { - Logger.log(LoggerType.error, this, - 'Authorization error : ${response.data} with $data and ${response.requestOptions.headers}'); showDialog( context: context, builder: (_) => AlertDialog(