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 9ccf2795..91f4c28d 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 @@ -302,6 +302,27 @@ class CodePushClient { return Release.fromJson(body); } + /// Updates the specified release's status to [status]. + Future updateReleaseStatus({ + required int releaseId, + required String platform, + required ReleaseStatus status, + }) async { + final response = await _httpClient.patch( + Uri.parse('$_v1/releases/$releaseId'), + body: json.encode( + UpdateReleaseRequest( + status: status, + platform: platform, + ).toJson(), + ), + ); + + if (response.statusCode != HttpStatus.noContent) { + throw _parseErrorResponse(response.statusCode, response.body); + } + } + /// Remove [userId] as a collaborator from [appId]. Future deleteCollaborator({ required String appId, 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 738cb6cf..fc8b58f5 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 @@ -1136,6 +1136,69 @@ void main() { }); }); + group('updateReleaseStatus', () { + const releaseId = 42; + const platform = 'android'; + + test('makes the correct request', () async { + codePushClient + .updateReleaseStatus( + releaseId: releaseId, + platform: platform, + status: ReleaseStatus.active, + ) + .ignore(); + final request = verify(() => httpClient.send(captureAny())) + .captured + .single as http.BaseRequest; + expect(request.method, equals('PATCH')); + expect(request.url, equals(v1('releases/$releaseId'))); + expect(request.hasStandardHeaders, isTrue); + }); + + test('throws an exception if the response is not a 204', () async { + when(() => httpClient.send(any())).thenAnswer( + (_) async => http.StreamedResponse( + const Stream.empty(), + HttpStatus.badRequest, + ), + ); + + expect( + codePushClient.updateReleaseStatus( + releaseId: releaseId, + platform: platform, + status: ReleaseStatus.active, + ), + throwsA( + isA().having( + (e) => e.message, + 'message', + CodePushClient.unknownErrorMessage, + ), + ), + ); + }); + + test('completes when the server responds with a 204', () async { + when(() => httpClient.send(any())).thenAnswer( + (_) async => http.StreamedResponse( + const Stream.empty(), + HttpStatus.noContent, + ), + ); + + expect( + codePushClient.updateReleaseStatus( + releaseId: releaseId, + platform: platform, + status: ReleaseStatus.active, + ), + completes, + ); + }); + }); + group('deleteCollaborator', () { const appId = 'test-app-id'; const userId = 42;