send method

Future<void> send(
  1. List<Message> messages
)

Send a list of messages. Typically you only pass one message, but all messages provided here are sent to SocketLabs in one API call, so if you know that you're going to send them, it's more efficient to do it with one call.

Implementation

Future<void> send(List<Message> messages) async {
  _log.finest('Sending email.');

  final body = {
    'ServerId': serverId,
    'ApiKey': apiKey,
    'Messages': messages..map((message) => message.toJson()).toList(),
  };
  final response = await httpClient.post(
    apiUrl.replace(
        pathSegments: List.from(apiUrl.pathSegments)..add('email')),
    headers: {'Content-Type': 'application/json'},
    body: jsonEncode(body),
  );

  try {
    final responseJson = jsonDecode(response.body);
    if (responseJson['ErrorCode'] != 'Success') {
      throw SocketLabsException(responseJson['ErrorCode'], response.body);
    }
  } catch (e) {
    _log.warning('Error sending email: $e');
    _log.info(response.body);
    if (e is SocketLabsException) rethrow;
    throw SocketLabsException('InvalidResponse', response.body);
  }
}