80 Commits

Author SHA1 Message Date
Developer 02
b78b3e43f4 Add support for custom headers in HTTP requests
Enhanced HTTP request handling by adding support for dynamically including custom headers from the `request.Headers` collection. Implemented a null check to ensure robustness and prevent null reference exceptions when processing headers. This change improves flexibility and customization of HTTP requests.
2025-12-01 09:45:41 +01:00
Developer 02
07afcf3aa2 Add logging for unexpected BodyQuery and IsReturnedNoData
Improve observability by adding a warning log when `request.BodyQuery` is not null but `request.IsReturnedNoData.BodyQuery` is false.

The log message highlights potential issues, such as skipped `BodyQueryBehavior` execution or missing control steps, and includes `ProfileId` and `ActionId` for better debugging context.
2025-11-29 01:12:03 +01:00
Developer 02
e0736ff6df Improve HeaderQueryBehavior error handling and logging
Updated HeaderQueryBehavior to handle null or missing
REQUEST_HEADER gracefully by logging warnings and setting
action.IsReturnedNoData.HeaderQuery to true instead of
throwing exceptions. Enhanced log messages for failed
RawHeader deserialization to improve clarity and added
pipeline continuity by calling await next(cancel).
Refactored log formatting for consistency.
2025-11-29 00:55:43 +01:00
Developer 02
0d801466cf Add properties for query tracking and post-processing
Added `IsReturnedNoData` property as a tuple to track the state
of `BodyQuery` and `HeaderQuery` data returns. Introduced
`PostprocessingQuery` property to support initialization of
post-processing queries. These changes enhance the functionality
of the `RecActionDto` class by enabling better query handling
and processing capabilities.
2025-11-29 00:36:03 +01:00
Developer 02
ff53be5d13 Throw exception for null RawHeader in HeaderQueryBehavior
Previously, the code continued execution when the RawHeader
property of the query result was null. This change introduces
an InvalidOperationException to handle this case, ensuring
that the absence of a valid RawHeader is treated as an error.
The exception message includes ProfileId and ActionId for
better debugging context.
2025-11-28 23:41:35 +01:00
Developer 02
cc787f445a Improve null-checking and error handling in Handle
Simplified null-checks in the `Handle` method of the
`BodyQueryBehavior` class using the null-conditional operator.
Enhanced the exception message to include `ProfileId` and
`ActionId` for better debugging context when `result?.RawBody`
is null.
2025-11-28 23:37:09 +01:00
Developer 02
97c57d4fb1 Add validation for BodyQuery result in Handle method
Previously, the `Handle` method in `BodyQueryBehavior<TRecAction>`
did not validate the `BodyQuery` result before setting `action.Body`.
This change introduces a check to ensure that the result and its
`RawBody` are not null. If either is null, an
`InvalidOperationException` is thrown with a clear error message.
This ensures `action.Body` is only set when a valid result is
retrieved, improving robustness and preventing potential null
reference issues.
2025-11-28 23:32:28 +01:00
Developer 02
ff3908cdd2 Support HTTP request body in InvokeRecActionCommandHandler
Added logic to handle non-null `request.Body` in HTTP requests.
Introduced a `StringContent` object to encapsulate the body
content and assigned it to the `Content` property of the
HTTP request. This ensures that the request body is included
when sending data to the specified endpoint.
2025-11-28 23:24:36 +01:00
6f5409f528 Implement HeaderQueryBehavior with database support
Updated `HeaderQueryBehavior` to include dependency injection
for `IRecDbContext` and `ILogger`, enabling database interaction
and logging. Replaced the placeholder `NotImplementedException`
in the `Handle` method with functionality to process `HeaderQuery`
SQL results, deserialize JSON headers, and assign them to the
`Headers` property of the action. Added error handling and
logging for deserialization failures. Updated constructor and
method signatures to support new dependencies and asynchronous
behavior. Added necessary `using` directives for new features.
2025-11-27 17:16:13 +01:00
03498275d7 Implement BodyQueryBehavior and add SQL Server support
Updated BodyQueryBehavior to process BodyQuery using the
IRecDbContext dependency. Added logic to execute SQL queries
via FromSqlRaw and assign results to the action's Body property.
Removed placeholder NotImplementedException.

