h3

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2024 License: MIT Imports: 12 Imported by: 0

README

H3

A simple encapsulation of Golang HTTP client.

package demo

import "fmt"

func main() {
	client := h3.New()
	client.BaseURL = "https://httpbin.org"

	req := client.Req("POST", "/anything")
	// application/json
	form := h3.Json{}
	if err := form.Set(map[string]interface{}{
		"a": 1,
		"b": []string{"1", "2", "3"},
	}); err != nil {
		panic(err)
	}
	req.Body = form.Form()

	res, err := client.Do(req)
	if err != nil {
		panic(err)
	}

	data := map[string]interface{}{}
	if err := res.Json(&data); err != nil {
		panic(err)
	}
	fmt.Printf("%#v\n", data)
}

More examples

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultClient = New()

Functions

This section is empty.

Types

type Client

type Client struct {
	BaseURL    string
	BaseHeader http.Header
	BaseParams url.Values
	// contains filtered or unexported fields
}

func New

func New() *Client

func (Client) Delete

func (c Client) Delete(path string) *Request

func (Client) Get

func (c Client) Get(path string) *Request

func (Client) Head

func (c Client) Head(path string) *Request

func (*Client) OnAfter

func (c *Client) OnAfter(fn func(res *Response) error)

func (*Client) OnBefore

func (c *Client) OnBefore(fn func(req *http.Request) error)

func (Client) Patch

func (c Client) Patch(path string, body io.Reader) *Request

func (Client) Post

func (c Client) Post(path string, body io.Reader) *Request

func (Client) Put

func (c Client) Put(path string, body io.Reader) *Request

func (*Client) Req

func (c *Client) Req(method string, path string) *Request

func (*Client) SetDump

func (c *Client) SetDump(enable bool)

func (*Client) SetJar

func (c *Client) SetJar(jar http.CookieJar)

func (*Client) SetTimeout

func (c *Client) SetTimeout(timeout time.Duration)

func (*Client) SetTransport

func (c *Client) SetTransport(transport http.RoundTripper)

type ContentBody

type ContentBody struct {
	*bytes.Buffer
	Type string
}

func (ContentBody) Read

func (c ContentBody) Read(p []byte) (n int, err error)

type FormData

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

func (*FormData) Add

func (f *FormData) Add(key string, value string) error

func (*FormData) AddFile

func (f *FormData) AddFile(key string, filename string, file *os.File) error

func (FormData) Form

func (f FormData) Form() ContentBody

type Json

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

func (Json) Form

func (j Json) Form() ContentBody

func (*Json) Set

func (j *Json) Set(v interface{}) error

type Request

type Request struct {
	Method string
	Url    string
	Header http.Header
	Params url.Values
	Body   io.Reader
	Client *Client
	Dump   bool
}

func Delete

func Delete(path string) *Request

func Get

func Get(path string) *Request
func Head(path string) *Request

func NewRequest

func NewRequest(method string, path string) *Request

func Patch

func Patch(path string, body io.Reader) *Request

func Post

func Post(path string, body io.Reader) *Request

func Put

func Put(path string, body io.Reader) *Request

func Req

func Req(method string, path string) *Request

func (*Request) Do

func (r *Request) Do() (res *Response, err error)

type Response

type Response struct {
	*http.Response
	// contains filtered or unexported fields
}

func (Response) Json

func (r Response) Json(v interface{}) error

func (*Response) Raw

func (r *Response) Raw() ([]byte, error)

func (Response) String

func (r Response) String() (string, error)

type Urlencoded

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

func (*Urlencoded) Add

func (u *Urlencoded) Add(key string, value string)

func (*Urlencoded) Del

func (u *Urlencoded) Del(key string)

func (Urlencoded) Form

func (u Urlencoded) Form() ContentBody

func (*Urlencoded) Get

func (u *Urlencoded) Get(key string) string

func (*Urlencoded) Set

func (u *Urlencoded) Set(key string, value string)

Jump to

Keyboard shortcuts

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