-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShared.cs
47 lines (41 loc) · 1.88 KB
/
Shared.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
40
41
42
43
44
45
46
47
using Microsoft.AspNetCore.Mvc;
using Model;
using System.Net.Http;
using System.Threading.Tasks;
namespace PublicAPIs
{
public interface IShared
{
Task<IActionResult> SendRequest(string endpointType, RequestModel model);
Task<IActionResult> SendRequest(string endpointType);
}
public class Shared : IShared
{
private readonly HttpClient _client;
public Shared(IHttpClientFactory httpClientFactory)
{
_client = httpClientFactory.CreateClient();
}
public async Task<IActionResult> SendRequest(string endpointType, RequestModel model)
{
string url = $"https://api.publicapis.org/{endpointType}?";
url = model.title is null ? url : url + "title=" + model.title.Trim().ToLower() + "&";
url = model.description is null ? url : url + "description=" + model.description.Trim().ToLower() + "&";
url = model.auth is null ? url : url + "auth=" + model.auth.Trim().ToLower() + "&";
url = model.https is null ? url : url + "https=" + model.https.Trim().ToLower() + "&";
url = model.cors is null ? url : url + "cors=" + model.cors.Trim().ToLower() + "&";
url = model.category is null ? url : url + "category=" + model.category.Trim().ToLower() + "&";
url = url[..^1];
HttpResponseMessage response = await _client.GetAsync(url);
string result = await response.Content.ReadAsStringAsync();
return new OkObjectResult(result);
}
public async Task<IActionResult> SendRequest(string endpointType)
{
string url = $"https://api.publicapis.org/{endpointType}";
HttpResponseMessage response = await _client.GetAsync(url);
string result = await response.Content.ReadAsStringAsync();
return new OkObjectResult(result);
}
}
}