rama creada apartir de master en j
This commit is contained in:
@@ -3,16 +3,49 @@ using Serilog;
|
||||
|
||||
namespace adas_core.module.ProxyDevices.Devices;
|
||||
|
||||
/// <summary>
|
||||
/// Device that can be used to get content via http with digest auth
|
||||
/// </summary>
|
||||
public class HttpDevice : IProxyDevice
|
||||
{
|
||||
/// <summary>
|
||||
/// Domain for digest auth. Optional, only used if username is provided
|
||||
/// </summary>
|
||||
private string _domain = "";
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether the device has been initialized with parameters. This is important to prevent multiple initializations and to ensure that the device is ready for processing requests.
|
||||
/// </summary>
|
||||
private bool _isInitialized;
|
||||
|
||||
/// <summary>
|
||||
/// Name of the device. Optional, can be used for logging or identification purposes.
|
||||
/// </summary>
|
||||
private string _name = "";
|
||||
|
||||
/// <summary>
|
||||
/// Password for digest auth. Optional, only used if username is provided
|
||||
/// </summary>
|
||||
private string _password = "";
|
||||
|
||||
/// <summary>
|
||||
/// URI of the device. This is a required parameter and must be a valid absolute URI. It represents the endpoint that the device will interact with when processing requests.
|
||||
/// </summary>
|
||||
private Uri _uri = null!;
|
||||
|
||||
/// <summary>
|
||||
/// Username for digest auth. Optional, if not provided, the device will attempt to access the URI without authentication.
|
||||
/// If provided, it will be used in conjunction with the password and domain (if specified) for authentication purposes.
|
||||
/// </summary>
|
||||
private string _username = "";
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the device with the provided parameters. The parameters are expected to be in a dictionary format, where the key is a string representing the parameter name and the value is an object that can be cast to the appropriate type.
|
||||
/// The method checks for the presence of required parameters (like "url") and validates them, throwing exceptions if any issues are found. Optional parameters (like "name", "username", "password", and "domain") are also processed if they are present in the dictionary.
|
||||
/// </summary>
|
||||
/// <param name="deviceParameters">A dictionary containing the parameters required to initialize the device.</param>
|
||||
/// <exception cref="InvalidOperationException">Thrown when the device is already initialized.</exception>
|
||||
/// <exception cref="ArgumentNullException">Thrown when a required parameter is missing or invalid.</exception>
|
||||
public void Initialize(Dictionary<string, object?> deviceParameters)
|
||||
{
|
||||
if (_isInitialized) throw new InvalidOperationException("Device already initialized");
|
||||
@@ -28,6 +61,13 @@ public class HttpDevice : IProxyDevice
|
||||
_isInitialized = true;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Processes a request by sending an HTTP GET request to the specified URI using the HttpClient. If authentication parameters were provided during initialization, they will be used for the request.
|
||||
/// The method handles potential timeouts by catching TaskCanceledException and rethrowing it as a TimeoutException with a descriptive message. The response from the HTTP request is returned as an HttpResponseMessage.
|
||||
/// </summary>
|
||||
/// <returns>An HttpResponseMessage representing the response from the HTTP request.</returns>
|
||||
/// <exception cref="TimeoutException">Thrown when the request times out.</exception>
|
||||
public async Task<HttpResponseMessage> Process()
|
||||
{
|
||||
try
|
||||
@@ -42,6 +82,13 @@ public class HttpDevice : IProxyDevice
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Streams content from the specified URI using an HTTP GET request with the option to read response headers immediately. This method is similar to Process(), but it allows for streaming the response content as it is received,
|
||||
/// which can be useful for large responses or when you want to start processing the data before the entire response is available.
|
||||
/// Like Process(), it handles potential timeouts by catching TaskCanceledException and rethrowing it as a TimeoutException with a descriptive message.
|
||||
/// </summary>
|
||||
/// <returns>An HttpResponseMessage representing the response from the HTTP request.</returns>
|
||||
/// <exception cref="TimeoutException">Thrown when the request times out.</exception>
|
||||
public async Task<HttpResponseMessage> Stream()
|
||||
{
|
||||
try
|
||||
@@ -57,6 +104,12 @@ public class HttpDevice : IProxyDevice
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates and configures an HttpClient instance based on the initialization parameters of the device.
|
||||
/// If authentication parameters (username, password, and optionally domain) were provided during initialization, they will be set in the HttpClientHandler's Credentials property to enable digest authentication for the HTTP requests.
|
||||
/// </summary>
|
||||
/// <returns>An HttpClient instance configured with the appropriate credentials.</returns>
|
||||
/// <exception cref="InvalidOperationException">Thrown when the device is not initialized.</exception>
|
||||
private HttpClient GetHttpClient()
|
||||
{
|
||||
if (!_isInitialized) throw new InvalidOperationException("Device not initialized");
|
||||
@@ -67,6 +120,10 @@ public class HttpDevice : IProxyDevice
|
||||
return new HttpClient(clientHandler);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a string representation of the HttpDevice instance, including its name, URI, username, password, and domain. This can be useful for logging or debugging purposes to quickly identify the configuration of the device.
|
||||
/// </summary>
|
||||
/// <returns>A string representation of the HttpDevice instance.</returns>
|
||||
public override string ToString()
|
||||
{
|
||||
return $"[HttpDevice] ({_name} {_uri} {_username} {_password} {_domain})";
|
||||
|
||||
@@ -1,8 +1,27 @@
|
||||
namespace adas_core.module.ProxyDevices.Devices;
|
||||
|
||||
/// <summary>
|
||||
/// Interface for a proxy device that can be used to process and stream data. This interface defines the methods that must be implemented by any class that wants to act as a proxy device.
|
||||
/// The Initialize method is used to set up the device with the necessary parameters, while the Process and Stream methods are used to handle data processing and streaming respectively.
|
||||
/// </summary>
|
||||
public interface IProxyDevice
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes the proxy device with the given parameters. This method should be called before any processing or streaming is done.
|
||||
/// The parameters can include any necessary configuration settings for the device, such as connection details, authentication information, or other relevant data.
|
||||
/// </summary>
|
||||
/// <param name="deviceParameters">A dictionary containing the parameters required to initialize the device.</param>
|
||||
void Initialize(Dictionary<string, object?> deviceParameters);
|
||||
|
||||
/// <summary>
|
||||
/// Processes the data received by the proxy device. This method should contain the logic for handling incoming data, performing any necessary transformations or computations, and preparing the data for further use.
|
||||
/// </summary>
|
||||
/// <returns>A task that represents the asynchronous operation. The task result contains the HttpResponseMessage returned by the device's Process method.</returns>
|
||||
Task<HttpResponseMessage> Process();
|
||||
|
||||
/// <summary>
|
||||
/// Streams data from the proxy device. This method should contain the logic for handling outgoing data, performing any necessary transformations or computations, and preparing the data for further use.
|
||||
/// </summary>
|
||||
/// <returns>A task that represents the asynchronous operation. The task result contains the HttpResponseMessage returned by the device's Stream method.</returns>
|
||||
Task<HttpResponseMessage> Stream();
|
||||
}
|
||||
Reference in New Issue
Block a user