From 42ededbb2bd2b97a7dafc293f78c9dbcba7c8c2d Mon Sep 17 00:00:00 2001 From: Felix Angelov Date: Thu, 15 Jun 2023 12:47:29 -0700 Subject: [PATCH] feat(code_push_protocol): add `GetUsageResponse` (#666) --- .../lib/src/messages/get_usage/get_usage.dart | 1 + .../get_usage/get_usage_response.dart | 111 +++++++++++++++++ .../get_usage/get_usage_response.g.dart | 114 ++++++++++++++++++ .../lib/src/messages/messages.dart | 1 + .../get_usage/get_usage_response_test.dart | 31 +++++ 5 files changed, 258 insertions(+) create mode 100644 packages/shorebird_code_push_protocol/lib/src/messages/get_usage/get_usage.dart create mode 100644 packages/shorebird_code_push_protocol/lib/src/messages/get_usage/get_usage_response.dart create mode 100644 packages/shorebird_code_push_protocol/lib/src/messages/get_usage/get_usage_response.g.dart create mode 100644 packages/shorebird_code_push_protocol/test/src/messages/get_usage/get_usage_response_test.dart diff --git a/packages/shorebird_code_push_protocol/lib/src/messages/get_usage/get_usage.dart b/packages/shorebird_code_push_protocol/lib/src/messages/get_usage/get_usage.dart new file mode 100644 index 00000000..fe9d8e45 --- /dev/null +++ b/packages/shorebird_code_push_protocol/lib/src/messages/get_usage/get_usage.dart @@ -0,0 +1 @@ +export 'get_usage_response.dart'; diff --git a/packages/shorebird_code_push_protocol/lib/src/messages/get_usage/get_usage_response.dart b/packages/shorebird_code_push_protocol/lib/src/messages/get_usage/get_usage_response.dart new file mode 100644 index 00000000..36fa83b4 --- /dev/null +++ b/packages/shorebird_code_push_protocol/lib/src/messages/get_usage/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 to a [GetUsageResponse]. + factory GetUsageResponse.fromJson(Map json) => + _$GetUsageResponseFromJson(json); + + /// Converts a [GetUsageResponse] to a Map. + Json toJson() => _$GetUsageResponseToJson(this); + + /// The usage per app. + final List 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 to a [AppUsage]. + factory AppUsage.fromJson(Map json) => + _$AppUsageFromJson(json); + + /// Converts a [AppUsage] to a Map. + Json toJson() => _$AppUsageToJson(this); + + /// The id of the app. + final String id; + + /// The usage per platform. + final List 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 to a [PlatformUsage]. + factory PlatformUsage.fromJson(Map json) => + _$PlatformUsageFromJson(json); + + /// Converts a [PlatformUsage] to a Map. + Json toJson() => _$PlatformUsageToJson(this); + + /// The name of the platform. + final String name; + + /// The usage per arch. + final List 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 to a [ArchUsage]. + factory ArchUsage.fromJson(Map json) => + _$ArchUsageFromJson(json); + + /// Converts a [ArchUsage] to a Map. + Json toJson() => _$ArchUsageToJson(this); + + /// The name of the architecture. + final String name; + + /// The usage per patch. + final List 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 to a [PatchUsage]. + factory PatchUsage.fromJson(Map json) => + _$PatchUsageFromJson(json); + + /// Converts a [PatchUsage] to a Map. + Json toJson() => _$PatchUsageToJson(this); + + /// The id of the patch. + final int id; + + /// The number of times the patch has been installed. + final int installCount; +} diff --git a/packages/shorebird_code_push_protocol/lib/src/messages/get_usage/get_usage_response.g.dart b/packages/shorebird_code_push_protocol/lib/src/messages/get_usage/get_usage_response.g.dart new file mode 100644 index 00000000..2655f087 --- /dev/null +++ b/packages/shorebird_code_push_protocol/lib/src/messages/get_usage/get_usage_response.g.dart @@ -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 json) => + $checkedCreate( + 'GetUsageResponse', + json, + ($checkedConvert) { + final val = GetUsageResponse( + apps: $checkedConvert( + 'apps', + (v) => (v as List) + .map((e) => AppUsage.fromJson(e as Map)) + .toList()), + ); + return val; + }, + ); + +Map _$GetUsageResponseToJson(GetUsageResponse instance) => + { + 'apps': instance.apps.map((e) => e.toJson()).toList(), + }; + +AppUsage _$AppUsageFromJson(Map json) => $checkedCreate( + 'AppUsage', + json, + ($checkedConvert) { + final val = AppUsage( + id: $checkedConvert('id', (v) => v as String), + platforms: $checkedConvert( + 'platforms', + (v) => (v as List) + .map((e) => PlatformUsage.fromJson(e as Map)) + .toList()), + ); + return val; + }, + ); + +Map _$AppUsageToJson(AppUsage instance) => { + 'id': instance.id, + 'platforms': instance.platforms.map((e) => e.toJson()).toList(), + }; + +PlatformUsage _$PlatformUsageFromJson(Map json) => + $checkedCreate( + 'PlatformUsage', + json, + ($checkedConvert) { + final val = PlatformUsage( + name: $checkedConvert('name', (v) => v as String), + arches: $checkedConvert( + 'arches', + (v) => (v as List) + .map((e) => ArchUsage.fromJson(e as Map)) + .toList()), + ); + return val; + }, + ); + +Map _$PlatformUsageToJson(PlatformUsage instance) => + { + 'name': instance.name, + 'arches': instance.arches.map((e) => e.toJson()).toList(), + }; + +ArchUsage _$ArchUsageFromJson(Map json) => $checkedCreate( + 'ArchUsage', + json, + ($checkedConvert) { + final val = ArchUsage( + name: $checkedConvert('name', (v) => v as String), + patches: $checkedConvert( + 'patches', + (v) => (v as List) + .map((e) => PatchUsage.fromJson(e as Map)) + .toList()), + ); + return val; + }, + ); + +Map _$ArchUsageToJson(ArchUsage instance) => { + 'name': instance.name, + 'patches': instance.patches.map((e) => e.toJson()).toList(), + }; + +PatchUsage _$PatchUsageFromJson(Map 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 _$PatchUsageToJson(PatchUsage instance) => + { + 'id': instance.id, + 'install_count': instance.installCount, + }; diff --git a/packages/shorebird_code_push_protocol/lib/src/messages/messages.dart b/packages/shorebird_code_push_protocol/lib/src/messages/messages.dart index 4ac3f23b..4d7220c5 100644 --- a/packages/shorebird_code_push_protocol/lib/src/messages/messages.dart +++ b/packages/shorebird_code_push_protocol/lib/src/messages/messages.dart @@ -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'; diff --git a/packages/shorebird_code_push_protocol/test/src/messages/get_usage/get_usage_response_test.dart b/packages/shorebird_code_push_protocol/test/src/messages/get_usage/get_usage_response_test.dart new file mode 100644 index 00000000..a81802c2 --- /dev/null +++ b/packages/shorebird_code_push_protocol/test/src/messages/get_usage/get_usage_response_test.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()), + ); + }); + }); +}