Baby steps on code refactoring
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
using AS1024.CommunityDocumentationPage.Interfaces;
|
||||
using AS1024.CommunityDocumentationPage.Models;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace AS1024.CommunityDocumentationPage.DIScopes
|
||||
{
|
||||
public class NetboxBgpCommunityDocumentation : IBgpCommunityDocumentation
|
||||
{
|
||||
private readonly IConfiguration configuration;
|
||||
|
||||
public string DcimName => "netbox";
|
||||
private HttpClient client;
|
||||
|
||||
public NetboxBgpCommunityDocumentation(IConfiguration configuration)
|
||||
{
|
||||
this.configuration = configuration;
|
||||
client = new HttpClient();
|
||||
}
|
||||
|
||||
public async Task<RouteTargets> GetBgpCommunities()
|
||||
{
|
||||
client.DefaultRequestHeaders.Clear();
|
||||
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
||||
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Token", configuration["APIKey"]);
|
||||
var result = await client.GetAsync(BuildNetBoxURI().AbsoluteUri);
|
||||
var stringResult = await result.Content.ReadAsStringAsync();
|
||||
#pragma warning disable CS8603 // Possible null reference return.
|
||||
return JsonConvert.DeserializeObject<RouteTargets>(stringResult);
|
||||
#pragma warning restore CS8603 // Possible null reference return.
|
||||
}
|
||||
|
||||
protected Uri BuildNetBoxURI()
|
||||
{
|
||||
var endUrl = new UriBuilder
|
||||
{
|
||||
Path = "/api/ipam/route-targets",
|
||||
Host = configuration["NetBoxHost"],
|
||||
Scheme = "https"
|
||||
};
|
||||
|
||||
return endUrl.Uri;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
namespace AS1024.CommunityDocumentationPage.DIScopes
|
||||
{
|
||||
public static class ValidateBgpCommunity
|
||||
{
|
||||
/// <summary>
|
||||
/// validates if this is a valid standard BGP Community
|
||||
/// </summary>
|
||||
/// <param name="Community">Community string represented as NNN:XXX</param>
|
||||
/// <returns>true if it's a valid bgp community, false if it isn't</returns>
|
||||
public static bool IsValidBgpCommunity(this string Community)
|
||||
{
|
||||
var splitStrings =
|
||||
Community.Split(":", StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
for (int i = 0; i < 1; i++)
|
||||
{
|
||||
if (!int.TryParse(splitStrings[i], out int communityParsed)
|
||||
&& communityParsed > 65536)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user