feat(code_push_client): add getReleaseArtifact (#186)

This commit is contained in:
Felix Angelov
2023-03-28 13:47:16 -05:00
committed by GitHub
parent dcec738667
commit 64b1dcfbfd
2 changed files with 116 additions and 0 deletions
@@ -261,6 +261,30 @@ class CodePushClient {
.toList();
}
/// Get a release artifact for a specific [releaseId], [arch], and [platform].
Future<ReleaseArtifact> getReleaseArtifact({
required int releaseId,
required String arch,
required String platform,
}) async {
final response = await _httpClient.get(
Uri.parse('$hostedUri/api/v1/releases/$releaseId/artifacts').replace(
queryParameters: {
'arch': arch,
'platform': platform,
},
),
headers: _apiKeyHeader,
);
if (response.statusCode != HttpStatus.ok) {
throw _parseErrorResponse(response.body);
}
final body = json.decode(response.body) as Map<String, dynamic>;
return ReleaseArtifact.fromJson(body);
}
/// Promote the [patchId] to the [channelId].
Future<void> promotePatch({
required int patchId,
@@ -1061,6 +1061,98 @@ void main() {
});
});
group('getReleaseArtifact', () {
const releaseId = 0;
const arch = 'aarch64';
const platform = 'android';
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.getReleaseArtifact(
releaseId: releaseId,
arch: arch,
platform: platform,
),
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.getReleaseArtifact(
releaseId: releaseId,
arch: arch,
platform: platform,
),
throwsA(
isA<CodePushException>().having(
(e) => e.message,
'message',
errorResponse.message,
),
),
);
});
test('completes when request succeeds', () async {
final expected = ReleaseArtifact(
id: 0,
releaseId: releaseId,
arch: arch,
platform: platform,
url: 'https://example.com',
hash: '#',
size: 42,
);
when(
() => httpClient.get(
any(),
headers: any(named: 'headers'),
),
).thenAnswer(
(_) async => http.Response(json.encode(expected), HttpStatus.ok),
);
final actual = await codePushClient.getReleaseArtifact(
releaseId: releaseId,
arch: arch,
platform: platform,
);
expect(json.encode(actual), equals(json.encode(expected)));
});
});
group('promotePatch', () {
const patchId = 0;
const channelId = 0;