getPublisher method Null safety

Future<MillicastDirectorResponse> getPublisher(
  1. DirectorPublisherOptions options
)

Get publisher connection data.

options - Millicast options. Returns Future object which represents the result of getting the publishing connection path.

import 'package:millicast_flutter_sdk/millicast_flutter_sdk.dart';
void main() async {
  tokenGenerator() => Director.getPublisher(DirectorPublisherOptions(
      token: 'Valid Token', streamName: 'Valid Token Name'));
  MillicastDirectorResponse token = await tokenGenerator();
  // Use token
  print(token);
}

Implementation

static Future<MillicastDirectorResponse> getPublisher(
    DirectorPublisherOptions options) async {
  http.Client client = options.client ?? http.Client();
  _logger.i('Getting publisher connection path for stream name: ' +
      options.streamName);
  Map<String, dynamic> payload = {
    'streamName': options.streamName,
    'streamType': options.streamType ?? StreamTypes.webRTC
  };
  var url = Uri.parse('${getEndpoint()}/api/director/publish');
  Map<String, String> headers = {
    HttpHeaders.contentTypeHeader: 'application/json',
    HttpHeaders.authorizationHeader: 'Bearer ' + options.token
  };
  try {
    http.Response response = await client.post(
      url,
      headers: headers,
      body: jsonEncode(payload),
    );
    _logger.i(response.body);
    final Map<String, dynamic> responseBody =
        jsonDecode(response.body)['data'];
    MillicastDirectorResponse data =
        MillicastDirectorResponse.fromJson(responseBody);
    parseIncomingDirectorResponse(data);
    _logger.d('Getting publisher response:' + response.body);
    return data;
  } catch (e) {
    _logger
        .e('Error while getting publisher connection path:' + e.toString());
    throw Exception(e);
  } finally {
    client.close();
  }
}