Skip to content

Commit 0b8090f

Browse files
authored
Merge pull request #118 from signnow/create-doc-from-template
Create document from template
2 parents 2156807 + a3adbc6 commit 0b8090f

File tree

10 files changed

+173
-32
lines changed

10 files changed

+173
-32
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org).
88

99
## Added
1010
- `IDocumentService.CreateTemplateFromDocumentAsync` that allows to create template by flattening an existing document
11+
- `IDocumentService.CreateDocumentFromTemplateAsync` that allows to create document from the template
1112

1213

1314
## [0.7.0] - 2021-03-28

README.md

100755100644
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Get your account at <https://www.signnow.com/developers>
4545
- [Check the status of the document][check_sign_status example]
4646
- [Template](#template)
4747
- [Create a template by flattening an existing document](#create-template)
48+
- [Create document from the template][create_document example]
4849
6. [Contribution guidelines](#contribution-guidelines)
4950
- [XML doc generation](#xml-doc-generation)
5051
- [Important notes](#important-notes)
@@ -511,7 +512,7 @@ More examples: [Get document history][document_history example]
511512
## <a name="template"></a>Template
512513
### <a name="create-template"></a>Create Template by flattening the existing Document
513514

514-
Set required TemplateName and DocumentId properties in request to create the SignNow Template.
515+
Set required templateName and documentId parameters to create the SignNow Template.
515516

516517
```csharp
517518
public static class DocumentExamples
@@ -536,7 +537,7 @@ public static class DocumentExamples
536537
}
537538
```
538539

539-
More examples: [Create a template by flattening an existing document][create_template example]
540+
More examples: [Create a template by flattening an existing document][create_template example], [Create document from the template][create_document example]
540541

541542

542543
## <a name="contribution-guidelines"></a>Contribution guidelines
@@ -621,3 +622,4 @@ If you have questions about the SignNow API, please visit [SignNow API Reference
621622

622623
<!-- Templates -->
623624
[create_template example]: https://github.com/signnow/SignNow.NET/blob/develop/SignNow.Net.Examples/Documents/CreateTemplateFromTheDocument.cs
625+
[create_document example]: https://github.com/signnow/SignNow.NET/blob/develop/SignNow.Net.Examples/Documents/CreateDocumentFromTheTemplate.cs
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.Threading.Tasks;
2+
using SignNow.Net.Model;
3+
using SignNow.Net.Model.Responses;
4+
5+
namespace SignNow.Net.Examples.Documents
6+
{
7+
public static partial class DocumentExamples
8+
{
9+
/// <summary>
10+
/// Creates document from template.
11+
/// </summary>
12+
/// <param name="templateId">Identity of the template</param>
13+
/// <param name="documentName">The name of new document</param>
14+
/// <param name="token">Access token</param>
15+
/// <returns><see cref="CreateDocumentFromTemplateResponse"/>New document ID</returns>
16+
public static async Task<CreateDocumentFromTemplateResponse> CreateDocumentFromTheTemplate(string documentName, string templateId, Token token)
17+
{
18+
// using token from the Authorization step
19+
var signNowContext = new SignNowContext(token);
20+
21+
return await signNowContext.Documents
22+
.CreateDocumentFromTemplateAsync(documentName, templateId)
23+
.ConfigureAwait(false);
24+
}
25+
}
26+
}

SignNow.Net.Examples/ExamplesRunner.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,29 @@ public async Task CreateTemplateFromDocumentTest()
413413
await testContext.Documents.DeleteDocumentAsync(template.Id);
414414
}
415415

416+
/// <summary>
417+
/// Run test for example: <see cref="DocumentExamples.CreateDocumentFromTheTemplate"/>
418+
/// </summary>
419+
[TestMethod]
420+
public async Task CreateDocumentFromTemplateTest()
421+
{
422+
var testDocumentId = DocumentExamples
423+
.UploadDocumentWithFieldExtract(PdfWithSignatureField, token).Result?.Id;
424+
disposableDocumentId = testDocumentId;
425+
426+
var templateId = (await testContext.Documents.CreateTemplateFromDocumentAsync(testDocumentId, "TemplateName")).Id;
427+
var documentName = "Document Name";
428+
var result = await DocumentExamples.CreateDocumentFromTheTemplate(templateId, documentName, token);
429+
var document = await testContext.Documents.GetDocumentAsync(result.Id);
430+
431+
Assert.IsNotNull(document?.Id);
432+
Assert.IsFalse(document.IsTemplate);
433+
Assert.AreEqual(documentName, document.Name);
434+
435+
await testContext.Documents.DeleteDocumentAsync(document.Id);
436+
await testContext.Documents.DeleteDocumentAsync(templateId);
437+
}
438+
416439
#endregion
417440
}
418441
}

SignNow.Net.Test/AcceptanceTests/DocumentServiceTest.CreateTemplateFromDocument.cs

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System.Threading.Tasks;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using UnitTests;
4+
5+
namespace AcceptanceTests
6+
{
7+
public partial class DocumentServiceTest : AuthorizedApiTestBase
8+
{
9+
[TestMethod]
10+
public async Task CreateDocumentFromTemplateSuccessfully()
11+
{
12+
var createTemplateResult = await SignNowTestContext.Documents
13+
.CreateTemplateFromDocumentAsync(TestPdfDocumentId, "Template Name")
14+
.ConfigureAwait(false);
15+
16+
DisposableDocumentId = createTemplateResult.Id;
17+
18+
var documentName = "test document name";
19+
var result = await SignNowTestContext.Documents
20+
.CreateDocumentFromTemplateAsync(createTemplateResult.Id, documentName)
21+
.ConfigureAwait(false);
22+
23+
Assert.IsNotNull(result?.Id);
24+
25+
var document = await SignNowTestContext.Documents
26+
.GetDocumentAsync(result.Id)
27+
.ConfigureAwait(false);
28+
29+
Assert.IsNotNull(document.Id);
30+
Assert.AreEqual(documentName, document.Name);
31+
Assert.IsFalse(document.IsTemplate);
32+
33+
await SignNowTestContext.Documents.DeleteDocumentAsync(document.Id).ConfigureAwait(false);
34+
}
35+
36+
[TestMethod]
37+
public async Task CreateTemplateFromDocumentSuccessfully()
38+
{
39+
var templateName = "Template Name";
40+
var response = await SignNowTestContext.Documents
41+
.CreateTemplateFromDocumentAsync(TestPdfDocumentId, templateName)
42+
.ConfigureAwait(false);
43+
44+
Assert.IsNotNull(response.Id);
45+
DisposableDocumentId = response.Id;
46+
47+
var template = await SignNowTestContext.Documents
48+
.GetDocumentAsync(response.Id)
49+
.ConfigureAwait(false);
50+
51+
Assert.AreEqual(templateName, template.Name);
52+
Assert.IsTrue(template.IsTemplate);
53+
}
54+
}
55+
}

SignNow.Net/Interfaces/IDocumentService.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,16 @@ public interface IDocumentService
108108
/// <exception cref="System.ArgumentException">If `<paramref name="documentId"/>` is not valid.</exception>
109109
/// <exception cref="System.ArgumentException">If `<paramref name="templateName"/>` is null.</exception>
110110
Task<CreateTemplateFromDocumentResponse> CreateTemplateFromDocumentAsync(string documentId, string templateName, CancellationToken cancellationToken = default);
111+
112+
/// <summary>
113+
/// Creates document from template.
114+
/// </summary>
115+
/// <param name="templateId">Identity of the template which is the source of a document.</param>
116+
/// <param name="documentName">The name of new document</param>
117+
/// <param name="cancellationToken">Propagates notification that operations should be canceled.</param>
118+
/// <returns>Returns identity of new Document</returns>
119+
/// <exception cref="System.ArgumentException">If `<paramref name="templateId"/>` is not valid.</exception>
120+
/// <exception cref="System.ArgumentException">If `<paramref name="documentName"/>` is null.</exception>
121+
Task<CreateDocumentFromTemplateResponse> CreateDocumentFromTemplateAsync(string templateId, string documentName, CancellationToken cancellationToken = default);
111122
}
112123
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Newtonsoft.Json;
2+
3+
namespace SignNow.Net.Model.Responses
4+
{
5+
/// <summary>
6+
/// Represents response from SignNow API for Create Document from Template request.
7+
/// </summary>
8+
public class CreateDocumentFromTemplateResponse
9+
{
10+
/// <summary>
11+
/// Identity of new Document.
12+
/// </summary>
13+
[JsonProperty("id")]
14+
public string Id { get; set; }
15+
}
16+
}

SignNow.Net/Service/DocumentService.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,20 +210,37 @@ public async Task<DownloadLinkResponse> CreateOneTimeDownloadLinkAsync(string do
210210
/// <inheritdoc />
211211
public async Task<CreateTemplateFromDocumentResponse> CreateTemplateFromDocumentAsync(string documentId, string templateName, CancellationToken cancellationToken = default)
212212
{
213-
Guard.ArgumentNotNull(documentId, nameof(documentId));
214213
Guard.ArgumentNotNull(templateName, nameof(templateName));
215214

216215
var requestUrl = new Uri(ApiBaseUrl, $"/template");
217216
var requestOptions = new PostHttpRequestOptions
218217
{
219218
RequestUrl = requestUrl,
220219
Token = Token,
221-
Content = new JsonHttpContent(new CreateTemplateFromDocumentRequest(templateName, documentId)),
220+
Content = new JsonHttpContent(new CreateTemplateFromDocumentRequest(templateName, documentId.ValidateId())),
222221
};
223222

224223
return await SignNowClient
225224
.RequestAsync<CreateTemplateFromDocumentResponse>(requestOptions, cancellationToken)
226225
.ConfigureAwait(false);
227226
}
227+
228+
/// <inheritdoc />
229+
public async Task<CreateDocumentFromTemplateResponse> CreateDocumentFromTemplateAsync(string templateId, string documentName, CancellationToken cancellationToken = default)
230+
{
231+
Guard.ArgumentNotNull(documentName, nameof(documentName));
232+
233+
var requestUrl = new Uri(ApiBaseUrl, $"/template/{templateId.ValidateId()}/copy");
234+
var requestOptions = new PostHttpRequestOptions
235+
{
236+
RequestUrl = requestUrl,
237+
Token = Token,
238+
Content = new JsonHttpContent(new CreateDocumentFromTemplateRequest(documentName))
239+
};
240+
241+
return await SignNowClient
242+
.RequestAsync<CreateDocumentFromTemplateResponse>(requestOptions, cancellationToken)
243+
.ConfigureAwait(false);
244+
}
228245
}
229246
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Newtonsoft.Json;
2+
3+
namespace SignNow.Net.Internal.Requests
4+
{
5+
internal class CreateDocumentFromTemplateRequest
6+
{
7+
/// <summary>
8+
/// The new document name.
9+
/// </summary>
10+
[JsonProperty("document_name")]
11+
public string DocumentName { get; set; }
12+
13+
public CreateDocumentFromTemplateRequest(string documentName)
14+
{
15+
DocumentName = documentName;
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)