feat(code_push_client): add getChannels (#161)

This commit is contained in:
Felix Angelov
2023-03-24 12:47:19 -05:00
committed by Felix Angelov
parent 7ae6a5faad
commit f9d5a7e078
2 changed files with 105 additions and 0 deletions
@@ -194,6 +194,25 @@ class CodePushClient {
.toList();
}
/// List all channels for the provided [appId].
Future<List<Channel>> getChannels({required String appId}) async {
final response = await _httpClient.get(
Uri.parse('$hostedUri/api/v1/channels').replace(
queryParameters: {'appId': appId},
),
headers: _apiKeyHeader,
);
if (response.statusCode != HttpStatus.ok) {
throw _parseErrorResponse(response.body);
}
final channels = json.decode(response.body) as List;
return channels
.map((channel) => Channel.fromJson(channel as Map<String, dynamic>))
.toList();
}
/// List all release for the provided [appId].
Future<List<Release>> getReleases({required String appId}) async {
final response = await _httpClient.get(
@@ -756,6 +756,92 @@ void main() {
});
});
group('getChannels', () {
const appId = 'test-app-id';
test('throws an exception if the http request fails (unknown)', () async {
when(
() => httpClient.get(
any(),
headers: any(named: 'headers'),
),
).thenAnswer(
(_) async => http.Response(
'',
HttpStatus.failedDependency,
),
);
expect(
codePushClient.getChannels(appId: appId),
throwsA(
isA<CodePushException>().having(
(e) => e.message,
'message',
CodePushClient.unknownErrorMessage,
),
),
);
});
test('throws an exception if the http request fails', () async {
when(
() => httpClient.get(
any(),
headers: any(named: 'headers'),
),
).thenAnswer(
(_) async => http.Response(
json.encode(errorResponse.toJson()),
HttpStatus.failedDependency,
),
);
expect(
codePushClient.getChannels(appId: appId),
throwsA(
isA<CodePushException>().having(
(e) => e.message,
'message',
errorResponse.message,
),
),
);
});
test('completes when request succeeds (empty)', () async {
when(
() => httpClient.get(
any(),
headers: any(named: 'headers'),
),
).thenAnswer(
(_) async => http.Response(json.encode([]), HttpStatus.ok),
);
final apps = await codePushClient.getChannels(appId: appId);
expect(apps, isEmpty);
});
test('completes when request succeeds (populated)', () async {
final expected = [
Channel(id: 0, appId: '1', name: 'stable'),
Channel(id: 1, appId: '2', name: 'development'),
];
when(
() => httpClient.get(
any(),
headers: any(named: 'headers'),
),
).thenAnswer(
(_) async => http.Response(json.encode(expected), HttpStatus.ok),
);
final actual = await codePushClient.getChannels(appId: appId);
expect(json.encode(actual), equals(json.encode(expected)));
});
});
group('getReleases', () {
const appId = 'test-app-id';
test('throws an exception if the http request fails (unknown)', () async {