addDomain method

Create a domain through the Resend Email API.

Implementation

Future<ResendResult<ResendAddDomainResponse>> addDomain(

    /// The name of the domain you want to create.
    String name,
    {
    /// The region where emails will be sent from. Possible values: us-east-1' | 'eu-west-1' | 'sa-east-1' | 'ap-northeast-1'
    ResendDomainRegion? region}) async {
  // Validation
  assert(name.isNotEmpty, 'The domain name can not be empty.');

  // Construct the request URI
  final Uri uri =
      Uri(scheme: _baseUri.scheme, host: _baseUri.host, path: _baseUri.path);

  // Send GET request to the API
  final http.Response response = await http.post(uri,
      headers: <String, String>{
        'Authorization': 'Bearer $_apiKey',
        'Content-Type': 'application/json'
      },
      body: jsonEncode(<String, dynamic>{
        'name': name,
        if (region != null) 'region': region.id
      }));

  // Decode the response
  final Json body = json.decode(response.body);

  // Return Failure when statusCode is not OK
  if (response.statusCode != 200) {
    return ResendFailure.fromJson(body);
  }

  // Return parsed data when statusCode is OK
  final ResendAddDomainResponse result =
      ResendAddDomainResponse.fromJson(body);
  return ResendResult<ResendAddDomainResponse>.success(result);
}