Added Microsoft.EntityFrameworkCore.SqlServer package to
ReC.Application.csproj to enable SQL Server database operations.
2025-11-27 17:05:12 +01:00
f8581deaf9 Refactor DbContext and add IRecDbContext interface
Refactored `DependencyInjection` to use a generic `TRecDbContext`
for flexibility and added scoped registration for `IRecDbContext`.
Updated `RecDbContext` to implement the new `IRecDbContext`
interface, ensuring adherence to the application-layer contract.

Introduced the `IRecDbContext` interface in the application layer,
defining `DbSet` properties for key entities and a `SaveChangesAsync`
method. Updated `ReC.Infrastructure.csproj` to reference the
application project for interface access.
2025-11-27 16:51:49 +01:00
8ea7d47868 Add DbSets and configure keyless entity in RecDbContext
Added `HeaderQueryResults` and `BodyQueryResults` DbSet
properties to the `RecDbContext` class for interacting
with `HeaderQueryResult` and `BodyQueryResult` entities.

Overrode the `OnModelCreating` method to configure the
`RecAction` entity as keyless using
`modelBuilder.Entity<RecAction>().HasNoKey()`.
2025-11-27 16:40:33 +01:00
2bbfd96d62 Add keyless query result entities for headers and bodies
Added `HeaderQueryResult` and `BodyQueryResult` classes to represent
query results for headers and bodies, respectively. These classes
map their properties to database columns (`REQUEST_HEADER` and
`REQUEST_BODY`) using the `[Column]` attribute. Both properties
are nullable and immutable.

Updated `RecDbContext` to configure these entities as keyless
using the `HasNoKey()` method in the `OnModelCreating` method,
indicating they are used for read-only queries.
2025-11-27 16:37:30 +01:00
4f364e3eb2 Refactor behaviors to Common namespace
Moved `BodyQueryBehavior` and `HeaderQueryBehavior` from
`ReC.Application.RecActions.Behaviors` to
`ReC.Application.Common.Behaviors` to reflect their broader
applicability. Updated `DependencyInjection.cs` to use the new
namespace. This reorganization improves code structure and
maintainability.
2025-11-27 15:54:24 +01:00
0c4d145e20 Add HeaderQueryBehavior and update DI configuration
Added `HeaderQueryBehavior<TRecAction>` to handle header query
processing in the MediatR pipeline. Updated the `DependencyInjection`
class to register `HeaderQueryBehavior<>` alongside
`BodyQueryBehavior<>` in the MediatR configuration. The new behavior
currently throws a `NotImplementedException` as its logic is pending
implementation.
2025-11-27 15:47:12 +01:00
1757c0e055 Add BodyQueryBehavior and register in MediatR pipeline
Introduced the `BodyQueryBehavior<TRecAction>` class, which implements the `IPipelineBehavior` interface to handle actions of type `RecActionDto`. The behavior is currently unimplemented and throws a `NotImplementedException`.

Updated the `DependencyInjection` class to register `BodyQueryBehavior<>` as an open generic pipeline behavior in MediatR. Added necessary `using` directives in both `DependencyInjection.cs` and `BodyQueryBehavior.cs` to support the new behavior.
2025-11-27 15:46:07 +01:00
b1df50bc32 Enhance RecActionDto with HTTP and query handling
Added new properties to `RecActionDto`:
- `Headers` to store HTTP headers as a dictionary.
- `BodyQuery` to represent a query related to the body.
- `Body` to store the body content.
- `PostprocessingQuery` to represent a query for postprocessing.

