Add User Agent

This commit is contained in:
l.gabrysiak 2025-03-24 21:14:46 +01:00
parent 011ddc1623
commit fdcca0655c
11 changed files with 165 additions and 36 deletions

View File

@ -1,7 +1,6 @@
using Microsoft.Extensions.Configuration; using Google.Apis.Auth.OAuth2;
using Google.Apis.Auth.OAuth2;
using Google.Cloud.BigQuery.V2; using Google.Cloud.BigQuery.V2;
using System.Linq.Expressions; using Google.Apis.Http;
namespace OSAFeedXML namespace OSAFeedXML
{ {
@ -38,22 +37,13 @@ namespace OSAFeedXML
private readonly string DomainRedirect; private readonly string DomainRedirect;
private readonly string GoogleProjectId, GoogleDatabase; private readonly string GoogleProjectId, GoogleDatabase;
public GoogleBigQuery() public GoogleBigQuery(BigQueryClient bigQueryClient)
{ {
string googleCredentialsJson = Environment.GetEnvironmentVariable("GOOGLE_CREDENTIALS_JSON") ?? throw new Exception("Brak danych uwierzytelniających Google.");
if (string.IsNullOrEmpty(googleCredentialsJson))
{
throw new InvalidOperationException("Brak danych uwierzytelniających Google.");
}
GoogleCredential googleCredential = GoogleCredential.FromJson(googleCredentialsJson);
GoogleProjectId = Environment.GetEnvironmentVariable("GOOGLE_PROJECT_ID") ?? throw new Exception("Brak identyfikatora projektu Google."); GoogleProjectId = Environment.GetEnvironmentVariable("GOOGLE_PROJECT_ID") ?? throw new Exception("Brak identyfikatora projektu Google.");
GoogleDatabase = Environment.GetEnvironmentVariable("GOOGLE_DATABASE") ?? throw new Exception("Brak nazwy bazy danych Google."); GoogleDatabase = Environment.GetEnvironmentVariable("GOOGLE_DATABASE") ?? throw new Exception("Brak nazwy bazy danych Google.");
bigqueryClient = BigQueryClient.Create(GoogleProjectId, googleCredential);
DomainRedirect = Environment.GetEnvironmentVariable("DOMAIN_REDIRECT") ?? throw new Exception("Brak domeny przekierowania."); DomainRedirect = Environment.GetEnvironmentVariable("DOMAIN_REDIRECT") ?? throw new Exception("Brak domeny przekierowania.");
this.bigqueryClient = bigQueryClient;
} }
public dynamic GetDataBigQuery(List<Destinations> Destinations, bool WithoutBg = false) public dynamic GetDataBigQuery(List<Destinations> Destinations, bool WithoutBg = false)

View File

@ -1,6 +1,7 @@
using Microsoft.Azure.Functions.Worker; using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Migration.Services;
using OSAFeedXML; using OSAFeedXML;
var host = new HostBuilder() var host = new HostBuilder()
@ -10,6 +11,13 @@ var host = new HostBuilder()
services.AddApplicationInsightsTelemetryWorkerService(); services.AddApplicationInsightsTelemetryWorkerService();
services.ConfigureFunctionsApplicationInsights(); services.ConfigureFunctionsApplicationInsights();
services.AddSingleton<IBigQuery, GoogleBigQuery>(); services.AddSingleton<IBigQuery, GoogleBigQuery>();
services.AddSingleton<IGoogleBigQueryClientFactory, GoogleBigQueryClientFactory>();
services.AddSingleton(sp =>
{
var factory = sp.GetRequiredService<IGoogleBigQueryClientFactory>();
return factory.CreateClient();
});
}) })
.Build(); .Build();

View File

@ -0,0 +1,8 @@
using Google.Cloud.BigQuery.V2;
namespace Migration.Services;
public interface IGoogleBigQueryClientFactory
{
BigQueryClient CreateClient();
BigQueryTable GetTable(string datasetId, string tableId);
}

View File

@ -0,0 +1,60 @@
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> bigqueryClient;
public GoogleBigQueryClientFactory()
{
bigqueryClient = new Lazy<BigQueryClient>(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);
}
}

View File

