mirror of
https://git.selfprivacy.org/kherel/selfprivacy.org.app.git
synced 2025-01-08 00:51:20 +00:00
chore: Replace dynamic blobs with HetznerServerType
This commit is contained in:
parent
ffa13aee04
commit
2a66d246c7
|
@ -147,7 +147,7 @@ class HetznerApi extends ServerProviderApi with VolumeProviderApi {
|
|||
'format': 'ext4'
|
||||
},
|
||||
);
|
||||
volume = HetznerVolume.fromJson(createVolumeResponse.data);
|
||||
volume = HetznerVolume.fromJson(createVolumeResponse.data['volume']);
|
||||
} catch (e) {
|
||||
print(e);
|
||||
return GenericResult(
|
||||
|
@ -167,8 +167,10 @@ class HetznerApi extends ServerProviderApi with VolumeProviderApi {
|
|||
);
|
||||
}
|
||||
|
||||
Future<GenericResult<List>> getVolumes({final String? status}) async {
|
||||
List volumes = [];
|
||||
Future<GenericResult<List<HetznerVolume>>> getVolumes({
|
||||
final String? status,
|
||||
}) async {
|
||||
final List<HetznerVolume> volumes = [];
|
||||
|
||||
Response? getVolumesResonse;
|
||||
final Dio client = await getClient();
|
||||
|
@ -179,7 +181,9 @@ class HetznerApi extends ServerProviderApi with VolumeProviderApi {
|
|||
'status': status,
|
||||
},
|
||||
);
|
||||
volumes = getVolumesResonse.data['volumes'];
|
||||
for (final volume in getVolumesResonse.data['volumes']) {
|
||||
volumes.add(HetznerVolume.fromJson(volume));
|
||||
}
|
||||
} catch (e) {
|
||||
print(e);
|
||||
return GenericResult(
|
||||
|
@ -199,34 +203,31 @@ class HetznerApi extends ServerProviderApi with VolumeProviderApi {
|
|||
);
|
||||
}
|
||||
|
||||
Future<ServerVolume?> getVolume(
|
||||
Future<GenericResult<HetznerVolume?>> getVolume(
|
||||
final String volumeId,
|
||||
) async {
|
||||
ServerVolume? volume;
|
||||
HetznerVolume? volume;
|
||||
|
||||
final Response getVolumeResponse;
|
||||
final Dio client = await getClient();
|
||||
try {
|
||||
getVolumeResponse = await client.get('/volumes/$volumeId');
|
||||
final int responseVolumeId = getVolumeResponse.data['volume']['id'];
|
||||
final int volumeSize = getVolumeResponse.data['volume']['size'];
|
||||
final int volumeServer = getVolumeResponse.data['volume']['server'];
|
||||
final String volumeName = getVolumeResponse.data['volume']['name'];
|
||||
final volumeDevice = getVolumeResponse.data['volume']['linux_device'];
|
||||
volume = ServerVolume(
|
||||
id: responseVolumeId,
|
||||
name: volumeName,
|
||||
sizeByte: volumeSize,
|
||||
serverId: volumeServer,
|
||||
linuxDevice: volumeDevice,
|
||||
);
|
||||
volume = HetznerVolume.fromJson(getVolumeResponse.data['volume']);
|
||||
} catch (e) {
|
||||
print(e);
|
||||
return GenericResult(
|
||||
data: null,
|
||||
success: false,
|
||||
message: e.toString(),
|
||||
);
|
||||
} finally {
|
||||
client.close();
|
||||
}
|
||||
|
||||
return volume;
|
||||
return GenericResult(
|
||||
data: volume,
|
||||
success: true,
|
||||
);
|
||||
}
|
||||
|
||||
Future<GenericResult<bool>> deleteVolume(final int volumeId) async {
|
||||
|
@ -547,15 +548,18 @@ class HetznerApi extends ServerProviderApi with VolumeProviderApi {
|
|||
return GenericResult(success: true, data: locations);
|
||||
}
|
||||
|
||||
Future<GenericResult<List>> getAvailableServerTypes() async {
|
||||
List types = [];
|
||||
Future<GenericResult<List<HetznerServerTypeInfo>>>
|
||||
getAvailableServerTypes() async {
|
||||
final List<HetznerServerTypeInfo> types = [];
|
||||
|
||||
final Dio client = await getClient();
|
||||
try {
|
||||
final Response response = await client.get(
|
||||
'/server_types',
|
||||
);
|
||||
types = response.data!['server_types'];
|
||||
for (final type in response.data!['server_types']) {
|
||||
types.add(HetznerServerTypeInfo.fromJson(type));
|
||||
}
|
||||
} catch (e) {
|
||||
print(e);
|
||||
return GenericResult(
|
||||
|
|
|
@ -72,11 +72,21 @@ enum ServerStatus {
|
|||
|
||||
@JsonSerializable()
|
||||
class HetznerServerTypeInfo {
|
||||
HetznerServerTypeInfo(this.cores, this.memory, this.disk, this.prices);
|
||||
HetznerServerTypeInfo(
|
||||
this.cores,
|
||||
this.memory,
|
||||
this.disk,
|
||||
this.prices,
|
||||
this.name,
|
||||
this.description,
|
||||
);
|
||||
final int cores;
|
||||
final num memory;
|
||||
final int disk;
|
||||
|
||||
final String name;
|
||||
final String description;
|
||||
|
||||
final List<HetznerPriceInfo> prices;
|
||||
|
||||
static HetznerServerTypeInfo fromJson(final Map<String, dynamic> json) =>
|
||||
|
@ -85,7 +95,11 @@ class HetznerServerTypeInfo {
|
|||
|
||||
@JsonSerializable()
|
||||
class HetznerPriceInfo {
|
||||
HetznerPriceInfo(this.hourly, this.monthly);
|
||||
HetznerPriceInfo(
|
||||
this.hourly,
|
||||
this.monthly,
|
||||
this.location,
|
||||
);
|
||||
|
||||
@JsonKey(name: 'price_hourly', fromJson: HetznerPriceInfo.getPrice)
|
||||
final double hourly;
|
||||
|
@ -93,6 +107,8 @@ class HetznerPriceInfo {
|
|||
@JsonKey(name: 'price_monthly', fromJson: HetznerPriceInfo.getPrice)
|
||||
final double monthly;
|
||||
|
||||
final String location;
|
||||
|
||||
static HetznerPriceInfo fromJson(final Map<String, dynamic> json) =>
|
||||
_$HetznerPriceInfoFromJson(json);
|
||||
|
||||
|
|
|
@ -150,19 +150,19 @@ class HetznerServerProvider extends ServerProvider {
|
|||
);
|
||||
}
|
||||
|
||||
final List rawTypes = result.data;
|
||||
final rawTypes = result.data;
|
||||
for (final rawType in rawTypes) {
|
||||
for (final rawPrice in rawType['prices']) {
|
||||
if (rawPrice['location'].toString() == location.identifier) {
|
||||
for (final rawPrice in rawType.prices) {
|
||||
if (rawPrice.location == location.identifier) {
|
||||
types.add(
|
||||
ServerType(
|
||||
title: rawType['description'],
|
||||
identifier: rawType['name'],
|
||||
ram: rawType['memory'],
|
||||
cores: rawType['cores'],
|
||||
disk: DiskSize(byte: rawType['disk'] * 1024 * 1024 * 1024),
|
||||
title: rawType.description,
|
||||
identifier: rawType.name,
|
||||
ram: rawType.memory.toDouble(),
|
||||
cores: rawType.cores,
|
||||
disk: DiskSize(byte: rawType.disk * 1024 * 1024 * 1024),
|
||||
price: Price(
|
||||
value: double.parse(rawPrice['price_monthly']['gross']),
|
||||
value: rawPrice.monthly,
|
||||
currency: 'EUR',
|
||||
),
|
||||
location: location,
|
||||
|
@ -532,7 +532,7 @@ class HetznerServerProvider extends ServerProvider {
|
|||
volume: ServerVolume(
|
||||
id: volume.id,
|
||||
name: volume.name,
|
||||
sizeByte: volume.sizeByte,
|
||||
sizeByte: volume.sizeByte * 1024 * 1024 * 1024,
|
||||
serverId: volume.serverId,
|
||||
linuxDevice: volume.linuxDevice,
|
||||
),
|
||||
|
@ -676,7 +676,7 @@ class HetznerServerProvider extends ServerProvider {
|
|||
volume = ServerVolume(
|
||||
id: result.data!.id,
|
||||
name: result.data!.name,
|
||||
sizeByte: result.data!.sizeByte,
|
||||
sizeByte: result.data!.sizeByte * 1024 * 1024 * 1024,
|
||||
serverId: result.data!.serverId,
|
||||
linuxDevice: result.data!.linuxDevice,
|
||||
);
|
||||
|
@ -716,11 +716,11 @@ class HetznerServerProvider extends ServerProvider {
|
|||
|
||||
try {
|
||||
for (final rawVolume in result.data) {
|
||||
final int volumeId = rawVolume['id'];
|
||||
final int volumeSize = rawVolume['size'] * 1024 * 1024 * 1024;
|
||||
final volumeServer = rawVolume['server'];
|
||||
final String volumeName = rawVolume['name'];
|
||||
final volumeDevice = rawVolume['linux_device'];
|
||||
final int volumeId = rawVolume.id;
|
||||
final int volumeSize = rawVolume.sizeByte * 1024 * 1024 * 1024;
|
||||
final volumeServer = rawVolume.serverId;
|
||||
final String volumeName = rawVolume.name;
|
||||
final volumeDevice = rawVolume.linuxDevice;
|
||||
final volume = ServerVolume(
|
||||
id: volumeId,
|
||||
name: volumeName,
|
||||
|
|
Loading…
Reference in a new issue