yeti

package module
v0.0.0-...-187fd60 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 27, 2026 License: Unlicense Imports: 17 Imported by: 0

README

Yeti HTTP Client

中文文档

A Go HTTP client library with a fluent Builder pattern.

Install

go get github.com/Li-giegie/yeti

Quick Start

import "github.com/Li-giegie/yeti"

// Create client
c := yeti.NewClient()

// Send GET request
var result MyStruct
c.Get().
    SetUrl("https://api.example.com/users").
    AddPath("123").
    AddQuery("fields", "name,email").
    DoResponse(ctx).
    JSON(&result)

Core Components

Client

Main entry point, created via NewClient().

c := yeti.NewClient()

Supports hook methods to intercept requests/responses:

  • AddBeforeRequest(fn) - Modify Requester before building request
  • AddAfterRequest(fn) - Modify *http.Request before sending
  • AddAfterResponse(fn) - Process response before returning
Requester

Obtained via yeti.R() or shortcut methods (Get(), Post(), Put(), Patch(), Delete(), etc.).

Builder methods:

Method Description
SetMethod(method) Set HTTP method
SetUrl(url) Set request URL
AddPath(segment) Add URL path segment (auto handles slashes)
AddQuery(key, value) Set query parameter
AddQueryAny(key, value) Add query parameter with auto type conversion
SetHeader(key, value) Set header (overwrite)
AddHeader(key, value) Add header (appendable)
SetHeaderAny(key, value) Set header with auto type conversion
AddHeaderAny(key, value) Add header with auto type conversion

Request body methods:

Method Content-Type
SetBodyJSON(v) application/json
SetBodyXML(v) application/xml
SetBodyForm(url.Values) application/x-www-form-urlencoded
SetBodyMultipartForm(map) multipart/form-data
SetBodyText(string) text/plain
SetBodyBinary(io.Reader) application/octet-stream
Response

Returned by DoResponse(), provides response handling:

Method Description
EqStatusCode(code) Assert status code
JSON(v) Parse JSON into struct
XML(v) Parse XML into struct
Validate(fn) Custom validation

Debugging

Enable debug logging via context:

ctx := context.WithValue(context.TODO(), yeti.ANNE_DEBUG, true)
// Or enable separately
ctx := context.WithValue(context.TODO(), yeti.ANNE_REQUEST_DEBUG, true)
ctx := context.WithValue(context.TODO(), yeti.ANNE_RESPONSE_DEBUG, true)

// Use DoDebug() shorthand
resp, err := requester.DoDebug(ctx)

Examples

Basic Requests
// GET request
c.Get().
    SetUrl("https://api.example.com").
    AddPath("users").
    AddQuery("page", 1).
    DoResponse(ctx)

// POST JSON
c.Post().
    SetUrl("https://api.example.com/users").
    SetBodyJSON(User{Name: "Alice", Email: "alice@example.com"}).
    DoResponse(ctx)

// File upload
c.Post().
    SetUrl("https://api.example.com/upload").
    SetBodyMultipartForm(map[string]any{
        "name": "my-file",
        "file": os.Open("test.png"),
    }).
    DoResponse(ctx)
Custom HTTP Client
c := yeti.NewClient()
c.Client = &http.Client{Timeout: 10 * time.Second}
Using Hooks
c := yeti.NewClient()

// Add auth header before request
c.AddBeforeRequest(func(r *yeti.Requester) error {
    r.SetHeader("Authorization", "Bearer token")
    return nil
})

// Log response after response
c.AddAfterResponse(func(resp *http.Response) error {
    log.Printf("Response: %s", resp.Status)
    return nil
})

Build Commands

go build ./...     # Build all packages
go test ./...      # Run all tests
go test -v ./...   # Verbose output

Documentation

Index

Constants

View Source
const (
	ANNE_DEBUG          = "ANNE_DEBUG"
	ANNE_REQUEST_DEBUG  = "ANNE_REQUEST_DEBUG"
	ANNE_RESPONSE_DEBUG = "ANNE_RESPONSE_DEBUG"
)

Variables

Functions

This section is empty.

Types

type Client

type Client struct {
	Client        HttpClient
	BeforeRequest []func(*Requester) error
	AfterRequest  []func(*http.Request) error
	AfterResponse []func(*http.Response) error
}

func NewClient

func NewClient(opts ...Option) *Client

func (*Client) AddAfterRequest

func (c *Client) AddAfterRequest(fn func(*http.Request) error)

func (*Client) AddAfterResponse

func (c *Client) AddAfterResponse(fn func(*http.Response) error)

func (*Client) AddBeforeRequest

