From 7f95a0a931d11e5a04edf4fc358ee78b6191e493 Mon Sep 17 00:00:00 2001 From: Felix Angelov Date: Fri, 16 Jun 2023 12:24:57 -0700 Subject: [PATCH] feat(code_push_client): add `getUsage` (#678) --- .../lib/src/code_push_client.dart | 14 ++++ .../test/src/code_push_client_test.dart | 75 +++++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/packages/shorebird_code_push_client/lib/src/code_push_client.dart b/packages/shorebird_code_push_client/lib/src/code_push_client.dart index e1ce99c5..7d5f0088 100644 --- a/packages/shorebird_code_push_client/lib/src/code_push_client.dart +++ b/packages/shorebird_code_push_client/lib/src/code_push_client.dart @@ -445,6 +445,20 @@ class CodePushClient { return decoded.artifacts; } + /// Get all usage information for the associated account. + Future> getUsage() async { + final response = await _httpClient.get(Uri.parse('$_v1/usage')); + + if (response.statusCode != HttpStatus.ok) { + throw _parseErrorResponse(response.statusCode, response.body); + } + + final decoded = GetUsageResponse.fromJson( + json.decode(response.body) as Map, + ); + return decoded.apps; + } + /// Promote the [patchId] to the [channelId]. Future promotePatch({ required int patchId, diff --git a/packages/shorebird_code_push_client/test/src/code_push_client_test.dart b/packages/shorebird_code_push_client/test/src/code_push_client_test.dart index 9990c044..0670cb67 100644 --- a/packages/shorebird_code_push_client/test/src/code_push_client_test.dart +++ b/packages/shorebird_code_push_client/test/src/code_push_client_test.dart @@ -1860,6 +1860,81 @@ void main() { }); }); + group('getUsage', () { + test('makes the correct request', () async { + codePushClient.getUsage().ignore(); + final request = verify(() => httpClient.send(captureAny())) + .captured + .single as http.BaseRequest; + expect(request.method, equals('GET')); + expect(request.url, equals(v1('usage'))); + expect(request.hasStandardHeaders, isTrue); + }); + + test('throws an exception if the http request fails (unknown)', () async { + when(() => httpClient.send(any())).thenAnswer( + (_) async => http.StreamedResponse( + const Stream.empty(), + HttpStatus.failedDependency, + ), + ); + + expect( + codePushClient.getUsage(), + throwsA( + isA().having( + (e) => e.message, + 'message', + CodePushClient.unknownErrorMessage, + ), + ), + ); + }); + + test('throws an exception if the http request fails', () async { + when(() => httpClient.send(any())).thenAnswer( + (_) async => http.StreamedResponse( + Stream.value(utf8.encode(json.encode(errorResponse.toJson()))), + HttpStatus.failedDependency, + ), + ); + + expect( + codePushClient.getUsage(), + throwsA( + isA().having( + (e) => e.message, + 'message', + errorResponse.message, + ), + ), + ); + }); + + test('completes when request succeeds', () async { + final expected = [ + AppUsage( + id: 'test-app-id', + platforms: [], + ) + ]; + + when(() => httpClient.send(any())).thenAnswer( + (_) async => http.StreamedResponse( + Stream.value( + utf8.encode( + json.encode(GetUsageResponse(apps: expected)), + ), + ), + HttpStatus.ok, + ), + ); + + final actual = await codePushClient.getUsage(); + expect(json.encode(actual), equals(json.encode(expected))); + }); + }); + group('promotePatch', () { const patchId = 0; const channelId = 0;