From f2295c3084e88048bd59fa76f29016ad17d21ef8 Mon Sep 17 00:00:00 2001 From: wjqserver <114663932+WJQSERVER@users.noreply.github.com> Date: Tue, 21 Apr 2026 22:55:26 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20httpc=20=E9=9B=86=E6=88=90=E6=94=B9?= =?UTF-8?q?=E8=BF=9B=EF=BC=8C=E8=87=AA=E5=8A=A8=E5=85=B3=E8=81=94=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=20Context?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 contextHTTPClient 包装器,自动关联请求 Context - 新增 Context.HTTPC() 方法返回 contextHTTPClient - Client() 标记为 Deprecated - 添加 GetHTTPC() go:fix inline 兼容函数 当请求被取消时,出站 HTTP 请求也会自动取消。 --- compat.go | 17 +++++++++++++- context.go | 18 ++++++++++----- context_httpc.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 7 deletions(-) create mode 100644 context_httpc.go diff --git a/compat.go b/compat.go index 6a49c89..4e40687 100644 --- a/compat.go +++ b/compat.go @@ -4,7 +4,12 @@ // All rights reserved by WJQSERVER, related rights can be exercised by the infinite-iroha organization. package touka -import "github.com/fenthope/reco" +import ( + "github.com/WJQSERVER-STUDIO/httpc" + "github.com/fenthope/reco" +) + +// --- reco 兼容函数 --- // GetLogReco 返回底层的 reco.Logger 实例 // 用于需要访问 reco 特定功能的场景 @@ -35,3 +40,13 @@ func (c *Context) GetLoggerReco() *reco.Logger { } return c.engine.LogReco } + +// --- httpc 兼容函数 --- + +// GetHTTPC 返回底层的 httpc.Client 实例 +// Deprecated: 使用 HTTPClient() 替代,新方法会自动关联请求 Context +// +//go:fix inline +func (c *Context) GetHTTPC() *httpc.Client { + return c.Client() +} diff --git a/context.go b/context.go index 324386e..c720de3 100644 --- a/context.go +++ b/context.go @@ -865,11 +865,22 @@ func (c *Context) GetErrors() []error { } // Client 返回 Engine 提供的 HTTPClient -// 方便在请求处理函数中进行出站 HTTP 请求 +// 方便在请求处理函数中进行出站 HTTP请求 +// +// Deprecated: 使用 HTTPC() 替代,新方法会自动关联请求 Context func (c *Context) Client() *httpc.Client { return c.HTTPClient } +// HTTPC 返回自动关联请求 Context 的 HTTP 客户端 +// 当请求被取消时,通过此客户端发起的出站请求也会自动取消 +func (c *Context) HTTPC() *contextHTTPClient { + return &contextHTTPClient{ + client: c.engine.HTTPClient, + ctx: c.ctx, + } +} + // Context() 返回请求的上下文,用于取消操作 // 这是 Go 标准库的 `context.Context`,用于请求的取消和超时管理 func (c *Context) Context() context.Context { @@ -1130,11 +1141,6 @@ func (c *Context) GetProtocol() string { return c.Request.Proto } -// GetHTTPC 获取框架自带传递的httpc -func (c *Context) GetHTTPC() *httpc.Client { - return c.HTTPClient -} - // GetLogger 获取engine的Logger接口 func (c *Context) GetLogger() Logger { return c.engine.logger diff --git a/context_httpc.go b/context_httpc.go new file mode 100644 index 0000000..3256a3b --- /dev/null +++ b/context_httpc.go @@ -0,0 +1,58 @@ +// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at https://mozilla.org/MPL/2.0/. +// Copyright 2024 WJQSERVER. All rights reserved. +// All rights reserved by WJQSERVER, related rights can be exercised by the infinite-iroha organization. +package touka + +import ( + "context" + + "github.com/WJQSERVER-STUDIO/httpc" +) + +// contextHTTPClient 包装 httpc.Client,自动关联请求的 Context +// 当请求被取消时,出站 HTTP 请求也会自动取消 +type contextHTTPClient struct { + client *httpc.Client + ctx context.Context +} + +// NewRequestBuilder 创建请求构建器,自动关联请求 Context +func (c *contextHTTPClient) NewRequestBuilder(method, urlStr string) *httpc.RequestBuilder { + return c.client.NewRequestBuilder(method, urlStr).WithContext(c.ctx) +} + +// GET 创建 GET 请求构建器 +func (c *contextHTTPClient) GET(urlStr string) *httpc.RequestBuilder { + return c.client.GET(urlStr).WithContext(c.ctx) +} + +// POST 创建 POST 请求构建器 +func (c *contextHTTPClient) POST(urlStr string) *httpc.RequestBuilder { + return c.client.POST(urlStr).WithContext(c.ctx) +} + +// PUT 创建 PUT 请求构建器 +func (c *contextHTTPClient) PUT(urlStr string) *httpc.RequestBuilder { + return c.client.PUT(urlStr).WithContext(c.ctx) +} + +// DELETE 创建 DELETE 请求构建器 +func (c *contextHTTPClient) DELETE(urlStr string) *httpc.RequestBuilder { + return c.client.DELETE(urlStr).WithContext(c.ctx) +} + +// PATCH 创建 PATCH 请求构建器 +func (c *contextHTTPClient) PATCH(urlStr string) *httpc.RequestBuilder { + return c.client.PATCH(urlStr).WithContext(c.ctx) +} + +// HEAD 创建 HEAD 请求构建器 +func (c *contextHTTPClient) HEAD(urlStr string) *httpc.RequestBuilder { + return c.client.HEAD(urlStr).WithContext(c.ctx) +} + +// OPTIONS 创建 OPTIONS 请求构建器 +func (c *contextHTTPClient) OPTIONS(urlStr string) *httpc.RequestBuilder { + return c.client.OPTIONS(urlStr).WithContext(c.ctx) +}