Documentation
¶
Overview ¶
Package miner is the core of this project, use to request for http api.
Example:
package main import ( "fmt" "github.com/hunterhug/marmot/miner" ) func main() { miner.SetLogLevel(miner.DEBUG) // Use Default Worker, You can Also New One: //worker, _ := miner.New(nil) //worker = miner.NewWorkerWithNoProxy() //worker = miner.NewAPI() //worker, _ = miner.NewWorkerWithProxy("socks5://127.0.0.1:1080") worker := miner.Clone() _, err := worker.SetUrl("https://www.bing.com").Go() if err != nil { fmt.Println(err.Error()) } else { fmt.Println(worker.ToString()) } }
Index ¶
- Constants
- Variables
- func CloneHeader(h map[string][]string) map[string][]string
- func CopyM(h http.Header) http.Header
- func Delete() (body []byte, e error)
- func Get() (body []byte, e error)
- func GetCookies() []*http.Cookie
- func GetResponseStatusCode() int
- func Go() (body []byte, e error)
- func GoByMethod(method string) (body []byte, e error)
- func JsonToString() (string, error)
- func Log() golog.LoggerInterface
- func MergeCookie(before []*http.Cookie, after []*http.Cookie) []*http.Cookie
- func NewClient() *http.Client
- func NewJar() *cookiejar.Jar
- func NewProxyClient(proxyString string) (*http.Client, error)
- func OtherGo(method, contentType string) (body []byte, e error)
- func OutputMaps(uuid string, info string, args map[string][]string)
- func Post() (body []byte, e error)
- func PostFILE() (body []byte, e error)
- func PostJSON() (body []byte, e error)
- func PostXML() (body []byte, e error)
- func Put() (body []byte, e error)
- func PutFILE() (body []byte, e error)
- func PutJSON() (body []byte, e error)
- func PutXML() (body []byte, e error)
- func RandomUa() string
- func SetDefaultTimeOut(num int)
- func SetGlobalTimeout(num int)
- func SetLogLevel(level Level)
- func ToString() string
- func TooSortSizes(data []byte, sizes float64) error
- func UaInit()
- func Wait(waitTime int)
- type Level
- type Request
- type Response
- type Worker
- func Clear() *Worker
- func ClearAll() *Worker
- func ClearCookie() *Worker
- func Clone() *Worker
- func New(ipString interface{}) (*Worker, error)
- func NewAPI() *Worker
- func NewWorker(proxyIpString interface{}) (*Worker, error)
- func NewWorkerByClient(client *http.Client) *Worker
- func NewWorkerWithNoProxy() *Worker
- func NewWorkerWithProxy(proxyIpString interface{}) (*Worker, error)
- func SetAfterAction(fc func(context.Context, *Worker)) *Worker
- func SetBData(data []byte) *Worker
- func SetBeforeAction(fc func(context.Context, *Worker)) *Worker
- func SetContext(ctx context.Context) *Worker
- func SetCookie(v string) *Worker
- func SetCookieByFile(file string) (*Worker, error)
- func SetFileInfo(fileName, fileFormName string) *Worker
- func SetForm(form url.Values) *Worker
- func SetFormParam(k, v string) *Worker
- func SetHeader(header http.Header) *Worker
- func SetHeaderParam(k, v string) *Worker
- func SetMethod(method string) *Worker
- func SetRefer(refer string) *Worker
- func SetUa(ua string) *Worker
- func SetUrl(url string) *Worker
- func SetWaitTime(num int) *Worker
- func (worker *Worker) Clear() *Worker
- func (worker *Worker) ClearAll() *Worker
- func (worker *Worker) ClearCookie() *Worker
- func (worker *Worker) Clone() *Worker
- func (worker *Worker) Delete() (body []byte, e error)
- func (worker *Worker) Get() (body []byte, e error)
- func (worker *Worker) GetCookies() []*http.Cookie
- func (worker *Worker) GetResponseStatusCode() int
- func (worker *Worker) Go() (body []byte, e error)
- func (worker *Worker) GoByMethod(method string) (body []byte, e error)
- func (worker *Worker) JsonToString() (string, error)
- func (worker *Worker) OtherGo(method, contentType string) (body []byte, e error)
- func (worker *Worker) OtherGoBinary(method, contentType string) (body []byte, e error)
- func (worker *Worker) Post() (body []byte, e error)
- func (worker *Worker) PostFILE() (body []byte, e error)
- func (worker *Worker) PostJSON() (body []byte, e error)
- func (worker *Worker) PostXML() (body []byte, e error)
- func (worker *Worker) Put() (body []byte, e error)
- func (worker *Worker) PutFILE() (body []byte, e error)
- func (worker *Worker) PutJSON() (body []byte, e error)
- func (worker *Worker) PutXML() (body []byte, e error)
- func (worker *Worker) SetAfterAction(fc func(context.Context, *Worker)) *Worker
- func (worker *Worker) SetBData(data []byte) *Worker
- func (worker *Worker) SetBeforeAction(fc func(context.Context, *Worker)) *Worker
- func (worker *Worker) SetContext(ctx context.Context) *Worker
- func (worker *Worker) SetCookie(v string) *Worker
- func (worker *Worker) SetCookieByFile(file string) (*Worker, error)
- func (worker *Worker) SetFileInfo(fileName, fileFormName string) *Worker
- func (worker *Worker) SetForm(form url.Values) *Worker
- func (worker *Worker) SetFormParam(k, v string) *Worker
- func (worker *Worker) SetHeader(header http.Header) *Worker
- func (worker *Worker) SetHeaderParam(k, v string) *Worker
- func (worker *Worker) SetHost(host string) *Worker
- func (worker *Worker) SetMethod(method string) *Worker
- func (worker *Worker) SetRefer(refer string) *Worker
- func (worker *Worker) SetUa(ua string) *Worker
- func (worker *Worker) SetUrl(url string) *Worker
- func (worker *Worker) SetWaitTime(num int) *Worker
- func (worker *Worker) ToString() string
Constants ¶
const ( VERSION = "1.0.13" // GET HTTP method GET = "GET" POST = "POST" POSTJSON = "POSTJSON" POSTXML = "POSTXML" POSTFILE = "POSTFILE" PUT = "PUT" PUTJSON = "PUTJSON" PUTXML = "PUTXML" PUTFILE = "PUTFILE" DELETE = "DELETE" OTHER = "OTHER" // this stand for you can use other method this lib not own. // HTTPFORMContentType HTTP content type HTTPFORMContentType = "application/x-www-form-urlencoded" HTTPJSONContentType = "application/json" HTTPXMLContentType = "text/xml" HTTPFILEContentType = "multipart/form-data" )
const ( DEBUG = golog.DebugLevel INFO = golog.InfoLevel WARN = golog.WarnLevel ERROR = golog.ErrorLevel )
Variables ¶
var ( // Client Save Cookie Client = &http.Client{ CheckRedirect: func(req *http.Request, via []*http.Request) error { Logger.Debugf("[GoWorker] Redirect: %v", req.URL) return nil }, Jar: NewJar(), Timeout: util.Second(DefaultTimeOut), Transport: &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, }, } // NoCookieClient Not Save Cookie NoCookieClient = &http.Client{ CheckRedirect: func(req *http.Request, via []*http.Request) error { Logger.Debugf("[GoWorker] Redirect: %v", req.URL) return nil }, Timeout: util.Second(DefaultTimeOut), Transport: &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, }, } )
Default Client
var ( DefaultHeader = map[string][]string{ "User-Agent": { ourLoveUa, }, } // DefaultTimeOut http get and post No timeout DefaultTimeOut = 0 )
var (
Logger = golog.New()
)
Logger Global logger config for debug
var Pool = &_Workers{ws: make(map[string]*Worker)}
Pool for many Worker, every Worker can only serial execution
var Ua = map[int]string{}
Ua Global User-Agent provide
Functions ¶
func CloneHeader ¶
CloneHeader Clone a header, If not exist Ua, Set our Ua!
func CopyM ¶
CopyM Header map[string][]string ,can use to copy a http header, so that they are not effect each other
func GetCookies ¶
func GetResponseStatusCode ¶
func GetResponseStatusCode() int
func GoByMethod ¶
func JsonToString ¶
JsonToString This make effect only your Worker exec serial! Attention! Change Your JSON like Raw data to string
func MergeCookie ¶
MergeCookie Merge Cookie, not use
func NewProxyClient ¶
NewProxyClient New a Proxy client, Default save cookie, Can timeout We should support some proxy way such as http(s) or socks
func OutputMaps ¶
OutputMaps Just debug a map
func SetDefaultTimeOut ¶ added in v1.0.13
func SetDefaultTimeOut(num int)
func SetGlobalTimeout ¶
func SetGlobalTimeout(num int)
SetGlobalTimeout Set global timeout, it can only by this way!
func ToString ¶
func ToString() string
ToString This make effect only your Worker exec serial! Attention! Change Your Raw data To string
func TooSortSizes ¶
TooSortSizes if a file size smaller than sizes(KB), it will be thrown an error
Types ¶
type Worker ¶
type Worker struct { // In order fast chain func call I put the basic config below *Request *Response // Which url we want Url string // Get,Post method Method string // Our Client Client *http.Client // Wait Sleep Time Wait int // Worker proxy ip, just for user to record their proxy ip, default: localhost Ip string // AOP like Java Ctx context.Context BeforeAction func(context.Context, *Worker) AfterAction func(context.Context, *Worker) // Http header Header http.Header // contains filtered or unexported fields }
Worker is the main object to sent http request and return result of response
var DefaultWorker *Worker
DefaultWorker Global Worker
func ClearCookie ¶
func ClearCookie() *Worker
func NewAPI ¶
func NewAPI() *Worker
NewAPI New API Worker, No Cookie Keep, share the same http client
func NewWorker ¶
NewWorker New a worker, if ipString is a proxy address, New a proxy client. evey time gen a new http client! Proxy address such as:
http://[user]:[password@]ip:port, [] stand it can choose or not. case: socks5://127.0.0.1:1080
func NewWorkerByClient ¶
NewWorkerByClient New Worker by Your Client
func NewWorkerWithNoProxy ¶ added in v1.0.7
func NewWorkerWithNoProxy() *Worker
NewWorkerWithNoProxy Alias func
func NewWorkerWithProxy ¶ added in v1.0.7
NewWorkerWithProxy Alias func
func SetContext ¶
func SetCookieByFile ¶
func SetFileInfo ¶
func SetFormParam ¶ added in v1.0.9
func SetHeaderParam ¶ added in v1.0.9
func SetWaitTime ¶
func (*Worker) GetCookies ¶
GetCookies Get Cookies
func (*Worker) GetResponseStatusCode ¶
GetResponseStatusCode Get ResponseStatusCode
func (*Worker) JsonToString ¶
JsonToString This make effect only your worker exec serial! Attention! Change Your JSON' like Raw data to string
func (*Worker) OtherGo ¶
OtherGo Method
Method = "OPTIONS" ; Section 9.2 | "GET" ; Section 9.3 | "HEAD" ; Section 9.4 | "POST" ; Section 9.5 | "PUT" ; Section 9.6 | "DELETE" ; Section 9.7 | "TRACE" ; Section 9.8 | "CONNECT" ; Section 9.9 | extension-method extension-method = token token = 1*<any CHAR except CTLs or separators>
Content Type
"application/x-www-form-urlencoded" "application/json" "text/xml" "multipart/form-data"
func (*Worker) OtherGoBinary ¶
func (*Worker) Post ¶
Post Almost include bellow:
"application/x-www-form-urlencoded" "application/json" "text/xml" "multipart/form-data"
func (*Worker) SetAfterAction ¶
func (*Worker) SetBeforeAction ¶
func (*Worker) SetContext ¶
SetContext Set Context so Action can soft
func (*Worker) SetCookieByFile ¶
SetCookieByFile Set Cookie by file.