Skip to content

Commit

Permalink
add 'format with template' functionality, update JSON lib
Browse files Browse the repository at this point in the history
  • Loading branch information
JW Wesson committed Jul 10, 2024
1 parent a1ac371 commit 6d54417
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 11 deletions.
16 changes: 16 additions & 0 deletions src/Textkernel.Tx.SDK.Tests/IntegrationTests/FormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,21 @@ public void TestResumeGeneration()
Assert.NotNull(response?.Value?.DocumentAsBase64String);
Assert.IsNotEmpty(response?.Value?.DocumentAsBase64String);
}

[Test]
public void TestResumeGenerationWithTemplate()
{
FormatResumeWithTemplateRequest request = new FormatResumeWithTemplateRequest(TestParsedResume, "TestData/template1.docx", ResumeType.DOCX);
FormatResumeResponse response = null;

Assert.DoesNotThrowAsync(async () => { response = await Client.FormatResumeWithTemplate(request); });
Assert.NotNull(response?.Value?.DocumentAsBase64String);
Assert.IsNotEmpty(response?.Value?.DocumentAsBase64String);

request.OutputType = ResumeType.PDF;
Assert.DoesNotThrowAsync(async () => { response = await Client.FormatResumeWithTemplate(request); });
Assert.NotNull(response?.Value?.DocumentAsBase64String);
Assert.IsNotEmpty(response?.Value?.DocumentAsBase64String);
}
}
}
Binary file not shown.
4 changes: 4 additions & 0 deletions src/Textkernel.Tx.SDK.Tests/Textkernel.Tx.SDK.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@

<ItemGroup>
<None Remove="TestData\resume.docx" />
<None Remove="TestData\template1.docx" />
</ItemGroup>

<ItemGroup>
<Content Include="TestData\resume.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="TestData\template1.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
Expand Down
3 changes: 2 additions & 1 deletion src/Textkernel.Tx.SDK/ApiEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ private string Sanitize(string indexOrDocId)
internal HttpRequestMessage GetAccountInfo() => new HttpRequestMessage(HttpMethod.Get, $"{Prefix()}account");

internal HttpRequestMessage FormatResume() => new HttpRequestMessage(HttpMethod.Post, $"{Prefix()}formatter/resume");

internal HttpRequestMessage FormatResumeWithTemplate() => new HttpRequestMessage(HttpMethod.Post, $"{Prefix()}formatter/resume/template");

internal HttpRequestMessage CreateIndex(string id) => new HttpRequestMessage(HttpMethod.Post, $"{Prefix()}index/{Sanitize(id)}");
internal HttpRequestMessage GetIndexDocumentCount(string id) => new HttpRequestMessage(HttpMethod.Get, $"{Prefix()}index/{Sanitize(id)}/count");
internal HttpRequestMessage DeleteIndex(string id) => new HttpRequestMessage(HttpMethod.Delete, $"{Prefix()}index/{Sanitize(id)}");
Expand Down
12 changes: 12 additions & 0 deletions src/Textkernel.Tx.SDK/ITxClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public interface ITxClient
/// <exception cref="TxException">Thrown when an API error occurs</exception>
Task<GetAccountInfoResponse> GetAccountInfo();

#region Formatter

/// <summary>
/// Format a parsed resume into a standardized/templated resume
/// </summary>
Expand All @@ -45,6 +47,16 @@ public interface ITxClient
/// <exception cref="TxException">Thrown when an API error occurred</exception>
Task<FormatResumeResponse> FormatResume(FormatResumeRequest request);

/// <summary>
/// Format a parsed resume into a template that you provide
/// </summary>
/// <param name="request">The request body</param>
/// <returns>The formatted resume document</returns>
/// <exception cref="TxException">Thrown when an API error occurred</exception>
Task<FormatResumeResponse> FormatResumeWithTemplate(FormatResumeWithTemplateRequest request);

#endregion

#region Parsing

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

using Textkernel.Tx.Models.Resume;
using Textkernel.Tx.Models.API.Parsing;
using System.Text.Json.Serialization;
using System;

namespace Textkernel.Tx.Models.API.Formatter
{
Expand All @@ -30,12 +32,74 @@ public class FormatResumeRequest
/// <param name="resume">
/// Either<see cref="ParseResumeResponseValue.ResumeData"/> to include the candidate's personal information or
/// <see cref="ParseResumeResponseValue.RedactedResumeData"/> to exclude it.
/// </param>
/// <param name="docType">The output document type</param>
/// </param>
public FormatResumeRequest(ParsedResume resume, ResumeType docType)
{
ResumeData = resume;
Options.OutputType = docType;
}
}

