Documentation
¶
Index ¶
- Constants
- Variables
- func Get(ctx context.Context, url string, options ...Option) (httpStatusCode int, respBody []byte, err error)
- func GetHttpClient() *http.Client
- func Post(ctx context.Context, url string, data []byte, options ...Option) (httpStatusCode int, respBody []byte, err error)
- func Request(method string, url string, options ...Option) (httpStatusCode int, respBody []byte, err error)
- func SetHttpClient(c *http.Client)
- type Config
- type Interface
- type LogLevel
- type Option
- type Writer
Examples ¶
Constants ¶
View Source
const ( Reset = "\033[0m" Red = "\033[31m" Green = "\033[32m" Yellow = "\033[33m" Magenta = "\033[35m" BlueBold = "\033[34;1m" )
Colors
Variables ¶
Functions ¶
func Get ¶
func Get(ctx context.Context, url string, options ...Option) (httpStatusCode int, respBody []byte, err error)
Get 发起GET请求
Example ¶
ExampleGet 展示GET请求的使用方法
// 创建上下文
ctx := context.Background()
// 基本GET请求
statusCode, body, err := Get(ctx, "https://api.example.com/data")
if err != nil {
// 处理错误
return
}
_ = statusCode
_ = body
// 带自定义选项的GET请求
options := []Option{
WithTimeout(10 * time.Second), // 设置超时
WithHeaders(map[string]string{ // 设置请求头
"Authorization": "Bearer token123",
"X-Custom-Header": "value",
}),
WithSlowThreshold(100 * time.Millisecond), // 设置慢请求阈值
}
statusCode, body, err = Get(ctx, "https://api.example.com/data", options...)
func Post ¶
func Post(ctx context.Context, url string, data []byte, options ...Option) (httpStatusCode int, respBody []byte, err error)
Post 发起POST请求
Example ¶
ExamplePost 展示POST请求的使用方法
ctx := context.Background()
// 准备POST数据
data := []byte(`{
"name": "张三",
"age": 25,
"email": "zhangsan@example.com"
}`)
// 基本POST请求
statusCode, body, err := Post(ctx, "https://api.example.com/users", data)
if err != nil {
// 处理错误
return
}
_ = statusCode
_ = body
// 带自定义选项的POST请求
options := []Option{
WithTimeout(10 * time.Second),
WithHeaders(map[string]string{
"Authorization": "Bearer token123",
}),
WithSlowThreshold(100 * time.Millisecond),
}
statusCode, body, err = Post(ctx, "https://api.example.com/users", data, options...)
func Request ¶
func Request(method string, url string, options ...Option) (httpStatusCode int, respBody []byte, err error)
Example ¶
ExampleRequest 展示通用Request方法的使用
ctx := context.Background()
// PUT请求示例
putData := []byte(`{
"name": "李四",
"age": 30
}`)
options := []Option{
WithContext(ctx),
WithData(putData),
WithHeaders(map[string]string{
"Authorization": "Bearer token123",
}),
WithTimeout(10 * time.Second),
}
statusCode, body, err := Request("PUT", "https://api.example.com/users/1", options...)
if err != nil {
// 处理错误
return
}
_ = statusCode
_ = body
// DELETE请求示例
statusCode, body, err = Request("DELETE", "https://api.example.com/users/1", WithContext(ctx))
Types ¶
type Interface ¶
type Interface interface {
LogMode(LogLevel) Interface
Debug(context.Context, string, ...interface{})
Info(context.Context, string, ...interface{})
Warn(context.Context, string, ...interface{})
Error(context.Context, string, ...interface{})
}
Interface logger interface
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
func WithContext ¶
Example ¶
ExampleWithContext 展示使用带超时的上下文
// 创建带超时的上下文
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
// 使用带超时的上下文发送请求
statusCode, body, err := Get(ctx, "https://api.example.com/data")
if err != nil {
// 处理错误
return
}
_ = statusCode
_ = body
func WithHeaders ¶
func WithLogger ¶
Example ¶
ExampleWithLogger 展示自定义日志记录器的使用
ctx := context.Background()
// 创建自定义日志记录器
customLogger := New(log.New(os.Stdout, "", log.LstdFlags), Config{
LogLevel: Debug,
Colorful: true,
})
// 使用自定义日志记录器发送请求
options := []Option{
WithContext(ctx),
WithLogger(customLogger),
WithSlowThreshold(100 * time.Millisecond),
}
statusCode, body, err := Get(ctx, "https://api.example.com/data", options...)
if err != nil {
// 处理错误
return
}
_ = statusCode
_ = body
func WithSlowThreshold ¶
WithSlowThreshold 设置慢请求阈值 单位:毫秒
Example ¶
ExampleWithSlowThreshold 展示慢请求监控
ctx := context.Background()
// 设置慢请求阈值为100毫秒
options := []Option{
WithContext(ctx),
WithSlowThreshold(100 * time.Millisecond),
}
// 发送请求,如果响应时间超过阈值,会记录警告日志
statusCode, body, err := Get(ctx, "https://api.example.com/data", options...)
if err != nil {
// 处理错误
return
}
_ = statusCode
_ = body
func WithTimeout ¶
Click to show internal directories.
Click to hide internal directories.