Skip to content

Ethern-Myth/efetch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 NuGet Downloads


🚀 Installation

Install via NuGet Package Manager:

Install-Package efetch

Or via .NET CLI:

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:

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

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

builder.Services.AddEfetch(builder.Configuration);

🧪 Usage

Inject IEfetch:

public class VaultController : ControllerBase
{
    private readonly IEfetch _efetch;

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

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

📦 Example

public class MyService
{
    private readonly IEfetch _efetch;

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

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

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

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

🛠 API

IEfetch

  • GetAsync<T>(...)
  • PostAsync<T, TBody>(...)
  • PutAsync<T, TBody>(...)
  • PatchAsync<T, TBody>(...)
  • DeleteAsync<T>(...)

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

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

builder.Services.AddTransient<IEfetch, Efetch>();

👤 Author

efetch is created by Ethern Myth.


📄 License

Licensed under the MIT License.

About

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.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

Packages

 
 
 

Contributors

Languages