コード例 #1
0
 private void OnExtractTables(TableReturn response, Dictionary <string, object> customData)
 {
     Log.Debug("TestCompareComplyV1.OnExtractTables()", "ExtractTables Response: {0}", customData["json"].ToString());
     Test(response != null);
     Test(response.Document.Title == "Untitled spreadsheet");
     extractTablesTested = true;
 }
コード例 #2
0
        /// <summary>
        /// Extract a document's tables.
        ///
        /// Uploads a file. The response includes an analysis of the document's tables.
        /// </summary>
        /// <param name="file">The file on which to run table extraction.</param>
        /// <param name="modelId">The analysis model to be used by the service. For the `/v1/element_classification` and
        /// `/v1/comparison` methods, the default is `contracts`. For the `/v1/tables` method, the default is `tables`.
        /// These defaults apply to the standalone methods as well as to the methods' use in batch-processing requests.
        /// (optional)</param>
        /// <param name="fileContentType">The content type of file. (optional)</param>
        /// <param name="customData">Custom data object to pass data including custom request headers.</param>
        /// <returns><see cref="TableReturn" />TableReturn</returns>
        public TableReturn ExtractTables(System.IO.FileStream file, string modelId = null, string fileContentType = null, Dictionary <string, object> customData = null)
        {
            if (file == null)
            {
                throw new ArgumentNullException(nameof(file));
            }

            if (string.IsNullOrEmpty(VersionDate))
            {
                throw new ArgumentNullException("versionDate cannot be null.");
            }

            TableReturn result = null;

            try
            {
                var formData = new MultipartFormDataContent();

                if (file != null)
                {
                    var fileContent = new ByteArrayContent((file as Stream).ReadAllBytes());
                    System.Net.Http.Headers.MediaTypeHeaderValue contentType;
                    System.Net.Http.Headers.MediaTypeHeaderValue.TryParse(fileContentType, out contentType);
                    fileContent.Headers.ContentType = contentType;
                    formData.Add(fileContent, "file", file.Name);
                }

                IClient client      = this.Client.WithAuthentication(_tokenManager.GetToken());
                var     restRequest = client.PostAsync($"{this.Endpoint}/v1/tables");

                restRequest.WithArgument("version", VersionDate);
                if (!string.IsNullOrEmpty(modelId))
                {
                    restRequest.WithArgument("model_id", modelId);
                }
                restRequest.WithBodyContent(formData);
                if (customData != null)
                {
                    restRequest.WithCustomData(customData);
                }

                restRequest.WithHeader("X-IBMCloud-SDK-Analytics", "service_name=compare-comply;service_version=v1;operation_id=ExtractTables");
                result = restRequest.As <TableReturn>().Result;
                if (result == null)
                {
                    result = new TableReturn();
                }
                result.CustomData = restRequest.CustomData;
            }
            catch (AggregateException ae)
            {
                throw ae.Flatten();
            }

            return(result);
        }
コード例 #3
0
        public IEnumerator TestExtractTables()
        {
            Log.Debug("CompareComplyServiceV1IntegrationTests", "Attempting to ExtractTables...");
            TableReturn extractTablesResponse = null;

            using (FileStream fs = File.OpenRead(tableFilepath))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    fs.CopyTo(ms);
                    service.ExtractTables(
                        callback: (DetailedResponse <TableReturn> response, IBMError error) =>
                    {
                        Log.Debug("CompareComplyServiceV1IntegrationTests", "ExtractTables result: {0}", response.Response);
                        extractTablesResponse = response.Result;
                        Assert.IsNotNull(extractTablesResponse);
                        Assert.IsNotNull(extractTablesResponse.Tables);
                        Assert.IsNotNull(extractTablesResponse.Tables[0].BodyCells[0].RowHeaderIds);
                        Assert.IsNotNull(extractTablesResponse.Tables[0].BodyCells[0].RowHeaderTexts);
                        Assert.IsNotNull(extractTablesResponse.Tables[0].BodyCells[0].RowHeaderTextsNormalized);
                        Assert.IsNotNull(extractTablesResponse.Tables[0].BodyCells[0].ColumnHeaderIds);
                        Assert.IsNotNull(extractTablesResponse.Tables[0].BodyCells[0].ColumnHeaderTexts);
                        Assert.IsNotNull(extractTablesResponse.Tables[0].BodyCells[0].ColumnHeaderTextsNormalized);
                        //Assert.IsTrue(extractTablesResponse.Tables[0].BodyCells[0].RowHeaderIds.Count > 0);
                        //Assert.IsTrue(extractTablesResponse.Tables[0].BodyCells[0].RowHeaderTexts.Count > 0);
                        //Assert.IsTrue(extractTablesResponse.Tables[0].BodyCells[0].RowHeaderTextsNormalized.Count > 0);
                        //Assert.IsTrue(extractTablesResponse.Tables[0].BodyCells[0].ColumnHeaderIds.Count > 0);
                        //Assert.IsTrue(extractTablesResponse.Tables[0].BodyCells[0].ColumnHeaderTexts.Count > 0);
                        //Assert.IsTrue(extractTablesResponse.Tables[0].BodyCells[0].ColumnHeaderTextsNormalized.Count > 0);
                        Assert.IsNotNull(extractTablesResponse.Tables[0].KeyValuePairs);
                        //Assert.IsTrue(extractTablesResponse.Tables[0].KeyValuePairs.Count > 0);
                        Assert.IsNull(error);
                    },
                        file: ms,
                        model: "tables",
                        fileContentType: Utility.GetMimeType(Path.GetExtension(tableFilepath))
                        );

                    while (extractTablesResponse == null)
                    {
                        yield return(null);
                    }
                }
            }
        }
コード例 #4
0
 private void OnExtractTables(TableReturn response, Dictionary <string, object> customData)
 {
     Log.Debug("ExampleCompareComplyV1.OnExtractTables()", "ExtractTables Response: {0}", customData["json"].ToString());
     extractTablesTested = true;
 }