Skip to content

delly-net/Fetching

Repository files navigation

Delly.Fetching

NuGet License

简体中文 | English

A modern, simple, and powerful HTTP client library for .NET.

✨ Features

  • Simple API: Clean and intuitive interface for making HTTP requests
  • Multiple HTTP Methods: Built-in support for GET, POST, PUT, DELETE
  • Flexible Response Handling: Support for text and JSON deserialization
  • Request/Response Interceptors: Hook into the request and response lifecycle
  • Cross-platform: Supports .NET Standard 2.0 and .NET 5.0+
  • Modern Design: Built with async/await patterns

📦 Installation

Install the package via NuGet:

dotnet add package Delly.Fetching

Or via the NuGet Package Manager:

Install-Package Delly.Fetching

🚀 Quick Start

Basic GET Request

using Delly.Fetching;

// Simple GET request
string html = await FetchUtils.GetAsync("https://api.example.com/data");

// Using builder pattern
string html = await FetchUtils.Create("https://api.example.com/data")
    .Build()
    .GetAsync();

GET Request with JSON Response

// Get and deserialize JSON response
var user = await FetchUtils.GetAsync<User>("https://api.example.com/user/1");

POST Request with JSON Data

using Delly.Fetching;

// POST request with JSON data
var loginData = new
{
    userCode = "testuser",
    password = "testpass"
};

var response = await FetchUtils.Create("https://api.example.com/login")
    .AppendJsonData(loginData)
    .PostAsync();

// POST request with JSON data and custom serialization options
var options = new JsonSerializerOptions {
    PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};

var response = await FetchUtils.Create("https://api.example.com/users")
    .AppendJsonData(newUser, options)
    .PostAsync();

POST Request

using Delly.Fetching;

// Simple POST request
string response = await FetchUtils.PostAsync("https://api.example.com/users");

// POST request with JSON response
var newUser = await FetchUtils.PostAsync<User>("https://api.example.com/users");

Using the Builder Pattern

using Delly.Fetching;

// Create a client with custom configuration
var client = FetchUtils.Create("https://api.example.com/data")
    .Request(builder => {
        // Customize request
        builder.SetMethod(HttpMethod.Get);
        builder.OnBuilding(request => {
            request.Headers.Add("Authorization", "Bearer your-token");
        });
    })
    .Build();

// Send request
string response = await client.GetAsync();

PUT and DELETE Requests

// PUT request
string updated = await FetchUtils.PutAsync("https://api.example.com/users/1");

// DELETE request
string result = await FetchUtils.DeleteAsync("https://api.example.com/users/1");

// Using client
var client = FetchUtils.Create("https://api.example.com/users/1").Build();
await client.PutAsync();
await client.DeleteAsync();

🎯 Advanced Usage

JSON Request Body

Send JSON data easily with the AppendJsonData extension method:

using Delly.Fetching;

// Simple JSON POST request
var userData = new
{
    name = "John Doe",
    email = "john@example.com"
};

var response = await FetchUtils.Create("https://api.example.com/users")
    .AppendJsonData(userData)
    .PostAsync();

// JSON POST with custom serialization options
var options = new JsonSerializerOptions {
    PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
    WriteIndented = true
};

var response = await FetchUtils.Create("https://api.example.com/users")
    .AppendJsonData(userData, options)
    .PostAsync();

// JSON POST with additional headers
var response = await FetchUtils.Create("https://api.example.com/users")
    .Request(builder => {
        builder.AppendJsonData(userData);
        builder.OnBuilding(request => {
            request.Headers.Add("Authorization", "Bearer token");
        });
    })
    .PostAsync();

Request Interceptors

var client = FetchUtils.Create("https://api.example.com/data")
    .Build();

// Intercept and modify request before sending
client.OnRequesting(request => {
    request.Headers.Add("Custom-Header", "value");
    return request;
});

string response = await client.GetAsync();

Response Interceptors

var client = FetchUtils.Create("https://api.example.com/data")
    .Build();

// Intercept and modify response after receiving
client.OnResponsing(response => {
    // Log response status
    Console.WriteLine($"Status: {response.StatusCode}");
    return response;
});

string response = await client.GetAsync();

Custom HttpClientHandler

var builder = FetchUtils.Create("https://api.example.com/data");

// Set custom HTTP client handler
builder.SetHttpHandler(new HttpClientHandler {
    AutomaticDecompression = DecompressionMethods.GZip,
    AllowAutoRedirect = true,
    UseProxy = false
});

var client = builder.Build();
string response = await client.GetAsync();

JSON Deserialization with Custom Options

var options = new JsonSerializerOptions {
    PropertyNameCaseInsensitive = true,
    WriteIndented = true
};

var client = FetchUtils.Create("https://api.example.com/data")
    .Build();

// Get JSON with custom options
var data = await client.GetAsync<MyData>(options);

📋 API Reference

Static Methods (FetchUtils)

Method Description
Create(string url) Create a fetch builder with URL
GetAsync(string url) GET request returning string
GetAsync<T>(string url) GET request returning deserialized object
PostAsync(string url) POST request returning string
PostAsync<T>(string url) POST request returning deserialized object
PutAsync(string url) PUT request returning string
PutAsync<T>(string url) PUT request returning deserialized object
DeleteAsync(string url) DELETE request returning string
DeleteAsync<T>(string url) DELETE request returning deserialized object

Extension Methods

IFetchBuilder Extensions

Method Description
Request(Action<IFetchRequestBuilder>) Configure request builder
AppendJsonData<T>(T data, JsonSerializerOptions) Add JSON data to request body

IFetchRequestBuilder Extensions

Method Description
AppendJsonData<T>(T data, JsonSerializerOptions) Add JSON data to request body

Instance Methods (IFetchClient)

Method Description
GetAsync() GET request returning string
GetAsync<T>(JsonSerializerOptions) GET request returning deserialized object
PostAsync() POST request returning string
PostAsync<T>(JsonSerializerOptions) POST request returning deserialized object
PutAsync() PUT request returning string
PutAsync<T>(JsonSerializerOptions) PUT request returning deserialized object
DeleteAsync() DELETE request returning string
DeleteAsync<T>(JsonSerializerOptions) DELETE request returning deserialized object
SendAsync(HttpMethod) Send request with specified method
OnRequesting(Func) Register request interceptor
OnResponsing(Func) Register response interceptor

🏗️ Building from Source

# Clone the repository
git clone https://github.com/delly-net/Fetching.git
cd Fetching

# Restore dependencies
dotnet restore

# Build the solution
dotnet build

# Run tests
dotnet test

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

🔗 Links

🙏 Acknowledgments

Thanks to all contributors who have helped make this project better.


Made with ❤️ by delly.net

About

A http client for dotnet

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages