/// <summary> /// Burns a markup JSON file into a document, producing a new PDF. /// </summary> /// <param name="sourceDocument">Existing <see cref="RemoteWorkFile" /> /// to use as the source document.</param> /// <param name="markupJson">Existing <see cref="RemoteWorkFile" /> /// containing the markup JSON you want burned into the source /// document.</param> /// <returns>RemoteWorkFile for the new PDF.</returns> public async Task <RemoteWorkFile> BurnMarkupAsync(RemoteWorkFile sourceDocument, RemoteWorkFile markupJson) { // Make sure we use the existing affinity token, if defined. AffinitySession affinitySession = this.restClient.CreateAffinitySession(sourceDocument.AffinityToken); // Make sure markupJson has the same affinity as the sourceDocument markupJson = await markupJson.GetInstanceWithAffinity(affinitySession, sourceDocument.AffinityToken); string json = this.BuildPostMarkupBurnersRequestJson(sourceDocument, markupJson); // Start the redaction creation process using (HttpResponseMessage response = await affinitySession.PostAsync("/PCCIS/V1/MarkupBurner", new StringContent(json, Encoding.UTF8, "application/json"))) { await this.ThrowIfPostMarkupBurnersError(response); json = await response.Content.ReadAsStringAsync(); } JObject process = JObject.Parse(json); string processId = (string)process["processId"]; // Wait for the process to complete using (HttpResponseMessage response = await affinitySession.GetFinalProcessStatusAsync($"/PCCIS/V1/MarkupBurner/{processId}")) { await this.ThrowIfGetMarkupBurnersError(response); json = await response.Content.ReadAsStringAsync(); } process = JObject.Parse(json); return(new RemoteWorkFile(affinitySession, (string)process["output"]["documentFileId"], affinitySession.AffinityToken, "pdf")); }
/// <summary> /// Automatically create redaction definitions for a document and a given set of text-matching rules, /// producing a new markup JSON file that can be used in a subsequent operation to actually apply the /// redaction definitions to the document. /// </summary> /// <param name="sourceDocument">Source document the redactions should be created for.</param> /// <param name="rules">Rules defining what content in the document should have a redaction region created for it.</param> /// <returns><see cref="RemoteWorkFile"/> for the created markup JSON file.</returns> public async Task <RemoteWorkFile> CreateRedactionsAsync(RemoteWorkFile sourceDocument, IEnumerable <RedactionMatchRule> rules) { RedactionMatchRule[] rulesArray = rules.ToArray(); // Make sure we use the existing affinity token, if defined. AffinitySession affinitySession = this.restClient.CreateAffinitySession(sourceDocument.AffinityToken); string json = this.BuildPostRedactionCreatorsRequestJson(sourceDocument, rules.ToArray()); // Start the redaction creation process using (HttpResponseMessage response = await affinitySession.PostAsync("/v2/redactionCreators", new StringContent(json, Encoding.UTF8, "application/json"))) { await this.ThrowIfPostRedactionCreatorsError(rulesArray, response); json = await response.Content.ReadAsStringAsync(); } JObject process = JObject.Parse(json); string processId = (string)process["processId"]; // Wait for the process to complete using (HttpResponseMessage response = await affinitySession.GetFinalProcessStatusAsync($"/v2/redactionCreators/{processId}")) { await this.ThrowIfGetRedactionCreatorsError(response); json = await response.Content.ReadAsStringAsync(); } process = JObject.Parse(json); return(new RemoteWorkFile(affinitySession, (string)process["output"]["markupFileId"], affinitySession.AffinityToken, "json")); }
public async Task PostAsync() { mockServer .Given(Request.Create().WithPath("/wat").UsingPost()) .RespondWith(Response.Create().WithStatusCode(200).WithBody(req => $"You POSTed: {req.Body}")); AffinitySession session = client.CreateAffinitySession(); using (HttpResponseMessage response = await session.PostAsync("/wat", new StringContent("Hello world!"))) { response.EnsureSuccessStatusCode(); Assert.AreEqual("You POSTed: Hello world!", await response.Content.ReadAsStringAsync()); } }
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()); } }
internal static async Task <WorkFile> PostWorkFile(this AffinitySession session, Stream document, string fileExtension = "txt") { // Remove leading period on fileExtension if present. if (fileExtension != null && fileExtension.StartsWith(".")) { fileExtension = fileExtension.Substring(1); } string json; using (var response = await session.PostAsync($"/PCCIS/V1/WorkFile?FileExtension={fileExtension}", new StreamContent(document))) { response.EnsureSuccessStatusCode(); json = await response.Content.ReadAsStringAsync(); } var workFileInfo = JObject.Parse(json); var workFile = new WorkFile((string)workFileInfo["fileId"], session); return(workFile); }
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()); } }