RestCore 0.0.1
See the version list below for details.
dotnet add package RestCore --version 0.0.1
NuGet\Install-Package RestCore -Version 0.0.1
<PackageReference Include="RestCore" Version="0.0.1" />
<PackageVersion Include="RestCore" Version="0.0.1" />
<PackageReference Include="RestCore" />
paket add RestCore --version 0.0.1
#r "nuget: RestCore, 0.0.1"
#:package RestCore@0.0.1
#addin nuget:?package=RestCore&version=0.0.1
#tool nuget:?package=RestCore&version=0.0.1
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>withGetAsync,PostAsync,PutAsync,PatchAsync, andPostWithFileAsync - Request/response model —
ApiRequest/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
IsLogEnabledis 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— DeserializedT(or raw string if not JSON)ContentAsString— Raw response bodyIsSuccessful— True when HTTP status is successHeaders— Response headers (when created fromHttpResponseMessage)
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 | Versions 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. |
-
net8.0
- AngleSharp (>= 0.17.1)
- Microsoft.AspNetCore.Http.Features (>= 5.0.17)
- Microsoft.Extensions.Logging (>= 8.0.0)
- Newtonsoft.Json (>= 13.0.3)
- protobuf-net (>= 3.2.30)
- RestCoreCache (>= 1.0.0)
- StackExchange.Redis (>= 2.9.11)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Helpers for header settings created, name fixed