These changes improve the class's ability to handle HTTP requests and related operations, providing greater flexibility for preprocessing and postprocessing tasks.
2025-11-27 15:30:54 +01:00
a66a70fab3 Refactor HTTP request handling in InvokeRecActionCommand
Replaced the `reqMsg` variable with `httpReq` to simplify the
creation and usage of `HttpRequestMessage`. The `request.RestType`
is now used directly to create `httpReq`, which is passed to
`http.SendAsync`. This change reduces redundancy and improves
code clarity.
2025-11-27 14:54:28 +01:00
a84b5531b7 Add ToHttpRequestMessage and refactor HTTP handling
Introduced a new extension method `ToHttpRequestMessage` in
`HttpExtensions` to simplify `HttpRequestMessage` creation
with URI validation. Added `System.Diagnostics.CodeAnalysis`
for `StringSyntaxAttribute` support.

Refactored `InvokeRecActionCommandHandler` to use the new
`ToHttpRequestMessage` method, improving readability and
encapsulation. Renamed `msg` to `reqMsg` for clarity.
2025-11-27 14:51:43 +01:00
21e3171e11 Refactor ToHttpMethod to use a dictionary for mappings
Replaced the switch statement in the ToHttpMethod method with
a case-insensitive dictionary for mapping HTTP method strings
to HttpMethod objects. Introduced a private static dictionary
to store predefined HTTP methods, including the addition of
the CONNECT method. Improved performance and maintainability
by leveraging dictionary lookups for faster and cleaner code.
2025-11-27 14:47:41 +01:00
bbfff226de Add ToEndpointUriBuilder method to RecActionDto
Introduce the ToEndpointUriBuilder() method in the RecActionDto
class to simplify the creation of a UriBuilder object. The method
configures the UriBuilder based on the EndpointUri and ProfileType
properties, setting the port to -1 and the scheme to ProfileType
if provided. This enhances the flexibility and usability of the
RecActionDto class for endpoint URI construction.
2025-11-27 14:37:35 +01:00
7a4885c86a Refactor HTTP method handling and cleanup
Refactored HTTP method handling by introducing a `ToHttpMethod`
extension method in `HttpExtensions.cs` to convert string
representations of HTTP methods to `HttpMethod` objects.
Replaced manual `HttpMethod` instantiation with the new
extension method in `InvokeRecActionCommandHandler` for
improved readability and reusability.

Removed unused `using` directives from `HttpExtensions.cs`
and cleaned up the `namespace` declaration. Added a `using`
directive for `ReC.Application.Common` in
`InvokeRecActionCommand.cs` to support the new extension method.
2025-11-27 11:45:15 +01:00
a46d97467d Ensure proper disposal and add HttpExtensions class
Updated `InvokeRecActionCommand.cs` to use `using` statements for `HttpClient`, `HttpRequestMessage`, and `HttpResponseMessage` to prevent resource leaks.

Added a new `HttpExtensions.cs` file with a static `HttpExtensions` class as a placeholder for future HTTP-related extension methods. Included necessary `using` directives for potential LINQ, collections, and asynchronous programming.
2025-11-27 11:32:14 +01:00
2f3a685e7d Refactor Rec Actions handling with MediatR support
Refactored the handling of "Rec Actions" by introducing the `InvokeRecActionCommandHandler` class and leveraging the MediatR library for command handling. Simplified the logic in `InvokeBatchRecActionsCommand.cs` by delegating HTTP request handling to the `sender.Send` method, which now uses `InvokeRecActionCommand`.

Updated `InvokeRecActionCommand` to implement the `IRequest` interface, enabling MediatR pipeline compatibility. Added `InvokeRecActionCommandExtensions` for converting `RecActionDto` to `InvokeRecActionCommand`, improving readability.

