Skip to content

Commit

Permalink
add alias & priority support
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianMindee committed Nov 18, 2024
1 parent dfbf0ec commit 7eef138
Show file tree
Hide file tree
Showing 9 changed files with 215 additions and 85 deletions.
8 changes: 5 additions & 3 deletions docs/code_samples/workflow_execution.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Mindee;
using Mindee.Input;
using Mindee.Product.Generated;

string apiKey = "my-api-key";
string filePath = "/path/to/the/file.ext";
Expand All @@ -13,8 +12,11 @@ MindeeClient mindeeClient = new MindeeClient(apiKey);
// Other input types can be used, as mentioned in the docs
var inputSource = new LocalInputSource(filePath);

// Call the API and parse the input
var response = await mindeeClient.ExecuteWorkflowAsync<GeneratedV1>(workflowId, inputSource);
// Send the document to a workflow execution
var response = await mindeeClient.ExecuteWorkflowAsync(workflowId, inputSource);

// Alternatively, give it an alias:
// var response = await mindeeClient.ExecuteWorkflowAsync(workflowId, inputSource, new WorkflowOptions(alias: "my-alias"));

// Print the execution ID to make sure it worked.
System.Console.WriteLine(response.Execution.Id);
Expand Down
52 changes: 52 additions & 0 deletions src/Mindee/Http/GenericParameter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using Mindee.Exceptions;
using Mindee.Input;

namespace Mindee.Http
{
/// <summary>
/// G
/// </summary>
public class GenericParameter
{

/// <summary>
/// A local input source.
/// </summary>
public LocalInputSource LocalSource { get; }

/// <summary>
/// A URL input source.
/// </summary>
public UrlInputSource UrlSource { get; }

/// <summary>
/// Whether to include the full text data for async APIs.
/// This performs a full OCR operation on the server and will increase response time and payload size.
/// </summary>
/// <remarks>It is not available on all API.</remarks>
public bool FullText { get; }

/// <summary>
///
/// </summary>
/// <param name="localSource"></param>
/// <param name="urlSource"></param>
/// <param name="fullText"></param>
/// <exception cref="MindeeException"></exception>
public GenericParameter(LocalInputSource localSource, UrlInputSource urlSource, bool fullText)
{

if (localSource != null && urlSource != null)
{
throw new MindeeException("localSource and urlSource may not both be specified.");
}
if (localSource == null && urlSource == null)
{
throw new MindeeException("One of localSource or urlSource must be specified.");
}
LocalSource = localSource;
UrlSource = urlSource;
FullText = fullText;
}
}
}
4 changes: 2 additions & 2 deletions src/Mindee/Http/IHttpApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ string jobId
/// Send a document to a workflow.
/// </summary>
/// <param name="workflowId">The ID of the workflow.</param>
/// <param name="predictParameter"><see cref="PredictParameter"/></param>
/// <param name="workflowParameter"><see cref="PredictParameter"/></param>
/// <typeparam name="TModel">Document type.</typeparam>
Task<WorkflowResponse<TModel>> PostWorkflowExecution<TModel>(
string workflowId,
PredictParameter predictParameter)
WorkflowParameter workflowParameter)
where TModel : class, new();
}
}
36 changes: 34 additions & 2 deletions src/Mindee/Http/MindeeApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,21 +117,53 @@ string jobId

public async Task<WorkflowResponse<TModel>> PostWorkflowExecution<TModel>(
string workflowId,
PredictParameter predictParameter)
WorkflowParameter workflowParameter)
where TModel : class, new()
{
var request = new RestRequest(
$"v1/workflows/{workflowId}/executions"
, Method.Post);

AddPredictRequestParameters(predictParameter, request);
AddWorkflowRequestParameters(workflowParameter, request);

_logger?.LogInformation($"HTTP POST to {_baseUrl + request.Resource} ...");

var response = await _httpClient.ExecutePostAsync(request);
return ResponseHandler<WorkflowResponse<TModel>>(response);
}

private static void AddWorkflowRequestParameters(WorkflowParameter workflowParameter, RestRequest request)
{
if (workflowParameter.LocalSource != null)
{
request.AddFile(
"document",
workflowParameter.LocalSource.FileBytes,
workflowParameter.LocalSource.Filename);
}
else if (workflowParameter.UrlSource != null)
{
request.AddParameter(
"document",
workflowParameter.UrlSource.FileUrl.ToString());
}
if (workflowParameter.FullText)
{
request.AddQueryParameter(name: "full_text_ocr", value: "true");
}

if (workflowParameter.Alias != null)
{
request.AddParameter(name: "alias", value: workflowParameter.Alias);
}

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

}

