Example #1
0
        static Util()
        {
            RestClient = new PrizmDocRestClient(BaseUrl);

            if (ApiKey != null)
            {
                RestClient.DefaultRequestHeaders.Add("Acs-Api-Key", ApiKey);
            }
        }
        public DocumentProcessingHelper(Uri baseAddress, string apiKey)
        {
            client = new PrizmDocRestClient(baseAddress);

            if (apiKey != null)
            {
                client.DefaultRequestHeaders.Add("Acs-Api-Key", apiKey);
            }
        }
Example #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PrizmDocServerClient"/>
        /// class, allowing you to work with <see
        /// href="https://cloud.accusoft.com">PrizmDoc Cloud</see>.
        /// </summary>
        /// <param name="baseAddress">Base URL to PrizmDoc Server (e.g.
        /// <c>"https://api.accusoft.com"</c>).</param>
        /// <param name="apiKey">Your <see
        /// href="https://cloud.accusoft.com">PrizmDoc Cloud</see> API
        /// key.</param>
        public PrizmDocServerClient(Uri baseAddress, string apiKey)
        {
            this.restClient = new PrizmDocRestClient(baseAddress);

            if (apiKey != null)
            {
                this.restClient.DefaultRequestHeaders.Add("Acs-Api-Key", apiKey);
            }
        }
