Skip to content

Commit

Permalink
Add Document upload with Field extract example
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexNDRmac committed Apr 8, 2020
1 parent 569355e commit 85fb99b
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
28 changes: 28 additions & 0 deletions SignNow.Net.Examples/Documents/UploadDocumentWithFieldExtract.cs
@@ -0,0 +1,28 @@
using System.IO;
using System.Threading.Tasks;
using SignNow.Net.Model;

namespace SignNow.Net.Examples.Documents
{
public static class DocumentExamples
{
/// <summary>
/// Uploads a PDF with fillable field (Signature field)
/// </summary>
/// <param name="pdfFilePath">Full qualified path to your PDF with field tags.</param>
/// <param name="token">Access token</param>
public static async Task<SignNowDocument> UploadDocumentWithFieldExtract(string pdfFilePath, Token token)
{
var signNowContext = new SignNowContext(token);

await using var fileStream = File.OpenRead(pdfFilePath);
// Upload the document with field extract
var uploadResponse = signNowContext.Documents
.UploadDocumentWithFieldExtractAsync(fileStream, "DocumentSampleWithSignatureTextTag.pdf").Result;

var documentId = uploadResponse.Id;

return await signNowContext.Documents.GetDocumentAsync(documentId);
}
}
}
57 changes: 57 additions & 0 deletions SignNow.Net.Examples/ExamplesRunner.cs
@@ -1,15 +1,34 @@
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SignNow.Net.Examples.Authentication;
using SignNow.Net.Examples.Documents;
using SignNow.Net.Model;
using SignNow.Net.Test.Context;

namespace SignNow.Net.Examples
{
[TestClass]
public class ExamplesRunner
{
private readonly string baseTestExamplesPath = "../../../TestExamples/".Replace('/', Path.DirectorySeparatorChar);

private static CredentialModel _clientInfo, _userCredentials;

/// <summary>Token for ExampleRunner</summary>
private readonly Token token;

/// <summary>
/// SignNow service container used for ExampleRunner
/// </summary>
private SignNowContext testContext;

/// <summary>
/// Document Id which should be deleted after each test
/// </summary>
private string disposableDocumentId;

/// <summary>
/// SignNow API base Url (sandbox)
/// </summary>
Expand All @@ -26,6 +45,27 @@ public ExamplesRunner()
_clientInfo = new CredentialLoader(ApiBaseUrl).GetCredentials();
// Contains user Email and Password
_userCredentials = new CredentialLoader(ApplicationBaseUrl).GetCredentials();
// Token for test runner
token = AuthenticationExamples.RequestAccessToken(ApiBaseUrl, _clientInfo, _userCredentials).Result;
testContext = new SignNowContext(ApiBaseUrl, token);
}

[TestCleanup]
public void TearDown()
{
if (string.IsNullOrEmpty(disposableDocumentId))
{
return;
}

var documentTask = testContext.Documents
.DeleteDocumentAsync(disposableDocumentId);

Task.WaitAll(documentTask);

Assert.IsFalse(documentTask.IsFaulted);

disposableDocumentId = string.Empty;
}

[TestMethod]
Expand All @@ -37,5 +77,22 @@ public void RequestAccessTokenTest()
Assert.IsFalse(string.IsNullOrEmpty(requestAccessToken.AccessToken));
Assert.IsFalse(string.IsNullOrEmpty(requestAccessToken.RefreshToken));
}

[TestMethod]
public void UploadDocumentWithFieldExtractTest()
{
var pdfWithTags = Path.Combine(baseTestExamplesPath, "DocumentWithSignatureFieldTag.pdf");;

var documentWithFields = DocumentExamples
.UploadDocumentWithFieldExtract(pdfWithTags, token).Result;

disposableDocumentId = documentWithFields?.Id;

using var documentFields = documentWithFields.Fields.GetEnumerator();
documentFields.MoveNext();

Assert.AreEqual(FieldType.Text, documentFields.Current.Type);
Assert.IsTrue(documentWithFields.Fields.Count > 0);
}
}
}
Binary file not shown.

0 comments on commit 85fb99b

Please sign in to comment.