//using System; //using System.Collections.Generic; //using System.IO; //using System.Linq; //using System.Net.Http; //using System.Net.Http.Headers; //using System.Threading; //using System.Threading.Tasks; //namespace GB5Shared.JasperExport //{ // public class JasperExport : IJasperExport // { // private readonly HttpClient _httpClient; // private readonly string _jasperServerUrl; // private readonly string _jasperUser; // private readonly string _jasperPass; // public JasperExport(HttpClient httpClient, string jasperServerUrl, string jasperUser, string jasperPass) // { // _httpClient = httpClient; // _jasperServerUrl = jasperServerUrl.TrimEnd('/'); // _jasperUser = jasperUser; // _jasperPass = jasperPass; // } // public async Task ExportAsync( // string reportPath, // Dictionary parameters, // string format = "pdf", // CancellationToken cancellationToken = default) // { // using var request = BuildRequest(reportPath, parameters, format); // using var response = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseContentRead, cancellationToken); // if (!response.IsSuccessStatusCode) // { // var error = await response.Content.ReadAsStringAsync(cancellationToken); // throw new Exception($"Jasper error {response.StatusCode}: {error}"); // } // var bytes = await response.Content.ReadAsByteArrayAsync(cancellationToken); // if (bytes == null || bytes.Length == 0) // throw new Exception("Jasper returned empty content."); // return bytes; // } // public async Task ExportToStreamAsync( // string reportPath, // Dictionary parameters, // Stream outputStream, // string format = "pdf", // CancellationToken cancellationToken = default) // { // using var request = BuildRequest(reportPath, parameters, format); // using var response = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken); // if (!response.IsSuccessStatusCode) // { // var error = await response.Content.ReadAsStringAsync(cancellationToken); // throw new Exception($"Jasper error {response.StatusCode}: {error}"); // } // await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken); // await stream.CopyToAsync(outputStream, 128 * 1024, cancellationToken); // 128KB buffer // } // private HttpRequestMessage BuildRequest(string reportPath, Dictionary parameters, string format) // { // if (string.IsNullOrWhiteSpace(reportPath)) // throw new ArgumentException("Report path cannot be null or empty", nameof(reportPath)); // if (!reportPath.StartsWith("/")) // reportPath = "/" + reportPath; // string query = parameters?.Count > 0 // ? "?" + string.Join("&", parameters.Select(p => // $"{Uri.EscapeDataString(p.Key)}={Uri.EscapeDataString(p.Value?.ToString() ?? string.Empty)}")) // : string.Empty; // var url = $"{_jasperServerUrl}{reportPath}.{format}{query}"; // var request = new HttpRequestMessage(HttpMethod.Get, url); // var auth = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes($"{_jasperUser}:{_jasperPass}")); // request.Headers.Authorization = new AuthenticationHeaderValue("Basic", auth); // return request; // } // } //}