simplehttp

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2019 License: MIT Imports: 27 Imported by: 0

README

Base Go + Http😋

🔥 Quick Start


Quick Start

Search Github for simplehttp

package main

import "github.com/ljun20160606/simplehttp"

func main() {
     simplehttp.
            Get().
            SetUrl("https://github.com/search").
            Query("utf8", "✓").
            Query("q", "simplehttp").
            Send().
            String()
}

More example/github.go

Documentation

Index

Constants

View Source
const (
	Referer     = "Referer"
	UserAgent   = "User-Agent"
	Origin      = "Origin"
	ContentType = "Content-Type"
)

Variables

View Source
var (
	Verbose = false

	DefaultHttpClient = &http.Client{
		Transport: HTTP1.RoundTripper(),
	}
	DefaultDialContext = DialContext(30*time.Second, 30*time.Second, 0)
)
View Source
var ContentTypeMatchCharset = regexp.MustCompile(`[cC]harset=([\w|\-]*)`)
View Source
var DefaultClient = &HttpClient{Client: DefaultHttpClient}
View Source
var RoundTripFactory = map[ProtoMajor]func() http.RoundTripper{
	HTTP1: defaultHttpRoundTripperFunc,
	HTTP2: func() http.RoundTripper {
		tripper := defaultHttpRoundTripperFunc().(*http.Transport)
		_ = http2.ConfigureTransport(tripper)
		return tripper
	},
}

Functions

func BuildQueryEncoded added in v0.3.0

func BuildQueryEncoded(source [][2]string, charset Charset) ([]byte, error)

func CookieJar

func CookieJar() http.CookieJar

quick

func DialContext

func DialContext(connTimeout, KeepAlive, rwTimeout time.Duration) func(ctx context.Context, network, address string) (net.Conn, error)

func ReadCookies

func ReadCookies(lines []string, filter string) []*http.Cookie

lines, ok := h["Cookie"]

if !ok {
	return []*http.Cookie{}
}

Types

type Builder

type Builder struct {
	// Timeout specifies a time limit for requests made by this
	// Client. The timeout includes connection time, any
	// redirects, and reading the response body. The timer remains
	// running after Get, Head, Post, or Do return and will
	// interrupt reading of the Response.Body.
	//
	// A Timeout of zero means no timeout.
	//
	// The Client cancels requests to the underlying Transport
	// using the Request.Cancel mechanism. Requests passed
	// to Client.Do may still set Request.Cancel; both will
	// cancel the request.
	//
	// For compatibility, the Client will also use the deprecated
	// CancelRequest method on Transport if found. New
	// RoundTripper implementations should use Request.Cancel
	// instead of implementing CancelRequest.
	Timeout time.Duration

	// Would don't use cache If nil
	Cache cache.Cache

	// Default Http1
	ProtoMajor ProtoMajor

	Proxy          Proxy
	SessionID      string
	SessionTimeout time.Duration
	Client         *http.Client
}

func (*Builder) Build

func (b *Builder) Build() Client

type Charset added in v0.3.0

type Charset interface {
	Encode(s *string) error
}
var (
	UTF8    Charset = CharsetEncoderFunc(func(s *string) error { return nil })
	GB18030 Charset = CharsetEncoderFunc(func(s *string) error {
		v, err := simplifiedchinese.GB18030.NewEncoder().String(*s)
		if err != nil {
			return err
		}
		*s = v
		return nil
	})
)

type CharsetEncoderFunc added in v0.3.0

type CharsetEncoderFunc func(s *string) error

func (CharsetEncoderFunc) Encode added in v0.3.0

func (c CharsetEncoderFunc) Encode(s *string) error

type Client

type Client interface {
	Send(request *Request) *Response
}

func NewClient

func NewClient(builder *Builder) Client

type HttpClient

type HttpClient struct {
	*http.Client
	StoreCookie StoreCookie
}

func (*HttpClient) Send

func (h *HttpClient) Send(r *Request) (resp *Response)

