forked from SciSharp/BotSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoogleController.cs
More file actions
82 lines (70 loc) · 2.88 KB
/
GoogleController.cs
File metadata and controls
82 lines (70 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using BotSharp.Abstraction.Google.Models;
using BotSharp.Abstraction.Google.Settings;
using BotSharp.Abstraction.Options;
namespace BotSharp.OpenAPI.Controllers;
[Authorize]
[ApiController]
public class GoogleController : ControllerBase
{
private readonly IServiceProvider _services;
private readonly BotSharpOptions _options;
private readonly IHttpClientFactory _httpClientFactory;
private readonly ILogger _logger;
public GoogleController(IServiceProvider services,
IHttpClientFactory httpClientFactory,
BotSharpOptions options)
{
_services = services;
_options = options;
_httpClientFactory = httpClientFactory;
}
[HttpGet("/address/options")]
public async Task<GoogleAddressResult> GetAddressOptions([FromQuery] string address, [FromQuery] string language = "en")
{
var result = new GoogleAddressResult();
try
{
var settings = _services.GetRequiredService<GoogleApiSettings>();
using var client = _httpClientFactory.CreateClient();
var url = $"{settings.Map.Endpoint}?key={settings.ApiKey}&" +
$"components={settings.Map.Components}&" +
$"language={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;
}
[HttpGet("/video/options")]
public async Task<GoogleVideoResult> GetVideoOptions([FromQuery] string query, [FromQuery] int? maxResults, [FromQuery] string language = "en")
{
var result = new GoogleVideoResult();
try
{
var settings = _services.GetRequiredService<GoogleApiSettings>();
using var client = _httpClientFactory.CreateClient();
var url = $"{settings.Youtube.Endpoint}?key={settings.ApiKey}&" +
$"part={settings.Youtube.Part}&" +
$"relevanceLanguage={language}&" +
$"regionCode={settings.Youtube.RegionCode}&" +
$"q={query}";
if (maxResults.HasValue && maxResults > 0)
{
url += $"&maxResults={maxResults}";
}
var response = await client.GetAsync(url);
var responseStr = await response.Content.ReadAsStringAsync();
result = JsonSerializer.Deserialize<GoogleVideoResult>(responseStr, _options.JsonSerializerOptions);
}
catch (Exception ex)
{
_logger.LogError($"Error when calling google youtube search api... ${ex.Message}");
}
return result;
}
}