requests

package module
v1.3.4 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2024 License: Apache-2.0 Imports: 18 Imported by: 3

README

Requests

Go HTTP Client Library

Usage

New Request/Session or (Pool)

NewRequest(options ...RequestOption)
NewSession(options ...RequestOption)

// Pool
NewRequestPool(options ...RequestOption)
NewSessionPool(options ...RequestOption)

RequestOption

  • WithTimeout(t time.Duration)
  • WithHeader(headers ...map[string]string)
  • WithProxyEnv(proxy map[string]string)
  • WithProxyURL(proxy *url.URL)
  • WithProxyFunc(f func(*http.Request) (*url.URL, error))
  • WithUnsetProxy()
  • WithSkipTLS()
  • WithBasicAuth(username, password string)
  • WithBearerTokenAuth(token string)
  • WithTransport(tr http.RoundTripper)
  • WithDefaultTransport()
  • WithClient(client *http.Client)
  • WithDefaultClient()

DoHttpMethod

r := NewRequest(RequestOptions...)
r.SetXXX() // if needed
r.Get(originURL string, options ...MethodOption)
r.Post(originURL string, options ...MethodOption)
r.Put(originURL string, options ...MethodOption)
r.Delete(originURL string, options ...MethodOption)
r.CloseIdleConnections() // if needed

StreamMode

r.StringStream(originURL string, event chan string, options ...MethodOption)
r.JsonStream(originURL string, event chan map[string]any, options ...MethodOption)

Upload/Download

// rate is speed per second, e.g. 1024 ==> 1KiB, if rate <= 0 it means no limit
r.DownloadToWriter(originURL string, w io.Writer)
r.Download(filePath, originURL string, rate int64)
r.Upload(originURL string, data map[string]string, rate int64, filePaths ...string) 

Response

r.Status() // status code and status
r.Content() // []byte data
r.ContentToString() // string data

PoolMode

rp := NewRequestPool(RequestOptions...)
r := p.Get()
defer rp.Put(r)
// Do Http Method
...

MethodOption

  • WithParams(params map[string]string)
  • WithJsonData(data map[string]any) MethodOption
  • WithFormData(data map[string]string)

Proxy

NewProxy(scheme, addr string, auth *Auth)
NewHttpProxy(addr string, auth *Auth)
NewSocks5Proxy(addr string, auth *Auth)
ProxyFromEnvironment(req *http.Request)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultTransport is clone of http.DefaultTransport
	DefaultTransport = NewTransport()
	// DefaultClient set Transport to DefaultTransport
	DefaultClient = NewClient()
)

Functions

func NewClient added in v1.2.12

func NewClient() *http.Client

func NewTransport added in v1.2.12

func NewTransport() *http.Transport

NewTransport return clone of http.DefaultTransport

func ProxyFromEnvironment added in v1.2.3

func ProxyFromEnvironment(req *http.Request) (*url.URL, error)

ProxyFromEnvironment read proxy form env for every request http.ProxyFromEnvironment read only once

Types

type Auth added in v1.3.1

type Auth struct {
	Username string
	Password string
}

type MethodOption added in v1.3.1

type MethodOption func(r *Request)

func WithFormData added in v1.3.1

func WithFormData(data map[string]string) MethodOption

func WithJsonData added in v1.3.1

func WithJsonData(data map[string]any) MethodOption

func WithParams added in v1.3.1

func WithParams(params map[string]string) MethodOption

type Proxy added in v1.2.8

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

Proxy addr is host:port

func NewHttpProxy added in v1.2.8

func NewHttpProxy(addr string, auth *Auth) *Proxy

func NewProxy added in v1.3.2

func NewProxy(scheme, addr string, auth *Auth) *Proxy

func NewSocks5Proxy added in v1.3.1

func NewSocks5Proxy(addr string, auth *Auth) *Proxy

func (*Proxy) ProxyEnv added in v1.3.1

func (p *Proxy) ProxyEnv() map[string]string

func (*Proxy) ProxyRawURL added in v1.3.1

func (p *Proxy) ProxyRawURL() string

func (*Proxy) ProxyURL added in v1.3.1

func (p *Proxy) ProxyURL() (*url.URL, error)

type Request added in v1.0.2

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

func NewRequest added in v1.0.2

func NewRequest(options ...RequestOption) *Request

NewRequest use DefaultClient to do http request, RequestOption can be provided to set Request properties

func NewSession

func NewSession(options ...RequestOption) *Request

func (*Request) CloseIdleConnections added in v1.2.14

func (r *Request) CloseIdleConnections()

func (Request) Content added in v1.0.2

func (r Request) Content() []byte

func (Request) ContentToString added in v1.1.0

func (r Request) ContentToString() string

func (*Request) Delete added in v1.0.3

func (r *Request) Delete(originURL string, options ...MethodOption) error

func (*Request) Download added in v1.0.7

func (r *Request) Download(filePath, originURL string, rate int64) (int64, error)

