/// <summary>
        /// Converts specified input document to format specified in the convertSettings with specified options
        /// </summary>
        /// <param name="request">Request. <see cref="ConvertDocumentRequest" /></param>
        public List <StoredConvertedResult> ConvertDocument(ConvertDocumentRequest request)
        {
            // verify the required parameter 'convertSettings' is set
            if (request.convertSettings == null)
            {
                throw new ApiException(400,
                                       "Missing required parameter 'convertSettings' when calling ConvertDocument");
            }

            // create path and map variables
            var resourcePath = this.configuration.GetServerUrl() + "/conversion";

            resourcePath = Regex
                           .Replace(resourcePath, "\\*", string.Empty)
                           .Replace("&amp;", "&")
                           .Replace("/?", "?");
            var postBody = SerializationHelper.Serialize(request.convertSettings); // http body (model) parameter
            var response = this.apiInvoker.InvokeApi(
                resourcePath,
                "POST",
                postBody,
                null,
                null);

            if (response != null)
            {
                return((List <StoredConvertedResult>)SerializationHelper.Deserialize(response, typeof(List <StoredConvertedResult>)));
            }

            return(null);
        }
        /// <summary>
        /// Converts specified input document to format specified in the convertSettings with specified options and return result as stream
        /// </summary>
        /// <param name="request">Request. <see cref="ConvertDocumentRequest" /></param>
        public Stream ConvertDocumentDownload(ConvertDocumentRequest request)
        {
            // verify the required parameter 'convertSettings' is set
            if (request.convertSettings == null)
            {
                throw new ApiException(400,
                                       "Missing required parameter 'convertSettings' when calling ConvertDocument");
            }

            // create path and map variables
            var resourcePath = this.configuration.GetServerUrl() + "/conversion";

            resourcePath = Regex
                           .Replace(resourcePath, "\\*", string.Empty)
                           .Replace("&amp;", "&")
                           .Replace("/?", "?");
            var postBody = SerializationHelper.Serialize(request.convertSettings); // http body (model) parameter

            return(this.apiInvoker.InvokeBinaryApi(
                       resourcePath,
                       "POST",
                       postBody,
                       null,
                       null));
        }
Exemplo n.º 3
0
 public void TestConvertDocument()
 {
     using var documentStream = File.OpenRead(LocalTestDataFolder + localFolder + "/test_uploadfile.docx");
     var request = new ConvertDocumentRequest(
         document: documentStream,
         format: "pdf"
         );
     var actual = this.WordsApi.ConvertDocument(request);
 }
        public void TestConvertMissingSettings()
        {
            // Arrange
            var request = new ConvertDocumentRequest(null);

            // Act & Assert
            var ex = Assert.Throws <ApiException>(() =>
            {
                ConvertApi.ConvertDocumentDownload(request);
            });

            Assert.True(ex.Message.Contains("Missing required parameter 'convertSettings'"));
        }
        public void TestConversionFileNotFound()
        {
            var settings = new ConvertSettings
            {
                FilePath = TestFiles.NotExist.FullName,
                Format   = "pdf"
            };

            // Arrange
            var request = new ConvertDocumentRequest(settings);

            // Act & Assert
            var ex = Assert.Throws <ApiException>(() =>
            {
                ConvertApi.ConvertDocumentDownload(request);
            });

            Assert.True(ex.Message.Contains("The specified key does not exist"));
        }
