feat(code_push_client): add getUsage (#678)

This commit is contained in:
Felix Angelov
2023-06-16 12:24:57 -07:00
committed by GitHub
parent cb243c73ce
commit 7f95a0a931
2 changed files with 89 additions and 0 deletions
@@ -445,6 +445,20 @@ class CodePushClient {
return decoded.artifacts;
}
/// Get all usage information for the associated account.
Future<List<AppUsage>> 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<String, dynamic>,
);
return decoded.apps;
}
/// Promote the [patchId] to the [channelId].
Future<void> promotePatch({
required int patchId,
@@ -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<CodePushException>().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<CodePushException>().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;