send method

  1. @override
Future<Iterable<Response>> send(
  1. Iterable<PhoneNumber> to,
  2. Message message,
  3. Client client
)
override

Convert a list of phone numbers and a message to a list of requests

Implementation

@override
Future<Iterable<Response>> send(
    Iterable<PhoneNumber> to, Message message, http.Client client) async {
  final json = await generateJsonBody(to, message);
  final body = convert.json.encode(json);
  final headers = await generateHeaders(body);

  final request = http.Request(_method, _endpoint)
    ..body = body
    ..headers.addAll(headers);
  final streamedResponse = await client.send(request);
  final httpResponse = await http.Response.fromStream(streamedResponse);

  final results = convert.json.decode(httpResponse.body);
  final response = results['Response'] as Map;

  // If contains `Error` returns all phone numbers failed.
  if (response.containsKey('Error')) {
    return to.map((e) => Response(
        gateway: this, to: e, success: false, response: httpResponse));
  }

  final sendStatusSet = (response['SendStatusSet'] as List).cast<Map>();

  return sendStatusSet.map((e) {
    final phoneNumber = to.firstWhere(
        (element) => generatePhoneNumber(element) == e['PhoneNumber']);

    return Response(
      gateway: this,
      to: phoneNumber,
      success: e['Code'] == 'Ok',
      response: httpResponse,
    );
  });
}