-
Notifications
You must be signed in to change notification settings - Fork 426
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #351 from iceljc/features/add-google-api
add google api
- Loading branch information
Showing
6 changed files
with
88 additions
and
3 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
src/Infrastructure/BotSharp.Abstraction/Google/Models/GoogleAddressResult.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
9 changes: 9 additions & 0 deletions
9
src/Infrastructure/BotSharp.Abstraction/Google/Settings/GoogleApiSettings.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/Infrastructure/BotSharp.OpenAPI/Controllers/AddressController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters