add moat service
This commit is contained in:
@@ -0,0 +1,151 @@
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
|
||||
part 'moat.g.dart';
|
||||
|
||||
enum TransportType {
|
||||
obfs4('obfs4'),
|
||||
snowflake('snowflake'),
|
||||
meek('meek'),
|
||||
meekAzure('meek-azure'),
|
||||
webtunnel('webtunnel');
|
||||
|
||||
const TransportType(this.value);
|
||||
final String value;
|
||||
|
||||
@override
|
||||
String toString() => value;
|
||||
|
||||
static TransportType? fromString(String value) {
|
||||
for (final transport in TransportType.values) {
|
||||
if (transport.value == value) return transport;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@JsonSerializable()
|
||||
class SettingsRequest {
|
||||
final String? country;
|
||||
@JsonKey(toJson: _transportsToJson, fromJson: _transportsFromJson)
|
||||
final List<TransportType> transports;
|
||||
|
||||
const SettingsRequest({
|
||||
this.country,
|
||||
this.transports = const [
|
||||
TransportType.obfs4,
|
||||
TransportType.snowflake,
|
||||
TransportType.webtunnel,
|
||||
],
|
||||
});
|
||||
|
||||
static List<String> _transportsToJson(List<TransportType> transports) =>
|
||||
transports.map((t) => t.value).toList();
|
||||
|
||||
static List<TransportType> _transportsFromJson(List<dynamic> json) => json
|
||||
.cast<String>()
|
||||
.map((s) => TransportType.fromString(s))
|
||||
.where((t) => t != null)
|
||||
.cast<TransportType>()
|
||||
.toList();
|
||||
|
||||
factory SettingsRequest.fromJson(Map<String, dynamic> json) =>
|
||||
_$SettingsRequestFromJson(json);
|
||||
|
||||
Map<String, dynamic> toJson() => _$SettingsRequestToJson(this);
|
||||
}
|
||||
|
||||
@JsonSerializable()
|
||||
class SettingsResponse {
|
||||
final List<Setting>? settings;
|
||||
final String? country;
|
||||
final List<MoatError>? errors;
|
||||
|
||||
const SettingsResponse({this.settings, this.country, this.errors});
|
||||
|
||||
factory SettingsResponse.fromJson(Map<String, dynamic> json) =>
|
||||
_$SettingsResponseFromJson(json);
|
||||
|
||||
Map<String, dynamic> toJson() => _$SettingsResponseToJson(this);
|
||||
}
|
||||
|
||||
@JsonSerializable()
|
||||
class Setting {
|
||||
@JsonKey(name: 'bridges')
|
||||
final Bridge bridge;
|
||||
|
||||
const Setting({required this.bridge});
|
||||
|
||||
factory Setting.fromJson(Map<String, dynamic> json) =>
|
||||
_$SettingFromJson(json);
|
||||
|
||||
Map<String, dynamic> toJson() => _$SettingToJson(this);
|
||||
}
|
||||
|
||||
@JsonSerializable()
|
||||
class Bridge {
|
||||
final String type;
|
||||
final String source;
|
||||
@JsonKey(name: 'bridge_strings')
|
||||
final List<String>? bridges;
|
||||
|
||||
const Bridge({required this.type, required this.source, this.bridges});
|
||||
|
||||
factory Bridge.fromJson(Map<String, dynamic> json) => _$BridgeFromJson(json);
|
||||
|
||||
Map<String, dynamic> toJson() => _$BridgeToJson(this);
|
||||
}
|
||||
|
||||
@JsonSerializable()
|
||||
class MoatError implements Exception {
|
||||
final String? id;
|
||||
final String? type;
|
||||
final String? version;
|
||||
final int? code;
|
||||
final String? status;
|
||||
final String? detail;
|
||||
|
||||
const MoatError({
|
||||
this.id,
|
||||
this.type,
|
||||
this.version,
|
||||
this.code,
|
||||
this.status,
|
||||
this.detail,
|
||||
});
|
||||
|
||||
factory MoatError.fromJson(Map<String, dynamic> json) =>
|
||||
_$MoatErrorFromJson(json);
|
||||
|
||||
Map<String, dynamic> toJson() => _$MoatErrorToJson(this);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
if (detail != null && detail!.isNotEmpty) {
|
||||
return detail!;
|
||||
}
|
||||
return '$code $status';
|
||||
}
|
||||
}
|
||||
|
||||
@JsonSerializable()
|
||||
class BuiltInBridges {
|
||||
final List<String> meek;
|
||||
|
||||
@JsonKey(name: 'meek-azure')
|
||||
final List<String> meekAzure;
|
||||
|
||||
final List<String> obfs4;
|
||||
final List<String> snowflake;
|
||||
|
||||
const BuiltInBridges({
|
||||
required this.meek,
|
||||
required this.meekAzure,
|
||||
required this.obfs4,
|
||||
required this.snowflake,
|
||||
});
|
||||
|
||||
factory BuiltInBridges.fromJson(Map<String, dynamic> json) =>
|
||||
_$BuiltInBridgesFromJson(json);
|
||||
|
||||
Map<String, dynamic> toJson() => _$BuiltInBridgesToJson(this);
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'moat.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
SettingsRequest _$SettingsRequestFromJson(Map<String, dynamic> json) =>
|
||||
SettingsRequest(
|
||||
country: json['country'] as String?,
|
||||
transports: json['transports'] == null
|
||||
? const [
|
||||
TransportType.obfs4,
|
||||
TransportType.snowflake,
|
||||
TransportType.webtunnel,
|
||||
]
|
||||
: SettingsRequest._transportsFromJson(json['transports'] as List),
|
||||
);
|
||||
|
||||
Map<String, dynamic> _$SettingsRequestToJson(SettingsRequest instance) =>
|
||||
<String, dynamic>{
|
||||
'country': instance.country,
|
||||
'transports': SettingsRequest._transportsToJson(instance.transports),
|
||||
};
|
||||
|
||||
SettingsResponse _$SettingsResponseFromJson(Map<String, dynamic> json) =>
|
||||
SettingsResponse(
|
||||
settings: (json['settings'] as List<dynamic>?)
|
||||
?.map((e) => Setting.fromJson(e as Map<String, dynamic>))
|
||||
.toList(),
|
||||
country: json['country'] as String?,
|
||||
errors: (json['errors'] as List<dynamic>?)
|
||||
?.map((e) => MoatError.fromJson(e as Map<String, dynamic>))
|
||||
.toList(),
|
||||
);
|
||||
|
||||
Map<String, dynamic> _$SettingsResponseToJson(SettingsResponse instance) =>
|
||||
<String, dynamic>{
|
||||
'settings': instance.settings,
|
||||
'country': instance.country,
|
||||
'errors': instance.errors,
|
||||
};
|
||||
|
||||
Setting _$SettingFromJson(Map<String, dynamic> json) =>
|
||||
Setting(bridge: Bridge.fromJson(json['bridges'] as Map<String, dynamic>));
|
||||
|
||||
Map<String, dynamic> _$SettingToJson(Setting instance) => <String, dynamic>{
|
||||
'bridges': instance.bridge,
|
||||
};
|
||||
|
||||
Bridge _$BridgeFromJson(Map<String, dynamic> json) => Bridge(
|
||||
type: json['type'] as String,
|
||||
source: json['source'] as String,
|
||||
bridges: (json['bridge_strings'] as List<dynamic>?)
|
||||
?.map((e) => e as String)
|
||||
.toList(),
|
||||
);
|
||||
|
||||
Map<String, dynamic> _$BridgeToJson(Bridge instance) => <String, dynamic>{
|
||||
'type': instance.type,
|
||||
'source': instance.source,
|
||||
'bridge_strings': instance.bridges,
|
||||
};
|
||||
|
||||
MoatError _$MoatErrorFromJson(Map<String, dynamic> json) => MoatError(
|
||||
id: json['id'] as String?,
|
||||
type: json['type'] as String?,
|
||||
version: json['version'] as String?,
|
||||
code: (json['code'] as num?)?.toInt(),
|
||||
status: json['status'] as String?,
|
||||
detail: json['detail'] as String?,
|
||||
);
|
||||
|
||||
Map<String, dynamic> _$MoatErrorToJson(MoatError instance) => <String, dynamic>{
|
||||
'id': instance.id,
|
||||
'type': instance.type,
|
||||
'version': instance.version,
|
||||
'code': instance.code,
|
||||
'status': instance.status,
|
||||
'detail': instance.detail,
|
||||
};
|
||||
|
||||
BuiltInBridges _$BuiltInBridgesFromJson(Map<String, dynamic> json) =>
|
||||
BuiltInBridges(
|
||||
meek: (json['meek'] as List<dynamic>).map((e) => e as String).toList(),
|
||||
meekAzure: (json['meek-azure'] as List<dynamic>)
|
||||
.map((e) => e as String)
|
||||
.toList(),
|
||||
obfs4: (json['obfs4'] as List<dynamic>).map((e) => e as String).toList(),
|
||||
snowflake: (json['snowflake'] as List<dynamic>)
|
||||
.map((e) => e as String)
|
||||
.toList(),
|
||||
);
|
||||
|
||||
Map<String, dynamic> _$BuiltInBridgesToJson(BuiltInBridges instance) =>
|
||||
<String, dynamic>{
|
||||
'meek': instance.meek,
|
||||
'meek-azure': instance.meekAzure,
|
||||
'obfs4': instance.obfs4,
|
||||
'snowflake': instance.snowflake,
|
||||
};
|
||||
Reference in New Issue
Block a user