Skip to content

Commit 58c18c9

Browse files
authored
Merge pull request #351 from iceljc/features/add-google-api
add google api
2 parents dd66f10 + 7635886 commit 58c18c9

File tree

6 files changed

+88
-3
lines changed

6 files changed

+88
-3
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace BotSharp.Abstraction.Google.Models;
2+
3+
public class GoogleAddressResult
4+
{
5+
public IList<GoogleAddress> Results { get; set; } = new List<GoogleAddress>();
6+
public string Status { get; set; }
7+
}
8+
9+
10+
public class GoogleAddress
11+
{
12+
[JsonPropertyName("formatted_address")]
13+
public string FormatedAddress { get; set; }
14+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace BotSharp.Abstraction.Google.Settings;
2+
3+
public class GoogleApiSettings
4+
{
5+
public string ApiKey { get; set; }
6+
public string Endpoint { get; set; }
7+
public string Language { get; set; }
8+
public string Components { get; set; }
9+
}

src/Infrastructure/BotSharp.Core/BotSharpCoreExtensions.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
using BotSharp.Core.Plugins;
55
using BotSharp.Abstraction.Settings;
66
using BotSharp.Abstraction.Options;
7-
using BotSharp.Abstraction.Messaging;
8-
using System.Text.Json.Serialization;
9-
using Microsoft.Extensions.Options;
107
using BotSharp.Abstraction.Messaging.JsonConverters;
118

129
namespace BotSharp.Core;

src/Infrastructure/BotSharp.OpenAPI/BotSharpOpenApiExtensions.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
using Microsoft.OpenApi.Models;
1212
using Microsoft.IdentityModel.JsonWebTokens;
1313
using BotSharp.OpenAPI.BackgroundServices;
14+
using BotSharp.Abstraction.Settings;
15+
using BotSharp.Abstraction.Google.Settings;
1416

1517
namespace BotSharp.OpenAPI;
1618

@@ -32,6 +34,12 @@ public static IServiceCollection AddBotSharpOpenAPI(this IServiceCollection serv
3234
services.AddScoped<IUserIdentity, UserIdentity>();
3335
services.AddHostedService<ConversationTimeoutService>();
3436

37+
services.AddScoped(provider =>
38+
{
39+
var settingService = provider.GetRequiredService<ISettingService>();
40+
return settingService.Bind<GoogleApiSettings>("GoogleApi");
41+
});
42+
3543
// Add bearer authentication
3644
var schema = "MIXED_SCHEME";
3745
var builder = services.AddAuthentication(options =>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using BotSharp.Abstraction.Google.Models;
2+
using BotSharp.Abstraction.Google.Settings;
3+
using BotSharp.Abstraction.Options;
4+
5+
namespace BotSharp.OpenAPI.Controllers;
6+
7+
[Authorize]
8+
[ApiController]
9+
public class AddressController : ControllerBase
10+
{
11+
private readonly IServiceProvider _services;
12+
private readonly BotSharpOptions _options;
13+
private readonly IHttpClientFactory _httpClientFactory;
14+
private readonly ILogger _logger;
15+
16+
public AddressController(IServiceProvider services,
17+
IHttpClientFactory httpClientFactory,
18+
BotSharpOptions options)
19+
{
20+
_services = services;
21+
_options = options;
22+
_httpClientFactory = httpClientFactory;
23+
}
24+
25+
[HttpGet("/address/options")]
26+
public async Task<GoogleAddressResult> GetAddressOptions([FromQuery] string address)
27+
{
28+
var result = new GoogleAddressResult();
29+
30+
try
31+
{
32+
var settings = _services.GetRequiredService<GoogleApiSettings>();
33+
using var client = _httpClientFactory.CreateClient();
34+
var url = $"{settings.Endpoint}?key={settings.ApiKey}&" +
35+
$"components={settings.Components}&" +
36+
$"language={settings.Language}&" +
37+
$"address={address}";
38+
39+
var response = await client.GetAsync(url);
40+
var responseStr = await response.Content.ReadAsStringAsync();
41+
result = JsonSerializer.Deserialize<GoogleAddressResult>(responseStr, _options.JsonSerializerOptions);
42+
}
43+
catch (Exception ex)
44+
{
45+
_logger.LogError($"Error when calling google geocoding api... ${ex.Message}");
46+
}
47+
48+
return result;
49+
}
50+
}

src/WebStarter/appsettings.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,13 @@
215215
"ModelVersion": "V3_5"
216216
},
217217

218+
"GoogleApi": {
219+
"Endpoint": "https://maps.googleapis.com/maps/api/geocode/json",
220+
"ApiKey": "",
221+
"Components": "country=US|country=CA",
222+
"Language": "en"
223+
},
224+
218225
"PluginLoader": {
219226
"Assemblies": [
220227
"BotSharp.Core",

0 commit comments

Comments
 (0)