public async Task SaveToFile(string localFilePath) { using (var res = await session.GetAsync($"/PCCIS/V1/WorkFile/{FileId}")) { res.EnsureSuccessStatusCode(); using (var fileStream = File.OpenWrite(localFilePath)) { await res.Content.CopyToAsync(fileStream); } } }
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()); } }
/// <summary> /// Extracts text for each page, returning a string of plain text for each page in a RemoteWorkFile. /// </summary> public static async Task <string[]> ExtractPagesText(RemoteWorkFile remoteWorkFile) { AffinitySession session = Util.RestClient.CreateAffinitySession(); HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, "/v2/searchContexts"); if (remoteWorkFile.AffinityToken != null) { req.Headers.Add("Accusoft-Affinity-Token", remoteWorkFile.AffinityToken); } req.Content = new StringContent( @"{ ""input"": { ""documentIdentifier"": """ + remoteWorkFile.FileId + @""", ""source"": ""workFile"", ""fileId"": """ + remoteWorkFile.FileId + @""" } }", Encoding.UTF8, "application/json"); string json; using (HttpResponseMessage res = await session.SendAsync(req)) { res.EnsureSuccessStatusCode(); json = await res.Content.ReadAsStringAsync(); } JObject process = JObject.Parse(json); string contextId = (string)process["contextId"]; using (HttpResponseMessage res = await session.GetFinalProcessStatusAsync("/v2/searchContexts/" + contextId)) { res.EnsureSuccessStatusCode(); } using (HttpResponseMessage res = await session.GetAsync($"/v2/searchContexts/{contextId}/records?pages=0-")) { res.EnsureSuccessStatusCode(); json = await res.Content.ReadAsStringAsync(); } JObject data = JObject.Parse(json); JArray pages = (JArray)data["pages"]; return(pages.Select(x => (string)x["text"]).ToArray()); }
public async Task GetAsync() { mockServer .Given(Request.Create().WithPath("/wat/123").UsingGet()) .RespondWith(Response.Create().WithStatusCode(200).WithBody("GET Response")); AffinitySession session = client.CreateAffinitySession(); HttpResponseMessage response; response = await session.GetAsync("/wat/123"); response.EnsureSuccessStatusCode(); Assert.AreEqual("GET Response", await response.Content.ReadAsStringAsync()); }
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 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()); } }