/// <summary> /// Gets the results of a document read and writes them in the requested format /// to the specified path. /// </summary> /// <param name="resultsStream">The stream to write the results to</param> /// <param name="format">The format of the results required</param> /// <param name="cancellationToken"> /// The token to monitor for cancellation requests. The default value is /// <see cref="CancellationToken.None"/>. /// </param> /// <remarks>The Read method must be called before this method, otherwise a /// WaivesApiException will be thrown. /// </remarks> public async Task GetReadResultsAsync(Stream resultsStream, ReadResultsFormat format, CancellationToken cancellationToken = default) { if (resultsStream == null) { throw new ArgumentNullException(nameof(resultsStream)); } var readUrl = _behaviours["document:read"]; var request = new HttpRequestMessageTemplate(HttpMethod.Get, readUrl.CreateUri(), new Dictionary <string, string> { { "Accept", format.ToMimeType() } }); var response = await _requestSender.SendAsync(request, cancellationToken).ConfigureAwait(false); var responseBody = await response .Content .ReadAsStreamAsync() .ConfigureAwait(false); await responseBody .CopyToAsync(resultsStream) .ConfigureAwait(false); }
/// <summary> /// Gets the results of a document read and writes them in the requested format /// to the specified path. /// </summary> /// <param name="path">The path of the file to write the results to</param> /// <param name="format">The format of the results required</param> /// <param name="cancellationToken"> /// The token to monitor for cancellation requests. The default value is /// <see cref="CancellationToken.None"/>. /// </param> /// <remarks>The Read method must be called before this method, otherwise a /// WaivesApiException will be thrown. /// </remarks> public async Task GetReadResultsAsync(string path, ReadResultsFormat format, CancellationToken cancellationToken = default) { if (string.IsNullOrWhiteSpace(path)) { throw new ArgumentException("Value cannot be null or whitespace.", nameof(path)); } using (var fileStream = File.OpenWrite(path)) { await GetReadResultsAsync(fileStream, format, cancellationToken).ConfigureAwait(false); } }
public static string ToMimeType(this ReadResultsFormat format) { switch (format) { case ReadResultsFormat.Text: return("text/plain"); case ReadResultsFormat.Pdf: return("application/pdf"); default: return("application/vnd.waives.resultformats.read+zip"); } }
public async Task Get_read_results_sends_request_with_correct_url(ReadResultsFormat format, string expectedAcceptHeader) { _requestSender .SendAsync(Arg.Any <HttpRequestMessageTemplate>()) .Returns(ci => Response.GetReadResults(ci.Arg <HttpRequestMessageTemplate>(), $"Anonymous string {Guid.NewGuid()}")); await _sut.GetReadResultsAsync(_readResultsFilename, format); await _requestSender .Received(1) .SendAsync(Arg.Is <HttpRequestMessageTemplate>(m => m.Method == HttpMethod.Get && m.RequestUri.ToString() == _readUrl && m.Headers.Contains(new KeyValuePair <string, string>("Accept", expectedAcceptHeader)))); }