func (c *Client) AddBeforeRequest(fn func(*Requester) error)

func (*Client) Connect

func (c *Client) Connect() *Requester

func (*Client) Delete

func (c *Client) Delete() *Requester

func (*Client) Get

func (c *Client) Get() *Requester

func (*Client) HEAD

func (c *Client) HEAD() *Requester

func (*Client) Options

func (c *Client) Options() *Requester

func (*Client) Patch

func (c *Client) Patch() *Requester

func (*Client) Post

func (c *Client) Post() *Requester

func (*Client) Put

func (c *Client) Put() *Requester

func (*Client) R

func (c *Client) R(opts ...RequesterOption) *Requester

func (*Client) Trace

func (c *Client) Trace() *Requester

type File

type File struct {
	// contains filtered or unexported fields
}

func NewFile

func NewFile(name string, file io.Reader) *File

func (*File) Name

func (f *File) Name() string

func (*File) Read

func (f *File) Read(p []byte) (n int, err error)

type FileReader

type FileReader interface {
	Name() string
	Read(b []byte) (n int, err error)
}

type HttpClient

type HttpClient interface {
	Do(*http.Request) (*http.Response, error)
}

type Option

type Option func(client *Client)

type Requester

type Requester struct {
	Err    error
	Method string
	URL    string
	Paths  []string
	Query  url.Values
	Header http.Header
	Body   io.Reader
	// contains filtered or unexported fields
}

func (*Requester) AddHeader

func (b *Requester) AddHeader(key string, value string) *Requester

func (*Requester) AddHeaderAny

func (b *Requester) AddHeaderAny(key string, value any) *Requester

func (*Requester) AddPath

func (b *Requester) AddPath(p string) *Requester

func (*Requester) AddQuery

func (b *Requester) AddQuery(key, value string) *Requester

func (*Requester) AddQueryAny

func (b *Requester) AddQueryAny(key string, value any) *Requester

func (*Requester) Build

func (b *Requester) Build(ctx context.Context) (*http.Request, error)

func (*Requester) Do

func (b *Requester) Do(ctx context.Context) (resp *http.Response, err error)

func (*Requester) DoDebug

func (b *Requester) DoDebug(ctx context.Context) (*http.Response, error)

func (*Requester) DoResponse

func (b *Requester) DoResponse(ctx context.Context) *Response

func (*Requester) Reset

func (b *Requester) Reset() *Requester

Reset reset Err、Method、URL、Header、Body

func (*Requester) SetBody

func (b *Requester) SetBody(contentType string, body io.Reader) *Requester

func (*Requester) SetBodyBinary

func (b *Requester) SetBodyBinary(r io.Reader) *Requester

SetBodyBinary application/octet-stream

func (*Requester) SetBodyForm

func (b *Requester) SetBodyForm(form url.Values) *Requester

SetBodyForm application/x-www-form-urlencoded

func (*Requester) SetBodyFormMap

func (b *Requester) SetBodyFormMap(mForm map[string]any) *Requester

SetBodyFormMap application/x-www-form-urlencoded

func (*Requester) SetBodyJSON

func (b *Requester) SetBodyJSON(a any) *Requester

SetBodyJSON application/json

func (*Requester) SetBodyMultipartForm

func (b *Requester) SetBodyMultipartForm(m map[string]any) *Requester

SetBodyMultipartForm multipart/form-data,文件的 value 值必须是实现了 FileReader 接口的 *os.File *File或任意的实现接口的类型

func (*Requester) SetBodyText

func (b *Requester) SetBodyText(text string) *Requester

SetBodyText text/plain

func (*Requester) SetBodyXML

func (b *Requester) SetBodyXML(a any) *Requester

SetBodyXML application/xml

func (*Requester) SetHeader

func (b *Requester) SetHeader(key string, value string) *Requester

func (*Requester) SetHeaderAny

func (b *Requester) SetHeaderAny(key string, value any) *Requester

func (*Requester) SetMethod

func (b *Requester) SetMethod(method string) *Requester

func (*Requester) SetUrl

func (b *Requester) SetUrl(u string) *Requester

type RequesterOption

type RequesterOption func(r *Requester)

type Response

type Response struct {
	Err        error
	Status     string
	StatusCode int
	Header     http.Header
	Body       []byte
}

func (*Response) EqStatusCode

func (r *Response) EqStatusCode(code int) *Response

func (*Response) JSON

func (r *Response) JSON(a any) error

func (*Response) Validate

func (r *Response) Validate(fn func(r *Response) error) *Response

func (*Response) XML

func (r *Response) XML(a any) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL