From e40560aa7dcf8907f9376aae6e5e369d3c7acce2 Mon Sep 17 00:00:00 2001 From: emkartal1 Date: Wed, 26 Jul 2023 13:45:09 +0200 Subject: [PATCH] add tokenSpotify class :white_check_mark: --- .../lib/view_model/CommentViewModel.dart | 10 +++++ .../lib/view_model/MusicViewModel.dart | 19 ++++++++ .../lib/view_model/PostViewModel.dart | 31 +++++++++++++ .../lib/view_model/TokenSpotify.dart | 44 +++++++++++++++++++ .../lib/view_model/UserViewModel.dart | 13 ++++++ Sources/justMUSIC/pubspec.lock | 26 ++++++++++- Sources/justMUSIC/pubspec.yaml | 1 + 7 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 Sources/justMUSIC/lib/view_model/CommentViewModel.dart create mode 100644 Sources/justMUSIC/lib/view_model/MusicViewModel.dart create mode 100644 Sources/justMUSIC/lib/view_model/PostViewModel.dart create mode 100644 Sources/justMUSIC/lib/view_model/TokenSpotify.dart create mode 100644 Sources/justMUSIC/lib/view_model/UserViewModel.dart diff --git a/Sources/justMUSIC/lib/view_model/CommentViewModel.dart b/Sources/justMUSIC/lib/view_model/CommentViewModel.dart new file mode 100644 index 0000000..c4d591f --- /dev/null +++ b/Sources/justMUSIC/lib/view_model/CommentViewModel.dart @@ -0,0 +1,10 @@ +import '../model/Comment.dart'; + +class CommentViewModel { + + // Methods + List getCommentsPost(int idPost) { + throw new Error(); + } + +} \ No newline at end of file diff --git a/Sources/justMUSIC/lib/view_model/MusicViewModel.dart b/Sources/justMUSIC/lib/view_model/MusicViewModel.dart new file mode 100644 index 0000000..f5f8b3b --- /dev/null +++ b/Sources/justMUSIC/lib/view_model/MusicViewModel.dart @@ -0,0 +1,19 @@ +import '../model/Music.dart'; + +class MusicViewModel { + + final String API_URL = ""; + + // Methods + Music getMusic(int id) { + throw new Error(); + } + + Music getMusicWithName(String name) { + throw new Error(); + } + + List getMusics(String name) { + throw new Error(); + } +} \ No newline at end of file diff --git a/Sources/justMUSIC/lib/view_model/PostViewModel.dart b/Sources/justMUSIC/lib/view_model/PostViewModel.dart new file mode 100644 index 0000000..d1bcca3 --- /dev/null +++ b/Sources/justMUSIC/lib/view_model/PostViewModel.dart @@ -0,0 +1,31 @@ +import 'package:justmusic/model/Post.dart'; + +class PostViewModel { + List _postsFriends = []; + List _bestPosts = []; + + // Constructor + PostViewModel(); + + // Getters and setters + List get postsFriends => _postsFriends; + + List get bestPosts => _bestPosts; + + // Methods + List getPostsFriends() { + throw new Error(); + } + + List getMorePostsFriends() { + throw new Error(); + } + + List getBestPosts() { + throw new Error(); + } + + List getMoreBestPosts() { + throw new Error(); + } +} diff --git a/Sources/justMUSIC/lib/view_model/TokenSpotify.dart b/Sources/justMUSIC/lib/view_model/TokenSpotify.dart new file mode 100644 index 0000000..b84d15c --- /dev/null +++ b/Sources/justMUSIC/lib/view_model/TokenSpotify.dart @@ -0,0 +1,44 @@ +import 'dart:convert'; +import 'package:http/http.dart' as http; + +class TokenSpotify { + final String clientId = 'd9b82921bbdf43efa15d0c34c28c6f93'; + final String _clientSecret = 'ba01687f59ea4ab7ad00c769e89e44d8'; + late String _accessToken; + late DateTime _tokenEnd; + + TokenSpotify() { + _tokenEnd = DateTime.now().add(Duration(seconds: -1)); + } + + Future getAccessToken() async { + if (_isTokenExpired()) { + await _refreshToken(); + } + return _accessToken; + } + + _refreshToken() async { + final basicAuth = base64Encode(utf8.encode('$clientId:$_clientSecret')); + final response = await http.post( + Uri.parse('https://accounts.spotify.com/api/token'), + headers: { + 'Authorization': 'Basic $basicAuth', + 'Content-Type': 'application/x-www-form-urlencoded', + }, + body: 'grant_type=client_credentials', + ); + + if (response.statusCode == 200) { + final responseData = jsonDecode(response.body); + _accessToken = responseData['access_token']; + _tokenEnd = DateTime.now().add(Duration(seconds: responseData['expires_in'])); + } else { + print('Erreur lors de l\'actualisation du token : ${response.statusCode}'); + } + } + + bool _isTokenExpired() { + return DateTime.now().isAfter(_tokenEnd); + } +} diff --git a/Sources/justMUSIC/lib/view_model/UserViewModel.dart b/Sources/justMUSIC/lib/view_model/UserViewModel.dart new file mode 100644 index 0000000..bea7efd --- /dev/null +++ b/Sources/justMUSIC/lib/view_model/UserViewModel.dart @@ -0,0 +1,13 @@ +import '../model/User.dart'; + +class UserViewModel { + User? _userCurrent; + + // Constructor + UserViewModel(); + + // Methods + User getUser(int id) { + throw new Error(); + } +} diff --git a/Sources/justMUSIC/pubspec.lock b/Sources/justMUSIC/pubspec.lock index 035d204..f1937c2 100644 --- a/Sources/justMUSIC/pubspec.lock +++ b/Sources/justMUSIC/pubspec.lock @@ -75,6 +75,22 @@ packages: description: flutter source: sdk version: "0.0.0" + http: + dependency: "direct main" + description: + name: http + sha256: "5895291c13fa8a3bd82e76d5627f69e0d85ca6a30dcac95c4ea19a5d555879c2" + url: "https://pub.dev" + source: hosted + version: "0.13.6" + http_parser: + dependency: transitive + description: + name: http_parser + sha256: "2aa08ce0341cc9b354a498388e30986515406668dbcc4f7c950c3e715496693b" + url: "https://pub.dev" + source: hosted + version: "4.0.2" js: dependency: transitive description: @@ -176,6 +192,14 @@ packages: url: "https://pub.dev" source: hosted version: "0.4.16" + typed_data: + dependency: transitive + description: + name: typed_data + sha256: facc8d6582f16042dd49f2463ff1bd6e2c9ef9f3d5da3d9b087e244a7b564b3c + url: "https://pub.dev" + source: hosted + version: "1.3.2" vector_math: dependency: transitive description: @@ -185,4 +209,4 @@ packages: source: hosted version: "2.1.4" sdks: - dart: ">=2.18.2 <3.0.0" + dart: ">=2.19.0 <3.0.0" diff --git a/Sources/justMUSIC/pubspec.yaml b/Sources/justMUSIC/pubspec.yaml index 03f3822..d54e0cf 100644 --- a/Sources/justMUSIC/pubspec.yaml +++ b/Sources/justMUSIC/pubspec.yaml @@ -31,6 +31,7 @@ environment: dependencies: flutter: sdk: flutter + http: ^0.13.5 # The following adds the Cupertino Icons font to your application.