CG.Infrastructure.Responses 3.10.8

dotnet add package CG.Infrastructure.Responses --version 3.10.8
                    
NuGet\Install-Package CG.Infrastructure.Responses -Version 3.10.8
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CG.Infrastructure.Responses" Version="3.10.8" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CG.Infrastructure.Responses" Version="3.10.8" />
                    
Directory.Packages.props
<PackageReference Include="CG.Infrastructure.Responses" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CG.Infrastructure.Responses --version 3.10.8
                    
#r "nuget: CG.Infrastructure.Responses, 3.10.8"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package CG.Infrastructure.Responses@3.10.8
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CG.Infrastructure.Responses&version=3.10.8
                    
Install as a Cake Addin
#tool nuget:?package=CG.Infrastructure.Responses&version=3.10.8
                    
Install as a Cake Tool

Infrastructure.Responses

A .NET 9.0 library that provides comprehensive HTTP response handling services for web API interactions, including authentication, entity management, and response processing.

Overview

This library offers a robust set of response services designed to handle various HTTP operations in .NET applications. It provides both basic HTTP response handling and advanced features like authentication token management, entity CRUD operations, and response serialization.

Features

Core Response Services

  • HTTP Response Handling: Process and extract content from HTTP responses
  • Entity Management: CRUD operations for various entity types
  • Authentication Support: OAuth2 client credentials and password flows
  • Response Serialization: Convert responses to strongly-typed entities
  • Error Handling: Comprehensive logging and exception management
  • Custom Header Support: HTTP requests with configurable custom headers

HTTP Operations

  • GET Operations: Retrieve entities and web responses
  • POST Operations: Create entities and return responses or GUIDs
  • PUT Operations: Update existing entities
  • DELETE Operations: Remove entities by ID
  • Response Processing: Convert responses to strings, arrays, or typed objects

Authentication & Security

  • OAuth2 Integration: Support for client credentials and password flows
  • Token Management: Handle authentication tokens and user info requests
  • Discovery Documents: OAuth2 discovery document support
  • Secure Password Handling: SecureString support for password operations

Response Processing

  • Content Extraction: Extract string content from HTTP responses
  • Array Processing: Handle array-based responses
  • File Output: Save responses to file paths
  • Type Conversion: Automatic deserialization to strongly-typed entities

Dependencies

  • CG.Infrastructure.Http (v3.10.5) - HTTP client and request handling
  • CG.Infrastructure.Services (v3.10.2) - Service layer abstractions
  • IdentityModel.Client - OAuth2 and OpenID Connect client library

Custom Header Integration

The custom header functionality integrates with CG.Infrastructure.Http:

  • HttpClientEnum.Authenticated: Used internally for custom header requests
  • CustomHeaderOptions: Configuration binding from appsettings.json
  • Automatic Registration: Custom headers are registered via RegisterInfraHttpServices
  • Fallback Behavior: Defaults to standard HTTP client if no custom headers configured

Key Components

IResponseService Interface

public interface IResponseService : IService
{
    // HTTP Response Operations
    Task<HttpResponseMessage?> GetWebResponse(string? requestUri);
    Task<HttpResponseMessage?> GetWebResponseWithCustomHeaders(string? requestUri);
    Task<string> GetResponseAsString(HttpResponseMessage response);
    Task<string[]> GetResponseAsStringArray(HttpResponseMessage response);
    
    // Entity CRUD Operations
    Task<TEntity?> GetEntity<TEntity>(string? requestUri, string? baseUrl, TokenResponse? token);
    Task<TEntity?> GetEntityFromPost<TEntity>(string? requestUri, TEntity? entity, string? baseUrl, TokenResponse? token);
    Task<TEntity?> GetEntityFromPut<TEntity>(string? requestUri, TEntity? entity, string? baseUrl, TokenResponse? token);
    Task DeleteEntity(string? requestUri, Guid? id, string? baseUrl, TokenResponse? token);
    
    // Authentication Operations
    Task<TokenResponse?> GetClientCredentialsToken(string clientId, string? clientSecret, string? clientScope);
    Task<TokenResponse?> GetClientPasswordToken(string clientId, string? clientSecret, string? clientScope, string userName, SecureString password);
    Task<UserInfoResponse?> GetUserInfoRequest(TokenResponse token);
}

ResponseService Implementation

  • Handles all HTTP operations with proper error handling
  • Integrates with logging, HTTP, serialization, and document services
  • Provides comprehensive response processing capabilities
  • Supports both authenticated and unauthenticated requests

ServiceCollectionExtensions

  • Provides dependency injection registration for response services
  • Integrates with HTTP service registration
  • Enables easy service configuration

Usage Examples

Basic HTTP Response Handling

var responseService = serviceProvider.GetService<IResponseService>();