Centralized HTTP request logic in the new `InvokeRecActionCommandHandler`, which validates `RestType`, sends HTTP requests, and logs warnings for invalid actions. Removed unused `using` directives and added necessary ones for MediatR and logging.

This refactoring improves modularity, testability, and maintainability by separating concerns and streamlining the code structure.
2025-11-27 11:26:46 +01:00
d1e8f619f5 Refactor RecActionDto and add InvokeRecActionCommand
Converted RecActionDto from class to record for immutability
and value-based equality. Added nullable properties
`ActionId` and `ProfileId` to RecActionDto.

Introduced InvokeRecActionCommand.cs, which includes:
- A new InvokeRecActionCommand record inheriting from RecActionDto.
- Constructors for initializing InvokeRecActionCommand.
- An extension method `ToInvokeCommand` for converting
  RecActionDto to InvokeRecActionCommand.
2025-11-27 11:20:41 +01:00
0ec913b95e Refactor to support batch action invocation
Replaced `InvokeRecAction` with `InvokeBatchRecAction` in `ActionController` to transition from single-action to batch-action invocation.

Removed `InvokeRecActionCommand.cs`, which previously handled individual action invocations. Introduced `InvokeBatchRecActionsCommand.cs` to handle batch processing with similar concurrency control logic using a semaphore.

This redesign improves scalability and aligns with updated business requirements while maintaining compatibility with the existing application structure.
2025-11-27 11:05:21 +01:00
cb851a4ec6 Refactor RecAction commands and queries to use records
Refactored `InvokeRecActionCommand` and `ReadRecActionQuery`
to leverage C# `record` types for immutability and improved
data structure clarity. Introduced `ReadRecActionQueryBase`
as a shared base record to separate common properties and
logic. Updated `InvokeRecActionCommandHandler` to use the
new `ToReadQuery` method for compatibility. These changes
enhance code maintainability, reusability, and separation
of concerns while preserving existing functionality.
2025-11-27 10:56:21 +01:00
cd8f757c43 Add RecAction configuration support
Introduced a new `RecAction` configuration section in `appsettings.json` with a `MaxConcurrentInvocations` property. Updated `Program.cs` to use the `Configuration` object to pass the `RecAction` settings to the `AddRecServices` method. Replaced the `MediatRLicense` value in `appsettings.json` with no functional changes.
2025-11-27 10:12:44 +01:00
8aa43db909 Refactor and enhance DependencyInjection class
Organized the `DependencyInjection` class into regions for better code structure: `Required Services`, `Configuration`, `LuckyPennySoftwareLicenseKey`, and `ConfigureRecActions`.

Added `_requiredServices` dictionary to track the configuration status of required services. Updated `LuckyPennySoftwareLicenseKey` to include a null check before marking it as configured.

Moved `_configActions` to the `Configuration` region and added `ApplyConfigurations` to process queued configuration actions.

Enhanced `ConfigureRecActions` to prevent duplicate configurations by throwing an `InvalidOperationException` if already configured.