private static void AddPredictRequestParameters(PredictParameter predictParameter, RestRequest request)
{
if (predictParameter.LocalSource != null)
Expand Down
39 changes: 5 additions & 34 deletions src/Mindee/Http/PredictParameter.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,18 @@
using Mindee.Exceptions;
using Mindee.Input;

namespace Mindee.Http
{
/// <summary>
/// Parameter required to use the predict feature.
/// </summary>
public sealed class PredictParameter
public sealed class PredictParameter : GenericParameter
{
/// <summary>
/// A local input source.
/// </summary>
public LocalInputSource LocalSource { get; }

/// <summary>
/// A URL input source.
/// </summary>
public UrlInputSource UrlSource { get; }

/// <summary>
/// Want an OCR result ?
/// </summary>
/// <remarks>It is not available on all API.</remarks>
public bool AllWords { get; }

/// <summary>
/// Whether to include the full text data for async APIs.
/// This performs a full OCR operation on the server and will increase response time and payload size.
/// </summary>
/// <remarks>It is not available on all API.</remarks>
public bool FullText { get; }

/// <summary>
/// Want the cropping result about the document?
/// </summary>
Expand All @@ -40,30 +22,19 @@ public sealed class PredictParameter
/// <summary>
/// Prediction parameters for requests.
/// </summary>
/// <param name="localSource">Local input source containing the file.<see cref="LocalSource"/></param>
/// <param name="urlSource">Source URL to use.<see cref="UrlSource"/></param>
/// <param name="localSource">Local input source containing the file.<see cref="GenericParameter.LocalSource"/></param>
/// <param name="urlSource">Source URL to use.<see cref="GenericParameter.UrlSource"/></param>
/// <param name="allWords">Whether to include the full OCR response in the payload (compatible APIs only).<see cref="AllWords"/></param>
/// <param name="fullText">Whether to include the full text in the payload (compatible APIs only)<see cref="FullText"/></param>
/// <param name="fullText">Whether to include the full text in the payload (compatible APIs only)<see cref="GenericParameter.FullText"/></param>
/// <param name="cropper">Whether to crop the document before enqueuing on the API.<see cref="Cropper"/></param>
public PredictParameter(
LocalInputSource localSource,
UrlInputSource urlSource,
bool allWords,
bool fullText,
bool cropper)
bool cropper) : base(localSource, urlSource, fullText)
{
if (localSource != null && urlSource != null)
{
throw new MindeeException("localSource and urlSource may not both be specified.");
}
if (localSource == null && urlSource == null)
{
throw new MindeeException("One of localSource or urlSource must be specified.");
}
LocalSource = localSource;
UrlSource = urlSource;
AllWords = allWords;
FullText = fullText;
Cropper = cropper;
}
}
Expand Down
39 changes: 39 additions & 0 deletions src/Mindee/Http/WorkflowParameter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Mindee.Input;

namespace Mindee.Http
{
/// <summary>
/// Parameter required to use the workflow feature.
/// </summary>
public class WorkflowParameter : GenericParameter
{
/// <summary>
/// Alias to give to the file.
/// </summary>
public string Alias { get; }


/// <summary>
/// Priority to give to the execution.
/// </summary>
public string Priority { get; }

/// <summary>
/// Workflow parameters.
/// </summary>
/// <param name="localSource">Local input source containing the file.<see cref="GenericParameter.LocalSource"/></param>
/// <param name="urlSource">Source URL to use.<see cref="GenericParameter.UrlSource"/></param>
/// <param name="fullText">Whether to include the full text in the payload (compatible APIs only)<see cref="GenericParameter.FullText"/></param>
/// <param name="alias">Alias to give to the document.<see cref="Alias"/></param>
/// <param name="priority">Priority to give to the document.<see cref="Priority"/></param>
public WorkflowParameter(
LocalInputSource localSource,
UrlInputSource urlSource, bool fullText,
string alias, string priority) : base(localSource, urlSource,
fullText)
{
Alias = alias;
Priority = priority;
}
}
}
58 changes: 27 additions & 31 deletions src/Mindee/MindeeClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -614,71 +614,67 @@ LocalInputSource inputSource
/// </summary>
/// <param name="workflowId">The workflow id.</param>
/// <param name="inputSource"><see cref="LocalInputSource"/></param>
/// <param name="predictOptions"><see cref="PageOptions"/></param>
/// <param name="workflowOptions"><see cref="PageOptions"/></param>
/// <param name="pageOptions"><see cref="PageOptions"/></param>
/// <typeparam name="TInferenceModel">Set the prediction model used to parse the document.
/// The response object will be instantiated based on this parameter.</typeparam>
/// <returns><see cref="WorkflowResponse{TInferenceModel}"/></returns>
public async Task<WorkflowResponse<TInferenceModel>> ExecuteWorkflowAsync<TInferenceModel>(
public async Task<WorkflowResponse<GeneratedV1>> ExecuteWorkflowAsync(
string workflowId,
LocalInputSource inputSource
, PredictOptions predictOptions = null
, PageOptions pageOptions = null)
where TInferenceModel : class, new()
LocalInputSource inputSource,
WorkflowOptions workflowOptions = null,
PageOptions pageOptions = null)
{
_logger?.LogInformation("Asynchronous parsing of {} ...", typeof(TInferenceModel).Name);
_logger?.LogInformation("Workflow enqueing {} ...", inputSource.Filename);

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

if (predictOptions == null)
if (workflowOptions == null)
{
predictOptions = new PredictOptions();
workflowOptions = new WorkflowOptions();
}

return await _mindeeApi.PostWorkflowExecution<TInferenceModel>(
return await _mindeeApi.PostWorkflowExecution<GeneratedV1>(
workflowId,
new PredictParameter(
new WorkflowParameter(
localSource: inputSource,
urlSource: null,
allWords: predictOptions.AllWords,
fullText: predictOptions.FullText,
cropper: predictOptions.Cropper));
alias: workflowOptions.Alias,
priority: workflowOptions.Priority,
fullText: workflowOptions.FullText
));
}

/// <summary>
/// Send a remote file to a workflow execution.
/// </summary>
/// <param name="workflowId">The workflow id.</param>
/// <param name="inputSource"><see cref="LocalInputSource"/></param>
/// <param name="predictOptions"><see cref="PageOptions"/></param>
/// <typeparam name="TInferenceModel">Set the prediction model used to parse the document.
/// The response object will be instantiated based on this parameter.</typeparam>
/// <param name="workflowOptions"><see cref="PageOptions"/></param>
/// <returns><see cref="WorkflowResponse{TInferenceModel}"/></returns>
public async Task<WorkflowResponse<TInferenceModel>> ExecuteWorkflowAsync<TInferenceModel>(
public async Task<WorkflowResponse<GeneratedV1>> ExecuteWorkflowAsync(
string workflowId,
UrlInputSource inputSource
, PredictOptions predictOptions = null)
where TInferenceModel : class, new()
UrlInputSource inputSource,
WorkflowOptions workflowOptions = null)
{
_logger?.LogInformation("Asynchronous parsing of {} ...", typeof(TInferenceModel).Name);
_logger?.LogInformation("Asynchronous parsing of {} ...", inputSource.FileUrl);

if (predictOptions == null)
if (workflowOptions == null)
{
predictOptions = new PredictOptions();
workflowOptions = new WorkflowOptions();
}

return await _mindeeApi.PostWorkflowExecution<TInferenceModel>(
return await _mindeeApi.PostWorkflowExecution<GeneratedV1>(
workflowId,
new PredictParameter(
new WorkflowParameter(
localSource: null,
urlSource: inputSource,
allWords: predictOptions.AllWords,
fullText: predictOptions.FullText,
cropper: predictOptions.Cropper));
alias: workflowOptions.Alias,
priority: workflowOptions.Priority,
fullText: workflowOptions.FullText
));
}

/// <summary>
Expand Down
38 changes: 38 additions & 0 deletions src/Mindee/MindeeClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,42 @@ public AsyncPollingOptions(double initialDelaySec = 2.0, double intervalSec = 1.
IntervalMilliSec = (int)Math.Floor(IntervalSec * 1000);
}
}

/// <summary>
/// Options for workflow executions.
/// </summary>
public sealed class WorkflowOptions
{

/// <summary>
/// Alias to give to the file.
/// </summary>
public string Alias { get; }


/// <summary>
/// Priority to give to the execution.
/// </summary>
public string Priority { get; }


/// <summary>
/// Whether to include the full text data for async APIs.
/// This performs a full OCR operation on the server and will increase response time and payload size.
/// </summary>
public bool FullText { get; }

/// <summary>
/// Options for workflow executions.
/// </summary>
/// <param name="alias"></param>
/// <param name="priority"></param>
/// <param name="fullText"></param>
public WorkflowOptions(string alias = null, string priority = null, bool fullText = false)
{
Alias = alias;
Priority = priority;
FullText = fullText;
}
}
}
Loading

0 comments on commit 7eef138

Please sign in to comment.