Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Allow sending file with a Stream instead of filePath #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 44 additions & 5 deletions ZeroBounceSDK/ZeroBounce.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,27 @@ public class SendFileOptions
public void ScoringSendFile(string filePath, SendFileOptions options,
Action<ZBSendFileResponse> successCallback, Action<string> failureCallback)
{
_SendFile(true, filePath, options, successCallback, failureCallback);
try
{
var fileName = Path.GetFileName(filePath);
var file = File.OpenRead(filePath);
_SendFile(true, file, fileName, options, successCallback, failureCallback);
}
catch (Exception e)
{
failureCallback(e.Message);
}
}

/// <summary>
/// The scoringSendfile API allows user to send a file for bulk email validation
/// <param name="successCallback"> The success callback function, called with a ZBSendFileResponse object</param>
/// <param name="failureCallback"> The failure callback function, called with a string error message</param>
/// </summary>
public void ScoringSendFile(Stream file, string fileName, SendFileOptions options,
Action<ZBSendFileResponse> successCallback, Action<string> failureCallback)
{
_SendFile(true, file, fileName, options, successCallback, failureCallback);
}

/// <summary>
Expand All @@ -166,19 +186,38 @@ public void ScoringSendFile(string filePath, SendFileOptions options,
public void SendFile(string filePath, SendFileOptions options,
Action<ZBSendFileResponse> successCallback, Action<string> failureCallback)
{
_SendFile(false, filePath, options, successCallback, failureCallback);
try
{
var fileName = Path.GetFileName(filePath);
var file = File.OpenRead(filePath);
_SendFile(false, file, fileName, options, successCallback, failureCallback);
}
catch (Exception e)
{
failureCallback(e.Message);
}
}

private async void _SendFile(bool scoring, string filePath, SendFileOptions options,
/// <summary>
/// The sendfile API allows user to send a file for bulk email validation
/// <param name="successCallback"> The success callback function, called with a ZBSendFileResponse object</param>
/// <param name="failureCallback"> The failure callback function, called with a string error message</param>
/// </summary>
public void SendFile(Stream file, string fileName, SendFileOptions options,
Action<ZBSendFileResponse> successCallback, Action<string> failureCallback)
{
_SendFile(false, file, fileName, options, successCallback, failureCallback);
}

private async void _SendFile(bool scoring, Stream file, string fileName, SendFileOptions options,
Action<ZBSendFileResponse> successCallback, Action<string> failureCallback)
{
if (InvalidApiKey(failureCallback)) return;

try
{
var content = new MultipartFormDataContent();
var file = File.OpenRead(filePath);
content.Add(new StreamContent(file), "file", Path.GetFileName(filePath));
content.Add(new StreamContent(file), "file", fileName);

content.Add(new StringContent(_apiKey), "api_key");

Expand Down