selfprivacy.org.app/lib/logic/api_maps/backblaze.dart

49 lines
1.3 KiB
Dart
Raw Normal View History

2021-02-03 19:51:07 +00:00
import 'dart:io';
import 'package:dio/dio.dart';
2021-03-25 23:30:34 +00:00
import 'package:selfprivacy/config/get_it_config.dart';
2021-02-03 19:51:07 +00:00
import 'package:selfprivacy/logic/api_maps/api_map.dart';
2021-03-25 23:30:34 +00:00
class BackblazeApi extends ApiMap {
BackblazeApi({this.hasLoger = false, this.isWithToken = true});
BaseOptions get options {
var options = BaseOptions(baseUrl: rootAddress);
if (isWithToken) {
var backblazeCredential = getIt<ApiConfigModel>().backblazeCredential;
var token = backblazeCredential!.applicationKey;
options.headers = {'Authorization': 'Basic $token'};
}
if (validateStatus != null) {
options.validateStatus = validateStatus!;
2021-02-03 19:51:07 +00:00
}
2021-03-25 23:30:34 +00:00
return options;
2021-02-03 19:51:07 +00:00
}
@override
2021-03-25 23:30:34 +00:00
String rootAddress = 'https://api.backblazeb2.com/b2api/v2/';
2021-02-03 19:51:07 +00:00
2021-03-25 23:30:34 +00:00
Future<bool> isValid(String encodedApiKey) async {
var client = await getClient();
Response response = await client.get(
'b2_authorize_account',
options: Options(headers: {'Authorization': 'Basic $encodedApiKey'}),
);
2021-03-26 13:38:39 +00:00
close(client);
2021-02-03 19:51:07 +00:00
if (response.statusCode == HttpStatus.ok) {
return true;
} else if (response.statusCode == HttpStatus.unauthorized) {
return false;
} else {
throw Exception('code: ${response.statusCode}');
}
}
2021-03-25 23:30:34 +00:00
@override
bool hasLoger;
@override
bool isWithToken;
}