Afrowave.SharedTools.Api 0.0.3

dotnet add package Afrowave.SharedTools.Api --version 0.0.3
                    
NuGet\Install-Package Afrowave.SharedTools.Api -Version 0.0.3
                    
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="Afrowave.SharedTools.Api" Version="0.0.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Afrowave.SharedTools.Api" Version="0.0.3" />
                    
Directory.Packages.props
<PackageReference Include="Afrowave.SharedTools.Api" />
                    
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 Afrowave.SharedTools.Api --version 0.0.3
                    
#r "nuget: Afrowave.SharedTools.Api, 0.0.3"
                    
#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 Afrowave.SharedTools.Api@0.0.3
                    
#: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=Afrowave.SharedTools.Api&version=0.0.3
                    
Install as a Cake Addin
#tool nuget:?package=Afrowave.SharedTools.Api&version=0.0.3
                    
Install as a Cake Tool

Afrowave.SharedTools.Api

This module provides universal HTTP, API, and web interaction utilities for Afrowave projects. It includes both static and dependency-injectable (DI) variants for handling HTTP requests, cookies, and related web behaviors. All components are fully compatible with .NET Standard 2.1 and can be used in desktop, web, and service environments.


📦 Contents

Networking Core

  • Models/HttpRequestOptions.cs – Configuration for timeout, proxy, and retry policy
  • Models/RetryPolicyOptions.cs – Exponential retry with optional jitter
  • Interfaces/IHttpService.cs – Unified API interface for async HTTP operations
  • DI/HttpService.cs – Injectable service implementing the interface
  • Static/HttpClientHelper.cs – Lightweight static helper (no DI required)
  • Serialization/JsonOptions.cs – Preconfigured JSON serialization settings
  • Options/CookieSettings.cs – Defines domain, path, SameSite, and security policies with full enum-based configuration
  • Services/ICookieService.cs – Injectable cookie management interface for reading, writing, updating, and deleting cookies
  • Services/CookieService.cs – DI implementation supporting named profiles and long-lived cookies
  • Static/CookieHelper.cs – Static helper version for cookie operations without DI (ideal for controllers, scripts, or testing)

⚙️ Purpose

Afrowave.SharedTools.Api acts as the foundation for all HTTP, API, and web-based operations across Afrowave tools. It standardizes network access, proxy behavior, retry policies, and now includes high-level cookie management to ensure consistent, configurable, and secure session handling.


✅ IHttpService (DI)

A fully injectable HTTP service built around IHttpClientFactory. Supports JSON, XML, text, and binary transfers with automatic retry logic.

public class MyClient
{
    private readonly IHttpService _http;

    public MyClient(IHttpService http)
    {
        _http = http;
    }

    public async Task<Response<MyData>> GetUserAsync()
    {
        return await _http.GetJsonAsync<MyData>("https://api.example.com/user");
    }
}

Main methods:

  • GetJsonAsync<T>()
  • PostJsonAsync<TReq, TRes>()
  • GetStringAsync()
  • GetBytesAsync()
  • GetXmlAsync<T>()
  • PostFormUrlEncodedAsync()
  • GetJsonManyAsync<T>() (parallel requests)

Return type: All methods return Response<T> from Afrowave.SharedTools.Models, providing consistent success, data, and error metadata.


Starting with version 0.0.3, Afrowave.SharedTools.Api introduces a robust cookie management subsystem that simplifies HTTP cookie handling for ASP.NET Core and similar environments.

CookieSettings

Defines the behavior and security policies for all cookies.

public sealed class CookieSettings
{
    public string Domain { get; set; }
    public string Path { get; set; } = "/";
    public int ExpiryInDays { get; set; } = 30;
    public bool HttpOnly { get; set; } = true;
    public bool Secure { get; set; } = true;
    public bool IsEssential { get; set; } = true;
    public SameSitePolicy SameSite { get; set; } = SameSitePolicy.Lax;
    public CookieSecurePolicy SecurePolicy { get; set; } = CookieSecurePolicy.Always;
}

