https://github.com/coinbase-samples/core-dotnet
Coinbase .Net Core is a library that is used by other Java samples
https://github.com/coinbase-samples/core-dotnet
Last synced: 5 months ago
JSON representation
Coinbase .Net Core is a library that is used by other Java samples
- Host: GitHub
- URL: https://github.com/coinbase-samples/core-dotnet
- Owner: coinbase-samples
- License: apache-2.0
- Created: 2024-07-15T20:57:06.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2025-11-22T00:33:23.000Z (7 months ago)
- Last Synced: 2025-11-22T02:26:21.432Z (7 months ago)
- Language: C#
- Homepage: https://www.nuget.org/packages/CoinbaseSdk.Core
- Size: 80.1 KB
- Stars: 8
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Coinbase .NET Core Library
## Overview
The **Coinbase .NET Core Library** (`CoinbaseSdk.Core`) is the foundational building block for Coinbase .NET SDKs. It provides a standardized, robust, and thread-safe infrastructure for building API clients, handling authentication, HTTP communication, and serialization.
This library is primarily intended for internal use by Coinbase SDKs but is also available for advanced users building custom integrations requiring a standardized base.
## Key Features
* **Base Client Architecture**: Abstract `CoinbaseClient` managing request lifecycles with extensible hooks (`BuildRequest`, `ConfigureRequest`, `ValidateResponse`).
* **Resilient HTTP Communication**: Wrapper around `System.Net.Http.HttpClient` with built-in Polly retry policies for transient failure handling.
* **Robust Serialization**: `JsonUtility` wrapper around `System.Text.Json` featuring:
* `NullOnUnknownEnumConverter`: Graceful handling of new/unknown enum members.
* `UtcIso8601DateTimeOffsetConverter`: Standardized ISO-8601 date/time processing.
* Thread-safe initialization using `Lazy`.
* **Centralized Configuration**: `HttpClientDefaults` and `RetryPolicyDefaults` ensuring consistent default behavior across SDKs.
* **Standardized Error Handling**: Hierarchy of exceptions (`CoinbaseException`, `CoinbaseClientException`) for uniform error reporting.
## HTTP Client & Retries
The library uses `SystemNetHttpClient` to execute requests. By default, it performs a single attempt. You can enable retries using `CallOptions`, which leverages Polly's decorrelated jitter backoff strategy to handle transient failures (like rate limits or 5xx errors) without causing retry storms.
Retries can be configured at two levels:
1. **Per-Client**: Set default behavior for all requests made by a client instance.
2. **Per-Request**: Override behavior for a specific API call.
### Configuration Examples
**Per-Request Configuration:**
```csharp
var callOptions = new CallOptions
{
MaxRetries = 2,
ShouldRetryOnStatusCodes = true,
RetryableStatusCodes = new HashSet { HttpStatusCode.TooManyRequests }
};
// options are passed to the SendRequestAsync method
var response = await client.SendRequestAsync(..., callOptions);
```
**Per-Client Configuration:**
```csharp
var httpClient = new SystemNetHttpClient(
defaultCallOptions: new CallOptions
{
MaxRetries = 3,
ShouldRetryOnStatusCodes = true,
RetryableStatusCodes = new HashSet { HttpStatusCode.TooManyRequests }
});
var client = new MyCoinbaseClient(credentials, httpClient: httpClient);
```
### Retry Configuration Options
| Option | Default | Description |
| --- | --- | --- |
| `MaxRetries` | `0` | Maximum number of retry attempts (0 disables retries). |
| `MedianFirstRetryDelay` | 500ms | Target median delay for the first retry (subject to jitter). |
| `MaxRetryDelay` | 1s | Maximum delay cap for any retry attempt. |
| `ShouldRetryOnStatusCodes` | `false` | Whether to retry on specific HTTP status codes. |
| `RetryableStatusCodes` | empty | Set of status codes to retry (e.g., 429, 503). |
> **Note:** Retry attempts honor the `CancellationToken` provided to the request. If canceled, pending retries are aborted.
## Installation
```bash
dotnet add package CoinbaseSdk.Core
```
## License
This project is licensed under the [Apache License, Version 2.0](LICENSE).