コード例 #1
0
ファイル: ExpenseReader.cs プロジェクト: BlazorHub/UnoCash
 public static Task <IEnumerable <Expense> > GetAllAsync(string account) =>
 AzureTableStorage.GetAllAsync(nameof(Expense))
 .SelectAsync(ToExpense)
 .WhereAsync(expense => expense.Account == account);
コード例 #2
0
ファイル: ReceiptParser.cs プロジェクト: BlazorHub/UnoCash
        public static async Task <Receipt> ParseAsync(string blobName)
        {
            var container =
                await ConfigurationReader.GetAsync(StorageAccountConnectionString)
                .Map(CloudStorageAccount.Parse)
                .Map(client => client.CreateCloudBlobClient()
                     .GetContainerReference("receipts"))
                .ConfigureAwait(false);

            var blob = container.GetBlobReference(blobName);

            //blob.Uri

            var http = new HttpClient();

            const string fre = "FormRecognizerEndpoint";

            var endpoint = await ConfigurationReader.GetAsync(fre)
                           .ConfigureAwait(false);

            var url =
                $"https://{endpoint}/formrecognizer/" +
                "v1.0-preview/prebuilt/receipt/asyncBatchAnalyze";

            var blobStream = new MemoryStream();

            await blob.DownloadToStreamAsync(blobStream)
            .ConfigureAwait(false);

            string hash;

            using (var md5 = MD5.Create())
                hash = Convert.ToBase64String(md5.ComputeHash(blobStream));

            var existing =
                await AzureTableStorage.GetAllAsync("receipthashes")
                .ConfigureAwait(false);

            var entity =
                existing.SingleOrDefault(x => x.PartitionKey == "receipts" &&
                                         x.RowKey == hash);

            if (entity != null)
            {
                var res = entity.Properties["Results"].StringValue;

                var text =
                    await container.GetBlockBlobReference(res)
                    .DownloadTextAsync()
                    .ConfigureAwait(false);

                return(text.ToReceipt());
            }

            blobStream.Seek(0, SeekOrigin.Begin);

            var streamContent = new StreamContent(blobStream);

            streamContent.Headers.ContentType =
                new MediaTypeHeaderValue("application/octet-stream");

            var request = new HttpRequestMessage(HttpMethod.Post, url)
            {
                Content = streamContent
            };

            const string frk = "FormRecognizerKey";

            http.DefaultRequestHeaders
            .Add("Ocp-Apim-Subscription-Key", await SecretReader.GetAsync(frk)
                 .ConfigureAwait(false));

            var response = await http.SendAsync(request)
                           .ConfigureAwait(false);

            var content =
                await response.Content
                .ReadAsStringAsync()
                .ConfigureAwait(false);

            Console.WriteLine(content);

            var location =
                response.Headers.TryGetValues("Operation-Location", out var values) ?
                values.Single() :
                throw new Exception();

            response.EnsureSuccessStatusCode();

            bool resultAvailable;

            string responseContent;

            do
            {
                var result =
                    await http.GetAsync(location)
                    .ConfigureAwait(false);

                responseContent =
                    await result.Content
                    .ReadAsStringAsync()
                    .ConfigureAwait(false);

                var bodyResult =
                    JsonConvert.DeserializeObject <ReceiptRecognizerResult>(responseContent);

                resultAvailable = bodyResult.Status == "Succeeded";
            } while (!resultAvailable);

            var resultsBlobGuid = Guid.NewGuid().ToString();

            await container.GetBlockBlobReference(resultsBlobGuid)
            .UploadTextAsync(responseContent)
            .ConfigureAwait(false);

            await SaveResultsToCacheAsync(hash, resultsBlobGuid).ConfigureAwait(false);

            return(responseContent.ToReceipt());
        }
コード例 #3
0
 public static Task <IEnumerable <Expense> > GetAllAsync(string account, string email) =>
 AzureTableStorage.GetAllAsync(nameof(Expense), email + "+" + account)
 .SelectAsync(ToExpense);