Mud.HttpUtils 1.5.3

dotnet add package Mud.HttpUtils --version 1.5.3
                    
NuGet\Install-Package Mud.HttpUtils -Version 1.5.3
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Mud.HttpUtils" Version="1.5.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Mud.HttpUtils" Version="1.5.3" />
                    
Directory.Packages.props
<PackageReference Include="Mud.HttpUtils" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Mud.HttpUtils --version 1.5.3
                    
#r "nuget: Mud.HttpUtils, 1.5.3"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Mud.HttpUtils@1.5.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Mud.HttpUtils&version=1.5.3
                    
Install as a Cake Addin
#tool nuget:?package=Mud.HttpUtils&version=1.5.3
                    
Install as a Cake Tool

Mud.HttpUtils

概述

Mud.HttpUtils 是一个基于 Roslyn 的源代码生成器,自动为标记了 [HttpClientApi] 特性的接口生成 HttpClient 实现类。

功能特性

  • 自动代码生成:根据接口定义自动生成 HttpClient 实现
  • HTTP 方法支持:支持 GET、POST、PUT、DELETE、PATCH、HEAD、OPTIONS 等 HTTP 方法
  • 参数处理:自动处理 Path、Query、Header、Body 参数
  • Token 管理:支持多种 Token 类型(TenantAccessToken、UserAccessToken、AppAccessToken、Both)
  • 依赖注入:自动生成服务注册扩展方法
  • 类型安全:强类型的 API 调用,编译时检查

使用方法

1. 定义 API 接口

[HttpClientApi("https://api.example.com", Timeout = 60)]
public interface IExampleApi
{
    [Get("/users/{id}")]
    Task<UserInfo> GetUserAsync([Path] int id);
    
    [Post("/users")]
    Task<UserInfo> CreateUserAsync([Body] CreateUserRequest request);
    
    [Get("/users")]
    Task<List<UserInfo>> GetUsersAsync([Query] string? name = null, [Query] int page = 1);
}

2. 注册服务

// 在 Program.cs 或 Startup.cs 中
services.AddWebApiHttpClient();

3. 使用 API

public class UserService
{
    private readonly IExampleApi _api;
    
    public UserService(IExampleApi api)
    {
        _api = api;
    }
    
    public async Task<UserInfo> GetUserAsync(int id)
    {
        return await _api.GetUserAsync(id);
    }
}

特性说明

HttpClientApi 特性

[HttpClientApi(
    baseUrl: "https://api.example.com",  // API 基础地址
    Timeout = 30,                        // 超时时间(秒)
    TokenManage = "ITokenManager",       // Token 管理器类型
    RegistryGroupName = "Example"         // 注册组名称
)]

HTTP 方法特性

  • [Get] - GET 请求
  • [Post] - POST 请求
  • [Put] - PUT 请求
  • [Delete] - DELETE 请求
  • [Patch] - PATCH 请求
  • [Head] - HEAD 请求
  • [Options] - OPTIONS 请求

参数特性

  • [Path] - URL 路径参数
  • [Query] - URL 查询参数
  • [Header] - HTTP 头参数
  • [Body] - 请求体参数
  • [Token] - Token 参数

Token 类型

public enum TokenType
{
    TenantAccessToken,  // 租户访问令牌
    UserAccessToken,    // 用户访问令牌
    AppAccessToken,     // 应用访问令牌
    Both                // 两者都支持
}

项目结构

Mud.HttpUtils/
├── HttpInvoke/
│   ├── Generators/          # 代码生成器
│   │   ├── InterfaceCodeGenerator.cs
│   │   ├── ClassStructureGenerator.cs
│   │   ├── ConstructorGenerator.cs
│   │   └── RequestBuilder.cs
│   ├── Helpers/             # 辅助类
│   │   ├── BaseClassValidator.cs
│   │   └── ParameterValidationHelper.cs
│   └── Models/              # 数据模型
│       ├── HttpClientApiInfo.cs
│       ├── HttpClientApiInfoBase.cs
│       ├── MethodAnalysisResult.cs
│       ├── ParameterAttributeInfo.cs
│       └── ParameterInfo.cs
├── HttpInvokeBaseSourceGenerator.cs
├── HttpInvokeClassSourceGenerator.cs
├── HttpInvokeRegistrationGenerator.cs
├── GlobalUsings.cs
├── Mud.HttpUtils.csproj
└── README.md

依赖项

  • .NET Standard 2.0
  • Microsoft.CodeAnalysis.Analyzers
  • Microsoft.CodeAnalysis.CSharp

版本历史

1.0.0

  • 初始版本
  • 从 Mud.ServiceCodeGenerator 项目中独立出来
  • 支持基本的 HTTP API 代码生成功能

许可证

本项目遵循 MIT 许可证。详细信息请参见 LICENSE 文件。

贡献

欢迎提交 Issue 和 Pull Request 来改进这个项目。

相关项目

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Mud.HttpUtils:

Package Downloads
Mud.Feishu

飞书服务端 SDK 的 .NET 适配版,支持.NET4.6+、.NET 5 至 .NET 10 平台。提供类型安全的 API 封装,让开发者能够在 .NET 应用程序中便捷、高效地集成飞书服务端功能。

Mud.Feishu.Abstractions

适用于 .NET 全平台的飞书事件订阅组件抽象层,提供 WebSocket 与 Webhook 双模式支持,专用于处理飞书事件订阅。该组件基于策略模式构建了完整的事件处理机制,使开发者能够便捷、高效地在 .NET 应用中接收和处理飞书实时事件。

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.5.3 4 2/25/2026
1.5.2 296 1/28/2026
1.5.1 299 1/24/2026
1.5.0 91 1/24/2026