Skip to content

Commit

Permalink
Merge pull request #351 from iceljc/features/add-google-api
Browse files Browse the repository at this point in the history
add google api
  • Loading branch information
Oceania2018 committed Mar 20, 2024
2 parents dd66f10 + 7635886 commit 58c18c9
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 3 deletions.
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 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 =>
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

0 comments on commit 58c18c9

Please sign in to comment.