AS1024BGPCommunityDocPage/AS1024.CommunityDocumentati.../Controllers/HomeController.cs

77 lines
2.6 KiB
C#

using AS1024.CommunityDocumentationPage.Models;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json;
using AS1024.CommunityDocumentationPage.Interfaces;
using AS1024.CommunityDocumentationPage.DIScopes;
namespace AS1024.CommunityDocumentationPage.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly IBgpCommunityDocumentation _documentation;
private IConfiguration Configuration { get; }
public HomeController(ILogger<HomeController> logger,
IConfiguration configuration, IBgpCommunityDocumentation documentation)
{
_logger = logger;
Configuration = configuration;
_documentation = documentation;
}
public async Task<IActionResult> IndexAsync()
{
var results = await _documentation.GetBgpCommunities();
var filtered = results.Results.Where(b => b.Name.StartsWith(Configuration["ASN"]))
.ToList();
return View(filtered);
}
[NonAction]
private async Task<RouteTargets> ReadAPI(string token)
{
try
{
HttpClient client = new();
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Token", token);
var result = await client.GetAsync(BuildNetBoxURI().AbsoluteUri);
var stringResult = await result.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<RouteTargets>(stringResult);
} catch
{
throw new Exception("Something went wrong?");
}
}
[NonAction]
protected Uri BuildNetBoxURI()
{
var endUrl = new UriBuilder();
endUrl.Path = "/api/ipam/route-targets";
endUrl.Host = Configuration["NetBoxHost"];
endUrl.Scheme = "https";
return endUrl.Uri;
}
public IActionResult Privacy()
{
return View();
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}