feat(code_push_protocol): add GetUsageResponse (#666)

This commit is contained in:
Felix Angelov
2023-06-15 12:47:29 -07:00
committed by GitHub
parent 32f3b05377
commit 42ededbb2b
5 changed files with 258 additions and 0 deletions
@@ -0,0 +1 @@
export 'get_usage_response.dart';
@@ -0,0 +1,111 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:shorebird_code_push_protocol/shorebird_code_push_protocol.dart';
part 'get_usage_response.g.dart';
/// {@template get_usage_response}
/// The response body for GET /api/v1/usage
/// {@endtemplate}
@JsonSerializable()
class GetUsageResponse {
/// {@macro get_usage_response}
const GetUsageResponse({required this.apps});
/// Converts a Map<String, dynamic> to a [GetUsageResponse].
factory GetUsageResponse.fromJson(Map<String, dynamic> json) =>
_$GetUsageResponseFromJson(json);
/// Converts a [GetUsageResponse] to a Map<String, dynamic>.
Json toJson() => _$GetUsageResponseToJson(this);
/// The usage per app.
final List<AppUsage> apps;
}
/// {@template app_usage}
/// The usage for a single app.
/// {@endtemplate}
@JsonSerializable()
class AppUsage {
/// {@macro app_usage}
const AppUsage({required this.id, required this.platforms});
/// Converts a Map<String, dynamic> to a [AppUsage].
factory AppUsage.fromJson(Map<String, dynamic> json) =>
_$AppUsageFromJson(json);
/// Converts a [AppUsage] to a Map<String, dynamic>.
Json toJson() => _$AppUsageToJson(this);
/// The id of the app.
final String id;
/// The usage per platform.
final List<PlatformUsage> platforms;
}
/// {@template platform_usage}
/// The usage for a single platform.
/// {@endtemplate}
@JsonSerializable()
class PlatformUsage {
/// {@macro platform_usage}
const PlatformUsage({required this.name, required this.arches});
/// Converts a Map<String, dynamic> to a [PlatformUsage].
factory PlatformUsage.fromJson(Map<String, dynamic> json) =>
_$PlatformUsageFromJson(json);
/// Converts a [PlatformUsage] to a Map<String, dynamic>.
Json toJson() => _$PlatformUsageToJson(this);
/// The name of the platform.
final String name;
/// The usage per arch.
final List<ArchUsage> arches;
}
/// {@template arch_usage}
/// The usage for a single architecture.
/// {@endtemplate}
@JsonSerializable()
class ArchUsage {
/// {@macro arch_usage}
const ArchUsage({required this.name, required this.patches});
/// Converts a Map<String, dynamic> to a [ArchUsage].
factory ArchUsage.fromJson(Map<String, dynamic> json) =>
_$ArchUsageFromJson(json);
/// Converts a [ArchUsage] to a Map<String, dynamic>.
Json toJson() => _$ArchUsageToJson(this);
/// The name of the architecture.
final String name;
/// The usage per patch.
final List<PatchUsage> patches;
}
/// {@template patch_usage}
/// The usage for a single patch.
/// {@endtemplate}
@JsonSerializable()
class PatchUsage {
/// {@macro patch_usage}
const PatchUsage({required this.id, required this.installCount});
/// Converts a Map<String, dynamic> to a [PatchUsage].
factory PatchUsage.fromJson(Map<String, dynamic> json) =>
_$PatchUsageFromJson(json);
/// Converts a [PatchUsage] to a Map<String, dynamic>.
Json toJson() => _$PatchUsageToJson(this);
/// The id of the patch.
final int id;
/// The number of times the patch has been installed.
final int installCount;
}
@@ -0,0 +1,114 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: implicit_dynamic_parameter, require_trailing_commas, cast_nullable_to_non_nullable, lines_longer_than_80_chars
part of 'get_usage_response.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
GetUsageResponse _$GetUsageResponseFromJson(Map<String, dynamic> json) =>
$checkedCreate(
'GetUsageResponse',
json,
($checkedConvert) {
final val = GetUsageResponse(
apps: $checkedConvert(
'apps',
(v) => (v as List<dynamic>)
.map((e) => AppUsage.fromJson(e as Map<String, dynamic>))
.toList()),
);
return val;
},
);
Map<String, dynamic> _$GetUsageResponseToJson(GetUsageResponse instance) =>
<String, dynamic>{
'apps': instance.apps.map((e) => e.toJson()).toList(),
};
AppUsage _$AppUsageFromJson(Map<String, dynamic> json) => $checkedCreate(
'AppUsage',
json,
($checkedConvert) {
final val = AppUsage(
id: $checkedConvert('id', (v) => v as String),
platforms: $checkedConvert(
'platforms',
(v) => (v as List<dynamic>)
.map((e) => PlatformUsage.fromJson(e as Map<String, dynamic>))
.toList()),
);
return val;
},
);
Map<String, dynamic> _$AppUsageToJson(AppUsage instance) => <String, dynamic>{
'id': instance.id,
'platforms': instance.platforms.map((e) => e.toJson()).toList(),
};
PlatformUsage _$PlatformUsageFromJson(Map<String, dynamic> json) =>
$checkedCreate(
'PlatformUsage',
json,
($checkedConvert) {
final val = PlatformUsage(
name: $checkedConvert('name', (v) => v as String),
arches: $checkedConvert(
'arches',
(v) => (v as List<dynamic>)
.map((e) => ArchUsage.fromJson(e as Map<String, dynamic>))
.toList()),
);
return val;
},
);
Map<String, dynamic> _$PlatformUsageToJson(PlatformUsage instance) =>
<String, dynamic>{
'name': instance.name,
'arches': instance.arches.map((e) => e.toJson()).toList(),
};
ArchUsage _$ArchUsageFromJson(Map<String, dynamic> json) => $checkedCreate(
'ArchUsage',
json,
($checkedConvert) {
final val = ArchUsage(
name: $checkedConvert('name', (v) => v as String),
patches: $checkedConvert(
'patches',
(v) => (v as List<dynamic>)
.map((e) => PatchUsage.fromJson(e as Map<String, dynamic>))
.toList()),
);
return val;
},
);
Map<String, dynamic> _$ArchUsageToJson(ArchUsage instance) => <String, dynamic>{
'name': instance.name,
'patches': instance.patches.map((e) => e.toJson()).toList(),
};
PatchUsage _$PatchUsageFromJson(Map<String, dynamic> json) => $checkedCreate(
'PatchUsage',
json,
($checkedConvert) {
final val = PatchUsage(
id: $checkedConvert('id', (v) => v as int),
installCount: $checkedConvert('install_count', (v) => v as int),
);
return val;
},
fieldKeyMap: const {'installCount': 'install_count'},
);
Map<String, dynamic> _$PatchUsageToJson(PatchUsage instance) =>
<String, dynamic>{
'id': instance.id,
'install_count': instance.installCount,
};
@@ -10,4 +10,5 @@ export 'create_release/create_release.dart';
export 'create_release_artifact/create_release_artifact.dart';
export 'create_user/create_user.dart';
export 'get_release_artifacts/get_release_artifacts.dart';
export 'get_usage/get_usage.dart';
export 'promote_patch/promote_patch.dart';
@@ -0,0 +1,31 @@
import 'package:shorebird_code_push_protocol/shorebird_code_push_protocol.dart';
import 'package:test/test.dart';
void main() {
group(GetUsageResponse, () {
test('can be (de)serialized', () {
const response = GetUsageResponse(
apps: [
AppUsage(
id: 'app-id',
platforms: [
PlatformUsage(
name: 'android',
arches: [
ArchUsage(
name: 'arm64',
patches: [PatchUsage(id: 1, installCount: 42)],
),
],
)
],
),
],
);
expect(
GetUsageResponse.fromJson(response.toJson()).toJson(),
equals(response.toJson()),
);
});
});
}