From 8c4c0a0e6ed708dd896adb261825b8b4f9a24d6d Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Tue, 19 Mar 2024 17:32:17 -0500 Subject: [PATCH 1/2] add google api --- .../Google/Models/GoogleAddressResult.cs | 14 +++++++ .../Google/Settings/GoogleApiSettings.cs | 9 +++++ .../BotSharp.Core/BotSharpCoreExtensions.cs | 3 -- .../BotSharpOpenApiExtensions.cs | 8 ++++ .../Controllers/AddressController.cs | 39 +++++++++++++++++++ src/WebStarter/appsettings.json | 7 ++++ 6 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 src/Infrastructure/BotSharp.Abstraction/Google/Models/GoogleAddressResult.cs create mode 100644 src/Infrastructure/BotSharp.Abstraction/Google/Settings/GoogleApiSettings.cs create mode 100644 src/Infrastructure/BotSharp.OpenAPI/Controllers/AddressController.cs diff --git a/src/Infrastructure/BotSharp.Abstraction/Google/Models/GoogleAddressResult.cs b/src/Infrastructure/BotSharp.Abstraction/Google/Models/GoogleAddressResult.cs new file mode 100644 index 000000000..ec156261e --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Google/Models/GoogleAddressResult.cs @@ -0,0 +1,14 @@ +namespace BotSharp.Abstraction.Google.Models; + +public class GoogleAddressResult +{ + public IList Results { get; set; } + public string Status { get; set; } +} + + +public class GoogleAddress +{ + [JsonPropertyName("formatted_address")] + public string FormatedAddress { get; set; } +} \ No newline at end of file diff --git a/src/Infrastructure/BotSharp.Abstraction/Google/Settings/GoogleApiSettings.cs b/src/Infrastructure/BotSharp.Abstraction/Google/Settings/GoogleApiSettings.cs new file mode 100644 index 000000000..d43ec58b4 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Google/Settings/GoogleApiSettings.cs @@ -0,0 +1,9 @@ +namespace BotSharp.Abstraction.Google.Settings; + +public class GoogleApiSettings +{ + public string ApiKey { get; set; } + public string Endpoint { get; set; } + public string Language { get; set; } + public string Components { get; set; } +} diff --git a/src/Infrastructure/BotSharp.Core/BotSharpCoreExtensions.cs b/src/Infrastructure/BotSharp.Core/BotSharpCoreExtensions.cs index 44177b093..2b56281cc 100644 --- a/src/Infrastructure/BotSharp.Core/BotSharpCoreExtensions.cs +++ b/src/Infrastructure/BotSharp.Core/BotSharpCoreExtensions.cs @@ -4,9 +4,6 @@ using BotSharp.Core.Plugins; using BotSharp.Abstraction.Settings; using BotSharp.Abstraction.Options; -using BotSharp.Abstraction.Messaging; -using System.Text.Json.Serialization; -using Microsoft.Extensions.Options; using BotSharp.Abstraction.Messaging.JsonConverters; namespace BotSharp.Core; diff --git a/src/Infrastructure/BotSharp.OpenAPI/BotSharpOpenApiExtensions.cs b/src/Infrastructure/BotSharp.OpenAPI/BotSharpOpenApiExtensions.cs index 0434976e6..63a9bc921 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/BotSharpOpenApiExtensions.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/BotSharpOpenApiExtensions.cs @@ -11,6 +11,8 @@ using Microsoft.OpenApi.Models; using Microsoft.IdentityModel.JsonWebTokens; using BotSharp.OpenAPI.BackgroundServices; +using BotSharp.Abstraction.Settings; +using BotSharp.Abstraction.Google.Settings; namespace BotSharp.OpenAPI; @@ -32,6 +34,12 @@ public static IServiceCollection AddBotSharpOpenAPI(this IServiceCollection serv services.AddScoped(); services.AddHostedService(); + services.AddScoped(provider => + { + var settingService = provider.GetRequiredService(); + return settingService.Bind("GoogleApi"); + }); + // Add bearer authentication var schema = "MIXED_SCHEME"; var builder = services.AddAuthentication(options => diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/AddressController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/AddressController.cs new file mode 100644 index 000000000..5d674b215 --- /dev/null +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/AddressController.cs @@ -0,0 +1,39 @@ +using BotSharp.Abstraction.Google.Models; +using BotSharp.Abstraction.Google.Settings; +using BotSharp.Abstraction.Options; + +namespace BotSharp.OpenAPI.Controllers; + +[Authorize] +[ApiController] +public class AddressController : ControllerBase +{ + private readonly IServiceProvider _services; + private readonly BotSharpOptions _options; + private readonly IHttpClientFactory _httpClientFactory; + + public AddressController(IServiceProvider services, + IHttpClientFactory httpClientFactory, + BotSharpOptions options) + { + _services = services; + _options = options; + _httpClientFactory = httpClientFactory; + } + + [HttpGet("/address/options")] + public async Task GetAddressOptions([FromQuery] string address) + { + var settings = _services.GetRequiredService(); + using var client = _httpClientFactory.CreateClient(); + var url = $"{settings.Endpoint}?key={settings.ApiKey}&" + + $"components={settings.Components}&" + + $"language={settings.Language}&" + + $"address={address}"; + + var response = await client.GetAsync(url); + var responseStr = await response.Content.ReadAsStringAsync(); + var result = JsonSerializer.Deserialize(responseStr, _options.JsonSerializerOptions); + return result; + } +} diff --git a/src/WebStarter/appsettings.json b/src/WebStarter/appsettings.json index 46340bc3e..ed24f1dbb 100644 --- a/src/WebStarter/appsettings.json +++ b/src/WebStarter/appsettings.json @@ -215,6 +215,13 @@ "ModelVersion": "V3_5" }, + "GoogleApi": { + "Endpoint": "https://maps.googleapis.com/maps/api/geocode/json", + "ApiKey": "", + "Components": "country=US|country=CA", + "Language": "en" + }, + "PluginLoader": { "Assemblies": [ "BotSharp.Core", From 7635886ff9b139b395ae2da4bc980ddf1a534d3e Mon Sep 17 00:00:00 2001 From: Jicheng Lu Date: Wed, 20 Mar 2024 00:00:29 -0500 Subject: [PATCH 2/2] add try catch --- .../Google/Models/GoogleAddressResult.cs | 2 +- .../Controllers/AddressController.cs | 29 +++++++++++++------ 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Google/Models/GoogleAddressResult.cs b/src/Infrastructure/BotSharp.Abstraction/Google/Models/GoogleAddressResult.cs index ec156261e..a327102dc 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Google/Models/GoogleAddressResult.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Google/Models/GoogleAddressResult.cs @@ -2,7 +2,7 @@ namespace BotSharp.Abstraction.Google.Models; public class GoogleAddressResult { - public IList Results { get; set; } + public IList Results { get; set; } = new List(); public string Status { get; set; } } diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/AddressController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/AddressController.cs index 5d674b215..755b8232e 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/AddressController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/AddressController.cs @@ -11,6 +11,7 @@ public class AddressController : ControllerBase private readonly IServiceProvider _services; private readonly BotSharpOptions _options; private readonly IHttpClientFactory _httpClientFactory; + private readonly ILogger _logger; public AddressController(IServiceProvider services, IHttpClientFactory httpClientFactory, @@ -24,16 +25,26 @@ public AddressController(IServiceProvider services, [HttpGet("/address/options")] public async Task GetAddressOptions([FromQuery] string address) { - var settings = _services.GetRequiredService(); - using var client = _httpClientFactory.CreateClient(); - var url = $"{settings.Endpoint}?key={settings.ApiKey}&" + - $"components={settings.Components}&" + - $"language={settings.Language}&" + - $"address={address}"; + var result = new GoogleAddressResult(); + + try + { + var settings = _services.GetRequiredService(); + using var client = _httpClientFactory.CreateClient(); + var url = $"{settings.Endpoint}?key={settings.ApiKey}&" + + $"components={settings.Components}&" + + $"language={settings.Language}&" + + $"address={address}"; + + var response = await client.GetAsync(url); + var responseStr = await response.Content.ReadAsStringAsync(); + result = JsonSerializer.Deserialize(responseStr, _options.JsonSerializerOptions); + } + catch (Exception ex) + { + _logger.LogError($"Error when calling google geocoding api... ${ex.Message}"); + } - var response = await client.GetAsync(url); - var responseStr = await response.Content.ReadAsStringAsync(); - var result = JsonSerializer.Deserialize(responseStr, _options.JsonSerializerOptions); return result; } }