Example #4
0
        public async Task BaseAddress_with_trailing_slash_is_applied_correctly_to_each_request()
        {
            mockServer
            .Given(Request.Create().WithPath("/wat/123").UsingGet())
            .RespondWith(Response.Create().WithStatusCode(200).WithBody("GET Response"));

            mockServer
            .Given(Request.Create().WithPath("/wat").UsingPost())
            .RespondWith(Response.Create().WithStatusCode(200).WithBody("POST Response"));

            mockServer
            .Given(Request.Create().WithPath("/wat/123").UsingPut())
            .RespondWith(Response.Create().WithStatusCode(200).WithBody("PUT Response"));

            mockServer
            .Given(Request.Create().WithPath("/wat/123").UsingDelete())
            .RespondWith(Response.Create().WithStatusCode(200).WithBody("DELETE Response"));

            string baseAddressWithoutTrailingSlash = "http://localhost:" + mockServer.Ports.First();
            string baseAddressWithTrailingSlash    = baseAddressWithoutTrailingSlash + "/";

            client = new PrizmDocRestClient(baseAddressWithTrailingSlash);

            AffinitySession session = client.CreateAffinitySession();

            using (HttpResponseMessage response = await session.SendAsync(new HttpRequestMessage(HttpMethod.Get, "/wat/123")))
            {
                Assert.AreEqual(baseAddressWithoutTrailingSlash + "/wat/123", response.RequestMessage.RequestUri.ToString());
            }

            using (HttpResponseMessage response = await session.GetAsync("/wat/123"))
            {
                Assert.AreEqual(baseAddressWithoutTrailingSlash + "/wat/123", response.RequestMessage.RequestUri.ToString());
            }

            using (HttpResponseMessage response = await session.PostAsync("/wat", new StringContent("body")))
            {
                Assert.AreEqual(baseAddressWithoutTrailingSlash + "/wat", response.RequestMessage.RequestUri.ToString());
            }

            using (HttpResponseMessage response = await session.PutAsync("/wat/123", new StringContent("body")))
            {
                Assert.AreEqual(baseAddressWithoutTrailingSlash + "/wat/123", response.RequestMessage.RequestUri.ToString());
            }

            using (HttpResponseMessage response = await session.DeleteAsync("/wat/123"))
            {
                Assert.AreEqual(baseAddressWithoutTrailingSlash + "/wat/123", response.RequestMessage.RequestUri.ToString());
            }
        }
        public async Task Can_convert_a_DOCX_to_PDF_using_PrizmDoc_Cloud()
        {
            // Construct an instance of the PrizmDocRestClient.
            var    client = new PrizmDocRestClient(Environment.GetEnvironmentVariable("BASE_URL"));
            string apiKey = Environment.GetEnvironmentVariable("API_KEY");

            if (apiKey != null)
            {
                client.DefaultRequestHeaders.Add("Acs-Api-Key", apiKey);
            }

            // Create an affinity session for our processing work.
            //
            // You should use an affinity session anytime you have a group
            // of HTTP requests that go together as part of a processing
            // chain. The session ensures that all HTTP requests will
            // automatically use the same affinity (be routed to the same
            // PrizmDoc Server machine in the cluster).
            AffinitySession session = client.CreateAffinitySession();

            string json;

            // Create a new work file for the input document
            using (FileStream inputFileStream = File.OpenRead("input.docx"))
                using (HttpResponseMessage response = await session.PostAsync("/PCCIS/V1/WorkFile", new StreamContent(inputFileStream)))
                {
                    response.EnsureSuccessStatusCode();
                    json = await response.Content.ReadAsStringAsync();
                }


            JObject inputWorkFile = JObject.Parse(json);
            string  inputFileId   = (string)inputWorkFile["fileId"];

            // Start a conversion process using the input work file
            string postContentConvertersJson =
                @"{
    ""input"": {
        ""sources"": [
            {
                ""fileId"": """ + inputFileId + @"""
            }
        ],
        ""dest"": {
            ""format"": ""pdf""
        }
    }
}";

            using (HttpResponseMessage response = await session.PostAsync("/v2/contentConverters", new StringContent(postContentConvertersJson)))
            {
                response.EnsureSuccessStatusCode();
                json = await response.Content.ReadAsStringAsync();
            }

            JObject process   = JObject.Parse(json);
            string  processId = (string)process["processId"];

            // Wait for the process to finish
            using (HttpResponseMessage response = await session.GetFinalProcessStatusAsync($"/v2/contentConverters/{processId}"))
            {
                response.EnsureSuccessStatusCode();
                json = await response.Content.ReadAsStringAsync();
            }

            process = JObject.Parse(json);

            // Did the process error?
            if ((string)process["state"] != "complete")
            {
                throw new Exception("The process failed to complete:\n" + json);
            }

            // Download the output work file and save it to disk.
            string workFileId = (string)process["output"]["results"][0]["fileId"];

            using (HttpResponseMessage response = await session.GetAsync($"/PCCIS/V1/WorkFile/{workFileId}"))
            {
                response.EnsureSuccessStatusCode();

                using (Stream responseBodyStream = await response.Content.ReadAsStreamAsync())
                    using (FileStream outputFileStream = File.OpenWrite("output.pdf"))
                    {
                        await responseBodyStream.CopyToAsync(outputFileStream);
                    }
            }
        }
 public static void BeforeAll(TestContext context)
 {
     mockServer = FluentMockServer.Start();
     client     = new PrizmDocRestClient("http://localhost:" + mockServer.Ports.First());
     client.DefaultRequestHeaders.Add("Acs-Api-Key", System.Environment.GetEnvironmentVariable("API_KEY"));
 }
        public async Task DefaultRequestHeaders_are_correctly_applied_to_each_request()
        {
            mockServer
            .Given(Request.Create().WithPath("/wat/123").UsingGet())
            .RespondWith(Response.Create().WithStatusCode(200).WithBody("GET Response"));

            mockServer
            .Given(Request.Create().WithPath("/wat").UsingPost())
            .RespondWith(Response.Create().WithStatusCode(200).WithBody("POST Response"));

            mockServer
            .Given(Request.Create().WithPath("/wat/123").UsingPut())
            .RespondWith(Response.Create().WithStatusCode(200).WithBody("PUT Response"));

            mockServer
            .Given(Request.Create().WithPath("/wat/123").UsingDelete())
            .RespondWith(Response.Create().WithStatusCode(200).WithBody("DELETE Response"));

            string baseAddress = "http://localhost:" + mockServer.Ports.First();

            client = new PrizmDocRestClient(baseAddress)
            {
                DefaultRequestHeaders =
                {
                    { "Some-Header",       "An example value"      },
                    { "Some-Other-Header", "Another example value" },
                }
            };

            AffinitySession session = client.CreateAffinitySession();

            using (HttpResponseMessage response = await session.SendAsync(new HttpRequestMessage(HttpMethod.Get, "/wat/123")))
            {
                Assert.IsTrue(response.RequestMessage.Headers.Contains("Some-Header"));
                Assert.AreEqual("An example value", response.RequestMessage.Headers.GetValues("Some-Header").SingleOrDefault());
                Assert.IsTrue(response.RequestMessage.Headers.Contains("Some-Other-Header"));
                Assert.AreEqual("Another example value", response.RequestMessage.Headers.GetValues("Some-Other-Header").SingleOrDefault());
            }

            using (HttpResponseMessage response = await session.GetAsync("/wat/123"))
            {
                Assert.IsTrue(response.RequestMessage.Headers.Contains("Some-Header"));
                Assert.AreEqual("An example value", response.RequestMessage.Headers.GetValues("Some-Header").SingleOrDefault());
                Assert.IsTrue(response.RequestMessage.Headers.Contains("Some-Other-Header"));
                Assert.AreEqual("Another example value", response.RequestMessage.Headers.GetValues("Some-Other-Header").SingleOrDefault());
            }

            using (HttpResponseMessage response = await session.PostAsync("/wat", new StringContent("body")))
            {
                Assert.IsTrue(response.RequestMessage.Headers.Contains("Some-Header"));
                Assert.AreEqual("An example value", response.RequestMessage.Headers.GetValues("Some-Header").SingleOrDefault());
                Assert.IsTrue(response.RequestMessage.Headers.Contains("Some-Other-Header"));
                Assert.AreEqual("Another example value", response.RequestMessage.Headers.GetValues("Some-Other-Header").SingleOrDefault());
            }

            using (HttpResponseMessage response = await session.PutAsync("/wat/123", new StringContent("body")))
            {
                Assert.IsTrue(response.RequestMessage.Headers.Contains("Some-Header"));
                Assert.AreEqual("An example value", response.RequestMessage.Headers.GetValues("Some-Header").SingleOrDefault());
                Assert.IsTrue(response.RequestMessage.Headers.Contains("Some-Other-Header"));
                Assert.AreEqual("Another example value", response.RequestMessage.Headers.GetValues("Some-Other-Header").SingleOrDefault());
            }

            using (HttpResponseMessage response = await session.DeleteAsync("/wat/123"))
            {
                Assert.IsTrue(response.RequestMessage.Headers.Contains("Some-Header"));
                Assert.AreEqual("An example value", response.RequestMessage.Headers.GetValues("Some-Header").SingleOrDefault());
                Assert.IsTrue(response.RequestMessage.Headers.Contains("Some-Other-Header"));
                Assert.AreEqual("Another example value", response.RequestMessage.Headers.GetValues("Some-Other-Header").SingleOrDefault());
            }
        }