type ProtoMajor

type ProtoMajor int
const (
	HTTP1 ProtoMajor
	HTTP2
)

func (ProtoMajor) RoundTripper

func (t ProtoMajor) RoundTripper() http.RoundTripper

type Proxy

type Proxy func(*http.Request) (*url.URL, error)

type Request

type Request struct {
	Method   string
	Url      *strings.Builder
	Header   http.Header
	Querys   [][2]string
	Forms    [][2]string
	Body     io.Reader
	JsonData interface{}
	Cookies  []*http.Cookie
	Charset  Charset
	Client   Client
	Config   RequestConfig
}

func Delete

func Delete() *Request

func Get

func Get() *Request
func Head() *Request

func Method added in v0.3.0

func Method(m string) *Request

func NewPureRequest added in v0.3.0

func NewPureRequest() *Request

func NewRequest

func NewRequest(client Client) *Request

func Options

func Options() *Request

func Patch

func Patch() *Request

func Post

func Post() *Request

func Put

func Put() *Request

func (*Request) AddCookie

func (r *Request) AddCookie(ck *http.Cookie) *Request

func (*Request) ClearCookie

func (r *Request) ClearCookie() *Request

func (*Request) Connect

func (r *Request) Connect() *Request

func (*Request) Delete

func (r *Request) Delete() *Request

func (*Request) Form

func (r *Request) Form(k string, v string) *Request

func (*Request) GB18030

func (r *Request) GB18030() *Request

func (*Request) Get

func (r *Request) Get() *Request

func (*Request) Head

func (r *Request) Head(k, v string) *Request

func (*Request) MethodHead

func (r *Request) MethodHead() *Request

func (*Request) Options

func (r *Request) Options() *Request

func (*Request) OriginInHeader

func (r *Request) OriginInHeader(origin string) *Request

func (*Request) Patch

func (r *Request) Patch() *Request

func (*Request) Post

func (r *Request) Post() *Request

func (*Request) Query

func (r *Request) Query(k, v string) *Request

func (*Request) RefererInHeader

func (r *Request) RefererInHeader(refer string) *Request

func (*Request) Send

func (r *Request) Send() (resp *Response)

func (*Request) SetBody

func (r *Request) SetBody(body io.Reader) *Request

func (*Request) SetClient added in v0.3.0

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

func (*Request) SetCookies

func (r *Request) SetCookies(c []*http.Cookie) *Request

func (*Request) SetForms

func (r *Request) SetForms(forms [][2]string) *Request

func (*Request) SetJSON

func (r *Request) SetJSON(data interface{}) *Request

func (*Request) SetMethod

func (r *Request) SetMethod(method string) *Request

func (*Request) SetQuerys

func (r *Request) SetQuerys(querys [][2]string) *Request

func (*Request) SetRetry

func (r *Request) SetRetry(retry int) *Request

func (*Request) SetStringCookies

func (r *Request) SetStringCookies(cookie string) *Request

func (*Request) SetUrl

func (r *Request) SetUrl(rawurl string) *Request

func (*Request) UTF8

func (r *Request) UTF8() *Request

type RequestConfig

type RequestConfig struct {
	IsClearCookie bool
	Retry         int
}

type Response

type Response struct {
	*http.Response
	// contains filtered or unexported fields
}

func (*Response) Body

func (r *Response) Body() ([]byte, error)

func (*Response) DetectedEncode

func (r *Response) DetectedEncode() (err error)

func (*Response) Document

func (r *Response) Document() (doc *goquery.Document, err error)

func (*Response) DocumentDetectedEncode

func (r *Response) DocumentDetectedEncode() (doc *goquery.Document, err error)

func (*Response) Encode

func (r *Response) Encode(name string) *Response

http://www.w3.org/TR/encoding

func (*Response) Error

func (r *Response) Error() error

func (*Response) JSON

func (r *Response) JSON(data interface{}) error

func (*Response) String

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

type StoreCookie

type StoreCookie func(jar http.CookieJar)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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