// Get web response
var response = await responseService.GetWebResponse("https://api.example.com/data");

// Get web response with custom headers (API keys, custom Accept, etc.)
var authenticatedResponse = await responseService.GetWebResponseWithCustomHeaders("https://api.example.com/secure");

// Extract content as string
var content = await responseService.GetResponseAsString(response);

// Extract content as string array
var lines = await responseService.GetResponseAsStringArray(response);

Entity Operations

var responseService = serviceProvider.GetService<IResponseService>();

// Get entity with authentication
var entity = await responseService.GetEntity<MyEntity>(
    "api/entities/123", 
    "https://api.example.com", 
    authToken
);

// Create entity via POST
var newEntity = await responseService.GetEntityFromPost<MyEntity>(
    "api/entities", 
    entityData, 
    "https://api.example.com", 
    authToken
);

// Get GUID from POST response
var id = await responseService.GetGuidFromPost<MyEntity>(
    "api/entities", 
    entityData, 
    "https://api.example.com", 
    authToken
);

Authentication

var responseService = serviceProvider.GetService<IResponseService>();

// Get client credentials token
var token = await responseService.GetClientCredentialsToken(
    "clientId", 
    "clientSecret", 
    "api.scope"
);

// Get user info
var userInfo = await responseService.GetUserInfoRequest(token);

Custom Header Support

The GetWebResponseWithCustomHeaders method provides HTTP requests with configurable custom headers:

var responseService = serviceProvider.GetService<IResponseService>();

// Make authenticated request with custom headers
var response = await responseService.GetWebResponseWithCustomHeaders("https://api.example.com/protected");

// Custom headers are automatically applied based on configuration
// Common use cases include:
// - API key authentication (x-api-key, x-gg-bot-access)
// - Custom Accept headers
// - User-Agent specification
// - Custom authentication tokens

Configuration Required:

  • Custom headers must be configured in appsettings.json
  • Environment variables are supported for sensitive values
  • Headers are automatically applied when using this method
  • Falls back to default behavior if no custom headers are configured

Configuration

The library integrates with the .NET dependency injection container and can be configured through standard service registration:

// Register response services
services.RegisterInfraResponseServices(configuration);

// Or register individually
services.AddScoped<IResponseService, ResponseService>();

Custom Header Configuration

To use custom headers, configure them in your appsettings.json:

{
  "CustomHeaders": {
    "Headers": {
      "x-gg-bot-access": "{{ENV:GG_BOT_ACCESS_TOKEN}}",
      "Accept": "application/json",
      "User-Agent": "MyApp/1.0"
    }
  }
}

Environment Variable Support:

# Set environment variables
export GG_BOT_ACCESS_TOKEN="your-token-here"

# Or in Windows PowerShell
$env:GG_BOT_ACCESS_TOKEN="your-token-here"

Requirements

  • .NET 9.0 or later
  • Access to HTTP endpoints and services
  • OAuth2/OpenID Connect provider (for authentication features)
  • Proper configuration for HTTP services and serialization

Architecture

The library follows a layered architecture pattern:

  • Interface Layer: Defines contracts for response services
  • Service Layer: Implements business logic and HTTP operations
  • Extension Layer: Provides dependency injection configuration
  • Integration Layer: Works with HTTP, serialization, and document services

Best Practices

Custom Header Usage

  • Use environment variables for sensitive header values (API keys, tokens)
  • Configure fallback headers for consistent behavior
  • Test header configuration in development before production
  • Monitor header application through logging

Security Considerations

  • Never hardcode sensitive values in configuration files
  • Use environment variables for production deployments
  • Rotate API keys regularly and update environment variables
  • Validate header values to prevent injection attacks

Error Handling

  • Comprehensive logging for all operations
  • Graceful fallbacks for failed requests
  • Exception handling with detailed error messages
  • Null-safe response processing

License

This project is part of the CG Infrastructure Libraries and follows the same licensing terms as the parent project.

Contributing

Please refer to the main project documentation for contribution guidelines and coding standards.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on CG.Infrastructure.Responses:

Package Downloads
CG.Platform.ViewModels

Platform view models library with shared services

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.10.8 74 8/16/2025
3.10.7 101 8/15/2025
3.10.6 103 8/15/2025
3.10.5 139 8/10/2025
3.10.4 163 7/2/2025
3.10.3 162 6/17/2025
3.10.2 158 6/16/2025
3.10.1 302 6/12/2025
3.10.0 302 6/11/2025
3.9.0 143 12/10/2024
3.0.2 178 8/13/2024
3.0.1 164 8/12/2024
3.0.0 143 3/26/2024
2.0.0 269 6/2/2023
1.0.1 310 5/27/2022
1.0.0 251 5/27/2022