initial release
This commit is contained in:
30
Services/SearxngClient.cs
Normal file
30
Services/SearxngClient.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System.Text.Json;
|
||||
using OpenQuery.Models;
|
||||
|
||||
namespace OpenQuery.Services;
|
||||
|
||||
public class SearxngClient
|
||||
{
|
||||
private readonly HttpClient _httpClient;
|
||||
private readonly string _baseUrl;
|
||||
|
||||
public SearxngClient(string baseUrl)
|
||||
{
|
||||
_baseUrl = baseUrl.TrimEnd('/');
|
||||
_httpClient = new HttpClient();
|
||||
}
|
||||
|
||||
public async Task<List<SearxngResult>> SearchAsync(string query, int limit = 10)
|
||||
{
|
||||
var encodedQuery = Uri.EscapeDataString(query);
|
||||
var url = $"{_baseUrl}/search?q={encodedQuery}&format=json";
|
||||
|
||||
var response = await _httpClient.GetAsync(url);
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
var results = JsonSerializer.Deserialize<SearxngRoot>(json, AppJsonContext.Default.SearxngRoot);
|
||||
|
||||
return results?.Results?.Take(limit).ToList() ?? [];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user