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) { 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 : ${objectMap} ${response.data}'); } else { Logger.log(LoggerType.error, Requests, 'Authorization error : ${response.data} with $data and ${response.requestOptions.headers}'); } return response.statusCode == HttpStatus.ok; } }