Open guylando opened 5 years ago
Can you please change the library to target netstandard? Or at least to also target netcore? So that the library will be available for use in net core / net standard projects.
Just rewrite the damn thing. You don't want this code to production. It'll output ApiKey in console log, etc...
Here a dotnetcorized version of their main class, sorry, dropped the credit thingy, don't need.
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using ZeroBounce.Models;
namespace Common.ZeroBounce
{
public class ZeroBounceAPI
{
public string ApiKey { get; set; } = "";
public string IpAddress { get; set; } = "";
public string EmailToValidate { get; set; } = "";
public int RequestTimeOut { get; set; } = 100000;
public int ReadTimeOut { get; set; } = 100000;
public async Task<ZeroBounceResultsModel> ValidateEmail()
{
var oResults = new ZeroBounceResultsModel();
try
{
const string apiBaseAddress = "https://api.zerobounce.net/v2/validate";
var httpClient = new HttpClient { Timeout = TimeSpan.FromMilliseconds(ReadTimeOut), BaseAddress = new Uri(apiBaseAddress) };
using var response = await httpClient.GetAsync("?api_key=" + ApiKey + "&email=" + WebUtility.UrlEncode(EmailToValidate) + "&ip_address=" + WebUtility.UrlEncode(IpAddress));
string responseString = await response.Content.ReadAsStringAsync();
oResults = JsonConvert.DeserializeObject<ZeroBounceResultsModel>(responseString);
}
catch (Exception exception)
{
oResults.sub_status = exception.Message.Contains("The operation has timed out") ? "timeout_exceeded" : "exception_occurred";
oResults.status = "unknown";
oResults.domain = EmailToValidate.Substring(EmailToValidate.IndexOf("@", StringComparison.Ordinal) + 1).ToLower();
oResults.account = EmailToValidate.Substring(0, EmailToValidate.IndexOf("@", StringComparison.Ordinal)).ToLower();
oResults.address = EmailToValidate.ToLower();
oResults.error = exception.Message;
}
return oResults;
}
}
}
There is an incremental improvement and made it for AzureFunction support as well as restricted the way you fill up data.
Usage :
var zeroBounce = new ZeroBounceAPI(); // can give timeout in miliseconds
var zeroBounceResult = await zeroBounce.ValidateEmailAsync(contact.Email); // can also provide ipAddress as secondary param
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Common.ZeroBounce.Models;
using Newtonsoft.Json;
namespace Common.ZeroBounce
{
public class ZeroBounceAPI
{
public ZeroBounceAPI(int requestTimeOut = 100000)
{
_requestTimeOut = requestTimeOut;
}
private const string ZeroBounceApiKeyName = "ZEROBOUNCE_APIKEY";
private readonly int _requestTimeOut;
public async Task<ZeroBounceResultsModel> ValidateEmailAsync(string emailToValidate, string ipAddress = "")
{
var zeroBounceResult = new ZeroBounceResultsModel();
try
{
const string apiBaseAddress = "https://api.zerobounce.net/v2/validate";
var httpClient = new HttpClient { Timeout = TimeSpan.FromMilliseconds(_requestTimeOut), BaseAddress = new Uri(apiBaseAddress) };
string zeroBounceApiKey = Environment.GetEnvironmentVariable(ZeroBounceApiKeyName, EnvironmentVariableTarget.Process);
if (zeroBounceApiKey == null)
{
string errorMessage = $"{ZeroBounceApiKeyName} is not found in environment variables.";
throw new ArgumentException(errorMessage);
}
using var response = await httpClient.GetAsync("?api_key=" + zeroBounceApiKey + "&email=" + WebUtility.UrlEncode(emailToValidate) + "&ip_address=" + WebUtility.UrlEncode(ipAddress));
string responseString = await response.Content.ReadAsStringAsync();
zeroBounceResult = JsonConvert.DeserializeObject<ZeroBounceResultsModel>(responseString);
}
catch (Exception exception)
{
zeroBounceResult.Sub_status = exception.Message.Contains("The operation has timed out") ? "timeout_exceeded" : "exception_occurred";
zeroBounceResult.Status = "unknown";
zeroBounceResult.Domain = emailToValidate.Substring(emailToValidate.IndexOf("@", StringComparison.Ordinal) + 1).ToLower();
zeroBounceResult.Account = emailToValidate.Substring(0, emailToValidate.IndexOf("@", StringComparison.Ordinal)).ToLower();
zeroBounceResult.Address = emailToValidate.ToLower();
zeroBounceResult.Error = exception.Message;
}
return zeroBounceResult;
}
}
}
Can you please change the library to target netstandard? Or at least to also target netcore? So that the library will be available for use in net core / net standard projects.