mirror of
https://git.selfprivacy.org/kherel/selfprivacy.org.app.git
synced 2025-01-08 00:51:20 +00:00
chore: Implement better Price abstraction for Hetzner server provider
This commit is contained in:
parent
2a66d246c7
commit
49fe40bb38
|
@ -7,9 +7,7 @@ import 'package:selfprivacy/logic/api_maps/rest_maps/server_providers/server_pro
|
|||
import 'package:selfprivacy/logic/api_maps/staging_options.dart';
|
||||
import 'package:selfprivacy/logic/models/disk_size.dart';
|
||||
import 'package:selfprivacy/logic/models/json/hetzner_server_info.dart';
|
||||
import 'package:selfprivacy/logic/models/hive/server_details.dart';
|
||||
import 'package:selfprivacy/logic/models/hive/user.dart';
|
||||
import 'package:selfprivacy/logic/models/price.dart';
|
||||
import 'package:selfprivacy/utils/password_generator.dart';
|
||||
|
||||
class HetznerApi extends ServerProviderApi with VolumeProviderApi {
|
||||
|
@ -106,7 +104,7 @@ class HetznerApi extends ServerProviderApi with VolumeProviderApi {
|
|||
length: 64,
|
||||
);
|
||||
|
||||
Future<Price?> getPricePerGb() async {
|
||||
Future<GenericResult<double?>> getPricePerGb() async {
|
||||
double? price;
|
||||
|
||||
final Response pricingResponse;
|
||||
|
@ -119,16 +117,16 @@ class HetznerApi extends ServerProviderApi with VolumeProviderApi {
|
|||
price = double.parse(volumePrice);
|
||||
} catch (e) {
|
||||
print(e);
|
||||
return GenericResult(
|
||||
success: false,
|
||||
data: price,
|
||||
message: e.toString(),
|
||||
);
|
||||
} finally {
|
||||
client.close();
|
||||
}
|
||||
|
||||
return price == null
|
||||
? null
|
||||
: Price(
|
||||
value: price,
|
||||
currency: 'EUR',
|
||||
);
|
||||
return GenericResult(success: true, data: price);
|
||||
}
|
||||
|
||||
Future<GenericResult<HetznerVolume?>> createVolume() async {
|
||||
|
@ -252,7 +250,7 @@ class HetznerApi extends ServerProviderApi with VolumeProviderApi {
|
|||
}
|
||||
|
||||
Future<GenericResult<bool>> attachVolume(
|
||||
final ServerVolume volume,
|
||||
final HetznerVolume volume,
|
||||
final int serverId,
|
||||
) async {
|
||||
bool success = false;
|
||||
|
@ -312,7 +310,7 @@ class HetznerApi extends ServerProviderApi with VolumeProviderApi {
|
|||
}
|
||||
|
||||
Future<GenericResult<bool>> resizeVolume(
|
||||
final ServerVolume volume,
|
||||
final HetznerVolume volume,
|
||||
final DiskSize size,
|
||||
) async {
|
||||
bool success = false;
|
||||
|
|
|
@ -756,7 +756,13 @@ class HetznerServerProvider extends ServerProvider {
|
|||
final int serverId,
|
||||
) async =>
|
||||
_adapter.api().attachVolume(
|
||||
volume,
|
||||
HetznerVolume(
|
||||
volume.id,
|
||||
volume.sizeByte,
|
||||
volume.serverId,
|
||||
volume.name,
|
||||
volume.linuxDevice,
|
||||
),
|
||||
serverId,
|
||||
);
|
||||
|
||||
|
@ -774,10 +780,35 @@ class HetznerServerProvider extends ServerProvider {
|
|||
final DiskSize size,
|
||||
) async =>
|
||||
_adapter.api().resizeVolume(
|
||||
volume,
|
||||
HetznerVolume(
|
||||
volume.id,
|
||||
volume.sizeByte,
|
||||
volume.serverId,
|
||||
volume.name,
|
||||
volume.linuxDevice,
|
||||
),
|
||||
size,
|
||||
);
|
||||
|
||||
@override
|
||||
Future<Price?> getPricePerGb() async => _adapter.api().getPricePerGb();
|
||||
Future<GenericResult<Price?>> getPricePerGb() async {
|
||||
final result = await _adapter.api().getPricePerGb();
|
||||
|
||||
if (!result.success || result.data == null) {
|
||||
return GenericResult(
|
||||
data: null,
|
||||
success: false,
|
||||
message: result.message,
|
||||
code: result.code,
|
||||
);
|
||||
}
|
||||
|
||||
return GenericResult(
|
||||
success: true,
|
||||
data: Price(
|
||||
value: result.data!,
|
||||
currency: 'EUR',
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -37,7 +37,7 @@ abstract class ServerProvider {
|
|||
final DateTime end,
|
||||
);
|
||||
|
||||
Future<Price?> getPricePerGb();
|
||||
Future<GenericResult<Price?>> getPricePerGb();
|
||||
Future<GenericResult<List<ServerVolume>>> getVolumes({final String? status});
|
||||
Future<GenericResult<ServerVolume?>> createVolume();
|
||||
Future<GenericResult<void>> deleteVolume(final ServerVolume volume);
|
||||
|
|
Loading…
Reference in a new issue