mirror of
https://git.selfprivacy.org/kherel/selfprivacy.org.app.git
synced 2025-01-09 09:31:13 +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/api_maps/staging_options.dart';
|
||||||
import 'package:selfprivacy/logic/models/disk_size.dart';
|
import 'package:selfprivacy/logic/models/disk_size.dart';
|
||||||
import 'package:selfprivacy/logic/models/json/hetzner_server_info.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/hive/user.dart';
|
||||||
import 'package:selfprivacy/logic/models/price.dart';
|
|
||||||
import 'package:selfprivacy/utils/password_generator.dart';
|
import 'package:selfprivacy/utils/password_generator.dart';
|
||||||
|
|
||||||
class HetznerApi extends ServerProviderApi with VolumeProviderApi {
|
class HetznerApi extends ServerProviderApi with VolumeProviderApi {
|
||||||
|
@ -106,7 +104,7 @@ class HetznerApi extends ServerProviderApi with VolumeProviderApi {
|
||||||
length: 64,
|
length: 64,
|
||||||
);
|
);
|
||||||
|
|
||||||
Future<Price?> getPricePerGb() async {
|
Future<GenericResult<double?>> getPricePerGb() async {
|
||||||
double? price;
|
double? price;
|
||||||
|
|
||||||
final Response pricingResponse;
|
final Response pricingResponse;
|
||||||
|
@ -119,16 +117,16 @@ class HetznerApi extends ServerProviderApi with VolumeProviderApi {
|
||||||
price = double.parse(volumePrice);
|
price = double.parse(volumePrice);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
print(e);
|
print(e);
|
||||||
|
return GenericResult(
|
||||||
|
success: false,
|
||||||
|
data: price,
|
||||||
|
message: e.toString(),
|
||||||
|
);
|
||||||
} finally {
|
} finally {
|
||||||
client.close();
|
client.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
return price == null
|
return GenericResult(success: true, data: price);
|
||||||
? null
|
|
||||||
: Price(
|
|
||||||
value: price,
|
|
||||||
currency: 'EUR',
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<GenericResult<HetznerVolume?>> createVolume() async {
|
Future<GenericResult<HetznerVolume?>> createVolume() async {
|
||||||
|
@ -252,7 +250,7 @@ class HetznerApi extends ServerProviderApi with VolumeProviderApi {
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<GenericResult<bool>> attachVolume(
|
Future<GenericResult<bool>> attachVolume(
|
||||||
final ServerVolume volume,
|
final HetznerVolume volume,
|
||||||
final int serverId,
|
final int serverId,
|
||||||
) async {
|
) async {
|
||||||
bool success = false;
|
bool success = false;
|
||||||
|
@ -312,7 +310,7 @@ class HetznerApi extends ServerProviderApi with VolumeProviderApi {
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<GenericResult<bool>> resizeVolume(
|
Future<GenericResult<bool>> resizeVolume(
|
||||||
final ServerVolume volume,
|
final HetznerVolume volume,
|
||||||
final DiskSize size,
|
final DiskSize size,
|
||||||
) async {
|
) async {
|
||||||
bool success = false;
|
bool success = false;
|
||||||
|
|
|
@ -756,7 +756,13 @@ class HetznerServerProvider extends ServerProvider {
|
||||||
final int serverId,
|
final int serverId,
|
||||||
) async =>
|
) async =>
|
||||||
_adapter.api().attachVolume(
|
_adapter.api().attachVolume(
|
||||||
volume,
|
HetznerVolume(
|
||||||
|
volume.id,
|
||||||
|
volume.sizeByte,
|
||||||
|
volume.serverId,
|
||||||
|
volume.name,
|
||||||
|
volume.linuxDevice,
|
||||||
|
),
|
||||||
serverId,
|
serverId,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -774,10 +780,35 @@ class HetznerServerProvider extends ServerProvider {
|
||||||
final DiskSize size,
|
final DiskSize size,
|
||||||
) async =>
|
) async =>
|
||||||
_adapter.api().resizeVolume(
|
_adapter.api().resizeVolume(
|
||||||
volume,
|
HetznerVolume(
|
||||||
|
volume.id,
|
||||||
|
volume.sizeByte,
|
||||||
|
volume.serverId,
|
||||||
|
volume.name,
|
||||||
|
volume.linuxDevice,
|
||||||
|
),
|
||||||
size,
|
size,
|
||||||
);
|
);
|
||||||
|
|
||||||
@override
|
@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,
|
final DateTime end,
|
||||||
);
|
);
|
||||||
|
|
||||||
Future<Price?> getPricePerGb();
|
Future<GenericResult<Price?>> getPricePerGb();
|
||||||
Future<GenericResult<List<ServerVolume>>> getVolumes({final String? status});
|
Future<GenericResult<List<ServerVolume>>> getVolumes({final String? status});
|
||||||
Future<GenericResult<ServerVolume?>> createVolume();
|
Future<GenericResult<ServerVolume?>> createVolume();
|
||||||
Future<GenericResult<void>> deleteVolume(final ServerVolume volume);
|
Future<GenericResult<void>> deleteVolume(final ServerVolume volume);
|
||||||
|
|
Loading…
Reference in a new issue