Exemplo n.º 6
0
        public void IfTokenIsNotValidRefreshTokenShouldBeSuccessfully()
        {
            // Arrange
            var api = new WordsApi(
                new Configuration
            {
                ClientSecret = this.ClientSecret,
                ClientId     = this.ClientId,
                ApiBaseUrl   = "http://localhost:8081",
                AuthType     = AuthType.OAuth2,
                DebugMode    = true
            });

            using (var stream = this.ToStream("content"))
            {
                var request = new ConvertDocumentRequest(stream, "txt");
                api.ConvertDocument(request);

                Thread.Sleep(2000);
                stream.Flush();
                stream.Position = 0;

                var traceListenerMock = new Mock <TraceListener>();
                traceListenerMock.Setup(x => x.WriteLine(It.IsAny <string>())).Verifiable();
                Trace.Listeners.Add(traceListenerMock.Object);
                try
                {
                    // Act
                    api.ConvertDocument(request);

                    // Assert
                    traceListenerMock.Verify(x => x.WriteLine(It.Is <string>(s => s.Contains("grant_type=refresh_token"))), Times.Once);
                    traceListenerMock.Verify(x => x.WriteLine(It.IsAny <string>()), Times.AtLeast(2));
                }
                finally
                {
                    Trace.Listeners.Remove(traceListenerMock.Object);
                }
            }
        }
        /// <summary>
        /// Convert document to use in other Watson services.
        /// </summary>
        /// <param name="successCallback">The success callback.</param>
        /// <param name="failCallback">The fail callback.</param>
        /// <param name="documentPath"></param>
        /// <param name="conversionTarget"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public bool ConvertDocument(SuccessCallback <ConvertedDocument> successCallback, FailCallback failCallback, string documentPath, string conversionTarget, Dictionary <string, object> customData = null)
        {
            if (successCallback == null)
            {
                throw new ArgumentNullException("successCallback");
            }
            if (failCallback == null)
            {
                throw new ArgumentNullException("failCallback");
            }
            if (string.IsNullOrEmpty(documentPath))
            {
                throw new ArgumentNullException("A document path is needed to convert document.");
            }
            if (string.IsNullOrEmpty(conversionTarget))
            {
                throw new ArgumentNullException("A conversion target is needed to convert document.");
            }

            RESTConnector connector = RESTConnector.GetConnector(Credentials, ConvertDocumentEndpoint);

            if (connector == null)
            {
                return(false);
            }

            ConvertDocumentRequest req = new ConvertDocumentRequest();

            req.SuccessCallback       = successCallback;
            req.FailCallback          = failCallback;
            req.CustomData            = customData == null ? new Dictionary <string, object>() : customData;
            req.ConversionTarget      = conversionTarget;
            req.Parameters["version"] = Version.DocumentConversion;
            req.OnResponse            = ConvertDocumentResponse;

            byte[] documentData = null;
            if (documentPath != default(string))
            {
                if (LoadFile != null)
                {
                    documentData = LoadFile(documentPath);
                }
                else
                {
#if !UNITY_WEBPLAYER
                    documentData = File.ReadAllBytes(documentPath);
#endif
                }

                if (documentData == null)
                {
                    Log.Error("DocumentConversion.ConvertDocument()", "Failed to upload {0}!", documentPath);
                }
            }

            if (documentData != null)
            {
                req.Headers["Content-Type"] = "application/x-www-form-urlencoded";

                req.Forms           = new Dictionary <string, RESTConnector.Form>();
                req.Forms["file"]   = new RESTConnector.Form(documentData);
                req.Forms["config"] = new RESTConnector.Form(conversionTarget.ToString());
            }

            return(connector.Send(req));
        }
Exemplo n.º 8
0
        /// <summary>
        /// Convert document to use in other Watson services.
        /// </summary>
        /// <param name="callback"></param>
        /// <param name="documentPath"></param>
        /// <param name="conversionTarget"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public bool ConvertDocument(OnConvertDocument callback, string documentPath, string conversionTarget = ConversionTarget.ANSWER_UNITS, string data = null)
        {
            if (callback == null)
            {
                throw new ArgumentNullException("callback");
            }
            if (string.IsNullOrEmpty(documentPath))
            {
                throw new ArgumentNullException("A document path is needed to convert document.");
            }
            if (string.IsNullOrEmpty(conversionTarget))
            {
                throw new ArgumentNullException("A conversion target is needed to convert document.");
            }

            RESTConnector connector = RESTConnector.GetConnector(SERVICE_ID, FUNCTION_CONVERT_DOCUMENT);

            if (connector == null)
            {
                return(false);
            }

            ConvertDocumentRequest req = new ConvertDocumentRequest();

            req.Callback              = callback;
            req.OnResponse            = ConvertDocumentResponse;
            req.Data                  = data;
            req.ConversionTarget      = conversionTarget;
            req.Parameters["version"] = Version.DOCUMENT_CONVERSION;

            byte[] documentData = null;
            if (documentPath != default(string))
            {
                if (LoadFile != null)
                {
                    documentData = LoadFile(documentPath);
                }
                else
                {
#if !UNITY_WEBPLAYER
                    documentData = File.ReadAllBytes(documentPath);
#endif
                }

                if (documentData == null)
                {
                    Log.Error("DocumentConversion", "Failed to upload {0}!", documentPath);
                }
            }

            if (documentData != null)
            {
                req.Headers["Content-Type"] = "application/x-www-form-urlencoded";

                req.Forms           = new Dictionary <string, RESTConnector.Form>();
                req.Forms["file"]   = new RESTConnector.Form(documentData);
                req.Forms["config"] = new RESTConnector.Form(conversionTarget.ToString());
            }

            return(connector.Send(req));
        }