Skip to content

Commit

Permalink
store refresh and expires
Browse files Browse the repository at this point in the history
  • Loading branch information
Rivers authored and Rivers committed Sep 10, 2024
1 parent ae5c65f commit 8dd4717
Showing 1 changed file with 36 additions and 14 deletions.
50 changes: 36 additions & 14 deletions lib/services/backend_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,27 @@ class BackendService {
}
}

Future<void> deleteJob(String jobId) async {
final url = '$backendUrl/jobs/$jobId';
final response = await http.delete(
Uri.parse(url),
headers: await _getAuthHeaders(),
);

if (response.statusCode != 204) {
throw Exception('Failed to delete job: ${response.statusCode}');
}
}

Future<Map<String, dynamic>> processJob(String jobId) async {
final url = '$backendUrl/process_job/$jobId';
try {
final headers = await _getAuthHeaders();
final response = await http
.post(
Uri.parse(url),
headers: await _getAuthHeaders(),
headers: headers,
body: json.encode(await _getRequestBody()),
)
.timeout(const Duration(minutes: 5));

Expand Down Expand Up @@ -87,30 +101,38 @@ class BackendService {
}
}

Future<void> deleteJob(String jobId) async {
final url = '$backendUrl/jobs/$jobId';
final response = await http.delete(
Uri.parse(url),
headers: await _getAuthHeaders(),
);
Future<Map<String, String>> _getAuthHeaders() async {
final spotifyService = getIt<SpotifyService>();

if (response.statusCode != 204) {
throw Exception('Failed to delete job: ${response.statusCode}');
final credentials = await spotifyService.retrieveCredentials();

if (credentials == null) {
throw Exception("Failed to retrieve Spotify credentials");
}

// Send only the authorization header here
return {
'Authorization': 'Bearer ${credentials.accessToken}',
'Content-Type': 'application/json',
};
}

Future<Map<String, String>> _getAuthHeaders() async {
Future<Map<String, dynamic>> _getRequestBody() async {
final spotifyService = getIt<SpotifyService>();

final credentials = await spotifyService
.retrieveCredentials(); // This method already handles refreshing the token
final credentials = await spotifyService.retrieveCredentials();

if (credentials == null) {
throw Exception("Failed to retrieve Spotify credentials");
}

final expirationString =
(credentials.expiration?.millisecondsSinceEpoch ?? 0) ~/ 1000;

// Send refresh token and expiration in the body
return {
'Authorization': 'Bearer ${credentials.accessToken}',
'Content-Type': 'application/json',
'refresh_token': credentials.refreshToken ?? '',
'expires_at': expirationString,
};
}
}

0 comments on commit 8dd4717

Please sign in to comment.