touka/context_httpc.go
wjqserver f2295c3084 feat: httpc 集成改进,自动关联请求 Context
- 新增 contextHTTPClient 包装器,自动关联请求 Context
- 新增 Context.HTTPC() 方法返回 contextHTTPClient
- Client() 标记为 Deprecated
- 添加 GetHTTPC() go:fix inline 兼容函数

当请求被取消时,出站 HTTP 请求也会自动取消。
2026-04-21 22:55:26 +08:00

58 lines
2 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 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)
}