/// <summary> /// Upload a <see cref="Stream"/>, creating a new <see cref="RemoteWorkFile"/>. /// </summary> internal static async Task <RemoteWorkFile> UploadAsync(this AffinitySession affinitySession, Stream documentStream, string fileExtension = "txt", string affinityToken = null) { // Remove leading period on fileExtension if present. if (fileExtension != null && fileExtension.StartsWith(".")) { fileExtension = fileExtension.Substring(1); } var req = new HttpRequestMessage(HttpMethod.Post, $"/PCCIS/V1/WorkFile?FileExtension={fileExtension}") { Content = new StreamContent(documentStream), }; if (affinityToken != null) { req.Headers.Add("Accusoft-Affinity-Token", affinityToken); } string json; using (HttpResponseMessage response = await affinitySession.SendAsync(req)) { await response.ThrowIfRestApiError(); json = await response.Content.ReadAsStringAsync(); } var info = JsonConvert.DeserializeObject <PostWorkFileResponse>(json); var remoteWorkFile = new RemoteWorkFile(affinitySession, info.fileId, info.affinityToken, info.fileExtension); return(remoteWorkFile); }
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 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()); } }