Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace BotSharp.Abstraction.Google.Models;

public class GoogleAddressResult
{
public IList<GoogleAddress> Results { get; set; } = new List<GoogleAddress>();
public string Status { get; set; }
}


public class GoogleAddress
{
[JsonPropertyName("formatted_address")]
public string FormatedAddress { get; set; }
}
Original file line number Diff line number Diff line change
@@ -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; }
}
3 changes: 0 additions & 3 deletions src/Infrastructure/BotSharp.Core/BotSharpCoreExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -32,6 +34,12 @@ public static IServiceCollection AddBotSharpOpenAPI(this IServiceCollection serv
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 =>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
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;
private readonly ILogger _logger;

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 result = new GoogleAddressResult();

try
{
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();
result = JsonSerializer.Deserialize<GoogleAddressResult>(responseStr, _options.JsonSerializerOptions);
}
catch (Exception ex)
{
_logger.LogError($"Error when calling google geocoding api... ${ex.Message}");
}

return result;
}
}
7 changes: 7 additions & 0 deletions src/WebStarter/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down