Documentation ¶
Overview ¶
Package direwolf is a convenient and easy to use http client written in Golang.
Index ¶
- Variables
- func WrapErr(err error, msg string) error
- func WrapErrf(err error, format string, args ...interface{}) error
- type Body
- type CSSNode
- type CSSNodeList
- func (nodeList *CSSNodeList) At(index int) *CSSNode
- func (nodeList *CSSNodeList) Attr(attrName string, defaultValue ...string) (valueList []string)
- func (nodeList *CSSNodeList) CSS(queryStr string) *CSSNodeList
- func (nodeList *CSSNodeList) First() *CSSNode
- func (nodeList *CSSNodeList) Text() (textList []string)
- func (nodeList *CSSNodeList) TextAll() (textList []string)
- type Cookies
- type Error
- type Headers
- type Params
- type PostForm
- type Proxy
- type RedirectError
- type RedirectNum
- type Request
- type RequestOption
- type Response
- func Delete(URL string, args ...RequestOption) (*Response, error)
- func Get(URL string, args ...RequestOption) (*Response, error)
- func Head(URL string, args ...RequestOption) (*Response, error)
- func Patch(URL string, args ...RequestOption) (*Response, error)
- func Post(URL string, args ...RequestOption) (*Response, error)
- func Put(URL string, args ...RequestOption) (*Response, error)
- func Send(req *Request) (*Response, error)
- type Session
- func (session *Session) Cookies(URL string) Cookies
- func (session *Session) Delete(URL string, args ...RequestOption) (*Response, error)
- func (session *Session) Get(URL string, args ...RequestOption) (*Response, error)
- func (session *Session) Head(URL string, args ...RequestOption) (*Response, error)
- func (session *Session) Patch(URL string, args ...RequestOption) (*Response, error)
- func (session *Session) Post(URL string, args ...RequestOption) (*Response, error)
- func (session *Session) Put(URL string, args ...RequestOption) (*Response, error)
- func (session *Session) Send(req *Request) (*Response, error)
- func (session *Session) SetCookies(URL string, cookies Cookies)
- type SessionOptions
- type Timeout
Constants ¶
This section is empty.
Variables ¶
var ( ErrRequestBody = errors.New("request body can`t coexists with PostForm") ErrTimeout = errors.New("reqeust timeout") )
Functions ¶
Types ¶
type Body ¶ added in v0.2.0
type Body []byte
Body is the data you want to post, one of the Request Options.
type CSSNode ¶ added in v0.4.0
type CSSNode struct {
// contains filtered or unexported fields
}
CSSNode is a container that stores single selected results
func (*CSSNode) Attr ¶ added in v0.4.0
Attr return the attribute value of the CSSNode. You can set default value, if value isn`t exists, return default value.
type CSSNodeList ¶ added in v0.4.0
type CSSNodeList struct {
// contains filtered or unexported fields
}
CSSNodeList is a container that stores selected results
func (*CSSNodeList) At ¶ added in v0.4.0
func (nodeList *CSSNodeList) At(index int) *CSSNode
At return the cssNode of specified index position. Return a empty cssNode if there is no cssNode in CSSNodeList
func (*CSSNodeList) Attr ¶ added in v0.4.0
func (nodeList *CSSNodeList) Attr(attrName string, defaultValue ...string) (valueList []string)
Attr return a list of attribute value
func (*CSSNodeList) CSS ¶ added in v0.4.0
func (nodeList *CSSNodeList) CSS(queryStr string) *CSSNodeList
CSS return a CSSNodeList, so you can chain CSS
func (*CSSNodeList) First ¶ added in v0.4.0
func (nodeList *CSSNodeList) First() *CSSNode
First return the first cssNode of CSSNodeList. Return a empty cssNode if there is no cssNode in CSSNodeList
func (*CSSNodeList) Text ¶ added in v0.4.0
func (nodeList *CSSNodeList) Text() (textList []string)
Text return a list of text. Only include straight children node text
func (*CSSNodeList) TextAll ¶ added in v0.4.2
func (nodeList *CSSNodeList) TextAll() (textList []string)
TextAll return a list of text. Include all children node text
type Cookies ¶
Cookies is request cookies, as parameter in Request method. You should init it by using NewCookies like this:
cookies := dw.NewCookies( "key1", "value1", "key2", "value2", )
Note: mid symbol is comma.
func NewCookies ¶ added in v0.2.0
NewCookies new a Cookies type.
You can set key-value pair when you init it by sent parameters. Just like this:
cookies := NewCookies( "key1", "value1", "key2", "value2", )
But be careful, between the key and value is a comma. And if the number of parameters is not a multiple of 2, it will panic.
type Headers ¶
func NewHeaders ¶ added in v0.2.0
NewHeaders new a http.Header type.
You can set key-value pair when you init it by sent parameters. Just like this:
headers := NewHeaders( "key1", "value1", "key2", "value2", )
But be careful, between the key and value is a comma. And if the number of parameters is not a multiple of 2, it will panic.
type Params ¶
type Params struct {
// contains filtered or unexported fields
}
Params is url params you want to join to url, as parameter in Request method. You should init it by using NewParams like this:
params := dw.NewParams( "key1", "value1", "key2", "value2", )
Note: mid symbol is comma.
func NewParams ¶ added in v0.2.0
NewParams new a Params type.
You can set key-value pair when you init it by sent parameters. Just like this:
params := NewParams( "key1", "value1", "key2", "value2", )
But be careful, between the key and value is a comma. And if the number of parameters is not a multiple of 2, it will panic.
func (*Params) Add ¶ added in v0.2.1
func (ssm *Params) Add(key, value string)
Add key and value to stringSliceMap. If key exists, value will append to slice.
func (*Params) Get ¶ added in v0.2.1
Get get the value pair to given key. You can pass index to assign which value to get, when there are multiple values.
func (*Params) New ¶ added in v0.2.1
func (ssm *Params) New(keyValue ...string)
New is the way to create a strSliceMap. You can set key-value pair when you init it by sent params. Just like this:
stringSliceMap{}.New( "key1", "value1", "key2", "value2", )
But be careful, between the key and value is a comma. And if the number of parameters is not a multiple of 2, it will panic.
type PostForm ¶ added in v0.2.0
type PostForm struct {
// contains filtered or unexported fields
}
PostForm is the form you want to post, as parameter in Request method. You should init it by using NewPostForm like this:
postForm := dw.NewPostForm( "key1", "value1", "key2", "value2", )
Note: mid symbol is comma.
func NewPostForm ¶ added in v0.2.0
NewPostForm new a PostForm type.
You can set key-value pair when you init it by sent parameters. Just like this:
postForm := NewPostForm( "key1", "value1", "key2", "value2", )
But be careful, between the key and value is a comma. And if the number of parameters is not a multiple of 2, it will panic.
func (*PostForm) Add ¶ added in v0.2.1
func (ssm *PostForm) Add(key, value string)
Add key and value to stringSliceMap. If key exists, value will append to slice.
func (*PostForm) Del ¶ added in v0.2.1
func (ssm *PostForm) Del(key string)
Del delete the given key.
func (*PostForm) Get ¶ added in v0.2.1
Get get the value pair to given key. You can pass index to assign which value to get, when there are multiple values.
func (*PostForm) New ¶ added in v0.2.1
func (ssm *PostForm) New(keyValue ...string)
New is the way to create a strSliceMap. You can set key-value pair when you init it by sent params. Just like this:
stringSliceMap{}.New( "key1", "value1", "key2", "value2", )
But be careful, between the key and value is a comma. And if the number of parameters is not a multiple of 2, it will panic.
type Proxy ¶
Proxy is the proxy server address, like "http://127.0.0.1:1080". You can set different proxies for HTTP and HTTPS sites.
type RedirectError ¶ added in v0.3.0
type RedirectError struct {
RedirectNum int
}
func (*RedirectError) Error ¶ added in v0.5.1
func (e *RedirectError) Error() string
type RedirectNum ¶ added in v0.2.0
type RedirectNum int
RedirectNum is the number of request redirect allowed. If RedirectNum > 0, it means a redirect number limit for requests. If RedirectNum <= 0, it means ban redirect. If RedirectNum is not set, it means default 5 times redirect limit.
type Request ¶
type Request struct { Method string URL string Headers http.Header Body Body Params *Params PostForm *PostForm Cookies Cookies Proxy *Proxy RedirectNum int Timeout int }
Request is a prepared request setting, you should construct it by using NewRequest().
func NewRequest ¶ added in v0.5.3
func NewRequest(method string, URL string, args ...RequestOption) *Request
NewRequest construct a Request by passing the parameters.
You can construct this request by passing the following parameters:
method: Method for the request. url: URL for the request. http.Header: HTTP Headers to send. direwolf.Params: Parameters to send in the query string. direwolf.Cookies: Cookies to send. direwolf.PostForm: Post data form to send. direwolf.Body: Post body to send. direwolf.Proxy: Proxy url to use. direwolf.Timeout: Request Timeout. direwolf.RedirectNum: Number of Request allowed to redirect.
type RequestOption ¶ added in v0.6.0
type RequestOption interface {
// contains filtered or unexported methods
}
RequestOption is the interface of Request Options. Use to bind the options to Request.
type Response ¶
type Response struct { URL string StatusCode int Proto string Headers http.Header Cookies Cookies Request *Request Content []byte ContentLength int64 // contains filtered or unexported fields }
Response is the response from request.
func Delete ¶ added in v0.2.2
func Delete(URL string, args ...RequestOption) (*Response, error)
Delete is the method to constructs and sends a Delete request. Parameters are the same with direwolf.Get()
func Get ¶
func Get(URL string, args ...RequestOption) (*Response, error)
Get is the most common method of direwolf to constructs and sends a Get request.
You can construct this request by passing the following parameters:
url: URL for the request. http.Header: HTTP Headers to send. direwolf.Params: Parameters to send in the query string. direwolf.Cookies: Cookies to send. direwolf.PostForm: Post data form to send. direwolf.Body: Post body to send. direwolf.Proxy: Proxy url to use. direwolf.Timeout: Request Timeout. Default value is 30. direwolf.RedirectNum: Number of Request allowed to redirect. Default value is 5.
func Head ¶ added in v0.2.2
func Head(URL string, args ...RequestOption) (*Response, error)
Head is the method to constructs and sends a Head request. Parameters are the same with direwolf.Get()
func Patch ¶ added in v0.2.2
func Patch(URL string, args ...RequestOption) (*Response, error)
Patch is the method to constructs and sends a Patch request. Parameters are the same with direwolf.Get()
func Post ¶
func Post(URL string, args ...RequestOption) (*Response, error)
Post is the method to constructs and sends a Post request. Parameters are the same with direwolf.Get()
Note: direwolf.Body can`t existed with direwolf.PostForm.
func Put ¶ added in v0.2.2
func Put(URL string, args ...RequestOption) (*Response, error)
Put is the method to constructs and sends a Put request. Parameters are the same with direwolf.Get()
func Send ¶ added in v0.5.3
Send is different with Get and Post method, you should pass a Request to it. You can construct Request by use NewRequest method.
func (*Response) CSS ¶
func (resp *Response) CSS(queryStr string) *CSSNodeList
CSS is a method to extract data with css selector, it returns a CSSNodeList.
func (*Response) Encoding ¶ added in v0.3.0
Encoding can change and return the encoding type of response. Like this:
encoding := resp.Encoding("GBK")
You can specified encoding type. Such as GBK, GB18030, latin1. Default is UTF-8. It will decode the content to string if you specified encoding type. It will just return the encoding type of response if you do not pass parameter.
func (*Response) Re ¶
Re extract required data with regexp. It return a slice of string. Every time you call this method, it will transcode the Response.content to text once. So please try to extract required data at once.
func (*Response) ReSubmatch ¶
ReSubmatch extract required data with regexp. It return a slice of string from FindAllStringSubmatch. Every time you call this method, it will transcode the Response.content to text once. So please try to extract required data at once.
type Session ¶
type Session struct { Headers http.Header Proxy *Proxy Timeout int // contains filtered or unexported fields }
Session is the main object in direwolf. This is its main features: 1. handling redirects 2. automatically managing cookies
func NewSession ¶ added in v0.2.0
func NewSession(options ...*SessionOptions) *Session
NewSession new a Session object, and set a default Client and Transport.
func (*Session) Delete ¶ added in v0.2.2
func (session *Session) Delete(URL string, args ...RequestOption) (*Response, error)
Delete is a post method.
func (*Session) Get ¶
func (session *Session) Get(URL string, args ...RequestOption) (*Response, error)
Get is a get method.
func (*Session) Head ¶ added in v0.2.2
func (session *Session) Head(URL string, args ...RequestOption) (*Response, error)
Head is a post method.
func (*Session) Patch ¶ added in v0.2.2
func (session *Session) Patch(URL string, args ...RequestOption) (*Response, error)
Patch is a post method.
func (*Session) Post ¶
func (session *Session) Post(URL string, args ...RequestOption) (*Response, error)
Post is a post method.
func (*Session) Put ¶ added in v0.2.2
func (session *Session) Put(URL string, args ...RequestOption) (*Response, error)
Put is a post method.
func (*Session) SetCookies ¶ added in v0.5.0
SetCookies set cookies of the url in Session.
type SessionOptions ¶ added in v0.5.2
type SessionOptions struct { // DialTimeout is the maximum amount of time a dial will wait for // a connect to complete. // // When using TCP and dialing a host name with multiple IP // addresses, the timeout may be divided between them. // // With or without a timeout, the operating system may impose // its own earlier timeout. For instance, TCP timeouts are // often around 3 minutes. DialTimeout time.Duration // DialKeepAlive specifies the interval between keep-alive // probes for an active network connection. // // Network protocols or operating systems that do // not support keep-alives ignore this field. // If negative, keep-alive probes are disabled. DialKeepAlive time.Duration // MaxConnsPerHost optionally limits the total number of // connections per host, including connections in the dialing, // active, and idle states. On limit violation, dials will block. // // Zero means no limit. MaxConnsPerHost int // MaxIdleConns controls the maximum number of idle (keep-alive) // connections across all hosts. Zero means no limit. MaxIdleConns int // MaxIdleConnsPerHost, if non-zero, controls the maximum idle // (keep-alive) connections to keep per-host. If zero, // DefaultMaxIdleConnsPerHost is used. MaxIdleConnsPerHost int // IdleConnTimeout is the maximum amount of time an idle // (keep-alive) connection will remain idle before closing // itself. // Zero means no limit. IdleConnTimeout time.Duration // TLSHandshakeTimeout specifies the maximum amount of time waiting to // wait for a TLS handshake. Zero means no timeout. TLSHandshakeTimeout time.Duration // ExpectContinueTimeout, if non-zero, specifies the amount of // time to wait for a server's first response headers after fully // writing the request headers if the request has an // "Expect: 100-continue" header. Zero means no timeout and // causes the body to be sent immediately, without // waiting for the server to approve. // This time does not include the time to send the request header. ExpectContinueTimeout time.Duration // DisableCookieJar specifies whether disable session cookiejar. DisableCookieJar bool // DisableDialKeepAlives, if true, disables HTTP keep-alives and // will only use the connection to the server for a single // HTTP request. // // This is unrelated to the similarly named TCP keep-alives. DisableDialKeepAlives bool }
func DefaultSessionOptions ¶ added in v0.5.2
func DefaultSessionOptions() *SessionOptions
DefaultSessionOptions return a default SessionOptions object.