These changes improve code readability, maintainability, and robustness.
2025-11-27 10:09:20 +01:00
2a9b52ebe1 Ensure required services are configured in DI setup
Added `EnsureRequiredServices` to validate required services
in `ConfigurationOptions`, throwing an exception if any are
missing. Introduced `_requiredServices` dictionary to track
configuration status for `ConfigureRecActions` and
`LuckyPennySoftwareLicenseKey`. Updated property and methods
to mark services as configured. Integrated validation into
`AddRecServices` to enforce proper setup.
2025-11-27 09:58:49 +01:00
988d48581b Add configuration options and license key support
Updated `DependencyInjection.cs` to include support for
`ConfigurationOptions`, enabling queued configuration actions
and the ability to configure `RecActionOptions` via delegates
or `IConfiguration`. Added `LuckyPennySoftwareLicenseKey`
property to `ConfigurationOptions` and integrated it into
`AddAutoMapper` setup. Introduced `ApplyConfigurations`
method to process queued actions. Updated `using` directives
to include necessary namespaces.
2025-11-27 09:31:46 +01:00
46ef5e0d02 Add RecActionOptions class for concurrency configuration
A new `RecActionOptions` class was introduced in the
`ReC.Application.Common.Options` namespace. This class includes
a `MaxConcurrentInvocations` property with a default value of 5,
intended to configure the maximum number of concurrent invocations
allowed. This addition helps centralize and manage concurrency
settings in the application.
2025-11-27 09:20:13 +01:00
5e4f113145 Refactor namespaces for DTO classes
Updated `DtoMappingProfile` and `RecActionDto` to use the
`ReC.Application.Common.Dto` namespace instead of
`ReC.Application.Dto`. Adjusted `ReadRecActionQuery` to reflect
this namespace change. No functional changes were introduced.
2025-11-27 09:16:42 +01:00
3e9edcd8af Add error handling and logging to Rec action invocation
Enhanced the `InvokeRecActionCommandHandler` class by adding
a `catch` block to handle exceptions during HTTP request and
response processing. Logged exception details, including
`ProfileId` and `ActionId`, using `logger?.LogError` for
better observability. Ensured the `finally` block releasing
the semaphore remains unaffected.
2025-11-27 09:15:36 +01:00
Developer 02
dcc0e8c806 Add logging to handle null RestType in RecActionCommand
Enhanced the `InvokeRecActionCommandHandler` class by adding
logging functionality using `ILogger`. Updated the constructor
to accept an optional logger parameter. Introduced a check for
`RestType` being `null` and added a warning log to handle such
cases gracefully. This prevents processing invalid actions and
improves system robustness.
2025-11-26 21:54:22 +01:00
Developer 02
73ee0302ef Limit concurrent HTTP requests with SemaphoreSlim
Introduced a `SemaphoreSlim` with a concurrency limit of 5 to
control the number of simultaneous HTTP requests. Updated the
HTTP request logic to use `WaitAsync` and `Release` for
managing access to the critical section. Wrapped the HTTP
request handling in a `try-finally` block to ensure proper
semaphore release, even in case of exceptions. This change
improves resource management and prevents overwhelming the
HTTP client or the target server.
2025-11-26 21:49:09 +01:00
Developer 02
d80e0d3562 Refactor HTTP requests to enable parallel execution
Replaced sequential `foreach` loop with LINQ's `Select` to create a collection of tasks for sending HTTP requests asynchronously. Added `Task.WhenAll` to execute all requests concurrently, improving performance. Removed the old `foreach` implementation in favor of a functional programming approach.
2025-11-26 21:47:23 +01:00
Developer 02
1e35e4a057 Add HttpClient registration to DI container
Added a call to `services.AddHttpClient();` in the `DependencyInjection` class within `DependencyInjection.cs`. This change registers the `HttpClient` service in the dependency injection container, allowing the application to use managed `HttpClient` instances.
2025-11-26 21:44:24 +01:00
Developer 02
7c00060f74 Refactor HTTP handling in InvokeRecActionCommand
Updated `ReC.Application.csproj` to include `Microsoft.Extensions.Http` for improved HTTP client management. Refactored `InvokeRecActionCommandHandler` to use `IHttpClientFactory` for creating `HttpClient` instances, enhancing resource efficiency. Simplified HTTP method handling by replacing the switch statement with a dynamic `HttpMethod` and `HttpRequestMessage` approach. Removed redundant `using` directives and renamed `header` to `headers` for clarity. Removed `BadRequestException` for unsupported REST types, streamlining error handling.
2025-11-26 21:35:53 +01:00
Developer 02
0837bca6cb Add response body and headers processing
In the `InvokeRecActionCommandHandler` class, added code to read the response content as a string and store it in a `body` variable. Also, converted response headers into a dictionary and stored them in a `header` variable. These changes facilitate further processing of the response content and headers.
2025-11-26 17:49:01 +01:00
Developer 02
904448ca45 Add support for more HTTP methods
Extended the switch expression in `InvokeRecActionCommand.cs` to handle additional HTTP methods: "PATCH", "HEAD", "OPTIONS", and "TRACE".
Implemented the appropriate asynchronous HTTP requests using `HttpClient`.
"PATCH" uses `http.PatchAsync`, while "HEAD", "OPTIONS", and "TRACE" use `http.SendAsync` with a new `HttpRequestMessage`.
A `BadRequestException` is thrown for unsupported methods.
2025-11-26 17:34:52 +01:00
Developer 02
c16d58788a Refactor switch statement to switch expression
Replaced the `switch` statement with a `switch` expression to handle HTTP methods (`GET`, `POST`, `PUT`, `DELETE`). This refactoring reduces boilerplate code and improves readability by directly assigning the HTTP request result to the `response` variable, eliminating the need for multiple `using` and `break` statements.
2025-11-26 17:34:42 +01:00
Developer 02
a854720c75 Add DELETE request handling in command handler
Implemented the handling of DELETE HTTP requests in the
InvokeRecActionCommandHandler class. Previously, the DELETE
case was unhandled, and now it uses http.DeleteAsync to
send a DELETE request to the specified action.EndpointUri,
supporting cancellation with the cancel token.
2025-11-26 17:29:58 +01:00
Developer 02
c6bcdc3fac Add PUT request handling in InvokeRecActionCommandHandler
Implemented HTTP PUT request handling in the
InvokeRecActionCommandHandler class. Previously, the PUT case
only had a placeholder comment. Now, it uses
http.PutAsync to send a PUT request to the specified
action.EndpointUri, aligning it with the existing POST
request handling.
2025-11-26 17:29:29 +01:00
Developer 02
a57f53b6a1 Add POST request handling in InvokeRecActionCommandHandler
Previously, the handling of HTTP POST requests in the
InvokeRecActionCommandHandler class was not implemented.
This commit adds the necessary code to handle POST requests
by using the http.PostAsync method. The POST request is sent
to the specified action.EndpointUri with null content and a
cancellation token. This change ensures that POST operations
are now properly executed within the command handler.
2025-11-26 17:29:06 +01:00
Developer 02
eb402504ff Refactor HTTP request handling in action loop
Updated the HTTP request handling logic within the action loop to dynamically construct requests based on the `RestType` and `EndpointUri` of each action. Expanded the switch statement to handle GET, POST, PUT, and DELETE methods, allowing for specific logic to be implemented for each. Removed the hardcoded URL and response content retrieval, indicating potential refactoring of response handling. The default case continues to throw a `BadRequestException` for unsupported REST types.
2025-11-26 17:28:26 +01:00
Developer 02
5b7fce8fd6 Make RestType handling case-insensitive in switch
Convert RestType to uppercase in the switch statement to
ensure case-insensitive matching of REST types. This change
handles variations like "get", "Get", and "GET" consistently.
Additionally, use the null-conditional operator to prevent
NullReferenceException when RestType is null.
2025-11-26 17:23:51 +01:00
Developer 02
9a0a37471e Add REST action handling in InvokeRecActionCommand
Refactor `using` directives in `InvokeRecActionCommand.cs` to include `DigitalData.Core.Exceptions` and `System.Net.Http`.

Implement logic in `InvokeRecActionCommandHandler` to handle different REST actions using `HttpClient`. Introduce a `switch` statement to process "GET", "POST", "PUT", and "DELETE" actions, with a `BadRequestException` for unsupported types. Read HTTP response content asynchronously.
2025-11-26 17:23:14 +01:00
Developer 02
dcd399a92e add logic to read actions 2025-11-26 17:17:32 +01:00
Developer 02
59152a5621 umdate inveokeRecActionCommand 2025-11-26 17:16:01 +01:00