RestCore 0.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package RestCore --version 0.0.1
                    
NuGet\Install-Package RestCore -Version 0.0.1
                    
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="RestCore" Version="0.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="RestCore" Version="0.0.1" />
                    
Directory.Packages.props
<PackageReference Include="RestCore" />
                    
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 RestCore --version 0.0.1
                    
#r "nuget: RestCore, 0.0.1"
                    
#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 RestCore@0.0.1
                    
#: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=RestCore&version=0.0.1
                    
Install as a Cake Addin
#tool nuget:?package=RestCore&version=0.0.1
                    
Install as a Cake Tool

RestCore

.NET 8 client library for HTTP APIs with built-in caching, offline storage, and a typed request/response model.

Features

  • HTTP client base — Abstract HttpClientBase<T> with GetAsync, PostAsync, PutAsync, PatchAsync, and PostWithFileAsync
  • Request/response modelApiRequest / ApiResponse<T> with JSON serialization, headers, and cancellation
  • Response caching — Optional cache per request via CacheSettings (uses RestCoreCache in-memory + Redis)
  • Offline fallback — Save responses locally and read them when the network fails (SaveForOfflineSettings)
  • Logging — Optional request/response logging when IsLogEnabled is true
  • RestCoreCache — In-memory, distributed (Redis), or hybrid cache; register with DI and use for cached API calls

Requirements

  • .NET 8.0
  • For hybrid/distributed cache: Redis (optionally with Sentinel)

Solution structure

Project Description
RestCore Core HTTP client and models
RestCoreCache Caching (in-memory, Redis, hybrid)
RestCoreTest Sample/test web app
RestCoreUnitTest Unit tests for RestCore & RestCoreCache

Installation

Add a project reference to RestCore (and optionally RestCoreCache for caching):

<ProjectReference Include="path\to\RestCore\RestCore.csproj" />
<ProjectReference Include="path\to\RestCoreCache\RestCoreCache.csproj" />

Or install the NuGet package when published:

dotnet add package RestCore

Quick start

1. Register services

// In-memory cache only
services.RegisterInMemoryCache();

// Or hybrid cache (in-memory + Redis) — requires appsettings (see Configuration)
services.RegisterHybridCache(services, configuration);

// Register HttpClient for your API client
services.AddHttpClient<MyApiClient>(client =>
{
    client.BaseAddress = new Uri("https://api.example.com/");
});

2. Implement your API client

Inherit from HttpClientBase<T> (namespace Obiwan.Clients.Core):

using Microsoft.Extensions.Logging;
using RestCore.Models;
using RestCoreCache.Services;

namespace MyApp.Clients;

public class MyApiClient(
    HttpClient httpClient,
    ILogger<MyApiClient> logger,
    IHybridCacheService cache)
    : HttpClientBase<MyApiClient>(httpClient, logger, cache)
{
    public async Task<ApiResponse<User>> GetUserAsync(string id, CancellationToken ct = default)
    {
        var request = new ApiRequest
        {
            Resource = $"users/{id}",
            Source = nameof(GetUserAsync)
        };
        return await GetAsync<User>(request);
    }

    public async Task<ApiResponse<CreateResult>> CreateUserAsync(User user, CancellationToken ct = default)
    {
        var request = new ApiRequest
        {
            Resource = "users",
            Source = nameof(CreateUserAsync),
            Payload = user
        };
        return await PostAsync<CreateResult>(request);
    }
}

3. Optional: cache a request

var request = new ApiRequest
{
    Resource = "users/1",
    Source = "GetUser",
    CacheSettings = new StorageItem
    {
        Key = "user:1",
        IsEnabled = true,
        ExpireTime = TimeSpan.FromMinutes(10)
    }
};
var response = await GetAsync<User>(request);

4. Optional: offline fallback

When the request fails, RestCore can return a previously saved value:

var request = new ApiRequest
{
    Resource = "config",
    Source = "GetConfig",
    SaveForOfflineSettings = new StorageItem
    {
        Key = "config:app",
        IsEnabled = true,
        ExpireTime = TimeSpan.FromHours(24)
    }
};

Configuration

Hybrid cache (Redis)

In appsettings.json:

{
  "CacheSettings": {
    "Provider": "Redis",
    "DbIndexForHybridCache": 0,
    "CachePrefix": "myapp:",
    "ConnectionTimeout": 5000,
    "AllowAdmin": false,
    "RedisSentinel": {
      "MasterName": "mymaster",
      "Password": "your-redis-password",
      "SentinelHosts": ["localhost:26379", "localhost:26380"]
    }
  }
}

If CacheSettings is missing or invalid, RegisterHybridCache throws. Use RegisterInMemoryCache() when you don’t need Redis.

API overview

ApiRequest

Property Description
Resource Path (relative to HttpClient.BaseAddress)
Source Logical source (e.g. method name) for logging
Payload Body object (serialized as JSON for POST/PUT/PATCH)
Headers Optional request headers
File Optional file for PostWithFileAsync
CacheSettings Enable caching with key and expiry
SaveForOfflineSettings Enable save/read for offline fallback
IsLogEnabled Enable request/response logging
CancellationToken Cancellation token

ApiResponse<T>

  • Content — Deserialized T (or raw string if not JSON)
  • ContentAsString — Raw response body
  • IsSuccessful — True when HTTP status is success
  • Headers — Response headers (when created from HttpResponseMessage)

Exceptions

  • NotFoundException(key, objName) — For “entity not found by key” scenarios.

Building and testing

# Restore and build
dotnet restore
dotnet build

# Run unit tests
dotnet test RestCoreUnitTest/RestCoreUnitTest.csproj

License

See repository or package metadata for license information.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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. 
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.2 118 3/2/2026
0.0.1 108 3/2/2026

Helpers for header settings created, name fixed