publish method

Future<void> publish({
  1. required List<int> bundle,
})

Publish universal bundle to remote registry.

Implementation

Future<void> publish({required List<int> bundle}) async {
  var credentials = _credentials;

  if (credentials == null) {
    throw const MasonApiPublishFailure(
      message:
          '''User not found. Please make sure you are logged in and try again.''',
    );
  }

  if (credentials.areExpired) {
    try {
      credentials = await _refresh();
    } on MasonApiRefreshFailure catch (error) {
      throw MasonApiPublishFailure(
        message: 'Refresh failure: ${error.message}',
      );
    }
  }

  final http.Response response;
  try {
    response = await _httpClient.post(
      Uri.parse('$_hostedUri/api/v1/bricks'),
      headers: {
        'Authorization':
            '${credentials.tokenType} ${credentials.accessToken}',
        'Content-Type': 'application/octet-stream',
      },
      body: bundle,
    );
  } catch (error) {
    throw MasonApiPublishFailure(message: '$error');
  }

  if (response.statusCode != HttpStatus.created) {
    final ErrorResponse error;
    try {
      final body = json.decode(response.body) as Map<String, dynamic>;
      error = ErrorResponse.fromJson(body);
    } catch (_) {
      throw const MasonApiPublishFailure(message: _unknownErrorMessage);
    }
    throw MasonApiPublishFailure(
      message: error.message,
      details: error.details,
    );
  }
}