add google api

This commit is contained in:
Jicheng Lu 2024-03-19 17:32:17 -05:00
parent dd66f10d83
commit 8c4c0a0e6e
6 changed files with 77 additions and 3 deletions

View file

@ -0,0 +1,14 @@
namespace BotSharp.Abstraction.Google.Models;
public class GoogleAddressResult
{
public IList<GoogleAddress> Results { get; set; }
public string Status { get; set; }
}
public class GoogleAddress
{
[JsonPropertyName("formatted_address")]
public string FormatedAddress { get; set; }
}

View file

@ -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; }
}

View file

@ -4,9 +4,6 @@ using Microsoft.Extensions.Configuration;
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;

View file

@ -11,6 +11,8 @@ using Microsoft.Net.Http.Headers;
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 class BotSharpOpenApiExtensions
services.AddScoped<IUserIdentity, UserIdentity>();
services.AddHostedService<ConversationTimeoutService>();
services.AddScoped(provider =>
{
var settingService = provider.GetRequiredService<ISettingService>();
return settingService.Bind<GoogleApiSettings>("GoogleApi");
});
// Add bearer authentication
var schema = "MIXED_SCHEME";
var builder = services.AddAuthentication(options =>

View file

@ -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<GoogleAddressResult> GetAddressOptions([FromQuery] string address)
{
var settings = _services.GetRequiredService<GoogleApiSettings>();
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<GoogleAddressResult>(responseStr, _options.JsonSerializerOptions);
return result;
}
}

View file

@ -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",