/// <summary>
/// Request body for the Format Resume With Template endpoint
/// </summary>
public class FormatResumeWithTemplateRequest
{
/// <summary>
/// The <see cref="ParseResumeResponseValue.ResumeData"/> from a parse API call
/// </summary>
public ParsedResume ResumeData { get; set; }

/// <summary>
/// The output document type
/// </summary>
[JsonConverter(typeof(JsonStringEnumConverter))]
public ResumeType OutputType { get; set; }

/// <summary>
/// A base64-encoded string of the DOCX template document file bytes. This should use the standard 'base64'
/// encoding as defined in RFC 4648 Section 4 (not the 'base64url' variant). .NET users can use the
/// <see cref="Convert.ToBase64String(byte[])"/> method. For more information on creating custom templates,
/// see <see href="https://developer.textkernel.com/tx-platform/v10/resume-formatter/creating-custom-templates/">here</see>.
/// </summary>
public string Template { get; set; }

/// <summary>
/// Any data that the template needs that is not in the extracted CV data. For example:
/// <code>
/// {
/// "CandidateId": "12345",
/// "DateApplied": "2024-02-05"
/// }
/// </code>
/// </summary>
public object CustomData { get; set; }

/// <summary>
/// Creates a request to call the Resume Formatter endpoint with a provided template document.
/// </summary>
/// <param name="resume">The <see cref="ParseResumeResponseValue.ResumeData"/> from a parse API call</param>
/// <param name="docType">The output document type</param>
/// <param name="templatePath">The path to the template DOCX file on disk</param>
public FormatResumeWithTemplateRequest(ParsedResume resume, string templatePath, ResumeType docType)
{
ResumeData = resume;
OutputType = docType;
Template = new Document(templatePath).AsBase64;
}

/// <summary>
/// Creates a request to call the Resume Formatter endpoint with a provided template document.
/// </summary>
/// <param name="resume">The <see cref="ParseResumeResponseValue.ResumeData"/> from a parse API call</param>
/// <param name="docType">The output document type</param>
/// <param name="templateFileBytes">The bytes of the template DOCX file</param>
public FormatResumeWithTemplateRequest(ParsedResume resume, byte[] templateFileBytes, ResumeType docType)
{
ResumeData = resume;
OutputType = docType;
Template = new Document(templateFileBytes, DateTime.Today).AsBase64;
}
}
}
4 changes: 2 additions & 2 deletions src/Textkernel.Tx.SDK/Textkernel.Tx.SDK.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<Copyright>Copyright © $([System.DateTime]::UtcNow.Year) Textkernel BV. All rights reserved.</Copyright>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/textkernel/tx-dotnet</PackageProjectUrl>
<Version>3.2.1</Version>
<Version>3.3.0</Version>
<PackageIcon>images\icon.png</PackageIcon>
<PackageIconUrl>https://raw.githubusercontent.com/textkernel/tx-dotnet/master/src/Textkernel.Tx.SDK/icon.png</PackageIconUrl>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
Expand All @@ -30,7 +30,7 @@

<ItemGroup>

<PackageReference Include="System.Text.Json" Version="4.7.2" />
<PackageReference Include="System.Text.Json" Version="8.0.4" />
</ItemGroup>

<ItemGroup>
Expand Down
21 changes: 15 additions & 6 deletions src/Textkernel.Tx.SDK/TxClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,9 @@ public async Task<GetAccountInfoResponse> GetAccountInfo()
return await ProcessResponse<GetAccountInfoResponse>(response, await GetBodyIfDebug(apiRequest));
}

/// <summary>
/// Format a parsed resume into a standardized/templated resume
/// </summary>
/// <param name="request">The request body</param>
/// <returns>The formatted resume document</returns>
/// <exception cref="TxException">Thrown when an API error occurred</exception>
#region Formatter

/// <inheritdoc />
public async Task<FormatResumeResponse> FormatResume(FormatResumeRequest request)
{
HttpRequestMessage apiRequest = _endpoints.FormatResume();
Expand All @@ -265,6 +262,18 @@ public async Task<FormatResumeResponse> FormatResume(FormatResumeRequest request
return await DeserializeBody<FormatResumeResponse>(response);
}

/// <inheritdoc />
public async Task<FormatResumeResponse> FormatResumeWithTemplate(FormatResumeWithTemplateRequest request)
{
HttpRequestMessage apiRequest = _endpoints.FormatResumeWithTemplate();
apiRequest.AddJsonBody(request);
HttpResponseMessage response = await _httpClient.SendAsync(apiRequest);

return await DeserializeBody<FormatResumeResponse>(response);
}

#endregion

#region Parsing

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Textkernel.Tx.SDK/Utilities/TxJsonSerialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal static JsonSerializerOptions DefaultOptions
JsonSerializerOptions options = new JsonSerializerOptions
{
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
IgnoreNullValues = true
DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull
};
options.Converters.Add(new DateTimeConverter());
options.Converters.Add(new IntConverter());
Expand Down

0 comments on commit 6d54417

Please sign in to comment.