Skip to content

Commit

Permalink
Add new changes to DocumentAI
Browse files Browse the repository at this point in the history
  • Loading branch information
atir-naveed-geeksltd committed Jan 29, 2025
1 parent 4cd5622 commit c0cf0f5
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
68 changes: 66 additions & 2 deletions Olive.Azure.DocumentAnalyzer/DocumentAnalyze.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,53 @@ public async Task<DocumentAnalyzeModelResponse> ListDocumentAnalyzeModelsAsync(b

}

public async Task<DocumentAnalyzeResult> AnalyzeDocumentAsync(byte[] fileData, string modelId, bool useFormRecognizer = false)
public async Task<string> AnalyzeDocumentAsync(byte[] fileData, string modelId, bool useFormRecognizer = false)
{
var requestUrl = $"{endpoint}/{(useFormRecognizer ? "formrecognizer" : "documentintelligence")}/documentModels/{modelId}:analyze?api-version={apiVersion}";
var request = new HttpRequestMessage(HttpMethod.Post, requestUrl);
httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", apiKey);

// Convert the file to a Base64 string
string base64String = Convert.ToBase64String(fileData);

// Create the JSON payload
var payload = new
{
base64Source = base64String
};

request.Content = new StringContent(JsonConvert.SerializeObject(payload));
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

try
{
var response = await httpClient.SendAsync(request);

if (response.IsSuccessStatusCode)
{
var resultUrl = response.Headers.GetValues("Operation-Location").FirstOrDefault();
if (resultUrl is not null)
{
return resultUrl;
}
else
{
throw new Exception("No operation location header found in response.");
}
}
else
{
var message = await response.Content.ReadAsStringAsync();
throw new Exception($"Failed to analyze document. Error Message: {message}");
}
}
catch (Exception ex)
{
throw ex;
}
}

public async Task<DocumentAnalyzeResult> AnalyzeDocumentAsyncWithPoll(byte[] fileData, string modelId, bool useFormRecognizer = false)
{
var requestUrl = $"{endpoint}/{(useFormRecognizer ? "formrecognizer" : "documentintelligence")}/documentModels/{modelId}:analyze?api-version={apiVersion}";
var request = new HttpRequestMessage(HttpMethod.Post, requestUrl);
Expand Down Expand Up @@ -175,7 +221,7 @@ public async Task<DocumentAnalyzeResult> AnalyzeDocumentAsync(byte[] fileData, s

}

async Task<DocumentAnalyzeResult> PollForResults(string resultUrl)
private async Task<DocumentAnalyzeResult> PollForResults(string resultUrl)
{
httpClient.DefaultRequestHeaders.Clear();
httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", apiKey);
Expand Down Expand Up @@ -208,6 +254,24 @@ async Task<DocumentAnalyzeResult> PollForResults(string resultUrl)
}
}

public async Task<DocumentAnalyzeResult> GetPollResults(string resultUrl)
{
httpClient.DefaultRequestHeaders.Clear();
httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", apiKey);

HttpResponseMessage response = await httpClient.GetAsync(resultUrl);
string jsonResponse = await response.Content.ReadAsStringAsync();

if (response.IsSuccessStatusCode)
{
return JsonConvert.DeserializeObject<DocumentAnalyzeResult>(jsonResponse);
}
else
{
throw new Exception("Failed to poll for results.");
}
}

async Task<BuildDocumentAnalyzerResponse> GetOperationStatusAsync(string operationUrl)
{
httpClient.DefaultRequestHeaders.Clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>1.0.5</Version>
<Version>1.0.6</Version>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit c0cf0f5

Please sign in to comment.