Skip to content

Commit

Permalink
fix priority syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianMindee committed Nov 19, 2024
1 parent 7eef138 commit 798b656
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 13 deletions.
5 changes: 4 additions & 1 deletion src/Mindee/Http/MindeeApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,10 @@ private static void AddWorkflowRequestParameters(WorkflowParameter workflowParam

if (workflowParameter.Priority != null)
{
request.AddParameter(name: "priority", value: workflowParameter.Priority);
request.AddParameter(
name: "priority",
value: workflowParameter.Priority != null ?
workflowParameter.Priority.ToString()?.ToLower() : null);
}

}
Expand Down
5 changes: 3 additions & 2 deletions src/Mindee/Http/WorkflowParameter.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using Mindee.Input;

namespace Mindee.Http
Expand All @@ -16,7 +17,7 @@ public class WorkflowParameter : GenericParameter
/// <summary>
/// Priority to give to the execution.
/// </summary>
public string Priority { get; }
public ExecutionPriority? Priority { get; }

/// <summary>
/// Workflow parameters.
Expand All @@ -29,7 +30,7 @@ public class WorkflowParameter : GenericParameter
public WorkflowParameter(
LocalInputSource localSource,
UrlInputSource urlSource, bool fullText,
string alias, string priority) : base(localSource, urlSource,
string alias, ExecutionPriority? priority) : base(localSource, urlSource,
fullText)
{
Alias = alias;
Expand Down
60 changes: 60 additions & 0 deletions src/Mindee/Input/ExecutionPriority.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.ComponentModel;
using System.Runtime.Serialization;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Mindee.Input
{
/// <summary>
/// Priority for a workflow execution.
/// </summary>
public enum ExecutionPriority
{
/// <summary>
/// Low priority.
/// </summary>
[EnumMember(Value = "low")] Low,

/// <summary>
/// Medium priority.
/// </summary>
[EnumMember(Value = "medium")] Medium,

/// <summary>
/// Hight priority.
/// </summary>
[EnumMember(Value = "high")] High
}

/// <summary>
/// Deserializer for the ExecutionPriority enum.
/// </summary>
/// <typeparam name="T"></typeparam>
public class StringEnumConverter<T> : JsonConverter<T> where T : struct, Enum
{
/// <summary>
/// Read a JSON value.
/// </summary>
/// <param name="reader"></param>
/// <param name="typeToConvert"></param>
/// <param name="options"></param>
/// <returns></returns>
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
string value = reader.GetString();
return Enum.TryParse<T>(value, true, out var result) ? result : default;
}

/// <summary>
/// Retrieves a JSON value.
/// </summary>
/// <param name="writer"></param>
/// <param name="value"></param>
/// <param name="options"></param>
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString().ToLower());
}
}
}
7 changes: 2 additions & 5 deletions src/Mindee/MindeeClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -623,18 +623,15 @@ public async Task<WorkflowResponse<GeneratedV1>> ExecuteWorkflowAsync(
WorkflowOptions workflowOptions = null,
PageOptions pageOptions = null)
{
_logger?.LogInformation("Workflow enqueing {} ...", inputSource.Filename);
_logger?.LogInformation("Sending '{}' to workflow '{}'...", inputSource.Filename, workflowId);

if (pageOptions != null && inputSource.IsPdf())
{
inputSource.FileBytes = _pdfOperation.Split(
new SplitQuery(inputSource.FileBytes, pageOptions)).File;
}

if (workflowOptions == null)
{
workflowOptions = new WorkflowOptions();
}
workflowOptions ??= new WorkflowOptions();

return await _mindeeApi.PostWorkflowExecution<GeneratedV1>(
workflowId,
Expand Down
5 changes: 3 additions & 2 deletions src/Mindee/MindeeClientOptions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Mindee.Exceptions;
using Mindee.Input;

namespace Mindee
{
Expand Down Expand Up @@ -118,7 +119,7 @@ public sealed class WorkflowOptions
/// <summary>
/// Priority to give to the execution.
/// </summary>
public string Priority { get; }
public ExecutionPriority? Priority { get; }


/// <summary>
Expand All @@ -133,7 +134,7 @@ public sealed class WorkflowOptions
/// <param name="alias"></param>
/// <param name="priority"></param>
/// <param name="fullText"></param>
public WorkflowOptions(string alias = null, string priority = null, bool fullText = false)
public WorkflowOptions(string alias = null, ExecutionPriority? priority = null, bool fullText = false)
{
Alias = alias;
Priority = priority;
Expand Down
4 changes: 3 additions & 1 deletion src/Mindee/Parsing/Common/Execution.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Text.Json.Serialization;
using Mindee.Input;
using Mindee.Product.Generated;

namespace Mindee.Parsing.Common
Expand Down Expand Up @@ -44,7 +45,8 @@ namespace Mindee.Parsing.Common
/// Priority of the execution.
/// </summary>
[JsonPropertyName("priority")]
public string Priority { get; set; }
[JsonConverter(typeof(StringEnumConverter<ExecutionPriority>))]
public ExecutionPriority Priority { get; set; }

/// <summary>
/// The time at which the file was tagged as reviewed.
Expand Down
4 changes: 2 additions & 2 deletions tests/Mindee.UnitTests/Workflow/WorklowTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public async Task SendingADocumentToAnExecutionShouldDeserializeResponseCorrectl
Assert.Equal("default_sample.jpg", response.Execution.File.Name);
Assert.Equal("8c75c035-e083-4e77-ba3b-7c3598bd1d8a", response.Execution.Id);
Assert.Null(response.Execution.Inference);
Assert.Equal("medium", response.Execution.Priority);
Assert.Equal(ExecutionPriority.Medium, response.Execution.Priority);
Assert.Null(response.Execution.ReviewedAt);
Assert.Null(response.Execution.ReviewedPrediction);
Assert.Equal("processing", response.Execution.Status);
Expand Down Expand Up @@ -123,7 +123,7 @@ public async Task SendingADocumentToAnExecutionWithPriorityAndAliasShouldDeseria
Assert.Equal("default_sample.jpg", response.Execution.File.Name);
Assert.Equal("b743e123-e18c-4b62-8a07-811a4f72afd3", response.Execution.Id);
Assert.Null(response.Execution.Inference);
Assert.Equal("low", response.Execution.Priority);
Assert.Equal(ExecutionPriority.Low, response.Execution.Priority);
Assert.Null(response.Execution.ReviewedAt);
Assert.Null(response.Execution.ReviewedPrediction);
Assert.Equal("processing", response.Execution.Status);
Expand Down

0 comments on commit 798b656

Please sign in to comment.