Skip to content

Commit

Permalink
refactor(code_push_client): create http wrapper to standardize headers (
Browse files Browse the repository at this point in the history
  • Loading branch information
felangel authored Jun 14, 2023
1 parent 5baa22c commit 1450ae2
Show file tree
Hide file tree
Showing 2 changed files with 688 additions and 565 deletions.
45 changes: 22 additions & 23 deletions packages/shorebird_code_push_client/lib/src/code_push_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,26 @@ class CodePushNotFoundException extends CodePushException {
CodePushNotFoundException({required super.message, super.details});
}

/// A wrapper around [http.Client] that ensures all outbound requests
/// are consistent.
/// For example, all requests include the standard `x-version` header.
class _CodePushHttpClient extends http.BaseClient {
_CodePushHttpClient(this._client);

final http.Client _client;

@override
Future<http.StreamedResponse> send(http.BaseRequest request) {
return _client.send(request..headers.addAll(CodePushClient.headers));
}

@override
void close() {
_client.close();
super.close();
}
}

/// {@template code_push_client}
/// Dart client for the Shorebird CodePush API.
/// {@endtemplate}
Expand All @@ -46,7 +66,7 @@ class CodePushClient {
CodePushClient({
http.Client? httpClient,
Uri? hostedUri,
}) : _httpClient = httpClient ?? http.Client(),
}) : _httpClient = _CodePushHttpClient(httpClient ?? http.Client()),
hostedUri = hostedUri ?? Uri.https('api.shorebird.dev');

/// The standard headers applied to all requests.
Expand All @@ -70,7 +90,6 @@ class CodePushClient {
}) async {
final response = await _httpClient.post(
Uri.parse('$_v1/apps/$appId/collaborators'),
headers: headers,
body: json.encode(CreateAppCollaboratorRequest(email: email).toJson()),
);

Expand Down Expand Up @@ -107,7 +126,6 @@ class CodePushClient {
Uri.parse('$_v1/patches/$patchId/artifacts'),
);
final file = await http.MultipartFile.fromPath('file', artifactPath);
request.headers.addAll(headers);
request.fields.addAll({
'arch': arch,
'platform': platform,
Expand Down Expand Up @@ -141,7 +159,6 @@ class CodePushClient {
Future<Uri> createPaymentLink() async {
final response = await _httpClient.post(
Uri.parse('$_v1/subscriptions/payment_link'),
headers: headers,
);

if (response.statusCode != HttpStatus.ok) {
Expand All @@ -166,7 +183,6 @@ class CodePushClient {
Uri.parse('$_v1/releases/$releaseId/artifacts'),
);
final file = await http.MultipartFile.fromPath('file', artifactPath);
request.headers.addAll(headers);
request.fields.addAll({
'arch': arch,
'platform': platform,
Expand Down Expand Up @@ -201,7 +217,6 @@ class CodePushClient {
Future<App> createApp({required String displayName}) async {
final response = await _httpClient.post(
Uri.parse('$_v1/apps'),
headers: headers,
body: json.encode({'display_name': displayName}),
);

Expand All @@ -219,7 +234,6 @@ class CodePushClient {
}) async {
final response = await _httpClient.post(
Uri.parse('$_v1/channels'),
headers: headers,
body: json.encode({'app_id': appId, 'channel': channel}),
);

Expand All @@ -234,7 +248,6 @@ class CodePushClient {
Future<Patch> createPatch({required int releaseId}) async {
final response = await _httpClient.post(
Uri.parse('$_v1/patches'),
headers: headers,
body: json.encode({'release_id': releaseId}),
);

Expand All @@ -255,7 +268,6 @@ class CodePushClient {
}) async {
final response = await _httpClient.post(
Uri.parse('$_v1/releases'),
headers: headers,
body: json.encode({
'app_id': appId,
'version': version,
Expand All @@ -278,7 +290,6 @@ class CodePushClient {
}) async {
final response = await _httpClient.delete(
Uri.parse('$_v1/apps/$appId/collaborators/$userId'),
headers: headers,
);

if (response.statusCode != HttpStatus.noContent) {
Expand All @@ -290,7 +301,6 @@ class CodePushClient {
Future<void> deleteRelease({required int releaseId}) async {
final response = await _httpClient.delete(
Uri.parse('$_v1/releases/$releaseId'),
headers: headers,
);

if (response.statusCode != HttpStatus.noContent) {
Expand All @@ -306,7 +316,6 @@ class CodePushClient {
}) async {
final response = await _httpClient.post(
Uri.parse('$_v1/users'),
headers: headers,
body: jsonEncode(CreateUserRequest(name: name).toJson()),
);

Expand All @@ -322,7 +331,6 @@ class CodePushClient {
Future<void> deleteApp({required String appId}) async {
final response = await _httpClient.delete(
Uri.parse('$_v1/apps/$appId'),
headers: headers,
);

if (response.statusCode != HttpStatus.noContent) {
Expand All @@ -334,7 +342,6 @@ class CodePushClient {
Future<List<AppMetadata>> getApps() async {
final response = await _httpClient.get(
Uri.parse('$_v1/apps'),
headers: headers,
);

if (response.statusCode != HttpStatus.ok) {
Expand All @@ -353,7 +360,6 @@ class CodePushClient {
Uri.parse('$_v1/channels').replace(
queryParameters: {'appId': appId},
),
headers: headers,
);

if (response.statusCode != HttpStatus.ok) {
Expand All @@ -370,7 +376,6 @@ class CodePushClient {
Future<List<Collaborator>> getCollaborators({required String appId}) async {
final response = await _httpClient.get(
Uri.parse('$_v1/apps/$appId/collaborators'),
headers: headers,
);

if (response.statusCode != HttpStatus.ok) {
Expand All @@ -391,7 +396,6 @@ class CodePushClient {
Uri.parse('$_v1/releases').replace(
queryParameters: {'appId': appId},
),
headers: headers,
);

if (response.statusCode != HttpStatus.ok) {
Expand All @@ -417,7 +421,6 @@ class CodePushClient {
'platform': platform,
},
),
headers: headers,
);

if (response.statusCode != HttpStatus.ok) {
Expand All @@ -435,7 +438,6 @@ class CodePushClient {
}) async {
final response = await _httpClient.post(
Uri.parse('$_v1/patches/promote'),
headers: headers,
body: json.encode({'patch_id': patchId, 'channel_id': channelId}),
);

Expand All @@ -446,10 +448,7 @@ class CodePushClient {

/// Cancels the current user's subscription.
Future<DateTime> cancelSubscription() async {
final response = await _httpClient.delete(
Uri.parse('$_v1/subscriptions'),
headers: headers,
);
final response = await _httpClient.delete(Uri.parse('$_v1/subscriptions'));

if (response.statusCode != HttpStatus.ok) {
throw _parseErrorResponse(response.statusCode, response.body);
Expand Down
Loading

0 comments on commit 1450ae2

Please sign in to comment.