@ -1,5 +1,4 @@
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using Google.Apis.Auth.OAuth2;
using Google.Cloud.BigQuery.V2; using Google.Cloud.BigQuery.V2;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Migration.Services; using Migration.Services;
@ -15,18 +14,14 @@ public interface IBigQuery
public class GoogleBigQuery : IBigQuery public class GoogleBigQuery : IBigQuery
{ {
private readonly GoogleCredential googleCredential;
private readonly BigQueryClient bigqueryClient; private readonly BigQueryClient bigqueryClient;
private readonly string GoogleProjectId, GoogleDatabase; private readonly string GoogleDatabase;
private readonly IHttpClientFactory httpClientFactory; private readonly IHttpClientFactory httpClientFactory;
public GoogleBigQuery(IConfiguration configuration, IHttpClientFactory httpClientFactory) public GoogleBigQuery(IConfiguration configuration, IHttpClientFactory httpClientFactory, BigQueryClient bigQueryClient)
{ {
GoogleProjectId = configuration.GetValue<string>("BigQuery:ProjectId") ?? throw new Exception("Nie podano parametru identyfikatora projektu Google."); this.bigqueryClient = bigQueryClient;
GoogleDatabase = configuration.GetValue<string>("BigQuery:DbName") ?? throw new Exception("Nie podano parametru nazwy bazy danych BigQuery w konfiguracji"); GoogleDatabase = configuration.GetValue<string>("BigQuery:DbName") ?? throw new Exception("Nie podano parametru nazwy bazy danych BigQuery w konfiguracji");
googleCredential = GoogleCredential.FromFile(configuration.GetValue<string>("GoogleCredentialFile"));
bigqueryClient = BigQueryClient.Create(GoogleProjectId, googleCredential);
this.httpClientFactory = httpClientFactory; this.httpClientFactory = httpClientFactory;
} }

View File

@ -11,6 +11,14 @@ builder.Services.Configure<MainSettings>(builder.Configuration.GetSection(MainSe
builder.Services.AddSingleton<IDataverseProvider, DataverseProvider>(); builder.Services.AddSingleton<IDataverseProvider, DataverseProvider>();
builder.Services.AddHttpClient(DataverseProvider.HTTP_CLIENT, DataverseProvider.Setup); builder.Services.AddHttpClient(DataverseProvider.HTTP_CLIENT, DataverseProvider.Setup);
builder.Services.AddSingleton<IGoogleBigQueryClientFactory, GoogleBigQueryClientFactory>();
builder.Services.AddSingleton(sp =>
{
var factory = sp.GetRequiredService<IGoogleBigQueryClientFactory>();
var configuration = sp.GetRequiredService<IConfiguration>();
return factory.CreateClient(configuration);
});
var app = builder.Build(); var app = builder.Build();
if (app.Environment.IsDevelopment()) if (app.Environment.IsDevelopment())

View File

@ -1,10 +1,8 @@
using Migration.Settings; using Migration.Settings;
namespace Migration.Services namespace Migration.Services;
public interface IDataverseProvider
{ {
public interface IDataverseProvider
{
Uri Url { get; } Uri Url { get; }
string Token { get; } string Token { get; }
}
} }

View File

@ -0,0 +1,8 @@
using Google.Cloud.BigQuery.V2;
namespace Migration.Services;
public interface IGoogleBigQueryClientFactory
{
BigQueryClient CreateClient(IConfiguration configuration);
BigQueryTable GetTable(string datasetId, string tableId);
}

View File

@ -2,27 +2,21 @@
using System.Net.Mime; using System.Net.Mime;
using Migration.Settings; using Migration.Settings;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace Migration.Services; namespace Migration.Services;
public class DataverseProvider : IDataverseProvider public class DataverseProvider : IDataverseProvider
{ {
public const string HTTP_CLIENT = "FeedDataverseBQ"; public const string HTTP_CLIENT = "FeedDataverseBQ";
private static readonly ProductInfoHeaderValue USER_AGENT = ProductInfoHeaderValue.Parse("FeedDataverseBQ"); private static readonly ProductInfoHeaderValue USER_AGENT = ProductInfoHeaderValue.Parse("FeedDataverseBQ");
private static readonly MediaTypeWithQualityHeaderValue JSON_MEDIA_TYPE = new MediaTypeWithQualityHeaderValue(MediaTypeNames.Application.Json); private static readonly MediaTypeWithQualityHeaderValue JSON_MEDIA_TYPE = new MediaTypeWithQualityHeaderValue(MediaTypeNames.Application.Json);
public Uri Url { get; init; } public Uri Url { get; init; }
public string Token { get; init; } public string Token { get; init; }
public DataverseProvider(IOptions<MainSettings> mainSettingsOptions, IHttpClientFactory httpClientFactory) public DataverseProvider(IOptions<MainSettings> mainSettingsOptions, IHttpClientFactory httpClientFactory)
{ {
var DataverseSettings = mainSettingsOptions.Value; var DataverseSettings = mainSettingsOptions.Value;
Url = new Uri(DataverseSettings.Url); Url = new Uri(DataverseSettings.Url);
Token = DataverseSettings.Token; Token = DataverseSettings.Token;
} }
public static void Setup(IServiceProvider serviceProvider, HttpClient httpClient) public static void Setup(IServiceProvider serviceProvider, HttpClient httpClient)
{ {
var dataverseConnectionsProvider = serviceProvider.GetRequiredService<IDataverseProvider>(); var dataverseConnectionsProvider = serviceProvider.GetRequiredService<IDataverseProvider>();
@ -39,7 +33,6 @@ public class DataverseDelegatingHandler : DelegatingHandler
{ {
return SendAsync(request, cancellationToken).GetAwaiter().GetResult(); return SendAsync(request, cancellationToken).GetAwaiter().GetResult();
} }
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{ {
return await base.SendAsync(request, cancellationToken); return await base.SendAsync(request, cancellationToken);

View File

@ -0,0 +1,60 @@
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> bigqueryClient;
public GoogleBigQueryClientFactory(IConfiguration configuration)
{
bigqueryClient = new Lazy<BigQueryClient>(CreateClient(configuration));
}
public BigQueryClient CreateClient(IConfiguration configuration)
{
var projectId = configuration.GetValue<string>("BigQuery:ProjectId") ?? throw new Exception("Nie podano identyfikatora projektu Google.");
var userAgent = configuration.GetValue<string>("BigQuery:UserAgent") ?? throw new Exception("Nie podano UserAgent.");
var credentialFile = configuration.GetValue<string>("GoogleCredentialFile") ?? throw new Exception("Nie podano ścieżki do pliku GoogleCredentials.");
var credential = GoogleCredential.FromFile(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);
}
}

View File

@ -13,6 +13,7 @@
}, },
"BigQuery": { "BigQuery": {
"ProjectId": "", "ProjectId": "",
"UserAgent": "",
"DbName": "" "DbName": ""
} }
} }