-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
146 lines (111 loc) · 5 KB
/
Program.cs
File metadata and controls
146 lines (111 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using System;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Collections.Generic;
using System.Runtime.Serialization.Json;
using System.IO;
using RestSharp;
using Newtonsoft.Json;
//Demo of AI Scan Tech API calls from dotnet
//The API works whereby you upload an image of a point of sale receipt (see an example under receipt_images/receipt_test.jpg)
//in this repository) and are returned a token, you short poll the server using the token to listen for the results
//You will need to sign up for an API key at https://tabscanner.com
//Note: when testing the API the server will need time to launch for the first call, subsequent calls should complete in ~3 seconds
namespace aiscantech_demo
{
class Program
{
private static string api = "https://api.tabscanner.com";
private static readonly HttpClient client = new HttpClient();
private static async Task<string> UploadImage(string fileName, string url)
{
var client = new RestClient(url);
var request = new RestRequest(Method.POST);
request.AddFile("file", fileName, "image/jpeg");
var result = await client.ExecuteTaskAsync<ProcessResult>(request);
return result.Data.token;
}
private static async Task<ReceiptResult> ProcessResult(string url)
{
//convert to dotnet object
var serializer = new DataContractJsonSerializer(typeof(ReceiptResult));
var streamTask = client.GetStreamAsync(url);
var result = serializer.ReadObject(await streamTask) as ReceiptResult;
return result;
}
static void Main(string[] args)
{
if (args.Length == 0)
{
Console.WriteLine("API key must be provided as the first argument to the command.");
return;
}
if (args.Length == 1)
{
if (args[0] == "API_KEY_HERE")
{
Console.WriteLine("API key must be provided as the first argument to the command in your Dockerfile.");
return;
}
}
//build the url to make the request
string key = args[0];
string processUrl = String.Format("{0}/{1}/process", api, key);
Console.WriteLine("Uploading receipt...");
//the file name of the receipt to upload
string fileName = "receipt_images/receipt_test.jpg";
//upload the image and receive the token to use when listenign for the result
var token = UploadImage(fileName, processUrl).Result;
//build the url to listen for the resuly
string resultUrl = String.Format("{0}/{1}/result/{2}", api, key, token);
//check for a result every 5 seconds
//change this according to your preference
ReceiptResult result = ProcessResult(resultUrl).Result;
int pollLimit = 20;
int p = 0;
while (result.status == "pending" && p < pollLimit)
{
result = ProcessResult(resultUrl).Result;
//if the call returns a done status exit the listening process
if (result.status == "done")
{
break;
}
System.Threading.Thread.Sleep(5000);
p++;
Console.WriteLine("Listening for result...");
}
if (result.status != "done")
{
//something went wrong, please contact us!
Console.WriteLine(result.status);
Console.WriteLine(result.message);
Console.WriteLine("Polling for result failed.");
}
else
{
//the result is available deserialised as a dotnet class
Console.WriteLine("Result available.");
Receipt receipt = result.result;
//for a full list of properties see class definition in Receipt.cs
Console.WriteLine(receipt.establishment);
Console.WriteLine(receipt.total);
Console.WriteLine(receipt.cash);
Console.WriteLine(receipt.change);
Console.WriteLine(receipt.tax);
foreach (LineItem li in receipt.lineItems)
{
Console.WriteLine(li.desc);
Console.WriteLine(li.lineTotal);
Console.WriteLine("---------------");
}
//reserialize as json and save to a file
var json = JsonConvert.SerializeObject(result.result);
var file = File.CreateText("results/result.json");
file.Write(json);
file.Close();
}
}
}
}