Files
PostManCloneApp/PostmanCloneLibrary/ApiAccess.cs

48 lines
1.2 KiB
C#

using System.Text.Json;
namespace PostmanCloneLibrary;
public class ApiAccess : IApiAccess
{
private readonly HttpClient _httpClient = new();
public async Task<string> CallApiAsync(
string url,
bool formaOutput = true,
HttpAction action = HttpAction.GET
)
{
var response = await _httpClient.GetAsync(url);
if (response.IsSuccessStatusCode)
{
string json = await response.Content.ReadAsStringAsync();
if (json != null && formaOutput)
{
var jsonElement = JsonSerializer.Deserialize<JsonElement>(json);
json = JsonSerializer.Serialize(jsonElement,
new JsonSerializerOptions { WriteIndented = true });
}
return json;
}
else
{
return $"Error: {response.StatusCode}";
}
}
public bool IsValidUrl(string url)
{
if (string.IsNullOrWhiteSpace(url))
{
return false;
}
bool output = Uri.TryCreate(url, UriKind.Absolute, out Uri uriOutput) &&
(uriOutput.Scheme == Uri.UriSchemeHttps);
return output;
}
}