An open API service indexing awesome lists of open source software.

https://github.com/ethern-myth/efetch

efetch is a lightweight C# library for making HTTP requests with ease. It provides a simple interface for performing common HTTP operations such as GET, POST, PUT, PATCH, and DELETE. It also supports custom logging for requests, responses, and errors.
https://github.com/ethern-myth/efetch

csharp dotnet httpclient netcore restapi sdk singleton

Last synced: 20 days ago
JSON representation

efetch is a lightweight C# library for making HTTP requests with ease. It provides a simple interface for performing common HTTP operations such as GET, POST, PUT, PATCH, and DELETE. It also supports custom logging for requests, responses, and errors.

Awesome Lists containing this project

README

          

# efetch

**efetch** is a lightweight C# library for performing resilient HTTP requests with minimal boilerplate. It supports GET, POST, PUT, PATCH, DELETE operations, built-in retry policies via Polly, customizable headers, and optional structured logging.

![NuGet Version](https://img.shields.io/nuget/v/efetch)
![NuGet Downloads](https://img.shields.io/nuget/dt/efetch)

---

## ๐Ÿš€ Installation

Install via NuGet Package Manager:

```bash
Install-Package efetch
```

Or via .NET CLI:

```bash
dotnet add package efetch
```

---

## โœจ Features

- โœ… Clean abstraction with `IEfetch` interface
- ๐Ÿ” Retry support using Polly
- ๐Ÿ“ฆ JSON deserialization with support for primitives, objects, and arrays
- ๐Ÿ“ก Simple query parameter handling
- ๐Ÿ““ Optional request/response logging
- ๐Ÿงฉ Built-in support for dependency injection

---

## ๐Ÿงฉ Configuration

Add to your app's `appsettings.json`:

```json
"Efetch": {
"BaseUrl": "https://api.example.com",
"DefaultHeaders": {
"Authorization": "Bearer YOUR_TOKEN",
"Accept": "application/json"
},
"RetryCount": 3
}
```

Register `Efetch` in your DI container (`Program.cs`):

```csharp
builder.Services.AddEfetch(builder.Configuration);
```

---

## ๐Ÿงช Usage

### Inject `IEfetch`:

```csharp
public class VaultController : ControllerBase
{
private readonly IEfetch _efetch;

public VaultController(IEfetch efetch)
{
_efetch = efetch;
}

[HttpGet]
public async Task Get([FromQuery] string key)
{
var result = await _efetch.GetAsync("vault", null, new() { { "key", key } });
return Ok(result);
}
}
```

---

## ๐Ÿ“ฆ Example

```csharp
public class MyService
{
private readonly IEfetch _efetch;

public MyService(IEfetch efetch)
{
_efetch = efetch;
}

public async Task RunAsync()
{
// GET example
var result = await _efetch.GetAsync("/data");

// POST example
var body = new MyRequest { Name = "John" };
var response = await _efetch.PostAsync("/create", body);

// String response example
var plainText = await _efetch.GetAsync("/version");
}
}
```

---

## ๐Ÿ›  API

### `IEfetch`

- `GetAsync(...)`
- `PostAsync(...)`
- `PutAsync(...)`
- `PatchAsync(...)`
- `DeleteAsync(...)`

All methods support:
- Optional headers
- Optional query parameters
- Optional ID for REST-style routes
- Built-in retry policy

### `ILoggingProvider`

Optionally implement your own logger or use the built-in `ConsoleLoggingProvider`.

---

## ๐Ÿ”ง Advanced: Manual Configuration

```csharp
builder.Services.AddSingleton(new EfetchConfig
{
BaseUrl = "https://api.example.com",
DefaultHeaders = new()
{
{ "Authorization", "Bearer abc123" },
{ "Accept", "application/json" }
}
});

builder.Services.AddTransient();
```

---

## ๐Ÿ‘ค Author

**efetch** is created by [Ethern Myth](https://github.com/Ethern-Myth).

---

## ๐Ÿ“„ License

Licensed under the [MIT License](https://opensource.org/licenses/MIT).