58 lines
1.6 KiB
Dart
58 lines
1.6 KiB
Dart
import 'package:dio/dio.dart';
|
|
import '../config/api_config.dart';
|
|
import '../models/account.dart';
|
|
import '../models/event.dart';
|
|
import '../models/event_detail.dart';
|
|
import '../models/user_info.dart';
|
|
|
|
class ApiService {
|
|
late final Dio _dio;
|
|
|
|
ApiService() {
|
|
_dio = Dio(BaseOptions(
|
|
baseUrl: ApiConfig.baseUrl,
|
|
connectTimeout: const Duration(seconds: 10),
|
|
receiveTimeout: const Duration(seconds: 10),
|
|
));
|
|
}
|
|
|
|
void setBearerToken(String token) {
|
|
_dio.interceptors.add(
|
|
InterceptorsWrapper(
|
|
onRequest: (options, handler) {
|
|
options.headers['Authorization'] = 'Bearer $token';
|
|
return handler.next(options);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
|
|
void clearBearerToken() {
|
|
_dio.interceptors.clear();
|
|
}
|
|
|
|
Future<List<Account>> getAccounts() async {
|
|
final response = await _dio.get('/accounts');
|
|
return (response.data as List)
|
|
.map((json) => Account.fromJson(json as Map<String, dynamic>))
|
|
.toList();
|
|
}
|
|
|
|
Future<List<Event>> getSchedule(String account) async {
|
|
final response = await _dio.get('/schedule', queryParameters: {'account': account});
|
|
return (response.data as List)
|
|
.map((json) => Event.fromJson(json as Map<String, dynamic>))
|
|
.toList();
|
|
}
|
|
|
|
Future<EventDetail> getGameDetail(String gameId, String account) async {
|
|
final response = await _dio.get('/game/$gameId', queryParameters: {'account': account});
|
|
return EventDetail.fromJson(response.data as Map<String, dynamic>);
|
|
}
|
|
|
|
Future<UserInfo> getUserInfo() async {
|
|
final response = await _dio.get('/userinfo');
|
|
return UserInfo.fromJson(response.data as Map<String, dynamic>);
|
|
}
|
|
}
|