Download rate is download speed per second, e.g. 1024 ==> 1KiB/s, 1024*1024 ==> 1MiB/s, if rate <= 0 it means no limit

func (*Request) DownloadToWriter added in v1.2.13

func (r *Request) DownloadToWriter(originURL string, w io.Writer) (int64, error)

func (*Request) Get added in v1.0.2

func (r *Request) Get(originURL string, options ...MethodOption) error

func (*Request) JsonStream added in v1.3.4

func (r *Request) JsonStream(originURL string, event chan map[string]any, options ...MethodOption) error

func (*Request) Post added in v1.0.2

func (r *Request) Post(originURL string, options ...MethodOption) error

func (*Request) Put added in v1.0.3

func (r *Request) Put(originURL string, options ...MethodOption) error

func (Request) Request added in v1.2.1

func (r Request) Request() *http.Request

func (Request) Response added in v1.2.1

func (r Request) Response() *http.Response

func (*Request) SetBasicAuth added in v1.0.3

func (r *Request) SetBasicAuth(username, password string)

func (*Request) SetBearerTokenAuth added in v1.0.3

func (r *Request) SetBearerTokenAuth(token string)

func (*Request) SetClient added in v1.2.10

func (r *Request) SetClient(client *http.Client)

func (*Request) SetHeader added in v1.0.3

func (r *Request) SetHeader(header map[string]string)

func (*Request) SetProxyEnv added in v1.2.12

func (r *Request) SetProxyEnv(proxy map[string]string)

func (*Request) SetProxyFunc added in v1.2.12

func (r *Request) SetProxyFunc(f func(*http.Request) (*url.URL, error))

func (*Request) SetProxyURL added in v1.3.1

func (r *Request) SetProxyURL(proxy *url.URL)

func (*Request) SetSkipTLS added in v1.2.12

func (r *Request) SetSkipTLS()

func (*Request) SetTimeout added in v1.0.3

func (r *Request) SetTimeout(t time.Duration)

func (*Request) SetTransport added in v1.2.3

func (r *Request) SetTransport(rt http.RoundTripper)

func (Request) Status added in v1.0.2

func (r Request) Status() (int, string)

func (*Request) StringStream added in v1.3.4

func (r *Request) StringStream(originURL string, event chan string, options ...MethodOption) error

func (*Request) UnsetProxy added in v1.2.6

func (r *Request) UnsetProxy()

func (*Request) Upload added in v1.2.1

func (r *Request) Upload(originURL string, data map[string]string, rate int64, filePaths ...string) error

Upload rate is upload speed per second, e.g. 1024 ==> 1KiB, 1024*1024 ==> 1MiB/s, if rate <= 0 it means no limit

type RequestOption added in v1.3.1

type RequestOption func(*Request)

func WithBasicAuth added in v1.1.5

func WithBasicAuth(username, password string) RequestOption

func WithBearerTokenAuth added in v1.1.5

func WithBearerTokenAuth(token string) RequestOption

func WithClient added in v1.2.10

func WithClient(client *http.Client) RequestOption

func WithDefaultClient added in v1.2.10

func WithDefaultClient() RequestOption

func WithDefaultTransport added in v1.2.10

func WithDefaultTransport() RequestOption

func WithHeader added in v1.1.5

func WithHeader(headers ...map[string]string) RequestOption

func WithProxyEnv added in v1.2.12

func WithProxyEnv(proxy map[string]string) RequestOption

func WithProxyFunc added in v1.2.7

func WithProxyFunc(f func(*http.Request) (*url.URL, error)) RequestOption

func WithProxyURL added in v1.3.1

func WithProxyURL(proxy *url.URL) RequestOption

func WithSkipTLS added in v1.2.12

func WithSkipTLS() RequestOption

func WithTimeout added in v1.1.5

func WithTimeout(t time.Duration) RequestOption

func WithTransport added in v1.2.4

func WithTransport(tr http.RoundTripper) RequestOption

func WithUnsetProxy added in v1.2.6

func WithUnsetProxy() RequestOption

type RequestPool added in v1.3.0

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

RequestPool both RequestPool and SessionPool use sync.Pool to cache and reuse Request when you want to do bulk http requests with separate http.Client, default use WithClient(NewClient) RequestOption when creating pool RequestOption can be provided to Get to set Request properties, don't forget to call Put when http request done

func NewRequestPool added in v1.3.0

func NewRequestPool() *RequestPool

func (*RequestPool) Get added in v1.3.0

func (rp *RequestPool) Get(options ...RequestOption) *Request

func (*RequestPool) Put added in v1.3.0

func (rp *RequestPool) Put(r *Request)

Put to avoid connection leaks, call CloseIdleConnections before put back to pool

type SessionPool added in v1.3.0

type SessionPool struct {
	*RequestPool
}

func NewSessionPool added in v1.3.0

func NewSessionPool(options ...RequestOption) *SessionPool

Jump to

Keyboard shortcuts

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