SameSitePolicy Enum:

  • None – Send cookie in all contexts (cross-site allowed)
  • Lax – Default modern behavior (safe for most cases)
  • Strict – Cookie only sent in first-party contexts

The settings can be bound via configuration or named options to define multiple cookie policies (e.g., for session, analytics, or consent).


ICookieService and CookieService (DI)

Dependency-injectable cookie manager using IOptionsMonitor<CookieSettings> for per-profile configurations. Supports automatic option binding and long-lived (≈20 years) cookie lifetimes.

Example:

builder.Services.AddHttpContextAccessor();

builder.Services.AddConfiguredService<ICookieService, CookieService, CookieSettings>(options =>
{
    options.Domain = ".afrowave.ltd";
    options.SameSite = CookieSettings.SameSitePolicy.Lax;
    options.ExpiryInDays = 30;
    options.HttpOnly = true;
});

Usage in controllers:

public class LocaleController : ControllerBase
{
    private readonly ICookieService _cookies;

    public LocaleController(ICookieService cookies) => _cookies = cookies;

    [HttpPost("/lang")]
    public IActionResult SetLang(string code)
    {
        var res = _cookies.Update("lang", code);
        return res.Success ? Ok() : BadRequest(res.Message);
    }

    [HttpGet("/lang")]
    public IActionResult GetLang()
    {
        var val = _cookies.ReadOrCreate("lang", "en");
        return Ok(val);
    }
}

Key methods:

  • Write(name, value) – writes cookie only if it does not exist
  • Update(name, value) – creates or overwrites cookie
  • Read(name) – returns cookie value or null
  • ReadOrCreate(name, default) – returns cookie or creates one with the default value
  • Delete(name) – removes cookie with matching policy
  • JSON helpers: UpdateObject<T>(), ReadObjectOrCreate<T>()

CookieHelper (Static)

A fully static version of the same functionality, usable without DI. Ideal for quick use in middleware, test utilities, or lightweight apps.

var val = CookieHelper.ReadOrCreate(HttpContext, "theme", "dark");
CookieHelper.Update(HttpContext, "session", Guid.NewGuid().ToString());

Methods mirror the DI version, taking HttpContext as the first argument.


🌐 Example Integration (combined)

services.AddHttpClient("AfrowaveHttpService");
services.AddSingleton(new HttpRequestOptions
{
    Timeout = TimeSpan.FromSeconds(30),
    DefaultHeaders = new Dictionary<string,string>
    {
        ["User-Agent"] = "Afrowave-HttpService/1.0"
    }
});

// HTTP service
services.AddSingleton<IHttpService, HttpService>();

// Cookie service
builder.Services.AddHttpContextAccessor();
services.AddConfiguredService<ICookieService, CookieService, CookieSettings>(cfg =>
{
    cfg.Domain = ".afrowave.ltd";
    cfg.SameSite = CookieSettings.SameSitePolicy.Lax;
    cfg.ExpiryInDays = 7;
});

🧱 Design Principles

  • Compatible with .NET Standard 2.1
  • Unified, predictable interfaces for both DI and static usage
  • Fully documented XML and markdown-based documentation
  • Secure defaults: HttpOnly = true, SameSite = Lax, Secure = true
  • Long-lived cookie helpers for persistent preferences
  • Seamless integration with IOptionsMonitor<T> for named profiles

📦 Version 0.0.3 Release Notes

Version Changes
0.0.3 Added complete cookie management subsystem:<br>• CookieSettings with enum-based policies<br>• ICookieService + CookieService (DI)<br>• CookieHelper (static)<br>• Improved documentation structure for web tools
0.0.2 Initial release – HTTP/REST utilities and retry logic

✍️ This file is part of the multilingual documentation system. Translations will be managed automatically by LangHub.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.0.3 226 10/8/2025
0.0.2 197 10/6/2025
0.0.1 197 10/6/2025