-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIngredientSearch.cs
39 lines (34 loc) · 1.12 KB
/
IngredientSearch.cs
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
using Algolia.Search.Clients;
using Algolia.Search.Models.Search;
namespace ingrEZ;
public class IngredientSearch(string? applicationId, string? apiKey)
{
readonly SearchClient client = new SearchClient(applicationId, apiKey);
private record IngredientResult
{
public required string IdIngredient { get; set; }
public required string ObjectID { get; set; }
public required string StrIngredient { get; set; }
public string? StrType { get; set; }
public string? StrDescription { get; set; }
}
public async Task<IEnumerable<string>> SearchIngredients(string query, CancellationToken token)
{
try
{
var search = await client.SearchSingleIndexAsync<IngredientResult>("ingredients",
new SearchParams(new SearchParamsObject
{
Query = query
}), cancellationToken: token);
return search.Hits.Select(i => i.StrIngredient).ToArray();
}
catch (Algolia.Search.Exceptions.AlgoliaApiException ex)
{
// Handle exceptions from the Algolia API here
// Log the exception message: ex.Message
Console.WriteLine(ex.Message);
return [];
}
}
}