mirror of
https://github.com/freedbygrace/PSOPNSenseAPI.git
synced 2026-07-26 11:58:18 +00:00
Add verbose logging of API endpoint URLs
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
RootModule = 'lib\PSOPNSenseAPI.dll'
|
||||
|
||||
# Version number of this module.
|
||||
ModuleVersion = '2025.04.16.2230'
|
||||
ModuleVersion = '2025.04.18.1015'
|
||||
|
||||
# Supported PSEditions
|
||||
CompatiblePSEditions = @('Desktop', 'Core')
|
||||
|
||||
@@ -35,6 +35,8 @@ namespace PSOPNSenseAPI.Services
|
||||
_logger.Information("Getting DHCP leases");
|
||||
|
||||
var endpoint = _apiEndpoints.GetEndpoint("dhcp.leases.list");
|
||||
var fullUrl = _apiEndpoints.GetFullUrl("dhcp.leases.list");
|
||||
_logger.Verbose($"API URL: {fullUrl}");
|
||||
return _apiClient.Get<DHCPLeaseListResponse>(endpoint);
|
||||
}
|
||||
|
||||
@@ -48,6 +50,8 @@ namespace PSOPNSenseAPI.Services
|
||||
_logger.Information($"Getting DHCP lease for MAC address {macAddress}");
|
||||
|
||||
var endpoint = _apiEndpoints.GetEndpoint("dhcp.leases.getByMac", macAddress);
|
||||
var fullUrl = _apiEndpoints.GetFullUrl("dhcp.leases.getByMac", macAddress);
|
||||
_logger.Verbose($"API URL: {fullUrl}");
|
||||
return _apiClient.Get<DHCPLeaseResponse>(endpoint);
|
||||
}
|
||||
|
||||
@@ -61,6 +65,8 @@ namespace PSOPNSenseAPI.Services
|
||||
_logger.Information($"Deleting DHCP lease for MAC address {macAddress}");
|
||||
|
||||
var endpoint = _apiEndpoints.GetEndpoint("dhcp.leases.delete", macAddress);
|
||||
var fullUrl = _apiEndpoints.GetFullUrl("dhcp.leases.delete", macAddress);
|
||||
_logger.Verbose($"API URL: {fullUrl}");
|
||||
return _apiClient.Post<DHCPLeaseDeleteResponse>(endpoint);
|
||||
}
|
||||
|
||||
@@ -85,7 +91,9 @@ namespace PSOPNSenseAPI.Services
|
||||
{
|
||||
_logger.Information("Getting DHCP servers");
|
||||
|
||||
var endpoint = "dhcp/service/get";
|
||||
var endpoint = "dhcpd/service/get";
|
||||
var fullUrl = _apiClient.GetFullUrl(endpoint);
|
||||
_logger.Verbose($"API URL: {fullUrl}");
|
||||
return _apiClient.Get<DHCPServerListResponse>(endpoint);
|
||||
}
|
||||
|
||||
@@ -98,7 +106,9 @@ namespace PSOPNSenseAPI.Services
|
||||
{
|
||||
_logger.Information($"Getting DHCP server for interface {@interface}");
|
||||
|
||||
var endpoint = $"dhcp/service/getServer/{@interface}";
|
||||
var endpoint = $"dhcpd/service/getServer/{@interface}";
|
||||
var fullUrl = _apiClient.GetFullUrl(endpoint);
|
||||
_logger.Verbose($"API URL: {fullUrl}");
|
||||
return _apiClient.Get<DHCPServerResponse>(endpoint);
|
||||
}
|
||||
|
||||
@@ -112,7 +122,9 @@ namespace PSOPNSenseAPI.Services
|
||||
{
|
||||
_logger.Information($"Updating DHCP server for interface {@interface}");
|
||||
|
||||
var endpoint = $"dhcp/service/setServer/{@interface}";
|
||||
var endpoint = $"dhcpd/service/setServer/{@interface}";
|
||||
var fullUrl = _apiClient.GetFullUrl(endpoint);
|
||||
_logger.Verbose($"API URL: {fullUrl}");
|
||||
var data = new { server = server };
|
||||
return _apiClient.Post<DHCPServerUpdateResponse>(endpoint, data);
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ using System;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
using PSOPNSenseAPI.Logging;
|
||||
using PSOPNSenseAPI.Models;
|
||||
@@ -43,6 +42,16 @@ namespace PSOPNSenseAPI.Services
|
||||
/// </summary>
|
||||
public bool IsConnected { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the full URL for the given endpoint
|
||||
/// </summary>
|
||||
/// <param name="endpoint">The endpoint</param>
|
||||
/// <returns>The full URL</returns>
|
||||
public string GetFullUrl(string endpoint)
|
||||
{
|
||||
return $"{BaseUrl}/api/{endpoint.TrimStart('/')}";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="OPNSenseApiClient"/> class
|
||||
/// </summary>
|
||||
@@ -92,15 +101,15 @@ namespace PSOPNSenseAPI.Services
|
||||
/// <typeparam name="T">The type to deserialize the response to</typeparam>
|
||||
/// <param name="endpoint">The API endpoint</param>
|
||||
/// <returns>The deserialized response</returns>
|
||||
public async Task<T> GetAsync<T>(string endpoint)
|
||||
public T Get<T>(string endpoint)
|
||||
{
|
||||
var url = $"{BaseUrl}/api/{endpoint.TrimStart('/')}";
|
||||
_logger.Debug($"GET {url}");
|
||||
|
||||
try
|
||||
{
|
||||
var response = await _httpClient.GetAsync(url);
|
||||
return await ProcessResponseAsync<T>(response);
|
||||
var response = _httpClient.GetAsync(url).GetAwaiter().GetResult();
|
||||
return ProcessResponse<T>(response);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -116,7 +125,7 @@ namespace PSOPNSenseAPI.Services
|
||||
/// <param name="endpoint">The API endpoint</param>
|
||||
/// <param name="data">The data to send</param>
|
||||
/// <returns>The deserialized response</returns>
|
||||
public async Task<T> PostAsync<T>(string endpoint, object data = null)
|
||||
public T Post<T>(string endpoint, object data = null)
|
||||
{
|
||||
var url = $"{BaseUrl}/api/{endpoint.TrimStart('/')}";
|
||||
_logger.Debug($"POST {url}");
|
||||
@@ -131,8 +140,8 @@ namespace PSOPNSenseAPI.Services
|
||||
content = new StringContent(json, Encoding.UTF8, "application/json");
|
||||
}
|
||||
|
||||
var response = await _httpClient.PostAsync(url, content);
|
||||
return await ProcessResponseAsync<T>(response);
|
||||
var response = _httpClient.PostAsync(url, content).GetAwaiter().GetResult();
|
||||
return ProcessResponse<T>(response);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -147,9 +156,9 @@ namespace PSOPNSenseAPI.Services
|
||||
/// <typeparam name="T">The type to deserialize the response to</typeparam>
|
||||
/// <param name="response">The HTTP response</param>
|
||||
/// <returns>The deserialized response</returns>
|
||||
private async Task<T> ProcessResponseAsync<T>(HttpResponseMessage response)
|
||||
private T ProcessResponse<T>(HttpResponseMessage response)
|
||||
{
|
||||
var content = await response.Content.ReadAsStringAsync();
|
||||
var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
|
||||
|
||||
_logger.Debug($"Response status: {(int)response.StatusCode} {response.StatusCode}");
|
||||
_logger.Debug($"Response body: {content}");
|
||||
@@ -201,3 +210,4 @@ namespace PSOPNSenseAPI.Services
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -198,6 +198,18 @@ namespace PSOPNSenseAPI.Services
|
||||
return parameters.Length > 0 ? string.Format(endpoint, parameters) : endpoint;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the full URL for the given endpoint key
|
||||
/// </summary>
|
||||
/// <param name="endpointKey">The endpoint key</param>
|
||||
/// <param name="parameters">Optional parameters to format into the endpoint</param>
|
||||
/// <returns>The full URL</returns>
|
||||
public string GetFullUrl(string endpointKey, params object[] parameters)
|
||||
{
|
||||
var endpoint = GetEndpoint(endpointKey, parameters);
|
||||
return $"{_apiClient.BaseUrl}/api/{endpoint}";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the OPNSense version
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user