using Google.Apis.Auth.OAuth2; using Google.Apis.Http; using Google.Cloud.BigQuery.V2; using System.Net.Http.Headers; namespace Migration.Services; public class CustomHttpClientFactory : HttpClientFactory { private readonly string userAgent; public CustomHttpClientFactory(string userAgent) { if (string.IsNullOrWhiteSpace(userAgent)) { throw new ArgumentException("User-Agent cannot be null or empty", nameof(userAgent)); } this.userAgent = userAgent; } public new ConfigurableHttpClient CreateHttpClient(CreateHttpClientArgs args) { var httpClient = base.CreateHttpClient(args); httpClient.DefaultRequestHeaders.UserAgent.Clear(); httpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue(userAgent, "1.0")); return httpClient; } } public class GoogleBigQueryClientFactory : IGoogleBigQueryClientFactory { private readonly Lazy bigqueryClient; public GoogleBigQueryClientFactory() { bigqueryClient = new Lazy(CreateClient); } public BigQueryClient CreateClient() { var projectId = Environment.GetEnvironmentVariable("GOOGLE_PROJECT_ID") ?? throw new Exception("Brak identyfikatora projektu Google."); var userAgent = Environment.GetEnvironmentVariable("USER_AGENT") ?? throw new Exception("Nie podano UserAgent."); var credentialFile = Environment.GetEnvironmentVariable("GOOGLE_CREDENTIALS_JSON") ?? throw new Exception("Nie podano ścieżki do pliku GoogleCredentials."); var credential = GoogleCredential.FromJson(credentialFile); var httpClientFactory = new CustomHttpClientFactory(userAgent); return new BigQueryClientBuilder { ProjectId = projectId, Credential = credential, HttpClientFactory = httpClientFactory }.Build(); } public BigQueryTable GetTable(string datasetId, string tableId) { return bigqueryClient.Value.GetTable(datasetId, tableId); } }