ApiController for api-related stuff
This commit is contained in:
parent
a63f6b02a6
commit
67e14ea836
1 changed files with 35 additions and 0 deletions
35
lib/api_controller.dart
Normal file
35
lib/api_controller.dart
Normal file
|
|
@ -0,0 +1,35 @@
|
||||||
|
import 'package:dio/dio.dart';
|
||||||
|
import 'package:get/get.dart';
|
||||||
|
import 'package:get_storage/get_storage.dart';
|
||||||
|
import 'package:tuuli_api/tuuli_api.dart';
|
||||||
|
|
||||||
|
class ApiController extends GetxController {
|
||||||
|
static ApiController get to => Get.find();
|
||||||
|
|
||||||
|
final apiStorageBox = GetStorage();
|
||||||
|
|
||||||
|
final _endPoint = "http://127.0.0.1:8000".obs;
|
||||||
|
String get endPoint => _endPoint.value;
|
||||||
|
set endPoint(String value) => _endPoint.value = value;
|
||||||
|
|
||||||
|
String get token => apiStorageBox.read<String>("accessToken") ?? "";
|
||||||
|
set token(String value) => apiStorageBox.write("accessToken", value);
|
||||||
|
|
||||||
|
TuuliApi? _apiClientBase;
|
||||||
|
TuuliApi get apiClientBase {
|
||||||
|
_apiClientBase ??= TuuliApi(
|
||||||
|
dio: Dio(BaseOptions(
|
||||||
|
baseUrl: endPoint,
|
||||||
|
connectTimeout: 5000.milliseconds,
|
||||||
|
receiveTimeout: 3000.milliseconds,
|
||||||
|
receiveDataWhenStatusError: true,
|
||||||
|
)),
|
||||||
|
);
|
||||||
|
_apiClientBase!.setApiKey("access-token", token);
|
||||||
|
return _apiClientBase!;
|
||||||
|
}
|
||||||
|
|
||||||
|
DefaultApi get apiClient {
|
||||||
|
return apiClientBase.getDefaultApi();
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue