fetch

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2022 License: MIT Imports: 19 Imported by: 74

README

Fetch - HTTP Client

PkgGoDev Build Status Go Report Card Coverage Status GitHub issues Release

Installation

To install the package, run:

go get github.com/go-zoox/fetch

Getting Started

response, _ := fetch.Get("https://httpbin.zcorky.com/get")
url := response.Get("url")
method := response.Get("method")

fmt.Println(url, method)

Depencencies

  • gjson - Get JSON Whenever You Need, you don't define type first。
  • mozillazg/request - A developer-friendly HTTP request library for Gopher

License

GoZoox is released under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BaseURL = ""
View Source
var DELETE = "DELETE"
View Source
var ErrCannotCopyFile = errors.New("cannot copy file")
View Source
var ErrCannotCreateFormFile = errors.New("cannot create form file")
View Source
var ErrCannotCreateRequest = errors.New("cannot create request")
View Source
var ErrCannotSendBodyWithGet = errors.New("cannot send body with GET method")
View Source
var ErrInvalidBodyMultipart = errors.New("invalid body multipart")
View Source
var ErrInvalidContentType = errors.New("invalid content type")
View Source
var ErrInvalidJSONBody = errors.New("error marshalling body")
View Source
var ErrInvalidMethod = errors.New("invalid method")
View Source
var ErrInvalidUrlFormEncodedBody = errors.New("invalid url form encoded body")
View Source
var ErrReadingResponse = errors.New("error reading response")
View Source
var ErrSendingRequest = errors.New("error sending request")
View Source
var ErrTooManyArguments = errors.New("too many arguments")
View Source
var ErrorInvalidBody = errors.New("invalid body")
View Source
var GET = "GET"
View Source
var HEAD = "HEAD"
View Source
var METHODS = []string{
	HEAD,
	GET,
	POST,
	PUT,
	DELETE,
	PATCH,
}
View Source
var PATCH = "PATCH"
View Source
var POST = "POST"
View Source
var PUT = "PUT"
View Source
var Timeout = 60 * time.Second
View Source
var UserAgent = fmt.Sprintf("GoFetch/%s (github.com/go-zoox/fetch)", Version)
View Source
var Version = "1.2.0"

Functions

func DefaultUserAgent

func DefaultUserAgent() string

func SetBaseURL

func SetBaseURL(url string)

func SetTimeout

func SetTimeout(timeout time.Duration)

func SetUserAgent

func SetUserAgent(userAgent string)

Types

type Config

type Config struct {
	Url     string
	Method  string
	Headers ConfigHeaders
	Query   ConfigQuery
	Params  ConfigParams
	Body    ConfigBody
	//
	BaseURL string
	Timeout time.Duration
	//
	DownloadFilePath string
	//
	Proxy string
}

func DefaultConfig

func DefaultConfig() *Config

func (*Config) Merge

func (cfg *Config) Merge(config *Config)

type ConfigBody

type ConfigBody interface{}

type ConfigHeaders

type ConfigHeaders map[string]string

func (ConfigHeaders) Get

func (h ConfigHeaders) Get(key string) string

type ConfigParams

type ConfigParams map[string]string

func (ConfigParams) Get

func (h ConfigParams) Get(key string) string

type ConfigQuery

type ConfigQuery map[string]string

func (ConfigQuery) Get

func (h ConfigQuery) Get(key string) string

type DataSource added in v1.1.0

type DataSource interface {
	Get(key string) string
}

DataSource defines the interface for loading data from a data source.

type Fetch

type Fetch struct {
	Errors []error
	// contains filtered or unexported fields
}

func New

func New(cfg ...*Config) *Fetch

func (*Fetch) Clone

func (f *Fetch) Clone() *Fetch

func (*Fetch) Delete

func (f *Fetch) Delete(url string, config ...*Config) *Fetch

func (*Fetch) Download added in v1.1.8

func (f *Fetch) Download(url string, filepath string, config ...*Config) *Fetch

func (*Fetch) Execute

func (f *Fetch) Execute() (*Response, error)

func (*Fetch) Get

func (f *Fetch) Get(url string, config ...*Config) *Fetch

func (*Fetch) Head added in v1.1.7

func (f *Fetch) Head(url string, config ...*Config) *Fetch

func (*Fetch) Patch

func (f *Fetch) Patch(url string, config ...*Config) *Fetch

func (*Fetch) Post

func (f *Fetch) Post(url string, config ...*Config) *Fetch

func (*Fetch) Put

func (f *Fetch) Put(url string, config ...*Config) *Fetch

func (*Fetch) Send

func (f *Fetch) Send() (*Response, error)

func (*Fetch) SetAuthorization added in v1.1.1

func (f *Fetch) SetAuthorization(token string) *Fetch

func (*Fetch) SetBaseURL

func (f *Fetch) SetBaseURL(url string) *Fetch

func (*Fetch) SetBasicAuth added in v1.1.1

func (f *Fetch) SetBasicAuth(username, password string) *Fetch

func (*Fetch) SetBearToken added in v1.1.1

func (f *Fetch) SetBearToken(token string) *Fetch

func (*Fetch) SetBody

func (f *Fetch) SetBody(body ConfigBody) *Fetch

func (*Fetch) SetConfig

func (f *Fetch) SetConfig(configs ...*Config) *Fetch

func (*Fetch) SetDownloadFilePath added in v1.1.8

func (f *Fetch) SetDownloadFilePath(filepath string) *Fetch

func (*Fetch) SetHeader

func (f *Fetch) SetHeader(key, value string) *Fetch

func (*Fetch) SetMethod

func (f *Fetch) SetMethod(method string) *Fetch

func (*Fetch) SetParam

func (f *Fetch) SetParam(key, value string) *Fetch

func (*Fetch) SetProxy added in v1.2.0

func (f *Fetch) SetProxy(proxy string) *Fetch

SetProxy sets the proxy

	support http, https, socks5
 example:
		http://127.0.0.1:17890
	  https://127.0.0.1:17890
	  socks5://127.0.0.1:17890

func (*Fetch) SetQuery

func (f *Fetch) SetQuery(key, value string) *Fetch

func (*Fetch) SetTimeout

func (f *Fetch) SetTimeout(timeout time.Duration) *Fetch

func (*Fetch) SetUrl

func (f *Fetch) SetUrl(url string) *Fetch

func (*Fetch) SetUserAgent

func (f *Fetch) SetUserAgent(userAgent string) *Fetch

type Response

type Response struct {
	Status  int
	Headers http.Header
	Body    []byte
	// contains filtered or unexported fields
}

func Delete

func Delete(url string, config *Config) (*Response, error)

func Download added in v1.1.8

func Download(url string, filepath string, config ...interface{}) (*Response, error)

func Get

func Get(url string, config ...interface{}) (*Response, error)
func Head(url string, config ...interface{}) (*Response, error)

func Patch

func Patch(url string, config *Config) (*Response, error)

func Post

func Post(url string, config *Config) (*Response, error)

func Put

func Put(url string, config *Config) (*Response, error)

func (*Response) Get

func (r *Response) Get(key string) gjson.Result

func (*Response) JSON

func (r *Response) JSON() (string, error)

func (*Response) String

func (r *Response) String() string

func (*Response) UnmarshalJSON added in v1.1.4

func (r *Response) UnmarshalJSON(v interface{}) error

func (*Response) UnmarshalYAML added in v1.1.4

func (r *Response) UnmarshalYAML(v interface{}) error

func (*Response) Value

func (r *Response) Value() gjson.Result

Jump to

Keyboard shortcuts

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