fetch

package module
v1.3.3 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 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 = ""

BaseURL is the default base url

View Source
var DELETE = "DELETE"

DELETE is request method DELETE

View Source
var ErrCannotCopyFile = errors.New("cannot copy file")

ErrCannotCopyFile is the error when the file cannot be copied

View Source
var ErrCannotCreateFormFile = errors.New("cannot create form file")

ErrCannotCreateFormFile is the error when the form file cannot be created

View Source
var ErrCannotCreateRequest = errors.New("cannot create request")

ErrCannotCreateRequest is the error when the request cannot be created

View Source
var ErrCannotSendBodyWithGet = errors.New("cannot send body with GET method")

ErrCannotSendBodyWithGet is the error when the body cannot be sent with GET method

View Source
var ErrInvalidBodyMultipart = errors.New("invalid body multipart")

ErrInvalidBodyMultipart is the error when the body is invalid for multipart

View Source
var ErrInvalidContentType = errors.New("invalid content type")

ErrInvalidContentType is the error when the content type is invalid

View Source
var ErrInvalidJSONBody = errors.New("error marshalling body")

ErrInvalidJSONBody is the error when the body is not a valid JSON

View Source
var ErrInvalidMethod = errors.New("invalid method")

ErrInvalidMethod is the error when the method is invalid

View Source
var ErrInvalidURLFormEncodedBody = errors.New("invalid url form encoded body")

ErrInvalidURLFormEncodedBody is the error when the body is invalid for url form encoded

View Source
var ErrReadingResponse = errors.New("error reading response")

ErrReadingResponse is the error when the response cannot be read

View Source
var ErrSendingRequest = errors.New("error sending request")

ErrSendingRequest is the error when the request cannot be sent

View Source
var ErrTooManyArguments = errors.New("too many arguments")

ErrTooManyArguments is the error when the number of arguments is too many

View Source
var ErrorInvalidBody = errors.New("invalid body")

ErrorInvalidBody is the error when the body is invalid

View Source
var GET = "GET"

GET is request method GET

View Source
var HEAD = "HEAD"

HEAD is request method HEAD

View Source
var METHODS = []string{
	HEAD,
	GET,
	POST,
	PUT,
	DELETE,
	PATCH,
}

METHODS is the list of supported methods

View Source
var PATCH = "PATCH"

PATCH is request method PATCH

View Source
var POST = "POST"

POST is request method POST

View Source
var PUT = "PUT"

PUT is request method PUT

View Source
var Timeout = 60 * time.Second

Timeout is the default timeout

View Source
var UserAgent = fmt.Sprintf("GoFetch/%s (github.com/go-zoox/fetch)", Version)

UserAgent is the default user agent

View Source
var Version = "1.3.3"

Version is the version of this package

Functions

func DefaultUserAgent

func DefaultUserAgent() string

DefaultUserAgent returns the default user agent

func SetBaseURL

func SetBaseURL(url string)

SetBaseURL sets the base url

func SetTimeout

func SetTimeout(timeout time.Duration)

SetTimeout sets the timeout

func SetUserAgent

func SetUserAgent(userAgent string)

SetUserAgent sets the user agent

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
}

Config is the configuration for the fetch

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns the default config

func (*Config) Merge

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

Merge merges the config with the given config

type ConfigBody

type ConfigBody interface{}

ConfigBody is the body of the request

type ConfigHeaders

type ConfigHeaders map[string]string

ConfigHeaders is the headers of the request

func (ConfigHeaders) Get

func (h ConfigHeaders) Get(key string) string

Get returns the value of the given key

type ConfigParams

type ConfigParams map[string]string

ConfigParams is the params of the request

func (ConfigParams) Get

func (h ConfigParams) Get(key string) string

Get returns the value of the given key

type ConfigQuery

type ConfigQuery map[string]string

ConfigQuery is the query of the request

func (ConfigQuery) Get

func (h ConfigQuery) Get(key string) string

Get returns the value of the given key

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
}

Fetch is the Fetch Client

func New

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

New creates a fetch client

func (*Fetch) Clone

func (f *Fetch) Clone() *Fetch

Clone creates a new fetch

func (*Fetch) Config added in v1.3.0

func (f *Fetch) Config() (*Config, error)

Config returns the request config

func (*Fetch) Delete

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

Delete is http.delete

func (*Fetch) Download added in v1.1.8

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

Download downloads file by url

func (*Fetch) Execute

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

Execute executes the request

func (*Fetch) Get

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

Get is http.get

func (*Fetch) Head added in v1.1.7

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

Head is http.head

func (*Fetch) Patch

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

Patch is http.patch

func (*Fetch) Post

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

Post is http.post

func (*Fetch) Put

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

Put is http.put

func (*Fetch) Send

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

Send sends the request

func (*Fetch) SetAuthorization added in v1.1.1

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

SetAuthorization sets the authorization token

func (*Fetch) SetBaseURL

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

SetBaseURL sets the base url

func (*Fetch) SetBasicAuth added in v1.1.1

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

SetBasicAuth sets the basic auth username and password

func (*Fetch) SetBearToken added in v1.1.1

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

SetBearToken sets the bear token

func (*Fetch) SetBody

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

SetBody sets the body

func (*Fetch) SetConfig

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

SetConfig sets the config of fetch

func (*Fetch) SetDownloadFilePath added in v1.1.8

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

SetDownloadFilePath sets the download file path

func (*Fetch) SetHeader

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

SetHeader sets the header key and value

func (*Fetch) SetMethod

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

SetMethod sets the method

func (*Fetch) SetParam

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

SetParam sets the param key and value

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

SetQuery sets the query key and value

func (*Fetch) SetTimeout

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

SetTimeout sets the timeout

func (*Fetch) SetURL added in v1.2.1

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

SetURL sets the url of fetch

func (*Fetch) SetUserAgent

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

SetUserAgent sets the user agent

type Response

type Response struct {
	Status  int
	Headers http.Header
	Body    []byte

	//
	Request *Config
	// contains filtered or unexported fields
}

Response is the fetch response

func Delete

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

Delete is a wrapper for the Delete method of the Client

func Download added in v1.1.8

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

Download is a wrapper for the Download method of the Client

func Get

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

Get is a wrapper for the Get method of the Client

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

Head is a wrapper for the Head method of the Client

func Patch

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

Patch is a wrapper for the Patch method of the Client

func Post

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

Post is a wrapper for the Post method of the Client

func Put

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

Put is a wrapper for the Put method of the Client

func (*Response) Get

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

Get returns the value of the key

func (*Response) JSON

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

JSON returns the body as json string

func (*Response) Ok added in v1.3.3

func (r *Response) Ok() bool

Ok returns true if status code is 2xx

func (*Response) String

func (r *Response) String() string

String returns the body as string

func (*Response) UnmarshalJSON added in v1.1.4

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

UnmarshalJSON unmarshals body to json struct

@TODO bug when lint (go vet) method UnmarshalJSON(v interface{}) error should have signature UnmarshalJSON([]byte) error

func (*Response) UnmarshalYAML added in v1.1.4

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

UnmarshalYAML unmarshals body to yaml struct

func (*Response) Value

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

Value returns the body as gjson.Result

Jump to

Keyboard shortcuts

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